Skip to content
Snippets Groups Projects
Commit c3646d61 authored by da1l6's avatar da1l6
Browse files

Control: Add ds1307 RTC support module

parent 0e2c1e47
No related branches found
No related tags found
No related merge requests found
#include "../common/i2c_master.h"
#include "ds1307.h"
#include <stdio.h>
#include <avr/pgmspace.h>
extern void rtc_tick_handler(void);
void ds1307_init(void){
i2c_master_init();
}
uint8_t ds1307_get_date(date_t* date){
i2c_master_set_speed(I2C_BITRATE_REG_100K);
return i2c_master_read(DS1307_I2C_ADDRESS, DS1307_REG_DATE, sizeof(uint8_t), (uint8_t*)date, sizeof(date_t)) == I2C_SUCCESS;
}
uint8_t ds1307_set_date(date_t* date){
i2c_master_set_speed(I2C_BITRATE_REG_100K);
return i2c_master_write(DS1307_I2C_ADDRESS, DS1307_REG_DATE, sizeof(uint8_t), (uint8_t*)date, sizeof(date_t)) == I2C_SUCCESS;
}
uint8_t ds1307_set_time(time_t* time){
i2c_master_set_speed(I2C_BITRATE_REG_100K);
return i2c_master_write(DS1307_I2C_ADDRESS, DS1307_REG_DATE, sizeof(uint8_t), (uint8_t*)time, sizeof(time_t)) == I2C_SUCCESS;
}
void ds1307_write_date(date_t* date){
// char* wd[7] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
printf_P(PSTR("%02x-%02x-20%02x %02x:%02x:%02x"), date->day, date->month, date->year, date->hour, date->minute, date->second);
}
void ds1307_write_current_date(void){
date_t date;
uint8_t ret = i2c_master_read(DS1307_I2C_ADDRESS, DS1307_REG_DATE, sizeof(uint8_t), (uint8_t*)&date, sizeof(date_t));
if (ret == I2C_SUCCESS){
ds1307_write_date(&date);
} else {
printf_P(PSTR("I2C Err %02x"), ret);
}
}
\ No newline at end of file
#pragma once
#define DS1307_I2C_ADDRESS 0xD0
#define DS1307_REG_DATE 0x00
#define DS1307_REG_CONTROL 0x07
struct date_t{
uint8_t second;
uint8_t minute;
uint8_t hour;
uint8_t weekday;
uint8_t day;
uint8_t month;
uint8_t year;
} typedef date_t;
struct time_t{
uint8_t second;
uint8_t minute;
uint8_t hour;
} typedef time_t;
void ds1307_init(void);
uint8_t ds1307_get_date(date_t* date);
uint8_t ds1307_set_date(date_t* date);
uint8_t ds1307_set_time(time_t* time);
void ds1307_write_date(date_t* date);
void ds1307_write_current_date(void);
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment