欢迎来到天天文库
浏览记录
ID:37709257
大小:33.00 KB
页数:7页
时间:2019-05-29
《DS1302操作模块》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、·C/*DS1302时钟芯片驱动函数DS1302是产品中最常用的时钟芯片,此文件中的代码可以嵌入项目中使用。*/#include#include/*复位脚*/#defineRST_CLRPORTD&=~(1<2、=(1<3、=(1<4、低*/#defineIO_SETPORTD5、=(1<6、=(1<7、=(1<8、=(1<9、向输出*/#defineds1302_sec_add0x80//秒数据地址#defineds1302_min_add0x82//分数据地址#defineds1302_hr_add0x84//时数据地址#defineds1302_date_add0x86//日数据地址#defineds1302_month_add0x88//月数据地址#defineds1302_day_add0x8a//星期数据地址#defineds1302_year_add0x8c//年数据地址#defineds1302_control_add0x8e//控制数据地址#defineds1302_charger10、_add0x90#defineds1302_clkburst_add0xbeUnsignedchartime_buf[8]={0x20,0x12,0x03,0x21,0x10,0x20,0x55,0x03};/*向DS1302写入一字节数据*/voidds1302_write_byte(unsignedcharaddr,unsignedchard){unsignedchari;RST_SET;/*启动DS1302总线*//*写入目标地址:addr*/IO_OUT;addr=addr&0xFE;/*最低位置零*/for(i=0;i<8;i++){if(addr&0x01){IO11、_SET;}else{IO_CLR;}SCK_SET;SCK_CLR;addr=addr>>1;}/*写入数据:d*/IO_OUT;for(i=0;i<8;i++){if(d&0x01){IO_SET;}else{IO_CLR;}SCK_SET;SCK_CLR;d=d>>1;}RST_CLR;/*停止DS1302总线*/}/*从DS1302读出一字节数据*/unsignedchards1302_read_byte(unsignedcharaddr){unsignedchari;unsignedchartemp;RST_SET;/*启动DS1302总线*//*写入目标地址:ad12、dr*/IO_OUT;addr=addr13、0x01;/*最低位置高*/for(i=0;i<8;i++){if(addr&0x01){IO_SET;}else{IO_CLR;}SCK_SET;SCK_CLR;addr=addr>>1;}/*输出数据:temp*/IO_IN;for(i=0;i<8;i++){temp=temp>>1;if(IO_R){temp14、=0x80;}else{temp&=0x7F;}SCK_SET;SCK_CLR;}RST_CLR;/*停止DS1302总线*/returntemp;}/*向DS302写入时钟数据*/voidds1302_write_tim15、e(void){ds1302_write_byte(ds1302_control_add,0x00);//关闭写保护(即可写)ds1302_write_byte(ds1302_sec_add,0x80);//暂停ds1302_write_byte(ds1302_charger_add,0xa9);//慢充电ds1302_write_byte(ds1302_year_add,time_buf[1]);//年ds1302_write_byte(ds1302_month_add,time_buf[2]);//
2、=(1<3、=(1<4、低*/#defineIO_SETPORTD5、=(1<6、=(1<7、=(1<8、=(1<9、向输出*/#defineds1302_sec_add0x80//秒数据地址#defineds1302_min_add0x82//分数据地址#defineds1302_hr_add0x84//时数据地址#defineds1302_date_add0x86//日数据地址#defineds1302_month_add0x88//月数据地址#defineds1302_day_add0x8a//星期数据地址#defineds1302_year_add0x8c//年数据地址#defineds1302_control_add0x8e//控制数据地址#defineds1302_charger10、_add0x90#defineds1302_clkburst_add0xbeUnsignedchartime_buf[8]={0x20,0x12,0x03,0x21,0x10,0x20,0x55,0x03};/*向DS1302写入一字节数据*/voidds1302_write_byte(unsignedcharaddr,unsignedchard){unsignedchari;RST_SET;/*启动DS1302总线*//*写入目标地址:addr*/IO_OUT;addr=addr&0xFE;/*最低位置零*/for(i=0;i<8;i++){if(addr&0x01){IO11、_SET;}else{IO_CLR;}SCK_SET;SCK_CLR;addr=addr>>1;}/*写入数据:d*/IO_OUT;for(i=0;i<8;i++){if(d&0x01){IO_SET;}else{IO_CLR;}SCK_SET;SCK_CLR;d=d>>1;}RST_CLR;/*停止DS1302总线*/}/*从DS1302读出一字节数据*/unsignedchards1302_read_byte(unsignedcharaddr){unsignedchari;unsignedchartemp;RST_SET;/*启动DS1302总线*//*写入目标地址:ad12、dr*/IO_OUT;addr=addr13、0x01;/*最低位置高*/for(i=0;i<8;i++){if(addr&0x01){IO_SET;}else{IO_CLR;}SCK_SET;SCK_CLR;addr=addr>>1;}/*输出数据:temp*/IO_IN;for(i=0;i<8;i++){temp=temp>>1;if(IO_R){temp14、=0x80;}else{temp&=0x7F;}SCK_SET;SCK_CLR;}RST_CLR;/*停止DS1302总线*/returntemp;}/*向DS302写入时钟数据*/voidds1302_write_tim15、e(void){ds1302_write_byte(ds1302_control_add,0x00);//关闭写保护(即可写)ds1302_write_byte(ds1302_sec_add,0x80);//暂停ds1302_write_byte(ds1302_charger_add,0xa9);//慢充电ds1302_write_byte(ds1302_year_add,time_buf[1]);//年ds1302_write_byte(ds1302_month_add,time_buf[2]);//
3、=(1<4、低*/#defineIO_SETPORTD5、=(1<6、=(1<7、=(1<8、=(1<9、向输出*/#defineds1302_sec_add0x80//秒数据地址#defineds1302_min_add0x82//分数据地址#defineds1302_hr_add0x84//时数据地址#defineds1302_date_add0x86//日数据地址#defineds1302_month_add0x88//月数据地址#defineds1302_day_add0x8a//星期数据地址#defineds1302_year_add0x8c//年数据地址#defineds1302_control_add0x8e//控制数据地址#defineds1302_charger10、_add0x90#defineds1302_clkburst_add0xbeUnsignedchartime_buf[8]={0x20,0x12,0x03,0x21,0x10,0x20,0x55,0x03};/*向DS1302写入一字节数据*/voidds1302_write_byte(unsignedcharaddr,unsignedchard){unsignedchari;RST_SET;/*启动DS1302总线*//*写入目标地址:addr*/IO_OUT;addr=addr&0xFE;/*最低位置零*/for(i=0;i<8;i++){if(addr&0x01){IO11、_SET;}else{IO_CLR;}SCK_SET;SCK_CLR;addr=addr>>1;}/*写入数据:d*/IO_OUT;for(i=0;i<8;i++){if(d&0x01){IO_SET;}else{IO_CLR;}SCK_SET;SCK_CLR;d=d>>1;}RST_CLR;/*停止DS1302总线*/}/*从DS1302读出一字节数据*/unsignedchards1302_read_byte(unsignedcharaddr){unsignedchari;unsignedchartemp;RST_SET;/*启动DS1302总线*//*写入目标地址:ad12、dr*/IO_OUT;addr=addr13、0x01;/*最低位置高*/for(i=0;i<8;i++){if(addr&0x01){IO_SET;}else{IO_CLR;}SCK_SET;SCK_CLR;addr=addr>>1;}/*输出数据:temp*/IO_IN;for(i=0;i<8;i++){temp=temp>>1;if(IO_R){temp14、=0x80;}else{temp&=0x7F;}SCK_SET;SCK_CLR;}RST_CLR;/*停止DS1302总线*/returntemp;}/*向DS302写入时钟数据*/voidds1302_write_tim15、e(void){ds1302_write_byte(ds1302_control_add,0x00);//关闭写保护(即可写)ds1302_write_byte(ds1302_sec_add,0x80);//暂停ds1302_write_byte(ds1302_charger_add,0xa9);//慢充电ds1302_write_byte(ds1302_year_add,time_buf[1]);//年ds1302_write_byte(ds1302_month_add,time_buf[2]);//
4、低*/#defineIO_SETPORTD
5、=(1<6、=(1<7、=(1<8、=(1<9、向输出*/#defineds1302_sec_add0x80//秒数据地址#defineds1302_min_add0x82//分数据地址#defineds1302_hr_add0x84//时数据地址#defineds1302_date_add0x86//日数据地址#defineds1302_month_add0x88//月数据地址#defineds1302_day_add0x8a//星期数据地址#defineds1302_year_add0x8c//年数据地址#defineds1302_control_add0x8e//控制数据地址#defineds1302_charger10、_add0x90#defineds1302_clkburst_add0xbeUnsignedchartime_buf[8]={0x20,0x12,0x03,0x21,0x10,0x20,0x55,0x03};/*向DS1302写入一字节数据*/voidds1302_write_byte(unsignedcharaddr,unsignedchard){unsignedchari;RST_SET;/*启动DS1302总线*//*写入目标地址:addr*/IO_OUT;addr=addr&0xFE;/*最低位置零*/for(i=0;i<8;i++){if(addr&0x01){IO11、_SET;}else{IO_CLR;}SCK_SET;SCK_CLR;addr=addr>>1;}/*写入数据:d*/IO_OUT;for(i=0;i<8;i++){if(d&0x01){IO_SET;}else{IO_CLR;}SCK_SET;SCK_CLR;d=d>>1;}RST_CLR;/*停止DS1302总线*/}/*从DS1302读出一字节数据*/unsignedchards1302_read_byte(unsignedcharaddr){unsignedchari;unsignedchartemp;RST_SET;/*启动DS1302总线*//*写入目标地址:ad12、dr*/IO_OUT;addr=addr13、0x01;/*最低位置高*/for(i=0;i<8;i++){if(addr&0x01){IO_SET;}else{IO_CLR;}SCK_SET;SCK_CLR;addr=addr>>1;}/*输出数据:temp*/IO_IN;for(i=0;i<8;i++){temp=temp>>1;if(IO_R){temp14、=0x80;}else{temp&=0x7F;}SCK_SET;SCK_CLR;}RST_CLR;/*停止DS1302总线*/returntemp;}/*向DS302写入时钟数据*/voidds1302_write_tim15、e(void){ds1302_write_byte(ds1302_control_add,0x00);//关闭写保护(即可写)ds1302_write_byte(ds1302_sec_add,0x80);//暂停ds1302_write_byte(ds1302_charger_add,0xa9);//慢充电ds1302_write_byte(ds1302_year_add,time_buf[1]);//年ds1302_write_byte(ds1302_month_add,time_buf[2]);//
6、=(1<7、=(1<8、=(1<9、向输出*/#defineds1302_sec_add0x80//秒数据地址#defineds1302_min_add0x82//分数据地址#defineds1302_hr_add0x84//时数据地址#defineds1302_date_add0x86//日数据地址#defineds1302_month_add0x88//月数据地址#defineds1302_day_add0x8a//星期数据地址#defineds1302_year_add0x8c//年数据地址#defineds1302_control_add0x8e//控制数据地址#defineds1302_charger10、_add0x90#defineds1302_clkburst_add0xbeUnsignedchartime_buf[8]={0x20,0x12,0x03,0x21,0x10,0x20,0x55,0x03};/*向DS1302写入一字节数据*/voidds1302_write_byte(unsignedcharaddr,unsignedchard){unsignedchari;RST_SET;/*启动DS1302总线*//*写入目标地址:addr*/IO_OUT;addr=addr&0xFE;/*最低位置零*/for(i=0;i<8;i++){if(addr&0x01){IO11、_SET;}else{IO_CLR;}SCK_SET;SCK_CLR;addr=addr>>1;}/*写入数据:d*/IO_OUT;for(i=0;i<8;i++){if(d&0x01){IO_SET;}else{IO_CLR;}SCK_SET;SCK_CLR;d=d>>1;}RST_CLR;/*停止DS1302总线*/}/*从DS1302读出一字节数据*/unsignedchards1302_read_byte(unsignedcharaddr){unsignedchari;unsignedchartemp;RST_SET;/*启动DS1302总线*//*写入目标地址:ad12、dr*/IO_OUT;addr=addr13、0x01;/*最低位置高*/for(i=0;i<8;i++){if(addr&0x01){IO_SET;}else{IO_CLR;}SCK_SET;SCK_CLR;addr=addr>>1;}/*输出数据:temp*/IO_IN;for(i=0;i<8;i++){temp=temp>>1;if(IO_R){temp14、=0x80;}else{temp&=0x7F;}SCK_SET;SCK_CLR;}RST_CLR;/*停止DS1302总线*/returntemp;}/*向DS302写入时钟数据*/voidds1302_write_tim15、e(void){ds1302_write_byte(ds1302_control_add,0x00);//关闭写保护(即可写)ds1302_write_byte(ds1302_sec_add,0x80);//暂停ds1302_write_byte(ds1302_charger_add,0xa9);//慢充电ds1302_write_byte(ds1302_year_add,time_buf[1]);//年ds1302_write_byte(ds1302_month_add,time_buf[2]);//
7、=(1<8、=(1<9、向输出*/#defineds1302_sec_add0x80//秒数据地址#defineds1302_min_add0x82//分数据地址#defineds1302_hr_add0x84//时数据地址#defineds1302_date_add0x86//日数据地址#defineds1302_month_add0x88//月数据地址#defineds1302_day_add0x8a//星期数据地址#defineds1302_year_add0x8c//年数据地址#defineds1302_control_add0x8e//控制数据地址#defineds1302_charger10、_add0x90#defineds1302_clkburst_add0xbeUnsignedchartime_buf[8]={0x20,0x12,0x03,0x21,0x10,0x20,0x55,0x03};/*向DS1302写入一字节数据*/voidds1302_write_byte(unsignedcharaddr,unsignedchard){unsignedchari;RST_SET;/*启动DS1302总线*//*写入目标地址:addr*/IO_OUT;addr=addr&0xFE;/*最低位置零*/for(i=0;i<8;i++){if(addr&0x01){IO11、_SET;}else{IO_CLR;}SCK_SET;SCK_CLR;addr=addr>>1;}/*写入数据:d*/IO_OUT;for(i=0;i<8;i++){if(d&0x01){IO_SET;}else{IO_CLR;}SCK_SET;SCK_CLR;d=d>>1;}RST_CLR;/*停止DS1302总线*/}/*从DS1302读出一字节数据*/unsignedchards1302_read_byte(unsignedcharaddr){unsignedchari;unsignedchartemp;RST_SET;/*启动DS1302总线*//*写入目标地址:ad12、dr*/IO_OUT;addr=addr13、0x01;/*最低位置高*/for(i=0;i<8;i++){if(addr&0x01){IO_SET;}else{IO_CLR;}SCK_SET;SCK_CLR;addr=addr>>1;}/*输出数据:temp*/IO_IN;for(i=0;i<8;i++){temp=temp>>1;if(IO_R){temp14、=0x80;}else{temp&=0x7F;}SCK_SET;SCK_CLR;}RST_CLR;/*停止DS1302总线*/returntemp;}/*向DS302写入时钟数据*/voidds1302_write_tim15、e(void){ds1302_write_byte(ds1302_control_add,0x00);//关闭写保护(即可写)ds1302_write_byte(ds1302_sec_add,0x80);//暂停ds1302_write_byte(ds1302_charger_add,0xa9);//慢充电ds1302_write_byte(ds1302_year_add,time_buf[1]);//年ds1302_write_byte(ds1302_month_add,time_buf[2]);//
8、=(1<9、向输出*/#defineds1302_sec_add0x80//秒数据地址#defineds1302_min_add0x82//分数据地址#defineds1302_hr_add0x84//时数据地址#defineds1302_date_add0x86//日数据地址#defineds1302_month_add0x88//月数据地址#defineds1302_day_add0x8a//星期数据地址#defineds1302_year_add0x8c//年数据地址#defineds1302_control_add0x8e//控制数据地址#defineds1302_charger10、_add0x90#defineds1302_clkburst_add0xbeUnsignedchartime_buf[8]={0x20,0x12,0x03,0x21,0x10,0x20,0x55,0x03};/*向DS1302写入一字节数据*/voidds1302_write_byte(unsignedcharaddr,unsignedchard){unsignedchari;RST_SET;/*启动DS1302总线*//*写入目标地址:addr*/IO_OUT;addr=addr&0xFE;/*最低位置零*/for(i=0;i<8;i++){if(addr&0x01){IO11、_SET;}else{IO_CLR;}SCK_SET;SCK_CLR;addr=addr>>1;}/*写入数据:d*/IO_OUT;for(i=0;i<8;i++){if(d&0x01){IO_SET;}else{IO_CLR;}SCK_SET;SCK_CLR;d=d>>1;}RST_CLR;/*停止DS1302总线*/}/*从DS1302读出一字节数据*/unsignedchards1302_read_byte(unsignedcharaddr){unsignedchari;unsignedchartemp;RST_SET;/*启动DS1302总线*//*写入目标地址:ad12、dr*/IO_OUT;addr=addr13、0x01;/*最低位置高*/for(i=0;i<8;i++){if(addr&0x01){IO_SET;}else{IO_CLR;}SCK_SET;SCK_CLR;addr=addr>>1;}/*输出数据:temp*/IO_IN;for(i=0;i<8;i++){temp=temp>>1;if(IO_R){temp14、=0x80;}else{temp&=0x7F;}SCK_SET;SCK_CLR;}RST_CLR;/*停止DS1302总线*/returntemp;}/*向DS302写入时钟数据*/voidds1302_write_tim15、e(void){ds1302_write_byte(ds1302_control_add,0x00);//关闭写保护(即可写)ds1302_write_byte(ds1302_sec_add,0x80);//暂停ds1302_write_byte(ds1302_charger_add,0xa9);//慢充电ds1302_write_byte(ds1302_year_add,time_buf[1]);//年ds1302_write_byte(ds1302_month_add,time_buf[2]);//
9、向输出*/#defineds1302_sec_add0x80//秒数据地址#defineds1302_min_add0x82//分数据地址#defineds1302_hr_add0x84//时数据地址#defineds1302_date_add0x86//日数据地址#defineds1302_month_add0x88//月数据地址#defineds1302_day_add0x8a//星期数据地址#defineds1302_year_add0x8c//年数据地址#defineds1302_control_add0x8e//控制数据地址#defineds1302_charger
10、_add0x90#defineds1302_clkburst_add0xbeUnsignedchartime_buf[8]={0x20,0x12,0x03,0x21,0x10,0x20,0x55,0x03};/*向DS1302写入一字节数据*/voidds1302_write_byte(unsignedcharaddr,unsignedchard){unsignedchari;RST_SET;/*启动DS1302总线*//*写入目标地址:addr*/IO_OUT;addr=addr&0xFE;/*最低位置零*/for(i=0;i<8;i++){if(addr&0x01){IO
11、_SET;}else{IO_CLR;}SCK_SET;SCK_CLR;addr=addr>>1;}/*写入数据:d*/IO_OUT;for(i=0;i<8;i++){if(d&0x01){IO_SET;}else{IO_CLR;}SCK_SET;SCK_CLR;d=d>>1;}RST_CLR;/*停止DS1302总线*/}/*从DS1302读出一字节数据*/unsignedchards1302_read_byte(unsignedcharaddr){unsignedchari;unsignedchartemp;RST_SET;/*启动DS1302总线*//*写入目标地址:ad
12、dr*/IO_OUT;addr=addr
13、0x01;/*最低位置高*/for(i=0;i<8;i++){if(addr&0x01){IO_SET;}else{IO_CLR;}SCK_SET;SCK_CLR;addr=addr>>1;}/*输出数据:temp*/IO_IN;for(i=0;i<8;i++){temp=temp>>1;if(IO_R){temp
14、=0x80;}else{temp&=0x7F;}SCK_SET;SCK_CLR;}RST_CLR;/*停止DS1302总线*/returntemp;}/*向DS302写入时钟数据*/voidds1302_write_tim
15、e(void){ds1302_write_byte(ds1302_control_add,0x00);//关闭写保护(即可写)ds1302_write_byte(ds1302_sec_add,0x80);//暂停ds1302_write_byte(ds1302_charger_add,0xa9);//慢充电ds1302_write_byte(ds1302_year_add,time_buf[1]);//年ds1302_write_byte(ds1302_month_add,time_buf[2]);//
此文档下载收益归作者所有