LCD1602液晶显示DS1302实时时钟Proteus仿真
仿真程序:
#include
#include
#include
#defineuintunsignedint
#defineucharunsignedchar
//*******LCD*******
sbitLCDRS=P2^0; //LCD数据/命令选择端
sbitLCDRW=P2^1;
sbitLCDEN=P2^2; //LCD使能新号端
//******DS1302******
sbitIO=P1^0; //1302数据线
sbitSCLK=P1^1; //1302时钟线
sbitRST=P1^2; //复位
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读取的当前日期时间
//=======延时=======
voiddelay(uintz)
{
uintx,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
//********向1302写数据*********
voidwrite_ds1302(uchardat)
{
uchari;
for(i=0;i<8;i++)
{
IO=dat&0x01; //保持最后一位为1,读状态
SCLK=1;
delay(1);
SCLK=0;
dat>>=1;
}
}
//*********从1302读数据*********
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码转换
}
//*********从指定位置读数据********
ucharread_data(ucharaddr)
{
uchardat;
RST=0;
SCLK=0;
RST=1;
write_ds1302(addr);
dat=read_ds1302();
SCLK=1;
RST=0;
returndat;
}
//*********读取当前日期时间*********
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时,写指令,RS为1时,写数据
P0=com;
delay(5);
LCDEN=1;
delay(5);
LCDEN=0;
}
//----------LCD写数据----------
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显示模式设置
lcd_write_com(0x0c); //LCD显示开/关及光标设置
lcd_write_com(0x06); //当写一个字符后地址指针加1,且光标加1
lcd_write_com(0x01); //显示清屏
}
//---------设置液晶显示位置-----------
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);
}
}
//----------日期时间转换-----------
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); //时间转换
format_datetime(date_time[1],Time+10);
format_datetime(date_time[0],Time+13);
lcd_print(0x00,Date);
lcd_print(0x40,Time);
}
}
LCD1602液晶显示DS1302实时时 相关文章:
- Windows CE 进程、线程和内存管理(11-09)
- RedHatLinux新手入门教程(5)(11-12)
- uClinux介绍(11-09)
- openwebmailV1.60安装教学(11-12)
- Linux嵌入式系统开发平台选型探讨(11-09)
- Windows CE 进程、线程和内存管理(二)(11-09)