您好,欢迎访问三七文档
当前位置:首页 > 行业资料 > 酒店餐饮 > DS1302编程与PROTEUS仿真
第十章DS1302实时时钟本章主要讲述DS1302的使用方法。C程序://使用uchar数,否则出错//请注意intuintcharuchar的用法。#includereg51.h#includeintrins.h#defineuintunsignedint#defineucharunsignedchar#defineportP0sbitrst=P3^0;//ds1302sbitsclk=P3^1;//时钟图10.1:DS1302显示sbitio=P3^2;//数据口sbith=P3^6;//小时sbitm=P3^7;//分钟sbite=P2^2;//使能sbitrw=P2^1;//读写sbitrs=P2^0;//数据/命令sbitbusy=P0^7;//检测忙inthour=23,minute=51;//-----发送一个字节(8位)------voidsend(uchardt){uchari;for(i=0;i8;i++){sclk=0;io=dt&0x01;//取第一位dt=1;//左移一位sclk=1;//上升沿有效}}//------写地址、数据--------voidwrite(ucharaddr,uchardat){rst=0;sclk=0;rst=1;send(addr);//发送地址send(dat);//发送数据rst=1;}//---读取8位----ucharreceive(){uchari;uchardat=0;//循环8次for(i=0;i8;i++){sclk=1;dat=1;sclk=0;//下降沿读取io=1;//读引脚前置一if(io==1)dat|=0x80;}//此时dat为16进制比如说0x10dat=(dat/16)*10+dat%16;//转化为10进制,现在是10;return(dat);//返回值}//----读时钟------ucharread(ucharaddr){uchardat=0;rst=0;sclk=0;rst=1;send(addr);//发送地址dat=receive();//接收rst=0;return(dat);}//-----1602----------//延时voiddelay(uintn){uinti;for(;n0;n--)for(i=200;i0;i--);}//判断是否忙voidcheck(){rs=0;rw=1;//读e=1;port=0x00;e=1;while(busy);e=0;}//写指令voidsendcommand(ucharcommand){check();rs=0;//指令rw=0;//写e=0;port=command;e=1;_nop_();_nop_();e=0;//写入指令}//写数据voidwritedata(uchardat){check();rs=1;//数据rw=0;port=dat;e=1;_nop_();_nop_();e=0;}//光标位置voidpos(uintp){sendcommand(p|0x80);//第一行第一个0x80;}voidinit(){sendcommand(0x38);//8位数据,双列,5*7字形delay(1);sendcommand(0x0c);//开0x08关delay(1);sendcommand(0x06);//地址增加一delay(1);sendcommand(0x01);//清屏delay(1);}voidwelcome(){inti=0;ucharstring[]=data:;ucharup[]=time:;init();delay(10);pos(0);delay(1);while(string[i]!='\0'){writedata(string[i++]);delay(1);}i=0;delay(10);pos(0x40);delay(1);while(up[i]!='\0'){writedata(up[i++]);delay(1);}}voidwritetime(){inti=0;ucharnum[]=0123456789;ucharsign=':';ucharnote='-';//read(0x81);read(0x83);read(0x85);pos(5);delay(10);delay(1);writedata(num[read(0x8d)/10]);delay(1);writedata(num[read(0x8d)%10]);delay(1);writedata(note);delay(1);writedata(num[read(0x89)/10]);delay(1);writedata(num[read(0x89)%10]);delay(1);writedata(note);delay(1);writedata(num[read(0x87)/10]);delay(1);writedata(num[read(0x87)%10]);delay(10);pos(0x45);delay(1);writedata(num[read(0x85)/10]);delay(1);writedata(num[read(0x85)%10]);delay(1);writedata(sign);delay(1);writedata(num[read(0x83)/10]);delay(1);writedata(num[read(0x83)%10]);delay(1);writedata(sign);delay(1);writedata(num[read(0x81)/10]);delay(1);writedata(num[read(0x81)%10]);}//---------主程序-------voidmain(){write(0x8e,0x00);//开write(0x80,0x55);//写秒钟值write(0x82,0x59);//写分钟值write(0x84,0x23);//写小时值write(0x86,0x10);write(0x88,0x10);write(0x8c,0x11);write(0x8e,0x80);//关welcome();while(1){writetime();}}
本文标题:DS1302编程与PROTEUS仿真
链接地址:https://www.777doc.com/doc-4770417 .html