Code LCD hoàn chỉnh, có cả nạp CGRAM (tự tạo các kí tự mới)
#include <REGX51.H>
#include <stdio.h>
#include <string.h>
//------------Khoi tao bien va cac gia tri gan-------------//
sbit RS= P1^2;
sbit RW= P1^1;
sbit EN= P1^0;
//---------------Sau day la cac chuong trinh con------------//
//--------------Ham tre khoang thoi gian rat ngan phuc vu viec kiem tr co ban---------------//
void delay_short()
{
unsigned int i;
for(i=0;i<3;i++);
}//--------------****------------------//
//------------Ham tre 2---------------//
void delay(unsigned int time) // Thoi gian time ms
{
TMOD = 0x01; // Timer 0 che do 1
while (time--)
{
TL0=0x18;
TH0=0xfc;
TR0 = 1;
while (!TF0);
TR0 = 0;
TF0 = 0;
}
}
//-----------------******----------------//
//--------------Ham kiem tra co ban (busy_flag)--------------??
void wait()
{
unsigned char x;
P2 = 0xff;
RS= 0;
RW= 1;
do
{
EN= 1;
delay(10);
EN = 0;
x=P2;
x=x&0x80;
}
while(x!=0x80);
}//-----------*******--------------------//
//---------------Ham ghi lenh giao tiep voi lcd_---------------//
void lcd_control(unsigned char c)
{
wait();//?oi lcd_ san sang.
P2 = c; // Chon che do ghi du lieu tu 8051 vao lcd_
RS = 0; // Chon thanh ghi lenh
RW = 0;
EN = 1;
delay_short();
EN = 0;
}//------------*******-----------------//
//-----------Ham ghi ky tu ra lcd_------------//
void lcd_write(unsigned char c)
{
RS=1;//ghi du lieu
RW=0;//ghi du lieu len lcd_
P2=c;//gia tri C
EN=1;//cho phep muc cao
delay_short();
EN=0;//xung cao xuong thap
wait();//cho
}
//-----------Ham ghi chuoi ky tu ra lcd_------------//
void lcd_writes(unsigned char *s)
{
unsigned char data lens,count;
lens=strlen(s);
for(count=0;count<lens;count++)
{
lcd_write(*(s+count));
}
}
//-----------Ham ghi lan luot tung ky tu trong chuoi ky tu ra lcd_------------//
void lcd_writes2(unsigned char *s)
{
unsigned char data lens,count;
lens=strlen(s);
for(count=0;count<lens;count++)
{
lcd_write(*(s+count));
delay(20);
}
}
//-----------************------------//
//-----------Ham ghi so nguyen ra lcd_------------//
void lcd_writen(unsigned char so)
{
unsigned char a,b,c;
a=so/100; // Lay phan tram
b=(so-100*a)/10; // Lay phan chuc
c=(so-100*a-10*b); // Lay phan don vi
lcd_write(a+0x30); // Doi ra hang tram, ma ascii
lcd_write(b+0x30); // Doi ra hang chuc, ma ascii
lcd_write(c+0x30); // Doi ra hang don vi, ma ascii
}
//-----------Ham dieu chinh toa do con tro------------//
void lcd_move(char a,char b)
{
if (a==0) lcd_control(0x80+b);
else if (a==1) lcd_control(0xc0+b);
}
//-----------chuoi cac ki tu nap vao cgram------------//
unsigned char cgram[64]={ 0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0};
//-----------Ham nap cgram------------//
void nap_cgram(unsigned char const *c)
{
unsigned char n=0;
lcd_control(0x40); //dia chi bat dau ghi CGRAM
while (n!=64)//chua ghi du 8 ki tu user
{
lcd_write(c[n]);
n++;
}
}
//-----------Ham khoi tao lcd------------//
void lcd_init()
{
lcd_control(0x38);
lcd_control(0x01); // Xoa man hinh
lcd_control(0x0c); // Co dich hien thi
}
#include <REGX51.H>
#include <stdio.h>
#include <string.h>
//------------Khoi tao bien va cac gia tri gan-------------//
sbit RS= P1^2;
sbit RW= P1^1;
sbit EN= P1^0;
//---------------Sau day la cac chuong trinh con------------//
//--------------Ham tre khoang thoi gian rat ngan phuc vu viec kiem tr co ban---------------//
void delay_short()
{
unsigned int i;
for(i=0;i<3;i++);
}//--------------****------------------//
//------------Ham tre 2---------------//
void delay(unsigned int time) // Thoi gian time ms
{
TMOD = 0x01; // Timer 0 che do 1
while (time--)
{
TL0=0x18;
TH0=0xfc;
TR0 = 1;
while (!TF0);
TR0 = 0;
TF0 = 0;
}
}
//-----------------******----------------//
//--------------Ham kiem tra co ban (busy_flag)--------------??
void wait()
{
unsigned char x;
P2 = 0xff;
RS= 0;
RW= 1;
do
{
EN= 1;
delay(10);
EN = 0;
x=P2;
x=x&0x80;
}
while(x!=0x80);
}//-----------*******--------------------//
//---------------Ham ghi lenh giao tiep voi lcd_---------------//
void lcd_control(unsigned char c)
{
wait();//?oi lcd_ san sang.
P2 = c; // Chon che do ghi du lieu tu 8051 vao lcd_
RS = 0; // Chon thanh ghi lenh
RW = 0;
EN = 1;
delay_short();
EN = 0;
}//------------*******-----------------//
//-----------Ham ghi ky tu ra lcd_------------//
void lcd_write(unsigned char c)
{
RS=1;//ghi du lieu
RW=0;//ghi du lieu len lcd_
P2=c;//gia tri C
EN=1;//cho phep muc cao
delay_short();
EN=0;//xung cao xuong thap
wait();//cho
}
//-----------Ham ghi chuoi ky tu ra lcd_------------//
void lcd_writes(unsigned char *s)
{
unsigned char data lens,count;
lens=strlen(s);
for(count=0;count<lens;count++)
{
lcd_write(*(s+count));
}
}
//-----------Ham ghi lan luot tung ky tu trong chuoi ky tu ra lcd_------------//
void lcd_writes2(unsigned char *s)
{
unsigned char data lens,count;
lens=strlen(s);
for(count=0;count<lens;count++)
{
lcd_write(*(s+count));
delay(20);
}
}
//-----------************------------//
//-----------Ham ghi so nguyen ra lcd_------------//
void lcd_writen(unsigned char so)
{
unsigned char a,b,c;
a=so/100; // Lay phan tram
b=(so-100*a)/10; // Lay phan chuc
c=(so-100*a-10*b); // Lay phan don vi
lcd_write(a+0x30); // Doi ra hang tram, ma ascii
lcd_write(b+0x30); // Doi ra hang chuc, ma ascii
lcd_write(c+0x30); // Doi ra hang don vi, ma ascii
}
//-----------Ham dieu chinh toa do con tro------------//
void lcd_move(char a,char b)
{
if (a==0) lcd_control(0x80+b);
else if (a==1) lcd_control(0xc0+b);
}
//-----------chuoi cac ki tu nap vao cgram------------//
unsigned char cgram[64]={ 0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0};
//-----------Ham nap cgram------------//
void nap_cgram(unsigned char const *c)
{
unsigned char n=0;
lcd_control(0x40); //dia chi bat dau ghi CGRAM
while (n!=64)//chua ghi du 8 ki tu user
{
lcd_write(c[n]);
n++;
}
}
//-----------Ham khoi tao lcd------------//
void lcd_init()
{
lcd_control(0x38);
lcd_control(0x01); // Xoa man hinh
lcd_control(0x0c); // Co dich hien thi
}
Comment