Graphical LCD ST7920 with 8051

Discussion about project that uses other controller, or non controller based project such as using logic gates, FPGA, ARM, Mechanical ....etc. Welcome to post it here

Graphical LCD ST7920 with 8051

Postby moon_light_2010 » Thu Dec 15, 2011 1:20 am

Hello guys .. I've just bought a graphical lcd from Cytron on this link http://www.cytron.com.my/viewProduct.ph ... 8128x64%29

I have been trying to get it to work but it won't :S.. I went through the datasheet and interfaced it with AT89S51 in 8-bit parallel mode. Since I am not going to read from this lcd, I grounded the R/W pin and I also pulled RST and PSB pins to Vdd (5v).
I wrote the following code using Kiel .. it's pretty straight forward code. I just want to write anything to the graphical lcd. I am using 4.00 Mhz crystal.

CODE: SELECT_ALL_CODE
#include <reg51.h>

sbit   LCD_RS = P1^0;
sbit   LCD_E  = P1^1;

#define LCD_PORT P0

void write_data (unsigned char);
void write_command (unsigned char);
void wait (unsigned char);

unsigned char code welcome[] = {"Welcome"};
void main ()
{
   unsigned char size;
   // Let's first initilize the LCD
   wait(255);   
   write_command(0x30);
   wait(140);
   write_command(0x30);
   wait(140);
   // Display on off
   write_command(0x0F);
   wait(140);
   // clear
   write_command(0x01);
   wait(140);
   // Mode set
   write_command(0x06);
   wait(140);

   // Cursor control
   write_command(0x14);
   wait(140);


   
   // Now let's see if we can write WELCOME
   while (1)
   {
      
      write_command(0x01);
      wait(140);
      write_command(0x02);
      wait(140);

         for (size= 0 ; size< 7 ; size++)
      {
         write_data(welcome[size]);
         wait(140);
      }
         wait(140);
         
   }

}

void write_data (unsigned char value)
{
   LCD_RS = 1;
   LCD_PORT = value;   
   LCD_E = 1;
   LCD_E = 0;
}

void write_command (unsigned char value)
{
   LCD_RS = 0;
   LCD_PORT = value;   
   LCD_E = 1;
   LCD_E = 0;
}

void wait (unsigned char delay)
{
   unsigned char x,y;
   for (x = 0 ; x< 254 ; x++)
   {
      for (y = 0 ; y < delay ; y++);
   }
}


I would love to know why it is not working. I hope I didn't leave anything vague .. thanks in advance for your help :) ..
moon_light_2010
Freshie
 
Posts: 4
Joined: Thu Dec 15, 2011 12:51 am

Re: Graphical LCD ST7920 with 8051

Postby ABSF » Thu Dec 15, 2011 12:43 pm

Are you just wanting to display text on the Graphic LCD? The LCD does have a text mode but the input is serial. You have to refer to the Data sheet of ST7920 and the user manual of the said LCD.

PR21 also has an example of how to display pre-defined graphic on the LCD screen though it is written for PIC but you should be able to modify the program to 8051 C.

There is also a lengthy discussion on this topic here
Cytron Technical Forum • View topic - Need helps to how to interface PIC16F877A with graphic LCD
viewtopic.php?f=21&t=10858&hilit=help+on+graphic+lcd

Go to page 3 of the discussion and you'll find Brian has written a C program to make use of the LCD to display text and chinese characters.

Allen
The next war will determine NOT who is right BUT what is left.
User avatar
ABSF
Professional
 
Posts: 810
Joined: Wed Nov 10, 2010 9:32 am
Location: E Malaysia

Re: Graphical LCD ST7920 with 8051

Postby moon_light_2010 » Thu Dec 15, 2011 9:43 pm

thanks ABSF for your respond. Actually, what I want to do is to know how to handle this glcd. So, my first step is to write something to it and I want to do that in parallel mode. You just said something about the serial mode, do you mean by that that I can use this glcd in text mode only in serial mode !? I can't write some normal ASCII characters to this glcd in parallel mode?!
I tried the page you posted, yet I actually couldn't grasp all of that. It was merely focusing on serial mode.

