全部博文(2005)
分类:
2007-05-17 15:00:49
使用AT91RM9200对时钟芯片HT1380进行读写操作源码(gliethttp)
//---------------------------------------------------------------------------------------------------------------------
typedef struct __xHT1380
{
#define XHT1380_Install() AT91F_PIO_CfgOutput(AT91C_BASE_PIOA, 0x0f);AT91F_PMC_EnablePeriphClock(AT91C_BASE_PMC, 1 << AT91C_ID_PIOA)
#define XHT1380_sclk_Set() AT91F_PIO_SetOutput(AT91C_BASE_PIOA, 0x01 << 2)
#define XHT1380_rst_Set() AT91F_PIO_SetOutput(AT91C_BASE_PIOA, 0x01 << 3)
#define XHT1380_io_Set() AT91F_PIO_SetOutput(AT91C_BASE_PIOA, 0x03 << 0)
#define XHT1380_sclk_Clr() AT91F_PIO_ClearOutput(AT91C_BASE_PIOA, 0x01 << 2)
#define XHT1380_rst_Clr() AT91F_PIO_ClearOutput(AT91C_BASE_PIOA, 0x01 << 3)
#define XHT1380_io_Clr() AT91F_PIO_ClearOutput(AT91C_BASE_PIOA, 0x03 << 0)
#define xHT1380_io_IN() AT91F_PIO_CfgInput(AT91C_BASE_PIOA,0x03 << 0)
#define xHT1380_io_OUT() AT91F_PIO_CfgOutput(AT91C_BASE_PIOA,0x03 << 0)
#define xHT1380_io_Get() (AT91F_PIO_GetInput(AT91C_BASE_PIOA) & 0x03)
//public
void(*ini)(void);
void(*start)(void);//启动HT1380
void(*saveTime)(_xtime *time);
void(*readTime)(_xtime *time);
boolean ok;
}_xHT1380;
__inline void HT1380_OUT(uint8 data)
{uint8 i;
for(i = 0;i < 8;i++)
{
if(data & 0x01)XHT1380_io_Set();
else XHT1380_io_Clr();
data >>= 1;
XHT1380_sclk_Set();
XHT1380_sclk_Clr();
}
}
__inline uint8 HT1380_IN(void)
{uint8 i;
uint8 data = 0;
for(i = 0;i < 8;i++)
{
XHT1380_sclk_Set();
data >>= 1;
if(xHT1380_io_Get())data |= 0x80;
XHT1380_sclk_Clr();
}
return data;
}
//---------------------------------------------------------------------------------------------------------------------
void xHT1380_ini(void);
_xHT1380 xxHT1380 = {xHT1380_ini};
_xHT1380 *xHT1380 = &xxHT1380;
//---------------------------------------------------------------------------------------------------------------------
//【 版 本 】v1.0
//【 函 数 名 称 】void xHT1380_write(void)
//【 创建人及创建时间 】gliethttp 2006-06-28 15:18
//【 修改人及修改时间 】
//【 修 改 原 因 】
//【 功 能 描 述 】3V电压,所以时钟频率应该小于700K
//---------------------------------------------------------------------------------------------------------------------
void xHT1380_write(uint8 cmd,uint8 data)
{
xHT1380_io_OUT();
XHT1380_sclk_Clr();
XHT1380_rst_Clr();
XHT1380_rst_Set();
HT1380_OUT(cmd);
HT1380_OUT(data);
XHT1380_rst_Clr();
}
//---------------------------------------------------------------------------------------------------------------------
void xHT1380_read(uint8 cmd,uint8 *data)
{
XHT1380_sclk_Clr();
XHT1380_rst_Clr();
xHT1380_io_OUT();
XHT1380_rst_Set();
HT1380_OUT(cmd);
xHT1380_io_IN();
*data = HT1380_IN();
XHT1380_rst_Clr();
}
//---------------------------------------------------------------------------------------------------------------------
void xHT1380_start(void)
{
xHT1380_write(0x8E,0x00);//允许写
xHT1380_write(0x80,0x00);//允许晶振
}
//---------------------------------------------------------------------------------------------------------------------
void xHT1380_saveTime(_xtime *time)
{uint8 data;
data = ((time->second / 10) << 4) + (time->second % 10);
xHT1380_write(0x80,data);
data = ((time->minute / 10) << 4) + (time->minute % 10);
xHT1380_write(0x82,data);
//data = (time->hour % 12);
//if(time->hour >= 12)data |= 0x20;
data = ((time->hour / 10) << 4) + (time->hour % 10);
xHT1380_write(0x84,data);
data = ((time->day / 10) << 4) + (time->day % 10);
xHT1380_write(0x86,data);
data = ((time->month / 10) << 4) + (time->month % 10);
xHT1380_write(0x88,data);
data = ((time->year / 10) << 4) + (time->year % 10);
xHT1380_write(0x8C,data);
}
//---------------------------------------------------------------------------------------------------------------------
void xHT1380_readTime(_xtime *time)
{//假设能在1s内读完这些数据
xHT1380_read(0x81,&time->second);
xHT1380_read(0x83,&time->minute);
xHT1380_read(0x85,&time->hour);
xHT1380_read(0x87,&time->day);
xHT1380_read(0x89,&time->month);
xHT1380_read(0x8D,&time->year);
}
//---------------------------------------------------------------------------------------------------------------------
void xHT1380_ini(void)
{uint8 data,data0;
XHT1380_Install();
XHT1380_sclk_Set();
XHT1380_rst_Set();
XHT1380_io_Set();
xHT1380->start = xHT1380_start;
xHT1380->saveTime = xHT1380_saveTime;
xHT1380->readTime = xHT1380_readTime;
xHT1380_read(0x81,&data);
xHT1380_read(0x8F,&data0);
if(data0 & 0x80)data |= 0x80;
if(data & 0x80)xHT1380->start();//表示新的HT1380,需要启动晶振
xHT1380_read(0x81,&data);
if(data & 0x80)xHT1380->ok = false;//更换RTC电池
else xHT1380->ok = true; //HT1380已经正常工作
}