ST7920 - Displaying Characters Tutorial

Discussion about projects that used PIC Microcontroller, Hardware Interface, Programming Algorithm and etc......

ST7920 - Displaying Characters Tutorial

Postby Brian Griffin » Thu May 12, 2011 10:41 pm

Hello there!

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.
Attachments
2011-05-12 22.48.47.jpg
Picture of the setup
PIC - UIC00B from Cytron (replacement for my broken PICKit 2), Pickit 3, MikroC for PIC
dsPIC - MikroC for dsPIC, mikromedia board (dsPIC33)
AVR - AVR Dragon
Parallax - Prop tool
User avatar
Brian Griffin
Enthusiast
 
Posts: 403
Joined: Mon Jan 17, 2011 9:36 am

Re: ST7920 - Displaying Characters Tutorial

Postby shahrul » Fri May 13, 2011 2:14 am

This good. I want try later. :D
User avatar
shahrul
Professional
 
Posts: 812
Joined: Sat May 16, 2009 9:54 pm
Location: Selangor

Re: ST7920 - Displaying Characters Tutorial

Postby yonghui » Fri May 13, 2011 6:26 am

wow,

good!




regards,
yh
thanks&regards,
yh
yonghui
Professional
 
Posts: 732
Joined: Mon Sep 28, 2009 3:27 pm

Re: ST7920 - Displaying Characters Tutorial

Postby ober » Fri May 13, 2011 8:41 am

Brian,

Good work!
Ober Choo
Cytron Technologies Sdn Bhd
www.cytron.com.my
User avatar
ober
Moderator
 
Posts: 1486
Joined: Wed Apr 15, 2009 1:03 pm

Re: ST7920 - Displaying Characters Tutorial

Postby shiyan » Fri May 13, 2011 8:39 pm

Wow.... I want to try too! Thanks man! :mrgreen:
User avatar
shiyan
Amateur
 
Posts: 189
Joined: Wed Jun 09, 2010 10:59 am

Re: ST7920 - Displaying Characters Tutorial

Postby robosang » Sun May 22, 2011 5:51 pm

Great work!
robosang
Expert
 
Posts: 1239
Joined: Wed Jun 10, 2009 5:37 pm

Re: ST7920 - Displaying Characters Tutorial

Postby sich » Mon May 23, 2011 9:46 am

Hi Brian, nice work you have there! Cytron would like to put a link to this forum post or upload your source code to our SK40C and GLCD product pages. Is it okay with you? Acknowledgement goes to you...;)
~> How to ask QUESTIONS the SMART way in FORUM? <~
User avatar
sich
Moderator
 
Posts: 604
Joined: Tue Apr 21, 2009 2:15 pm

Re: ST7920 - Displaying Characters Tutorial

Postby Brian Griffin » Mon May 23, 2011 10:30 am

sich WROTE:Hi Brian, nice work you have there! Cytron would like to put a link to this forum post or upload your source code to our SK40C and GLCD product pages. Is it okay with you? Acknowledgement goes to you...;)


No problem man. I love to contribute to the Cytron. :)
PIC - UIC00B from Cytron (replacement for my broken PICKit 2), Pickit 3, MikroC for PIC
dsPIC - MikroC for dsPIC, mikromedia board (dsPIC33)
AVR - AVR Dragon
Parallax - Prop tool
User avatar
Brian Griffin
Enthusiast
 
Posts: 403
Joined: Mon Jan 17, 2011 9:36 am


Return to PIC Microcontroller

Who is online

Users browsing this forum: No registered users and 2 guests

cron