In summary I just want to know the simplest correct sequence of steps that would initialize the glcd and display something in it.
Was pulling RST and PSB up and grounding R/W right ? ( you know I didn't connect them to my MCU).
I would be thankful if you could check the code I typed and feed me back if it has some faults.
moon_light_2010
Freshie
 
Posts: 4
Joined: Thu Dec 15, 2011 12:51 am

Re: Graphical LCD ST7920 with 8051

Postby Brian Griffin » Mon Dec 19, 2011 9:34 am

moon_light_2010 WROTE:thanks ABSF for your respond. Actually, what I want to do is to know how to handle this glcd. So, my first step is to write something to it and I want to do that in parallel mode. You just said something about the serial mode, do you mean by that that I can use this glcd in text mode only in serial mode !? I can't write some normal ASCII characters to this glcd in parallel mode?!
I tried the page you posted, yet I actually couldn't grasp all of that. It was merely focusing on serial mode.

In summary I just want to know the simplest correct sequence of steps that would initialize the glcd and display something in it.
Was pulling RST and PSB up and grounding R/W right ? ( you know I didn't connect them to my MCU).
I would be thankful if you could check the code I typed and feed me back if it has some faults.


You can use it in parallel mode too, 4-bit or 8-bit mode.

By the way, according to the code, what's the "Wait(255)" mean there? Wait there for 255ms? 255microseconds?

The ST7920 cannot accept instructions and data too fast. Slow down the time taken for the microcontroller to send the data.

Here is a sample code in mbed using a 4-bit parallel mode. You may need to modify the "wait" inside to a desired delay function. Please notice that the waits in mbed takes in fraction of a second:

CODE: SELECT_ALL_CODE
#include "mbed.h"

DigitalOut myled(LED1);
DigitalOut RS(p9);
DigitalOut E(p10);
DigitalOut DB4(p11);
DigitalOut DB5(p12);
DigitalOut DB6(p13);
DigitalOut DB7(p14);

void ST7920_Init();
void ST7920_Command(unsigned char);
void ST7920_Data(unsigned char);
void ST7920_Print(unsigned char, unsigned char, char*);

int main() {
    ST7920_Init();
    ST7920_Print(0,1, "Hello World!");
       
    while(1) {
    }
}

void ST7920_Command(unsigned char data)
{
    RS = 0;
    wait(0.001);
    // Move higher nibble first
   
    DB4 = (data & (1 << 4)) >> 4;
    DB5 = (data & (1 << 5)) >> 5;
    DB6 = (data & (1 << 6)) >> 6;
    DB7 = (data & (1 << 7)) >> 7;
    E = 1;
    wait(0.00001);
    E = 0;
    // Then move the lower nibble
   
    DB4 = (data & (1 << 0)) >> 0;
    DB5 = (data & (1 << 1)) >> 1;
    DB6 = (data & (1 << 2)) >> 2;
    DB7 = (data & (1 << 3)) >> 3;
    E = 1;
    wait(0.00001);
    E = 0;
}

void ST7920_Init()
{
    wait(0.5);
   
    ST7920_Command(0x20); // 0b0010x0xx
    wait(0.001);
    ST7920_Command(0x20); // 0b0010x0xx
    wait(0.001);
    ST7920_Command(0x0F); // 0b00001DCB
    wait(0.001);
    ST7920_Command(0x01); // 0b00000001
    wait(0.01);
    ST7920_Command(0x06); // 0b000001(1/D)S
    ST7920_Command(0x02);
}

void ST7920_Data(unsigned char data)
{
    RS = 1;
    wait(0.001);
   
    // Move higher nibble first
    DB4 = (data & (1 << 4)) >> 4;
    DB5 = (data & (1 << 5)) >> 5;
    DB6 = (data & (1 << 6)) >> 6;
    DB7 = (data & (1 << 7)) >> 7;
    E = 1;
    wait(0.00001);                          // at least 25ns and above here
    E = 0;
   
    // Then move the lower nibble
    DB4 = (data & (1 << 0)) >> 0;
    DB5 = (data & (1 << 1)) >> 1;
    DB6 = (data & (1 << 2)) >> 2;
    DB7 = (data & (1 << 3)) >> 3;
    E = 1;
    wait(0.00001);
    E = 0;
 }
 
void ST7920_Print(unsigned char x, unsigned char y, char* string)
{
  switch(y)
  {
  case 1:
    x += 0x80;
    break;
  case 2:
    x += 0x90;
    break;
  case 3:
    x += 0x88;
    break;
  case 4:
    x += 0x98;
    break;
  default:
    x = 0x80;
    break;
  }
 
  ST7920_Command(x);
 
  while(*string)
      ST7920_Data(*string++);
}
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: Graphical LCD ST7920 with 8051

Postby moon_light_2010 » Tue Dec 20, 2011 2:46 pm

Thanks Brain .. I always maximize the delay in order to give the glcd enough time. This is straight forward code you written ( what I am looking for actually ) up there and I can see that the initialization bytes are different from mine. I will try it and feed you back .. I really appreciate it ..
moon_light_2010
Freshie
 
Posts: 4
Joined: Thu Dec 15, 2011 12:51 am

Re: Graphical LCD ST7920 with 8051

Postby moon_light_2010 » Wed Dec 21, 2011 3:45 pm

Thanks for your help Brian .. I followed your code line by line and compiled it correctly .. but nothing yet .. now I believe the glcd is defective :D .. I will try your code with another glcd later.
moon_light_2010
Freshie
 
Posts: 4
Joined: Thu Dec 15, 2011 12:51 am

Re: Graphical LCD ST7920 with 8051

Postby Brian Griffin » Wed Dec 21, 2011 8:45 pm

moon_light_2010 WROTE:Thanks for your help Brian .. I followed your code line by line and compiled it correctly .. but nothing yet .. now I believe the glcd is defective :D .. I will try your code with another glcd later.


Btw, don't blindly copy the code. And don't assume anything - make sure you can see a cursor first, or at least some garbage on the screen as a start.

And also, the contrast (V0) - please connect it to a potentiometer. At least 1K. Then tune to see whether you can see something or not.
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 Misc Project

Who is online

Users browsing this forum: No registered users and 11 guests

cron