QC

Điều Khiển Hiển Thị LCD 8 BIT 8051

  Chào mừng các bạn với khóa học 8051

II. SƠ ĐỒ KẾT NỐI 8051 VỚI LCD 16x2 8 BIT.



1. SỬ DỤNG CHÂN ĐIỀU KHIỂN ĐƯỢC define.

sbit rs=P3^0;          

sbit rw=P3^1;        

sbit en=P3^2;        

 2. Hàm Delay

void delay(unsigned int count)     /* Function to provide delay Approx 1ms */
{
    int i,j;
     for(i=0;i<count;i++)
     for(j=0;j<112;j++);
}


3.  Hàm điều khiển LCD : Hàm này có chức năng khởi tạo LCD 
LCD: Có 2 dòng dòng 0 và dòng 1. có 16 cột.

 VD : LCD_Command (0x38);  Con trỏ dịch tới đầu dòng 0.
          LCD_Command(0xC0);  Con trỏ dịch tới đầu dòng 1.
          LCD_Command (0x06);  Con trỏ tự động tăng dần VD bạn viết chử " ANH " Vào dòng 0 ,nó viết A vào dòng 0 ,xong con trở tăng  lên  cột 2 ghi N sau đó tăng Lên cột 3 ghi Chứ H...
           LCD_Command (0x01); Clear LCD.

void LCD_Command (unsigned char cmd) 
{
     P2= cmd;
     rs=0;  
     rw=0;
     en=1; 
     delay(1);
     en=0;
    delay(5);
}

4. Hàm ghi String.

void LCD_String (unsigned char *str)  
{
int i;
for(i=0;str[i]!=0;i++)
{
LCD_Char (str[i]);
}
}

Trong đó Chương trình con: 
void LCD_Char (unsigned char char_data)  
{
   P2=char_data;
   rs=1;
   rw=0;
   en=1;   
   delay(1);
   en=0;
   delay(5);
}

5. Hàm Ghi dữ liệu lên LCD Trực Tiếp.

void LCD_String_xy (char row, char pos, char *str)
{
if (row == 0)
LCD_Command((pos & 0x0F)|0x80);  
else if (row == 1)
LCD_Command((pos & 0x0F)|0xC0);
LCD_String(str);
}

6. Hàm Khởi Tạo LCD.
void LCD_Init (void)
{
delay(20);
LCD_Command (0x38);
LCD_Command (0x0C);
LCD_Command (0x06);
LCD_Command (0x01);
LCD_Command (0x80);
}

Vd : LCD_String_xy (0,0,"HELLO") // Dòng 0 , cột 0 bắt đầu hiển thị HELLO con trỏ tăng lên ghi từ Dòng 0, cột 0.

    BÀI TẬP 1 : Viết Chương trình Hiển Thi Lên Dòng 0 LCD Chữ  " HỌC 8051 "
Dòng 1 LCD chữ " CUNG MBED"


           Code Chương Trình
#include<reg51.h>


sbit rs=P3^0;          
sbit rw=P3^1;        
sbit en=P3^2;        


void delay(unsigned int count)    
{
    int i,j;
     for(i=0;i<count;i++)
     for(j=0;j<112;j++);
}


void LCD_Command (unsigned char cmd)  
{
     P2= cmd;
     rs=0;
     rw=0;
     en=1; 
  delay(1);
en=0;
delay(5);
}

void LCD_Char (unsigned char char_data)
{
   P2=char_data;
     
    rs=1;
    rw=0;
en=1;   
  delay(1);
en=0;
delay(5);
}

void LCD_String (unsigned char *str)
{
int i;
for(i=0;str[i]!=0;i++)
{
LCD_Char (str[i]);
}
}

void LCD_String_xy (char row, char pos, char *str)
{
if (row == 0)
LCD_Command((pos & 0x0F)|0x80);
else if (row == 1)
LCD_Command((pos & 0x0F)|0xC0);
LCD_String(str);
}


void LCD_Init (void)
{
delay(20);
//LCD_Command (0x38);
LCD_Command (0x0C);
LCD_Command (0x06);
LCD_Command (0x01);
LCD_Command (0x80);
}



void main()
{

LCD_Init();
    LCD_Command (0x38); // DUA CON TRO VE DONG O
LCD_String("HOC 8051");   
LCD_Command(0xC0);  // DUA CON TRO VE DONG 1
LCD_String("CUNG MBED");


while(1);  
}



KẾT QUẢ THỰC HIỆN: 





BÀI TẬP 2 : Viết Chương trình Hiển Thi Lên Dòng 0 CỘT 5 LCD Chữ  " HỌC 8051 "
Dòng 1 CỘT 5 LCD chữ " CUNG MBED" dùng hàm LCD_String_xy.

CODE CHƯƠNG TRÌNH
#include<reg51.h>


sbit rs=P3^0;          
sbit rw=P3^1;        
sbit en=P3^2;        


void delay(unsigned int count)    
{
    int i,j;
     for(i=0;i<count;i++)
     for(j=0;j<112;j++);
}


void LCD_Command (unsigned char cmd)  
{
     P2= cmd;
     rs=0;
     rw=0;
     en=1; 
  delay(1);
en=0;
delay(5);
}

void LCD_Char (unsigned char char_data)
{
   P2=char_data;
     
    rs=1;
    rw=0;
en=1;   
  delay(1);
en=0;
delay(5);
}

void LCD_String (unsigned char *str)
{
int i;
for(i=0;str[i]!=0;i++)
{
LCD_Char (str[i]);
}
}

void LCD_String_xy (char row, char pos, char *str)
{
if (row == 0)
LCD_Command((pos & 0x0F)|0x80);
else if (row == 1)
LCD_Command((pos & 0x0F)|0xC0);
LCD_String(str);
}


void LCD_Init (void)
{
delay(20);
LCD_Command (0x38);
LCD_Command (0x0C);
LCD_Command (0x06);
LCD_Command (0x01);
LCD_Command (0x80);
}



void main()
{

LCD_Init();
        LCD_String_xy(0,5,"HOC 8051"); // DÒNG 0 CỘT 5
LCD_String_xy(1,5,"CUNG MBED"); // DÒNG 1 CỘT 5

while(1);  
}
 



KẾT QUẢ THỰC HIỆN: 






Nap Code vào PY32F003 dùng Stlink

 Nap Code vào PY32F003 dùng Stlink Bước 1: Cài đặt  KeilC v5.39 theo link sau ( chú ý 5.39 keil c mới nạp ok). https://edge07.111.ir.cdn.ir/...