MagicJesus

Using the LCD on the PBMCUSLK project board with SPI (MC9S12DT256)

Discussion created by MagicJesus on Mar 17, 2012
Latest reply on Mar 24, 2012 by MagicJesus

Hi, I have been looking at a few different resources both on here and elsewhere on the internet about 4-bit LCD interfacing. I am using the SPI with the microcontroller stated in the title. I have managed to get the SPI working properly and the correct bits show up on the output of the 74HC595 serial in, parallel out shift register.

 

I can not for the life of me get the LCD to initialize or do anything. :smileysad:

when I plug the board in to do multilink debugging with codewarrior, the first of the two 8-digit lines fills all dark and the second line is clear.

 

the initialize logic and data sending techniques I have come up with is loosely based off of this site.

 

Anyway here is my code. If anyone has any ideas please let me know :smileyvery-happy:

 

#include <hidef.h>      /* common defines and macros */#include "derivative.h"      /* derivative-specific definitions */void SPI_Initialize(void);void SPI_Send(unsigned char data);void LCD_Initialize(void);void LCD_SendCommand(unsigned char data);void LCD_SendCharacter(unsigned char data);void Delay(unsigned int time); void Delay3us(unsigned int time);void main(void) {  SPI_Initialize(); LCD_Initialize();  while(1) {   LCD_SendCharacter(0x4F); }}void LCD_Initialize(void){ // wait 20ms for LCD to turn on Delay(21);  SPI_Send(0x83); SPI_Send(0x03); Delay(10);  SPI_Send(0x83); SPI_Send(0x03); Delay(1);  SPI_Send(0x83); SPI_Send(0x03); Delay(1); SPI_Send(0x82);  SPI_Send(0x02); Delay(1);  // Write 08 hex (don't shift display, hide cursor) LCD_SendCommand(0x08);  // Write 01 hex (clear and home display) LCD_SendCommand(0x01);  // Write 06 hex (move cursor right) LCD_SendCommand(0x06);  // Write 0C hex (turn on display) LCD_SendCommand(0x0F);}void LCD_SendCharacter(unsigned char data){ // break up data for transfer volatile unsigned char low = data&0x0F; volatile unsigned char high = (data&0xF0)>>4;   // set enable low SPI_Send(high);  // set enable bit to 1 SPI_Send(high|0xC0);  // set enable low SPI_Send(high);  // Delay Delay3us(200);  // send low byte SPI_Send(low); // set enable bit to 1 SPI_Send(low|0xC0);  // clear enable bit SPI_Send(low);  // delay 5ms Delay(1);}void LCD_SendCommand(unsigned char data){ // break up data for transfer volatile unsigned char low = data&0x0F; volatile unsigned char high = (data&0xF0)>>4;  // send high byte of data SPI_Send(high);  // set enable bit to 1 SPI_Send(high|0x80);  // set enable low SPI_Send(high);  // delay 5ms Delay(5);  // clear enable bit SPI_Send(low);  // set enable bit to 1 SPI_Send(low|0x80);  // clear enable bit SPI_Send(low);}void SPI_Initialize(void) { char clear;   // route SPI to port M MODRR_MODRR4 = 1; DDRM = 0b00111000;  // baud rate = 520.83 kHz SPI0BR = 0x23;  // SPI control register 2 SPI0CR2 = 0x10;  // enable SPI and set to master mode SPI0CR1 = 0x52;  // clear flag clear = SPI0SR; clear = SPI0DR;  }void SPI_Send(unsigned char data){  char clear;  // write to data register SPI0DR = data;  // wait until ready while (SPI0SR_SPIF == 0)  {  // wait for flag }  // reset flag  clear = SPI0SR; clear = SPI0DR;}void Delay(unsigned int time) { unsigned int i; unsigned int j;  for (i = 0; i < time; i++) {  for (j = 0; j < 328; j++)  {  } }}void Delay3us(unsigned int time){  int j;    for (j = 0; j < time; j++)  {  }}

Outcomes