Đây là code LCD4bit , mình chỉnh lại từ code của anh Linh
Code:
#define E PIN_D0
#define RS PIN_D1
#define RW PIN_D2
#define LCD_D4 PIN_D4
#define LCD_D5 PIN_D5
#define LCD_D6 PIN_D6
#define LCD_D7 PIN_D7
void LCD_Init ( void ); // ham khoi tao LCD
void LCD_SetPosition ( unsigned int data );//Thiet lap vi tri con tro
void LCD_PutChar ( unsigned int data ); // Ham viet1kitu/1chuoi len LCD
void LCD_PutCmd ( unsigned int data ) ; // Ham gui lenh len LCD
void send_to_LCD( unsigned int data);
void LCD_Init ( void )
{
delay_ms(200); // thoi gian de LCD khoi dong
output_low(RW); // RW = 0
output_low(RS); // che do gui lenh
send_to_LCD( 0x00 );
output_high ( E ); output_low ( E ); delay_ms ( 10 );
send_to_LCD( 0x03 ); /* init with specific nibbles to start 4-bit mode */
output_high ( E ); output_low ( E ); delay_ms ( 3 ); // tao xung tren chan E
output_high ( E ); output_low ( E ); delay_ms ( 3 );
output_high ( E ); output_low ( E ); delay_ms ( 3 );
send_to_LCD( 0x02 ); /* set 4-bit interface */
output_high ( E ); output_low ( E ); delay_ms ( 3 ); // tao xung tren chan E
LCD_PutCmd ( 0x2C ); /* function set (all lines, 5x7 characters) */
LCD_PutCmd ( 0x0C); /* display ON, cursor off, no blink */
LCD_PutCmd ( 0x06 ); /* entry mode set, increment & scroll left */
LCD_PutCmd ( 0x01 ); /* clear display */
}
void LCD_SetPosition ( unsigned int data )
{
send_to_LCD( swap ( data ) | 0x08 );
output_high ( E ); output_low ( E ); delay_ms ( 3 ); // tao xung tren chan E
send_to_LCD( swap ( data ) );
output_high ( E ); output_low ( E ); delay_ms ( 3 ); // tao xung tren chan E
}
void LCD_PutChar ( unsigned int data )
{
output_high( RS ); //RS = 1
output_low ( RW ); //RW = 0
send_to_LCD( swap ( data ) ); // gui 4 bit cao
output_high ( E ); output_low ( E ); delay_ms ( 3 ); // tao xung tren chan E
send_to_LCD( swap ( data ) ); //gui 4 bit thap
output_high ( E ); output_low ( E ); delay_ms ( 3 ); // tao xung tren chan E
}
void LCD_PutCmd ( unsigned int data )
{
output_low(RS); // RS = 0
output_low(RW); // RW = 0
send_to_LCD( swap(data) ); // gui 4 bit cao
output_high (E); output_low (E); delay_ms (3);
send_to_LCD( swap(data) ); // gui 4 bit thap
output_high (E); output_low (E); delay_ms ( 3 );
}
void send_to_LCD( unsigned int data )
{
output_bit ( LCD_D4, data & 0x01 );
output_bit ( LCD_D5, data & 0x02 );
output_bit ( LCD_D6, data & 0x04 );
output_bit ( LCD_D7, data & 0x08 );
}
bạn thick xuất PORT nào thì cứ thế mà chỉnh lại