void ds1302_set_time(RTC_TIME_t *tm) { /* Stop RTC */ ds1302_write(DS1302_SEC, ds1302_read(DS1302_SEC) | 0x80); ds1302_write(DS1302_SEC, bin2bcd(tm->tm_sec)); ds1302_write(DS1302_MIN, bin2bcd(tm->tm_min)); ds1302_write(DS1302_HOUR, bin2bcd(tm->tm_hour)); ds1302_write(DS1302_DAY, bin2bcd(tm->tm_wday)); ds1302_write(DS1302_DATE, bin2bcd(tm->tm_mday)); ds1302_write(DS1302_MON, bin2bcd(tm->tm_mon + 1)); ds1302_write(DS1302_YEAR, bin2bcd(tm->tm_year % 100)); /* Start RTC */ ds1302_write(DS1302_SEC, ds1302_read(DS1302_SEC) & ~0x80); }
void ds1302_CurTime_Get(unsigned char Curtime[]) { unsigned char i; unsigned char addr = DS1302_SEC|1; //读操作 // for (i=0;i<7;i++){ Curtime[i] = ds1302_read(addr); /*格式为: 秒 分 时 日 月 星期 年 */ addr += 2; // } }
void ds1302_init(void) { CLR_CLK; bus_nop; CLR_RST; bus_nop; DS1302_WP_DIS; //DS1302_CHG_EN; //允许涓流充电 ds1302_write(DS1302_SEC,ds1302_read(DS1302_SEC)&0x7f); //24小时制 DS1302_WP_EN; }
void ds1302_test() { char sec; lcd_cursor(0,0); //year/month/date lcd_hex8(0x20); lcd_hex8(ds1302_read(DS1302_YEAR)); lcd_puts("/"); lcd_hex8(ds1302_read(DS1302_MONTH)); lcd_puts("/"); lcd_hex8(ds1302_read(DS1302_DATE)); lcd_puts(" "); /*hour:min*/ sec=ds1302_read(DS1302_SEC); lcd_hex8(ds1302_read(DS1302_HOUR)); if(sec%2) lcd_puts(":"); else lcd_puts(" "); lcd_hex8(ds1302_read(DS1302_MIN)); }
unsigned char ds1302_read_time(RTC_TIME_t *tm) { tm->tm_sec = bcd2bin(ds1302_read(DS1302_SEC)); tm->tm_min = bcd2bin(ds1302_read(DS1302_MIN)); tm->tm_hour = bcd2bin(ds1302_read(DS1302_HOUR)); tm->tm_wday = bcd2bin(ds1302_read(DS1302_DAY)); tm->tm_mday = bcd2bin(ds1302_read(DS1302_DATE)); tm->tm_mon = bcd2bin(ds1302_read(DS1302_MON)) - 1; tm->tm_year = bcd2bin(ds1302_read(DS1302_YEAR)); return rtc_valid_tm(tm); }