MSP430和实时时钟HT1381的接口程序

发布时间:2008-11-6 21:48    发布者:MSP430
关键词: MSP430 , 实时时钟 , HT1381 , 接口程序
HT1381是台湾HT公司的一款串行实时时钟IC,工作电压: 2.0V~5.5V,最大输入串行时钟500kHz ( VDD=2V),2MHz(VDD=5V) ;工作电流:2V时小于300nA,5V时小于1mA。串行I/O 传送,二种数据传送方式:单字节或多字节(Burst方式),所有寄存器以BCD码格式存储具有接口简单、功耗低、工作电压范围宽、计时精确、功能全、成本低等优点,因此在实际应用中被广泛采用。该芯片提供秒、分、时、日、日期、月和年的信息。对于小于31天的月的月末日期能自动进行调整,还包括闰年校正功能。低功耗设计且时钟的运行可以采用24小时格式或带AM/PM指示的12小时的格式。 以下是接口程序,430测试频率为4M。 #define NOP _NOP();_NOP() #define NowSec cNowTime[0] #define NowMin cNowTime[1] #define NowHour cNowTime[2] #define NowDate cNowTime[3] #define NowMonth cNowTime[4] #define NowYear cNowTime[5] #define SetSec cSetTime[0] #define SetMin cSetTime[1] #define SetHour cSetTime[2] #define SetDate cSetTime[3] #define SetMonth cSetTime[4] #define SetYear cSetTime[5] uchar Temp_Count; uchar cSetTime[6]; uchar cNowTime[6]; void Timer_Out_Byte(uchar OutByte); uchar Timer_In_Byte(void); void ReadRealtime(void) { NOP; HT1381_OUTPORT &=~Timer_SCL; //Timer_SCL=0; NOP; HT1381_OUTPORT |= Timer_Rest; //Timer_Rest=1; Timer_Out_Byte(0xBF); //Read, Burst Mode for(TimeCount=0;TimeCount<6;TimeCount++) { if (TimeCount==5) Timer_In_Byte(); //dummy read. cNowTime[TimeCount]=Timer_In_Byte(); //change BCD to uchar cNowTime[TimeCount]=(cNowTime[TimeCount]/0x10)*10+(cNowTime[TimeCount]%0x10); } NOP; HT1381_OUTPORT &=~Timer_Rest; //Timer_Rest=0; } void WriteRealtime(void) { NOP; HT1381_OUTPORT &=~Timer_Rest; //Timer_Rest=0; //for sure to close rest. NOP; HT1381_OUTPORT &=~Timer_SCL; //Timer_SCL=0; NOP; HT1381_OUTPORT |= Timer_Rest; //Timer_Rest=1; Timer_Out_Byte(0x8E); //Write,Single Mode Timer_Out_Byte(0x00); //Write, Protect Byte=0, Disable protect NOP; HT1381_OUTPORT &=~Timer_Rest; //Timer_Rest=0; NOP; HT1381_OUTPORT |= Timer_Rest; //Timer_Rest=1; Timer_Out_Byte(0x80); //Write,Single Mode Timer_Out_Byte(0x00); //Write, OSC enalbe, old second distroied NOP; HT1381_OUTPORT &=~Timer_Rest; //Timer_Rest=0; for(TimeCount=0;TimeCount<6;TimeCount++) { //change uchar to BCD cSetTime[TimeCount]=(cSetTime[TimeCount]/10)*0x10+(cSetTime[TimeCount]%10); } NOP; HT1381_OUTPORT |= Timer_Rest; //Timer_Rest=1; Timer_Out_Byte(0XBE); //Write,Burst Mode Timer_Out_Byte(SetSec & 0x7F); //OSC enable for sure. Timer_Out_Byte(SetMin); Timer_Out_Byte(SetHour & 0x7F); //24 Hour Mode Timer_Out_Byte(SetDate); Timer_Out_Byte(SetMonth); Timer_Out_Byte(0x00); Timer_Out_Byte(SetYear); Timer_Out_Byte(0x00); NOP; HT1381_OUTPORT &=~Timer_Rest; //Timer_Rest=0; NOP; HT1381_OUTPORT |= Timer_Rest; //Timer_Rest=1; Timer_Out_Byte(0x8E); //Write,Single Mode Timer_Out_Byte(0x80); //Write, Protect Byte=0, enable protect NOP; HT1381_OUTPORT &=~Timer_Rest; //Timer_Rest=0; } void Timer_Out_Byte(uchar OutByte) { uchar Timer_Count; for (Timer_Count=0;Timer_Count<8;Timer_Count++) { HT1381_DIRPORT |= Timer_SDA; //output if ((OutByte & 0x01) == 0) HT1381_OUTPORT &=~Timer_SDA; else HT1381_OUTPORT |= Timer_SDA; NOP; HT1381_OUTPORT |= Timer_SCL; NOP; HT1381_OUTPORT &=~Timer_SCL; //Timer_SCL=0; utByte = OutByte >> 1; } } uchar Timer_In_Byte(void) { uchar InByte,Timer_Count; InByte=0x00; HT1381_DIRPORT &=~Timer_SDA; //Timer_SDA=1;//input for (Timer_Count=0;Timer_Count<8;Timer_Count++) { InByte >>= 1; NOP; HT1381_OUTPORT |= Timer_SCL; if(HT1381_INPORT&Timer_SDA) InByte |= 0x80; else InByte &= 0x7F; NOP; HT1381_OUTPORT &=~Timer_SCL; //Timer_SCL=0; } return(InByte); } #define NOP _NOP();_NOP() #define NowSec cNowTime[0] #define NowMin cNowTime[1] #define NowHour cNowTime[2] #define NowDate cNowTime[3] #define NowMonth cNowTime[4] #define NowYear cNowTime[5] #define SetSec cSetTime[0] #define SetMin cSetTime[1] #define SetHour cSetTime[2] #define SetDate cSetTime[3] #define SetMonth cSetTime[4] #define SetYear cSetTime[5] uchar Temp_Count; uchar cSetTime[6]; uchar cNowTime[6]; void Timer_Out_Byte(uchar OutByte); uchar Timer_In_Byte(void); void ReadRealtime(void) { NOP; HT1381_OUTPORT &=~Timer_SCL; //Timer_SCL=0; NOP; HT1381_OUTPORT |= Timer_Rest; //Timer_Rest=1; Timer_Out_Byte(0xBF); //Read, Burst Mode for(TimeCount=0;TimeCount<6;TimeCount++) { if (TimeCount==5) Timer_In_Byte(); //dummy read. cNowTime[TimeCount]=Timer_In_Byte(); //change BCD to uchar cNowTime[TimeCount]=(cNowTime[TimeCount]/0x10)*10+(cNowTime[TimeCount]%0x10); } NOP; HT1381_OUTPORT &=~Timer_Rest; //Timer_Rest=0; } void WriteRealtime(void) { NOP; HT1381_OUTPORT &=~Timer_Rest; //Timer_Rest=0; //for sure to close rest. NOP; HT1381_OUTPORT &=~Timer_SCL; //Timer_SCL=0; NOP; HT1381_OUTPORT |= Timer_Rest; //Timer_Rest=1; Timer_Out_Byte(0x8E); //Write,Single Mode Timer_Out_Byte(0x00); //Write, Protect Byte=0, Disable protect NOP; HT1381_OUTPORT &=~Timer_Rest; //Timer_Rest=0; NOP; HT1381_OUTPORT |= Timer_Rest; //Timer_Rest=1; Timer_Out_Byte(0x80); //Write,Single Mode Timer_Out_Byte(0x00); //Write, OSC enalbe, old second distroied NOP; HT1381_OUTPORT &=~Timer_Rest; //Timer_Rest=0; for(TimeCount=0;TimeCount<6;TimeCount++) { //change uchar to BCD cSetTime[TimeCount]=(cSetTime[TimeCount]/10)*0x10+(cSetTime[TimeCount]%10); } NOP; HT1381_OUTPORT |= Timer_Rest; //Timer_Rest=1; Timer_Out_Byte(0XBE); //Write,Burst Mode Timer_Out_Byte(SetSec & 0x7F); //OSC enable for sure. Timer_Out_Byte(SetMin); Timer_Out_Byte(SetHour & 0x7F); //24 Hour Mode Timer_Out_Byte(SetDate); Timer_Out_Byte(SetMonth); Timer_Out_Byte(0x00); Timer_Out_Byte(SetYear); Timer_Out_Byte(0x00); NOP; HT1381_OUTPORT &=~Timer_Rest; //Timer_Rest=0; NOP; HT1381_OUTPORT |= Timer_Rest; //Timer_Rest=1; Timer_Out_Byte(0x8E); //Write,Single Mode Timer_Out_Byte(0x80); //Write, Protect Byte=0, enable protect NOP; HT1381_OUTPORT &=~Timer_Rest; //Timer_Rest=0; } void Timer_Out_Byte(uchar OutByte) { uchar Timer_Count; for (Timer_Count=0;Timer_Count<8;Timer_Count++) { HT1381_DIRPORT |= Timer_SDA; //output if ((OutByte & 0x01) == 0) HT1381_OUTPORT &=~Timer_SDA; else HT1381_OUTPORT |= Timer_SDA; NOP; HT1381_OUTPORT |= Timer_SCL; NOP; HT1381_OUTPORT &=~Timer_SCL; //Timer_SCL=0; utByte = OutByte >> 1; } } uchar Timer_In_Byte(void) { uchar InByte,Timer_Count; InByte=0x00; HT1381_DIRPORT &=~Timer_SDA; //Timer_SDA=1;//input for (Timer_Count=0;Timer_Count<8;Timer_Count++) { InByte >>= 1; NOP; HT1381_OUTPORT |= Timer_SCL; if(HT1381_INPORT&Timer_SDA) InByte |= 0x80; else InByte &= 0x7F; NOP; HT1381_OUTPORT &=~Timer_SCL; //Timer_SCL=0; } return(InByte); }
本文地址:https://www.eechina.com/thread-2884-1-1.html     【打印本页】

本站部分文章为转载或网友发布,目的在于传递和分享信息,并不代表本网赞同其观点和对其真实性负责;文章版权归原作者及原出处所有,如涉及作品内容、版权和其它问题,我们将根据著作权人的要求,第一时间更正或删除。
您需要登录后才可以发表评论 登录 | 立即注册

厂商推荐

相关在线工具

关于我们  -  服务条款  -  使用指南  -  站点地图  -  友情链接  -  联系我们
电子工程网 © 版权所有   京ICP备16069177号 | 京公网安备11010502021702
快速回复 返回顶部 返回列表