文档库 最新最全的文档下载
当前位置:文档库 › 51单片机LCD1602显示程序

51单片机LCD1602显示程序

#include
#include
#define uint unsigned int
#define uchar unsigned char
#define Port P0
sbit LCD_RS = P2^6;
sbit LCD_RW = P2^5;
sbit LCD_EN = P2^7;
sbit K1=P3^0;
sbit K2=P3^1;
sbit K3=P3^2;
uchar T[9]={""};
uchar cnt,Time_s=0,Time_m=0,Time_h=0;
void delay(int i)
{
while (i--);
}

uchar changedate(void)
{
//uchar a,b,c,d,e,f;
T[8]=(char)(0x30+Time_s%10);
T[7]=(char)(0x30+Time_s/10);
T[6]=':';//(char)(0x30+Time_s%10);

T[5]=(char)(0x30+Time_m%10);//(char)(0x30+Time_s/10);
T[4]=(char)(0x30+Time_m/10);
T[3]=':';//(char)(0x30+Time_m/10);

T[2]=(char)(0x30+Time_h%10);
T[1]=(char)(0x30+Time_h/10);
T[0]=' ';
}
void write_cmd(uchar cmd)
{
LCD_RS=0;
LCD_RW=0;
delay(80);

Port=cmd;
LCD_EN=1;
delay(80);
LCD_EN=0;

}
void write_date(uchar date)
{
LCD_RS=1;
LCD_RW=0;
delay(80);

Port=date;
LCD_EN=1;
delay(80);
LCD_EN=0;
}
void init ()
{
LCD_RS=0; /*写入初始化命令*/
LCD_RW=0;
write_cmd(0x01);
//delay(10);
write_cmd(0x38);
//delay(10);
write_cmd(0x0f);
//delay(10);
write_cmd(0x06);


}
void main()
{
uchar i;
K1=1;
write_cmd(0x38);
init();
//if(K1==0)

//write_cmd(0x06);

cnt=Time_s=0;
TMOD=0x01; //设置工作方式1
TH0=(65536-10000)/256;//设定初值10ms
TL0=(65536-10000)%256;
ET0=1;//开启定时器中断允许
EA=1;
TR0=1;//开启定时器
while(1)
{
changedate();
write_cmd(0x80);
for(i=0;i<9;i++)
{
write_date(T[i]);
}
//write_date('A');
}
}

void time0(void) interrupt 1 //1,10ms@12MHz
{
TH0=(65536-10000)/256;//设定初值10ms
TL0=(65536-10000)%256;
cnt++;
if(cnt==100)
{
cnt=0;
//Time_s++;
if(++Time_s==60)
{
Time_s=0;
//Time_m++;
if(++Time_m==60)
{
Time_m=0;
//Time_h++;
if(++Time_h==24)
{
Time_h=0;
}
}
}
}
}

相关文档
相关文档 最新文档