LCD1602顯示電路圖及程序
電路圖
程序:
#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)
{ // 延時(shí)子程序
uchar i ;
while(ms--)
{
for(i = 0 ; i<250;i++) ;
}
}
void longdelay(uchar s) //長延時(shí)
{
while(s--)
{
delay(60) ;
}
}
BOOL lcd_bz()//測試LCD忙碌狀態(tài)
{
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)//寫字節(jié)
{
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)//在某一地址上顯示一字節(jié)
{
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)
}
}