#include<reg52.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit lcd_rs=P3^5; //数据命令选择口
sbit lcd_rw=P3^6; //读写选择口
sbit lcd_en=P3^4; //使能信号
sbit lcd_psb=P3^7; //串/并选择端
sbit wela=P2^6;
sbit dula=P2^7;
sbit diola=P2^5;
uchar i;
uchar code time3[]={"一二三四五六七"};
void delay(uint z) //延时函数
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void delay4us() //短延时
{
_nop_();
_nop_();
_nop_();
_nop_();
}
/* 检查LCD忙状态 */
/*lcd_busy为1时为忙,为0时为闲,可写指令数据*/
void lcd_busy()
{
uchar busy;
do
{
lcd_en=0;
lcd_rs=0;
lcd_rw=1;
delay4us();
lcd_en=1;
busy=P0; //在使能端的上升沿读走数据
delay4us();
lcd_en=0;
}
while(busy&0x80);
}
/*写指令到12864:RS=L,RW=L,E=高脉冲*/
void write_command(uchar cmd)
{
lcd_busy();
lcd_rs=0;
lcd_rw=0;
lcd_en=0;
P0=cmd;
delay4us();
lcd_en=1;
delay4us();
lcd_en=0;
}
/*写显示数据到12864:RS=H,RW=L,E=高脉冲*/
void write_data(uchar dat)
{
lcd_busy();
lcd_rs=1;
lcd_rw=0;
lcd_en=0;
P0=dat;
delay4us();
lcd_en=1;
delay4us();
lcd_en=0;
}
/*设定显示位置*/
void lcd_position(uchar x,uchar y)
{
if(x==0)
write_command(0x80+y);
else if (x==1)
write_command(0x90+y);
else if (x==2)
write_command(0x88+y);
else if (x==3)
write_command(0x98+y);
}
|