LCD1602液晶顯示DS1302實(shí)時(shí)時(shí)鐘Proteus仿真
仿真程序:
#include
#include
#include
#defineuintunsignedint
#defineucharunsignedchar
//*******LCD*******
sbitLCDRS=P2^0; //LCD數(shù)據(jù)/命令選擇端
sbitLCDRW=P2^1;
sbitLCDEN=P2^2; //LCD使能新號(hào)端
//******DS1302******
sbitIO=P1^0; //1302數(shù)據(jù)線
sbitSCLK=P1^1; //1302時(shí)鐘線
sbitRST=P1^2; //復(fù)位
uintnum=0;
//uchar week[]={"SUN","***","MON","TUS","WEN","THU","FRI","SAT"};
ucharDate[]={"Date:2000-00-00"};
ucharTime[]={"Time:*00:00-00*"};
uchardate_time[7]; //從ds1302讀取的當(dāng)前日期時(shí)間
//=======延時(shí)=======
voiddelay(uintz)
{
uintx,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
//********向1302寫數(shù)據(jù)*********
voidwrite_ds1302(uchardat)
{
uchari;
for(i=0;i<8;i++)
{
IO=dat&0x01; //保持最后一位為1,讀狀態(tài)
SCLK=1;
delay(1);
SCLK=0;
dat>>=1;
}
}
//*********從1302讀數(shù)據(jù)*********
ucharread_ds1302()
{
uchari,b=0x00;
for(i=0;i<8;i++)
{
b|=_crol_((uchar)IO,i);
SCLK=1;
delay(1);
SCLK=0;
}
returnb/16*10+b%16; //與BCD碼轉(zhuǎn)換
}
//*********從指定位置讀數(shù)據(jù)********
ucharread_data(ucharaddr)
{
uchardat;
RST=0;
SCLK=0;
RST=1;
write_ds1302(addr);
dat=read_ds1302();
SCLK=1;
RST=0;
returndat;
}
//*********讀取當(dāng)前日期時(shí)間*********
voidread_date_time()
{
uchari,addr=0x81;
for(i=0;i<7;i++)
{
date_time[i]=read_data(addr);
addr+=2;
}
}
//----------LCD寫指令----------
voidlcd_write_com(ucharcom)
{
LCDRS=0; //RS為0時(shí),寫指令,RS為1時(shí),寫數(shù)據(jù)
P0=com;
delay(5);
LCDEN=1;
delay(5);
LCDEN=0;
}
//----------LCD寫數(shù)據(jù)----------
voidlcd_write_data(uchardat)
{
LCDRS=1;
P0=dat;
delay(5);
LCDEN=1;
delay(5);
LCDEN=0;
}
//-------LCD初始化-------
voidlcd_init()
{
LCDEN=0;
lcd_write_com(0x38); //LCD顯示模式設(shè)置
lcd_write_com(0x0c); //LCD顯示開/關(guān)及光標(biāo)設(shè)置
lcd_write_com(0x06); //當(dāng)寫一個(gè)字符后地址指針加1,且光標(biāo)加1
lcd_write_com(0x01); //顯示清屏
}
//---------設(shè)置液晶顯示位置-----------
voidset_lcd_pos(ucharp)
{
lcd_write_com(p|0x80);
}
//---------液晶顯示程序----------
voidlcd_print(ucharp,uchar*s)
{
set_lcd_pos(p);
for(num=0;num<16;num++)
{
lcd_write_data(s[num]);
delay(10);
}
}
//----------日期時(shí)間轉(zhuǎn)換-----------
voidformat_datetime(uchard,uchar*p)
{
p[0]=d/10+0;
p[1]=d%10+0;
}
//---------主程序----------
voidmain()
{
LCDRW=0;
lcd_init();
while(1)
{
read_date_time();
format_datetime(date_time[6],Date+8); //年月日
format_datetime(date_time[4],Date+11);
format_datetime(date_time[3],Date+14);
// strcpy(Date+13,week[date_time[5]]);
format_datetime(date_time[2],Time+7); //時(shí)間轉(zhuǎn)換
format_datetime(date_time[1],Time+10);
format_datetime(date_time[0],Time+13);
lcd_print(0x00,Date);
lcd_print(0x40,Time);
}
}
評(píng)論