📄 ds1302.c
字号:
#include "ds1302.h"
#include "global.h"
#include "delay.h"
void Init_DS1302(void)
{
sbi(DS1302_DDR, DS1302_CLK);
sbi(DS1302_DDR, DS1302_CE);
sbi(DS1302_DDR, DS1302_IO);
cbi(DS1302_PORT, DS1302_CLK);
cbi(DS1302_PORT, DS1302_CE);
cbi(DS1302_PORT, DS1302_IO);
DS1302_Write(DS1302_WP, 0x00); // write protect off
DS1302_Write(DS1302_SET, 0xA5); // charge,diode,resistor
DS1302_Write(DS1302_HOUR, DS1302_Read(DS1302_HOUR) & 0x7f); // 24hour set
DS1302_Write(DS1302_SECOND, DS1302_Read(DS1302_SECOND)&0x7f); // ch clear
}
unsigned char DS1302_Read(unsigned char addr)
{
unsigned char dval;
unsigned char count;
dval = addr|0x01; // Read 疙飞 傈价矫 付瘤阜 厚飘绰 1肺 Set登绢具 茄促.
sbi(DS1302_DDR,DS1302_IO); // io= input ddr set
sbi(DS1302_PORT,DS1302_CE); // ce= low
cbi(DS1302_PORT,DS1302_IO); // io= input ddr set
delay_us(5);
for(count=0;count<8;count++)
{
// 窍困官捞飘何磐 傈价
if(dval&0x01)
sbi(DS1302_PORT,DS1302_IO); // io= high
else
cbi(DS1302_PORT,DS1302_IO); // io= low
dval>>=1;
cbi(DS1302_PORT,DS1302_CLK); // ck= low
NOP;
NOP;
sbi(DS1302_PORT,DS1302_CLK); // ck= high
}
cbi(DS1302_DDR,DS1302_IO); // io= input
sbi(DS1302_PORT,DS1302_IO); // io= input
dval=0x00;
for(count=0;count<8;count++)
{
if(bit_is_set(DS1302_PIN,DS1302_IO))
dval |= 0x80;
dval>>=1;
sbi(DS1302_PORT,DS1302_CLK); // ck= high
NOP;
NOP;
cbi(DS1302_PORT,DS1302_CLK); // ck= low
}
cbi(DS1302_PORT,DS1302_CE); // ce= low
NOP;
sbi(DS1302_DDR,DS1302_IO); // io= output ddr set
return dval;
}
void DS1302_Write(unsigned char addr, unsigned char data)
{
unsigned char count;
sbi(DS1302_DDR,DS1302_IO);
sbi(DS1302_PORT,DS1302_CE);
delay_us(5);
for(count=0;count<8;count++)
{
if(addr&0x01)
sbi(DS1302_PORT,DS1302_IO);
else
cbi(DS1302_PORT,DS1302_IO);
addr >>= 1;
sbi(DS1302_PORT,DS1302_CLK);
NOP;
NOP;
cbi(DS1302_PORT,DS1302_CLK);
}
for(count=0;count<8;count++)
{
if(data&0x01)
sbi(DS1302_PORT,DS1302_IO);
else
cbi(DS1302_PORT,DS1302_IO);
data>>=1;
sbi(DS1302_PORT,DS1302_CLK);
NOP;
NOP;
cbi(DS1302_PORT,DS1302_CLK);
}
cbi(DS1302_PORT,DS1302_CE);
NOP;
NOP;
cbi(DS1302_PORT,DS1302_IO);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -