#include <REGtenxTM52F5276B.H> #include<intrins.h> //包含_nop_()函数定义的头文件 #include "DS1302.H" /************************************************ *名称:DS1302_Sendchar() *描述:向DS1302写字节 *变量:dat ************************************************/ void DS1302_Sendchar(uchar dat) { uchar i; for(i=0;i<8;i++) { SCL = 0; IO = (bit)(dat & 0x01); dat >>= 1; SCL = 1; } } /************************************************ *名称:DS1302_Rendchar2() *描述:从ds1302读取字节 *变量:dat ************************************************/ uchar DS1302_Readchar() { uchar dat,i; for(i=0;i<8;i++) { if(IO == 1) { dat = dat|0x80; } SCL = 1; dat >>= 1; SCL = 0; } IO = 0;//将读1个字节函数最后加上I/O=0即可消除85显示 return dat; } /************************************************ *名称:DS1302_WriteADD() *描述:向1302寄存器写数据 *变量:com,dat ************************************************/ void DS1302_WriteADD(uchar com,uchar dat) { CE = 0; SCL = 0; CE = 1; DS1302_Sendchar(com); DS1302_Sendchar(dat); SCL = 1; CE = 0; } /************************************************ *名称:DS1302_ReadADD() *描述:从1302指定寄存器内读数据 *变量:com ************************************************/ uchar DS1302_ReadADD(uchar com) { uchar dat; CE = 0; SCL = 0; CE = 1; DS1302_Sendchar(com); dat = DS1302_Readchar(); SCL = 1; CE = 0; return dat; } /************************************************ *名称:DS1302_Init() *描述:初始化DS1302 *变量:无 ************************************************/ void Init_DS1302() { // uchar flag; // flag = DS1302_ReadADD(0x81); // if(flag & 0x80) // { DS1302_WriteADD(0x8e,0x00); //DS1302_WriteADD(0x80,((10/10)<<4|(10%10))&0x7f); //DS1302_WriteADD(0x82,((12/10)<<4|(12%10))); //DS1302_WriteADD(0x84,((13/10)<<4|(13%10))); //DS1302_WriteADD(0x86,((14/10)<<4|(14%10))); //DS1302_WriteADD(0x88,((1/10)<<4|(1%10))); //DS1302_WriteADD(0x8c,((1/10)<<4|(1%10))); DS1302_WriteADD(0x90, 0xa6); DS1302_WriteADD(0x8e,0x80); // } } //设置时间 void Set_1302_Time(unsigned char year,unsigned char month,unsigned char day,unsigned char week,unsigned char hour1,unsigned char min1,unsigned char second1) { DS1302_WriteADD(0x8e,0x00); DS1302_WriteADD(0x80,((second1/10)<<4|(second1%10))&0x7f); DS1302_WriteADD(0x82,((min1/10)<<4|(min1%10))); DS1302_WriteADD(0x84,((hour1/10)<<4|(hour1%10))); DS1302_WriteADD(0x86,((day/10)<<4|(day%10))); DS1302_WriteADD(0x88,((month/10)<<4|(month%10))); DS1302_WriteADD(0x8c,((year/10)<<4|(year%10))); DS1302_WriteADD(0x8a,((week/10)<<4|(week%10))); DS1302_WriteADD(0x8e,0x80); }