ds1302.c
来自「CVAVR的函数库的源代码。开发AVR的编译器CodeVision AVR最新版」· C语言 代码 · 共 53 行
C
53 行
/*
CodeVisionAVR C Compiler
(C) 1998-2004 Pavel Haiduc, HP InfoTech S.R.L.
Dallas Semiconductors DS1302 Real Time Clock functions
*/
#include <bcd.h>
void rtc_init(unsigned char tc_on,unsigned char diodes,unsigned char res)
{
res&=3;
if (tc_on) res|=0xa0;
if (diodes==1) res|=4;
else if (diodes==2) res|=8;
else res=0;
ds1302_write(0x8e,0);
ds1302_write(0x90,res);
ds1302_write(0x8e,0x80);
}
void rtc_get_time(unsigned char *hour,unsigned char *min,unsigned char *sec)
{
*hour=bcd2bin(ds1302_read(0x85));
*min=bcd2bin(ds1302_read(0x83));
*sec=bcd2bin(ds1302_read(0x81));
}
void rtc_set_time(unsigned char hour,unsigned char min,unsigned char sec)
{
ds1302_write(0x8e,0);
ds1302_write(0x84,bin2bcd(hour));
ds1302_write(0x82,bin2bcd(min));
ds1302_write(0x80,bin2bcd(sec));
ds1302_write(0x8e,0x80);
}
void rtc_get_date(unsigned char *date,unsigned char *month,unsigned char *year)
{
*date=bcd2bin(ds1302_read(0x87));
*month=bcd2bin(ds1302_read(0x89));
*year=bcd2bin(ds1302_read(0x8d));
}
void rtc_set_date(unsigned char date,unsigned char month,unsigned char year)
{
ds1302_write(0x8e,0);
ds1302_write(0x86,bin2bcd(date));
ds1302_write(0x88,bin2bcd(month));
ds1302_write(0x8c,bin2bcd(year));
ds1302_write(0x8e,0x80);
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?