Học Viện Hàng Không Việt Nam
Khoa Điện – Điện Tử
Học phần: Thực hành VXL-VĐK
Giảng viên: TS. NGUYỄN HỮU CHÂN THÀNH
Bài thực hành 4:
Giao tiếp LCD 4 bit trên KIT UNI-DS3
4.1 Kết nối phần cứng LCD 4 bit trên KIT UNI-DS3
Character LCD 4 bit mode hardware connection
3
4.1 Kết nối phần cứng LCD 4 bit trên KIT UNI-DS3
Character LCD 4 bit mode hardware connection
4
4.1 Kết nối phần cứng LCD 4 bit trên KIT UNI-DS3
Character LCD 4 bit mode hardware connection
5
4.2 Các library LCD 4 bit mikroC
6
4.2 Các library LCD 4 bit mikroC
7
4.2 Các library LCD 4 bit mikroC
These character based LCDs are commonly made using
HD44780 compatible controllers. It allows us to define 8
custom characters in addition to the standard pre-
programmed characters.
CGROM – Character Generator ROM
This is the memory which holds 5×8 or 5×10 dot patterns of
predefined characters in the LCD. It can generate 208 5×8
dot character patterns and 32 5×10 dot character patterns.
8
4.2 Các library LCD 4 bit mikroC
DDRAM – Display Data RAM
This is the memory which holds the character data which is
currently displayed on the LCD screen. Its capacity is 80×8
bits, ie 80 characters.
CGRAM – Character Generator RAM
This memory works similar to CGROM but as this is a RAM
we can modify its data any time. So we can store our
custom character patterns in this memory through
program. We can store up to eight 5×8 character dot
patterns or four 5×10 character dot patterns in this
memory. 9
4.2 Các library LCD 4 bit mikroC
10
4.3 Bài tập mẫu, hiển thị dòng chữ không dấu trên LCD
// LCD module connections
sbit LCD_RS at RD0_bit;
sbit LCD_EN at RD1_bit;
sbit LCD_D4 at RD4_bit;
sbit LCD_D5 at RD5_bit;
sbit LCD_D6 at RD6_bit;
sbit LCD_D7 at RD7_bit;
sbit LCD_RS_Direction at TRISD0_bit;
sbit LCD_EN_Direction at TRISD1_bit;
sbit LCD_D4_Direction at TRISD4_bit;
sbit LCD_D5_Direction at TRISD5_bit;
sbit LCD_D6_Direction at TRISD6_bit;
sbit LCD_D7_Direction at TRISD7_bit;
// End LCD module connections
char txt1[] = "Nguyen Huu";
char txt2[] = "Chan Thanh";
char i; // Loop variable
void Move_Delay() { // Function used for text moving
Delay_ms(500); // You can change the moving speed here
} 11
4.3 Bài tập mẫu, hiển thị dòng chữ không dấu trên LCD
void main(){
Lcd_Init(); // Initialize LCD
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
Lcd_Out(1,1,txt1); // Write text in first row
Lcd_Out(2,4,txt2); // Write text in second row
Delay_ms(2000);
while(1) { // Endless loop
for(i=0; i<8; i++) { // Move text to the left 7 times
Lcd_Cmd(_LCD_SHIFT_LEFT);
Move_Delay();
}
for(i=0; i<8; i++) { // Move text to the right 7 times
Lcd_Cmd(_LCD_SHIFT_RIGHT);
Move_Delay();
}
}
} 12
4.4 Bài tập mẫu, hiển thị dòng chữ tiếng Việt trên LCD
// LCD module connections
sbit LCD_RS at RD0_bit;
sbit LCD_EN at RD1_bit;
sbit LCD_D4 at RD4_bit;
sbit LCD_D5 at RD5_bit;
sbit LCD_D6 at RD6_bit;
sbit LCD_D7 at RD7_bit;
sbit LCD_RS_Direction at TRISD0_bit;
sbit LCD_EN_Direction at TRISD1_bit;
sbit LCD_D4_Direction at TRISD4_bit;
sbit LCD_D5_Direction at TRISD5_bit;
sbit LCD_D6_Direction at TRISD6_bit;
sbit LCD_D7_Direction at TRISD7_bit;
// End LCD module connections
const char character0[] = {4,10,0,14,17,30,16,15};
const char character1[] = {10,20,0,22,25,17,17,17};
const char character2[] = {10,20,1,1,18,18,18,13};
const char character3[] = {4,10,0,14,18,18,15,0};
const char character4[] = {4,2,0,14,18,18,15,0}; 13
4.4 Bài tập mẫu, hiển thị dòng chữ tiếng Việt trên LCD
void CustomChar0() {
char i;
Lcd_Cmd(64);
for (i = 0; i<=7; i++) Lcd_Chr_CP(character0[i]);
Lcd_Cmd(_LCD_RETURN_HOME);
}
void CustomChar1() {
char i;
Lcd_Cmd(72);
for (i = 0; i<=7; i++) Lcd_Chr_CP(character1[i]);
Lcd_Cmd(_LCD_RETURN_HOME);
}
void CustomChar2() {
char i;
Lcd_Cmd(80);
for (i = 0; i<=7; i++) Lcd_Chr_CP(character2[i]);
Lcd_Cmd(_LCD_RETURN_HOME);
}
14
4.4 Bài tập mẫu, hiển thị dòng chữ tiếng Việt trên LCD
void CustomChar3() {
char i;
Lcd_Cmd(89);
for (i = 0; i<=7; i++) Lcd_Chr_CP(character3[i]);
Lcd_Cmd(_LCD_RETURN_HOME);
}
void CustomChar4() {
char i;
Lcd_Cmd(97);
for (i = 0; i<=7; i++) Lcd_Chr_CP(character4[i]);
Lcd_Cmd(_LCD_RETURN_HOME);
}
void main() {
Lcd_Init(); // Initialize LCD
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
//Send Custom Charactors to CGRAM
CustomChar0(); //chu e
CustomChar1(); //chu n nga
CustomChar2(); //chu u
CustomChar3(); //chu a
CustomChar4(); //chu a huyen
15
4.4 Bài tập mẫu, hiển thị dòng chữ tiếng Việt trên LCD
//Hien thi Nguyen Huu Chan Thanh co dau tieng Viet
Lcd_Out(1, 1, "Nguy");
Lcd_Chr(1, 5, 0); //chu e, OK
Lcd_Chr(1, 6, 1); //chu n nga
Lcd_Out(1, 8, "H");
Lcd_Chr(1, 9, 2); //chu u nga
Lcd_Chr(1, 10, 'u');
Lcd_Out(2, 1, "Ch");
Lcd_Chr(2, 3, 3); //chu a
Lcd_Out(2, 4, "n Th");
Lcd_Chr(2, 8, 4); //chu a huyen
Lcd_Out(2, 9, "nh");
16
4.5 Bài tập lập trình điều khiển giao tiếp LCD 4 bit:
Exercises
Viết chương trình đọc trạng thái 8 nút nhấn kết nối port J và điều khiển hiển
thị LCD 4 bit mode như sau:
1/ nút nhấn PJ0 active: Hiển thị VN AVIATION ACADEMY trên LCD
2/ nút nhấn PJ1 active: Điều khiển dòng chữ dịch trái
3/ nút nhấn PJ2 active: Điều khiển dòng chữ dịch phải
4/ nút nhấn PJ3 active: Điều khiển dòng chữ chớp tắt
5/ nút nhấn PJ4 active: Hiển thị HV Hàng Không Việt Nam bằng tiếng Việt trên
LCD
6/ nút nhấn PJ5 active: Hiển thị tên thành viên 1 và 2 trong nhóm bằng tiếng
Việt ở hàng 1
7/ nút nhấn PJ6 active: Hiển thị tên thành viên 3 và 4 trong nhóm bằng tiếng
Việt ở hàng 2
8/ nút nhấn PJ7 active: Điều khiển tên các thành viên trong nhóm dịch trái,
sau đó dịch phải, cuối cùng chớp tắt 3 lần. 17