2

Author Topic: LCD initialization  (Read 3572 times)

0 Members and 1 Guest are viewing this topic.

Offline GWER57Topic starter

  • Full Member
  • ***
  • Posts: 65
  • Helpful? 2
LCD initialization
« on: March 03, 2010, 07:53:26 AM »
Hi, i wrote a function to initialize an lcd. But in vmlab it doesn't seem to be working?
I just want to know if this code seems right to you or if  i messed up somewhere.

//Function headers / macros
#define set(port, bit) port |= (1<<bit)
#define clear(port, bit) port &= ~(1<<bit)
#define pulse_ms(port, bit, length) (set(port,bit), _delay_ms(length), clear(port,bit))

//Defenitions
#define LCDdatap PORTA
#define LCDdatadr DDRA
#define LCDctrlp PORTG
#define LCDctrldr DDRG
#define LCDen 2
#define LCDrs 0
#define LCDrw 1




void LCDinit()
{
   //Set data direction registers
   LCDdatadr = 0xFF;
   LCDctrldr |= 0x07;

   //Initialization
   clear(LCDctrlp, LCDen);
   clear(LCDctrlp, LCDrs);
   clear(LCDctrlp, LCDrw);
   _delay_ms(20);
   
   for(int a = 0; a < 3; a++)
   {
      LCDchar(0x30);
      _delay_ms(20);
   }

   LCDchar(0x30);
   _delay_ms(20);

   LCDchar(0x08);
   _delay_ms(20);

   LCDchar(0x01);
   _delay_ms(20);

   LCDchar(0x06);
   _delay_ms(20);
}

void LCDclear()
{
   LCDchar(0x01);
   _delay_ms(5);
}

void LCDhome()
{
   LCDchar(0x10);
   _delay_ms(5);
}

void LCDchar(char c)
{
   LCDdatap = c;
   pulse_ms(LCDctrlp, LCDen, 5);
}


Thanks so much in advance for taking a look ;D.
GTW

Offline HDA

  • Jr. Member
  • **
  • Posts: 24
  • Helpful? 0
Re: LCD initialization
« Reply #1 on: March 03, 2010, 08:37:18 AM »
Hi,

Do you use 4 or 8 bits to drive your LCD ?
could you send the .prj from VMLAB ?

Offline GearMotion

  • Supreme Robot
  • *****
  • Posts: 489
  • Helpful? 24
  • Two decades+ of Embedded Design
Re: LCD initialization
« Reply #2 on: March 03, 2010, 12:13:55 PM »
Hi,

Do you use 4 or 8 bits to drive your LCD ?
could you send the .prj from VMLAB ?


Code: [Select]

for(int a = 0; a < 3; a++)
   {
      LCDchar(0x30);
      _delay_ms(20);
   }

Looks like 8 is expected.

This is an init sequence, but you don't try to display any data. Flip LCDrs and write out some text.

I use 0x33 0x33 0x38 0x0C 0x01 as an init sequence for a common display that I use - HD-44780 compatible.

Some other displays have different sequences.




Offline GWER57Topic starter

  • Full Member
  • ***
  • Posts: 65
  • Helpful? 2
Re: LCD initialization
« Reply #3 on: March 03, 2010, 12:53:47 PM »

This is an init sequence, but you don't try to display any data. Flip LCDrs and write out some text.


So to write text to the lcd rs has to be high? That might be why it is not working because rs has been low when ive tried to write text to the lcd.
GTW

Offline GearMotion

  • Supreme Robot
  • *****
  • Posts: 489
  • Helpful? 24
  • Two decades+ of Embedded Design
Re: LCD initialization
« Reply #4 on: March 03, 2010, 12:57:56 PM »
Yes. With the LCD RS pin low the things that you write are commands. With it high then the things that you write are characters.


Offline GearMotion

  • Supreme Robot
  • *****
  • Posts: 489
  • Helpful? 24
  • Two decades+ of Embedded Design
Re: LCD initialization
« Reply #5 on: March 03, 2010, 01:02:37 PM »
You might want something like this:

Code: [Select]
void LCDcommand(char c)
{
   // Talk to the control register
   clear(LCDctrlp, LCDrs);

   LCDdatap = c;
   pulse_ms(LCDctrlp, LCDen, 5);
}

Code: [Select]
void LCDchar(char c)
{
   // Send character to display
   set(LCDctrlp, LCDrs);

   LCDdatap = c;
   pulse_ms(LCDctrlp, LCDen, 5);
}

Change your init stuff to use LCDcommand, then write to the display using LCDchar.

Offline GWER57Topic starter

  • Full Member
  • ***
  • Posts: 65
  • Helpful? 2
Re: LCD initialization
« Reply #6 on: March 03, 2010, 01:05:31 PM »
The initialization sequence you use, will it work for a 2x16 8-bit controlled lcd? more specifically, the one from futurlec (HD-44780 compatable).

Thanks for your help!
GTW

Offline GearMotion

  • Supreme Robot
  • *****
  • Posts: 489
  • Helpful? 24
  • Two decades+ of Embedded Design
Re: LCD initialization
« Reply #7 on: March 03, 2010, 01:10:52 PM »
0x33 - 8 bit interface
0x33 - 8 bit interface again to be sure
0x38 - 8 bit interface, 2 lines
0x0C - Display on, blink off, cursor off
0x01 - Clear and set to 'home'

That should work OK.

Offline GWER57Topic starter

  • Full Member
  • ***
  • Posts: 65
  • Helpful? 2
Re: LCD initialization
« Reply #8 on: March 03, 2010, 01:12:51 PM »
Thank you so much! It finally works! I can't wait to get this going on the real deal.
GTW

Offline GearMotion

  • Supreme Robot
  • *****
  • Posts: 489
  • Helpful? 24
  • Two decades+ of Embedded Design
Re: LCD initialization
« Reply #9 on: March 03, 2010, 01:17:31 PM »
Thank you so much! It finally works! I can't wait to get this going on the real deal.

I'm glad that worked for you!

I remember the elation when I first got a character LCD to work. Sometime back in the 80s. I had very sketchy documentation and had to resort to using DIP switches on a solderless board and clocking in everything by hand.

LCDs are cool once they work. But since so many look like they are doing NOTHING if you don't get the initialization right, it can be frustrating.



 


Get Your Ad Here

data_list