char num1[10]=\
unsigned char time1[7]={0x50,0x59,0x23,0x04,0x11,0x02,0x14}; uint second,hour,min,num,a,time[7]; uint nian,yue,ri,shi,fen,miao,zhou;
void delay(uint z) {
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--); }
void writecom(uchar com) {
LCDE=0; RS=0; RW=0; PIN=com; delay(10); LCDE=1; delay(10); LCDE=0; }
void writedata(uchar dat) {
LCDE=0; RS=1; RW=0; PIN=dat; delay(10); LCDE=1; delay(10); LCDE=0; RS=0; }
void LCDINIT() {
delay(1000);
//延时程序 //LCD写命令 //LCD写数据 //初始化LCD 16
LCDE=0; RW=0;
writecom(0x38);设置显示模式
writecom(0x0c);不显示光标,光标不闪烁 writecom(0x06);写一个指针加1 writecom(0x01);清屏
writecom(0x80);设置数据指针起点 }
void ds1302writebyte(uint dat) {
uint i; sck=0; _nop_(); _nop_();
for(i=0;i<8;i++) {
io=dat&0x01; dat>>=1; _nop_(); _nop_(); sck=1; _nop_(); _nop_(); sck=0; _nop_();
_nop_(); } }
uint ds1302readbyte() //1302{
uint i,value; sck=0; _nop_(); _nop_();
for(i=0;i<8;i++)
//ds1302写单个数据读单个数据 17
{
value>>=1; if(io) {
value=value|0x80; } sck=1; _nop_(); _nop_(); sck=0; _nop_(); _nop_(); }
return value; }
void ds1302wr(uint add,uint dat) //多个字节写入 {
RST=0; _nop_(); sck=0; _nop_(); RST=1;
_nop_();
ds1302writebyte(add); ds1302writebyte(dat); RST=0; _nop_(); }
uint ds1302rd(uint add) //读取多个数据 {
uint ks; RST=0; _nop_(); sck=0; _nop_();
18
RST=1; _nop_();
ds1302writebyte(add); ks=ds1302readbyte(); RST=0; _nop_();
io=0; //此条语句必不可少。如果少了这条语句的话,液晶显示会不完全。
_nop_(); io=1; _nop_(); return ks; }
void Ds1302Init() {
unsigned char n;
ds1302wr(0x8E,0X00); //禁止写保护,就是关闭写保护功能 for(n=3;n<7;n++)//写入7个字节的时钟信号:分秒时日月周年 {
ds1302wr(w[n],time1[n]); }
ds1302wr(0x8E,0x80); //打开写保护功能 }
void rdtime() //1302读初始时间 {
miao=ds1302rd(r[0]); fen=ds1302rd(r[1]); shi=ds1302rd(r[2]); zhou=ds1302rd(r[5]); ri=ds1302rd(r[3]); yue=ds1302rd(r[4]);
nian=ds1302rd(r[6]); }
void display() {
//显示函数
19
int i,j,a[8];
writecom(0x80);
a[0]=(num1[(nian)/16]); // 年
a[1]=(num1[(nian)]); a[2]=0X3A;
a[3]=num1[yue/16]; //月 a[4]=num1[yue]; a[5]=0X3A;
a[6]=num1[ri/16]; //日 a[7]=num1[ri]; for( i=0;i<8;i++) {
writedata(a[i]); delay(10); }
writecom(0x80+0x40+7);
a[0]=(num1[(shi)/16]); //小时 a[1]=(num1[(shi)]); a[2]=0X3A;
a[3]=num1[fen/16]; //分钟 a[4]=num1[fen]; a[5]=0X3A;
a[6]=num1[miao/16]; 秒钟
a[7]=num1[miao]; for(j=0;j<8;j++) {
writedata(a[j]); } }
void showtime(uint address,uint time) {
delay(500);
writedata(num1[time]); writecom(0x80+0x40+address);
20
//