lcd (16x2 @ 4 bit) c programming
I'm working on something similar, but I'm having trouble getting my LCD (Sanyo DM1622, datasheet at
http://www.datasheetcatalog.com/datasheets_pdf/D/M/1/6/DM1622.shtml) to display anything besides 2 rows of 16 squares. Mainly I think it's the initialisation process that's tripping me up.
Pin setup is as follows:
1 GND
2 +5V
3 -5V
4 RA2
5 GND
6 RA3
7->10 GND
11->14 RB0->RB3
15 5V
16 GND
I don't understand some parts of the OP's code.
void IniPort()
{
ADCON1=0x06;
TRISA=0x00;
TRISB=0x00;
PORTA=0x00;
PORTB=0x00;
}
lcd_init(void)
{
LCD_RS = 0; // write control bytes
Why do you need to set LCD_RS = 0 in lcd_init again? When you set PORTA=0x00 in IniPort, isn't LCD_RS already set to 0?
void
lcd_init(void)
{
LCD_RS = 0; // write control bytes
DelayMs(15); // power on delay
PORTB = 0x2; // attention! Was originally 0x3, but laktronics suggested changing it to 0x2. Why?
LCD_STROBE; Step 1
DelayMs(5);
LCD_STROBE; Step 2
DelayUs(100);
LCD_STROBE; Step 3
DelayMs(5);
PORTB = 0x2; // set 4 bit mode
LCD_STROBE; Step 4
DelayUs(40);
lcd_write(0x28); // 4 bit mode, 1/16 duty, 5x8 font
lcd_write(0x08); // display off
lcd_write(0x0F); // display on, blink curson on
lcd_write(0x06); // entry mode
}
Right now I'm interpreting the lcd_init function as doing the following.
Step 1:
DB7 DB6 DB5 DB4 DB3 DB2 DB1 DB0
0 0 1 0 ? ? ? ?
Nothing happens yet, only first 4 bits written
Step 2:
DB7 DB6 DB5 DB4 DB3 DB2 DB1 DB0
0 0 1 0 0 0 1 0
The LCD is given the command to do a function set of 4 bits, 1 line, 5*7 dots.
Step 3
DB7 DB6 DB5 DB4 DB3 DB2 DB1 DB0
0 0 1 0 ? ? ? ?
Nothing happens yet, only first 4 bits written
Step 4
DB7 DB6 DB5 DB4 DB3 DB2 DB1 DB0
0 0 1 0 0 0 1 0
The LCD is given the command to do a function set of 4 bits, 1 line, 5*7 dots.
From the information I get from this page
http://www.8051projects.net/lcd-interfacing/lcd-4-bit-programming.php
the initialisation for 4-bit mode for a similar LCD is as follows
void lcd_reset()
{
lcd_port = 0xFF;
delayms(20);
lcd_port = 0x03+LCD_EN;
lcd_port = 0x03;
delayms(10);
lcd_port = 0x03+LCD_EN;
lcd_port = 0x03;
delayms(1);
lcd_port = 0x03+LCD_EN;
lcd_port = 0x03;
delayms(1);
lcd_port = 0x02+LCD_EN;
lcd_port = 0x02;
delayms(1);
}
How do you have
lcd_port = 0xFF;
when you're only using 4 bits?