Điện Tử Tổng Hợp
Giao tiếp với LCD 16×2 , 16×4 chi tiết
Giao tiếp với LCD 16×2 , 16×4 chi tiết hướng dẫn bên dưới đây hãy cùng tham khảo với hocdientu nhé ! Hôm nay tôi sẽ hướng dẫn các bạn cách giao tiếp LCD 16×2 với vi điều khiển PIC16F877A ở chế độ 4 bit.
Giao tiếp với LCD 16×2
I. Cách kết nối VĐK PIC với LCD16x2 ở chế độ 4bit.

II. Các hàm chính điều khiển LCD:
– Hàm khởi tạo LCD :
HTML:
void LCD_Init ( void ) { output_drive(LCD_D4); output_drive(LCD_D5); output_drive(LCD_D6); output_drive(LCD_D7); output_drive(LCD_EN); output_drive(LCD_RS); output_drive(LCD_RW); LCD_Send4Bit(0x00); delay_ms(20); output_low(LCD_RS); output_low(LCD_RW); LCD_Send4Bit(0x03); LCD_Enable(); delay_ms(5); LCD_Enable(); delay_us(100); LCD_Enable(); LCD_Send4Bit(0x02); LCD_Enable(); LCD_SendCommand( 0x28 ); // giao thuc 4 bit, hien thi 2 hang, ki tu 5x8 LCD_SendCommand( 0x0c); // cho phep hien thi man hinh LCD_SendCommand( 0x06 ); // tang ID, khong dich khung hinh LCD_SendCommand( 0x01 ); // xoa toan bo khung hinh }
– Hàm xóa LCD :
HTML:
void LCD_Clear() { LCD_SendCommand(0x01); delay_ms(10); }
– Hàm thiết lập vị trí con trỏ LCD:
HTML:
void LCD_Gotoxy(unsigned char x, unsigned char y) { unsigned char address; if(!y) address = (0x80+x); else address = (0xC0+x); delay_us(1000); LCD_SendCommand(address); delay_us(50); }
– Hàm gửi một kí tự ra LCD:
HTML:
void LCD_PutChar ( unsigned char Data ) { output_high(LCD_RS); LCD_SendCommand( Data ); output_low(LCD_RS); }
– Hàm gửi một chuỗi kí tự ra LCD:
HTML:
void LCD_Puts (char *s) { while (*s) { LCD_PutChar(*s); s++; } }
III. Bài toán ví dụ:
Để minh họa cách sử dụng các hàm trên tôi sẽ đưa ra một bài toán ví dụ sau:
– Hiển thị dòng chữ BanLinhKien.Vn lên LCD hàng 1 của LCD.
– Hiển thị giá trị của một biến x (x: 0-99) lên hàng thứ 2 của LCD, giá trị của x sẽ tăng lên 1 mỗi nửa giây.
– Đây là code của bài toán trên:
HTML:
void main() { int8 str[20]; int8 x; LCD_Init(); delay_ms(10); sprintf(str,"LCD DEMO"); LCD_Gotoxy(4,0); LCD_Puts(str); delay_ms(1000); LCD_Clear(); LCD_Gotoxy(1,0); sprintf(str,"BanLinhKien.Vn"); LCD_Puts(str); LCD_Gotoxy(1,1); sprintf(str,"Gia Tri x= "); LCD_Puts(str); while(TRUE) { LCD_Gotoxy(11,1); sprintf(str,"%2d",x); LCD_Puts(str); delay_ms(500); x++; if(x==100)x=0; } }
Giao tiếp LCD 16×4
Hướng dẫn sử dụng thư viện:
- Nên chép thư viện vào: “C:\Program Files\PICC\Drivers”
- Khai báo tiền xử lý:
#define LCD_RS PIN_xx
#define LCD_EN PIN_xx
#define LCD_D4 PIN_xx
#define LCD_D5 PIN_xx
#define LCD_D6 PIN_xx
#define LCD_D7 PIN_xx
#include<LCD1604.c>
#define LCD_EN PIN_xx
#define LCD_D4 PIN_xx
#define LCD_D5 PIN_xx
#define LCD_D6 PIN_xx
#define LCD_D7 PIN_xx
#include<LCD1604.c>
- Khai báo 1 mảng: unsigned char string[17];
- Gọi hàm này khi khởi động: LCD_Init();
- Các hàm hiển thị ra LCD:
LCD_Gotoxy([vị trí 0-15], [hàng 0-3]);
sprintf(string,”chuỗi cần hiển thị”);
LCD_Puts(string);
LCD_Clear_All();//Xoá toàn màn hình.
LCD_Clear_Row(hàng [0;3]);//Xoá hàng.
sprintf(string,”chuỗi cần hiển thị”);
LCD_Puts(string);
LCD_Clear_All();//Xoá toàn màn hình.
LCD_Clear_Row(hàng [0;3]);//Xoá hàng.