I am trying to interface a 16x4 LCD with an atmega32 chip. I wrote the following code which used to work for 16x2 LCD
#include<avr/io.h>
#include<util/delay.h>
#define dataport PORTC // LCD data port
#define commport PORTD
#define en PD6 // enable signal
#define wr PD5 // read/write signal
#define rs PD4 // register select signal
int wrcomm(unsigned int data)
{
dataport=data;
_delay_ms(10);
commport &= ~(1 << rs);
commport &= ~(1 << wr);
commport |= (1 << en);
commport &= ~(1 << en);
_delay_ms(10);
return 1;
}
int wrdata(unsigned int data)
{
dataport=data;
_delay_ms(10);
commport |= (1 << rs);
commport &= ~(1 << wr);
commport |= (1 << en);
commport &= ~(1 << en);
_delay_ms(10);
return 1;
}
int lcd_putchar(unsigned char x)
{
wrdata(x);
return 1;
}
void lcd_init(void)
{
DDRC=0xff;
DDRD=0x70;
wrcomm(0x38);
wrcomm(0x01); //turn on display and cursor
wrcomm(0x0E);//cursor line 1 postn 1
wrcomm(0x80);
}
int main(void)
{
lcd_init();
lcd_putchar('a');
return 0;
}
I am guessing that it should work for 16x4 LCD too. But it doesn't. The LCD is a JHD539 164b.
The one thing I have made progress with is that if I do not send any command the 2nd and 4th line remain all black but if I send any command they are all off. The pot is not helping at all.