电路图
程序:
#include reg52.h
#define uchar unsigned char
typedef bit BOOL ;
sbit rs = P2^6 ;
sbit rw = P3^6 ;
sbit ep = P2^7 ;
void delay(uchar ms)
{ // 延时子程序
uchar i ;
while(ms--)
{
for(i = 0 ; i250;i++) ;
}
}
void longdelay(uchar s) //长延时
{
while(s--)
{
delay(60) ;
}
}
BOOL lcd_bz()//测试LCD忙碌状态
{
BOOL result ;
rs = 0 ;
rw = 1 ;
ep = 1 ;
result = (BOOL)(P0 0x80) ;
ep = 0 ;
return result ;
}
void write_cmd(uchar cmd)// 写指令
{
while(lcd_bz()) ;
rs = 0 ;
rw = 0 ;
ep = 0 ;
P0 = cmd ;
ep = 1 ;
ep = 0 ;
}
void write_addr(uchar addr)//写地址
{
write_cmd(addr|0x80) ;
}
void write_byte(uchar dat)//写字节
{
while(lcd_bz()) ;
rs = 1 ;
rw = 0 ;
ep = 0 ;
P0 = dat ;
ep = 1 ;
ep = 0 ;
}
void lcd_init()// 初始化
{
write_cmd(0x38) ;
delay(1);
write_cmd(0x08) ;
delay(1);
write_cmd(0x01) ;
delay(1);
write_cmd(0x06) ;
delay(1);
write_cmd(0x0c) ;
delay(1);
}
void display(uchar addr, uchar q)//在某一地址上显示一字节
{
delay(10) ;
write_addr(addr) ;
write_byte(q) ;
longdelay(2) ;
}
main()
{
lcd_init() ; // 初始化LCD
delay(10) ;
// lcd_wcmd(0x07) ; //entry mode set
delay(1) ;
while(1)
{
display(0x00,'R') ;//在第一个位置显示R
display(0x01,0x31)// 在第二个位置显示1,(1的ASCII码为31)
}
}