//打开DS1302
void TimeSpiOpen(void)
{
TIMECLK = 0;
TIMERST = 0;//禁止DS1302
TIMEIO = 1;//释放数据总线
TIMERST = 1;//使能DS1302
}
//关闭DS1302
void TimeSpiClose(void)
{
TIMERST = 0;//禁止DS1302
TIMEIO = 1;//释放数据总线
TIMECLK = 1;
}
//读写DS1302
unsigned char TimeSpiReadWrite(unsigned char val)
{
unsigned char i;
ACC = val;//取8位数据
for (i = 8;i > 0; i--)
{
TIMECLK = 0;//时钟下降沿输入数据(DS1302读)
_nop_();//延时
CY = TIMEIO;//接收串行数据到CY
_rrca_();//右移一位数据到CY(先存后取)
TIMEIO = CY;//发送串行数据
TIMECLK = 1;//时钟上升沿打入数据(DS1302写)
TIMEIO = 1;//释放数据总线
}
val = ACC;
return val;
}
//读DS1302字节
unsigned char TimeByteRead(unsigned char address)
{
TimeSpiOpen();//打开DS1302
TimeSpiReadWrite(address);//写入DS1302命令
address = TimeSpiReadWrite(0xff);//读入DS1302数据
TimeSpiClose();//关闭DS1302
return address;//返回数据
}
//读DS1302RAM字节
unsigned char TimeRamByteRead(unsigned char address)
{
TimeSpiOpen();//打开DS1302
TimeSpiReadWrite((address << 1) | 0xc1);//写入DS1302命令
address = TimeSpiReadWrite(0xff);//读入DS1302数据
TimeSpiClose();//关闭DS1302
return address;//返回数据
}
//写DS1302字节
void TimeByteWrite(unsigned char address, unsigned char val)
{
TimeSpiOpen();//打开DS1302
TimeSpiReadWrite(address);//写地址
TimeSpiReadWrite(val);//写数据
TimeSpiClose();//关闭DS1302
}
//写DS1302字节
void TimeRamByteWrite(unsigned char address, unsigned char val)
{
TimeSpiOpen();//打开DS1302
TimeSpiReadWrite((address << 1) | 0xc0);//写地址
TimeSpiReadWrite(val);//写数据
TimeSpiClose();//关闭DS1302
}
//使能DS1302写保护
void TimeWriteEnable(void)
{
TimeByteWrite(timewriteregall, timewriteseg);//使能DS1302写保护
}
//开放DS1302写保护
void TimeWrieDisbale(void)
{
TimeByteWrite(timewriteregall, 0);//开放DS1302写保护
}
//DS1302工作开始
void TimeWorkStart(void)
{
unsigned char val;
val = TimeByteRead(timereadseg);//读秒数据
val &= 0x7f;//打开晶振
TimeWrieDisbale();//开放DS1302写保护
TimeByteWrite(timewriteseg, val);//打开晶振
TimeByteWrite(tricklewrite, 0xa5);//打开充电二极管
TimeWriteEnable();//使能DS1302写保护
}
上一篇:CH375读写U盘C51示例源程序
下一篇:时钟芯片ds12c887的C51驱动程序
推荐阅读最新更新时间:2024-11-17 07:56
设计资源 培训 开发板 精华推荐
- LM285Z-1.2 1.2V 参考的 1.5V 电池典型应用电路
- DC782A-A,用于 LTC2249 高速、80 Msps、14 位 1MHz 的演示板Ain< 70MHz
- DC826C-B,用于 LTC2951-2 按钮开/关控制器的演示板
- #第五届立创电子设计大赛#32位单片机开发板
- NXQ1TXH5插件板
- art扩展板
- LPRP 低功耗参考平台具有板级电源管理框架 (PMF),适用于电池供电的 Nios II/SOPC Builder 设计
- STC8A8K64S4A12_C84390核心板
- C500623-GD32F150K8U6/R8T6方案验证板
- #第五届立创电子设计大赛#云浙智能家居-触控面板