//This library contains the functions regarding ds3231.In order to function properly,twi core functions library must be included #include "ds3231.h" void ds3231_init(void) { twi_init(); twi_start(); twi_send(0xD0);//initial address of ds3231+0 to write next byte twi_send(0x0E);//write location of control resistor twi_send(0x00);//Enable SQW at 1hz.This is to make atmega sync with DS3231 every second twi_stop(); } void ds3231_set(uint8_t hr,uint8_t min,uint8_t sec,uint8_t ampm,uint8_t yr,uint8_t mnth,uint8_t dt,uint8_t day) { hr &=0b00011111;//clear first three bits. hr |=0b01000000;//make D7=0,D6=1 for am/pm view,D5=0 for AM/1 for PM. hr |=(ampm<<5); twi_start(); twi_send(0xD0);//initial address of ds3231+0 to write next byte twi_send(0x00);//select location 0 to update twi_send(sec);//update second twi_send(min);//write location of control resistor twi_send(hr);//write 0 to the content of the resistor twi_send(day);//update day of the week twi_send(dt);//update date twi_send(mnth);//update month twi_send(yr);//update year twi_stop(); } void ds3231_get(uint8_t *h,uint8_t *m,uint8_t *s,uint8_t *yr,uint8_t *mnth,uint8_t *dt,uint8_t *day) { twi_start(); twi_send(0xD0); twi_send(0x00); twi_start(); twi_send(0xD1);//ds3231 address+1 to read *s=twi_receive(1);//1 to send ACK *m=twi_receive(1);//1 to send ACK *h=twi_receive(1);//0 to send NACK *day=twi_receive(1);//1 to send ACK,read day *dt=twi_receive(1);//1 to send ACK,read date *mnth=twi_receive(1);//1 to send ACK,read month *yr=twi_receive(0);//0 to send NACK,read year twi_stop(); }