Since the ST7920 LCD controller guides are a bit scarce in the net, I managed to obtain some working examples from the Arduino playground http://www.arduino.cc/playground/Code/LCD12864 (courtesy of Markos Kyritsis), and the dfRobot ST7920 product page http://www.dfrobot.com/index.php?route=product/product&path=53&product_id=240 with the examples. However these are coded with Arduino, so I will translate a bit to ordinary C so you guys can play around with the GLCD.
The interface will be serial, and only disadvantage on it is you can't read data from the GLCD. You need parallel interface for reading the data from GLCD. (datasheet - page 22)
I'll include the drawing of pictures and graphical contents in GLCD later.
Board used is the Cytron SK40C w/ PIC16F877A microcontroller. Pinouts are defined in the "defines section".
- CODE: SELECT_ALL_CODE
#define CLK_P PORTC.RC3 // CLK_P is connected to E pin
#define DATA_P PORTD.RD0 // DATA_P is connected to R/W pin
#define LATCH_P PORTD.RD1 // LATCH_P is connected to R/S pin
unsigned char i;
char chineseSentence[] = { 0xc4, 0xe3, 0xba, 0xc3, 0xc2, 0xf0, 0xa3, 0xbf, 0x00 };
char chineseSentence1[] = { 0xce, 0xd2, 0xba, 0xc3, 0xa1, 0xa3, 0x00 };
void serial_send(unsigned char sendData)
{
for (i = 0; i < 8; i++)
{
DATA_P = (sendData >> 7) & 0x01;
sendData = sendData << 1;
CLK_P = 0;
CLK_P = 1;
}
}
void ST7920_Command(unsigned char command)
{
LATCH_P = 1;
serial_send(0b11111000);
serial_send(command & 0xF0);
serial_send((command & 0x0F) << 4);
LATCH_P = 0;
}
void ST7920_Data(unsigned char buffer)
{
LATCH_P = 1;
serial_send(0b11111010);
serial_send(buffer & 0xF0);
serial_send((buffer & 0x0F) << 4);
LATCH_P = 0;
}
void ST7920_Print(unsigned char x, unsigned char y, char* string)
{
unsigned char n = 0;
switch(y)
{
case 1:
x = 0x80 + x;
break;
case 2:
x = 0x90 + x;
break;
case 3:
x = 0x88 + x;
break;
case 4:
x = 0x98 + x;
break;
default:
x = 0x80;
break;
}
ST7920_Command(x);
while(string[n] != 0x00)
{
ST7920_Data(string[n]);
n++;
}
n = 0;
}
void ST7920_Serial_Init()
{
ST7920_Command(0b00110000);
delay_us(150);
ST7920_Command(0b00110000);
delay_us(50);
ST7920_Command(0b00001100);
delay_us(150);
ST7920_Command(0b00000001);
delay_ms(50);
}
void main()
{
TRISC.RC3 = 0;
TRISD = 0x00;
PORTD = 0x00;
ST7920_Serial_Init();
ST7920_Print(0,1,"Hello World!");
ST7920_Print(0,2,chineseSentence);
ST7920_Print(0,3,"I'm fine!");
ST7920_Print(0,4,chineseSentence1);
while(1);
}
The compiler used is in MikroC - but you can port the code for other compilers of your choice.
serial_send function courtesy of Mazidi, Mckinlay and Causey, "PIC Microcontroller and Embedded Systems - Using Assembly and C for PIC18" by Pearson Education, 2009.
For the array of Chinese characters, it is important to add a 0x00 (null termination) after a Chinese sentence. This will provide a "full stop" and prevent the system from displaying gibberish after the sentence.