具有接口简单、功耗低、工作电压范围宽、计时精确、功能全、成本低等优点,
因此在实际应用中被广泛采用。
该芯片提供秒、分、时、日、日期、月和年的信息。
对于小于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;
OutByte = 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;
OutByte = 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);
}
上一篇:MSP430 Flash编程程序
下一篇:MSP430与ATMEL 93C46通讯子程序
推荐阅读最新更新时间:2024-03-16 15:05
设计资源 培训 开发板 精华推荐
- BeagleBone Black 开发板 DIY 创意设计大赛
- 阅1SP0350V SCALE-2单通道即插即用型门极驱动器,参与PI答题享好礼!
- 英飞凌MERUS™ D类音频解决方案|【阅读赢好礼,下单送开发板】
- 赢京东卡 室内空气隐患大作战——英飞凌XENSIV™PAS CO2传感器
- 【社区大讲堂】顶嵌与你一起:动手做嵌入式产品
- 泰克“无忧计划”买与租的自由转换!赏漫画、填问卷 参与活动好礼送!
- TI有奖直播|C2000™ F280013x实现更低成本且更高效的实时控制方案
- 有奖直播:英飞凌工业半导体在电机驱动行业中的应用 2020年4月21日 上午10:00-11:30 准时开启!
- 贸泽翻牌挑战赛—一键配齐的乐趣,它不香吗?快来参与贸泽翻牌挑战赛!