📄 ds1302.c
字号:
sbit IO = P3^4;
sbit SCLK = P3^3;
sbit RSTB = P3^5;
/***************************************************************************/
/* Prototypes */
/***************************************************************************/
uchar rt_rbyte();
void rt_reset();
void rt_wbyte(uchar);
void get_time(time *tm);
void set_time(time *tm);
/* ----------------------------------------------------------------------- */
void rt_reset()
{
SCLK = 0;
RSTB = 0;
RSTB = 1;
}
/* ----------------------------------------------------------------------- */
void rt_wbyte(uchar W_Byte)
{
uchar i;
for(i = 0; i < 8; ++i)
{
IO = 0;
if(W_Byte & 0x01)
{
IO = 1; /* set port pin high to read data */
}
SCLK = 0;
SCLK = 1;
W_Byte >>= 1;
}
}
/* ----------------------------------------------------------------------- */
uchar rt_rbyte()
{
uchar i;
uchar R_Byte;
R_Byte = 0x00;
IO = 1;
for(i=0; i<8; ++i)
{
R_Byte >>= 1;
SCLK = 1;
SCLK = 0;
if(IO){
R_Byte |= 0x80;
}
}
return R_Byte;
}
/* ----------------------------------------- */
void get_time(time *tm)
{
rt_reset();
rt_wbyte(0xbf); /* clock burst read (eight registers) */
tm->sec = rt_rbyte();
tm->min = rt_rbyte();
tm->hour = rt_rbyte();
tm->day = rt_rbyte(); //date
tm->month = rt_rbyte();
tm->year = rt_rbyte(); //day
tm->year = rt_rbyte();
rt_rbyte(); /* must read control register in
burst mode */
rt_reset();
RSTB = 0;
}
/* ----------------------------------------- */
void set_time(time *tm)
{
rt_reset();
rt_wbyte(0x8e); /* control register */
rt_wbyte(0); /* disable write protect */
rt_reset();
rt_wbyte(0x90); /* trickle charger register */
rt_wbyte(0xab); /* enable, 2 diodes, 8K resistor */
rt_reset();
rt_wbyte(0xbe); /* clock burst write (eight registers) */
rt_wbyte(tm->sec);
rt_wbyte(tm->min);
rt_wbyte(tm->hour);
rt_wbyte(tm->day); //date
rt_wbyte(tm->month);
rt_wbyte(1); //day
rt_wbyte(tm->year);
rt_wbyte(0x80); /* must write control register in
burst mode, enable write protect*/
rt_reset();
RSTB = 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -