欢迎来到天天文库
浏览记录
ID:30410987
大小:72.54 KB
页数:7页
时间:2018-12-29
《单片机与时钟芯片ds1302b例程b有电路》由会员上传分享,免费在线阅读,更多相关内容在应用文档-天天文库。
1、51单片机与时钟芯片DS1302例程(有电路图)Figure1:ProgramListing/***************************************************************************//*DEMO1302.C*//***************************************************************************//*ThisprogramisforexampleonlyandisnotsupportedbyDallasMaxim*/#include2、.h>#include/*RegisterdeclarationsforDS5000*/sbitCE=P0^0;sbitSCLK=P0^1;sbitIO=P0^2;/***************************************************************************//*Prototypes*//***************************************************************************/ucharrbyte_3w();voidreset_3w(3、);voidwbyte_3w(uchar);voidwritebyte();voidwrite_clk_regs();voidread_clk_regs();voidburstramrd();voidburstramwr();/***************************************************************************/voidreset_3w()/*-----resetandenablethe3-wireinterface------*/{SCLK=0;CE=0;CE=1;}voidwbyte_3w(ucharW4、_Byte)/*------writeonebytetothedevice-------*/{uchari; for(i=0;i<8;++i){IO=0;if(W_Byte&0x01){IO=1;/*setportpinhightoreaddata*/}SCLK=0;SCLK=1;W_Byte>>=1;}}ucharrbyte_3w()/*-------readonebytefromthedevice--------*/{uchari;ucharR_Byte;ucharTmpByte; R_Byte=0x00;IO=1;for(i=0;i<8;i++){SCLK=1;SC5、LK=0;TmpByte=(uchar)IO;TmpByte<<=7;R_Byte>>=1;R_Byte6、=TmpByte;}returnR_Byte;}voidwritebyte()/*---writeonebyteusingvaluesenteredbyuser---*/{ucharClkAdd;ucharClkData; /*GetClockAddress&Data*/printf("Enterregisterwriteaddress(hex):");scanf("%bx",&ClkAdd);printf("Enterdata(hex):");scanf("%b7、x",&ClkData); reset_3w();wbyte_3w(ClkAdd);wbyte_3w(ClkData);reset_3w();}voidread_clk_regs()/*----loopread&displayclockregisters----*/{ucharprev_sec=99,sec,min,hrs,dte,mon,day,yr; printf("Yr/Mn/DtDyHr/Mn/Sec");do/*Read&DisplayClockRegisters*/{reset_3w();wbyte_3w(0xBF);/*clockburst*/sec=r8、byte_3w();min=rbyte_3w();hrs=rbyte_3w();dte=rbyte_3w();mon=rbyte_3w();day=rbyte_3w();yr=rbyte_3w();reset_3w();if(sec!=prev_sec)/*printoncepersecond*/{printf("%02bX/%02bX/%02bX%02bX",yr,dte,mon,day);printf("%02bX:%02bX:%02bX",hrs,min,sec);prev_sec=sec;}}while(!RI);
2、.h>#include/*RegisterdeclarationsforDS5000*/sbitCE=P0^0;sbitSCLK=P0^1;sbitIO=P0^2;/***************************************************************************//*Prototypes*//***************************************************************************/ucharrbyte_3w();voidreset_3w(
3、);voidwbyte_3w(uchar);voidwritebyte();voidwrite_clk_regs();voidread_clk_regs();voidburstramrd();voidburstramwr();/***************************************************************************/voidreset_3w()/*-----resetandenablethe3-wireinterface------*/{SCLK=0;CE=0;CE=1;}voidwbyte_3w(ucharW
4、_Byte)/*------writeonebytetothedevice-------*/{uchari; for(i=0;i<8;++i){IO=0;if(W_Byte&0x01){IO=1;/*setportpinhightoreaddata*/}SCLK=0;SCLK=1;W_Byte>>=1;}}ucharrbyte_3w()/*-------readonebytefromthedevice--------*/{uchari;ucharR_Byte;ucharTmpByte; R_Byte=0x00;IO=1;for(i=0;i<8;i++){SCLK=1;SC
5、LK=0;TmpByte=(uchar)IO;TmpByte<<=7;R_Byte>>=1;R_Byte
6、=TmpByte;}returnR_Byte;}voidwritebyte()/*---writeonebyteusingvaluesenteredbyuser---*/{ucharClkAdd;ucharClkData; /*GetClockAddress&Data*/printf("Enterregisterwriteaddress(hex):");scanf("%bx",&ClkAdd);printf("Enterdata(hex):");scanf("%b
7、x",&ClkData); reset_3w();wbyte_3w(ClkAdd);wbyte_3w(ClkData);reset_3w();}voidread_clk_regs()/*----loopread&displayclockregisters----*/{ucharprev_sec=99,sec,min,hrs,dte,mon,day,yr; printf("Yr/Mn/DtDyHr/Mn/Sec");do/*Read&DisplayClockRegisters*/{reset_3w();wbyte_3w(0xBF);/*clockburst*/sec=r
8、byte_3w();min=rbyte_3w();hrs=rbyte_3w();dte=rbyte_3w();mon=rbyte_3w();day=rbyte_3w();yr=rbyte_3w();reset_3w();if(sec!=prev_sec)/*printoncepersecond*/{printf("%02bX/%02bX/%02bX%02bX",yr,dte,mon,day);printf("%02bX:%02bX:%02bX",hrs,min,sec);prev_sec=sec;}}while(!RI);
此文档下载收益归作者所有