2017-06-01 12:17 AM
Hey guys, this is my first post in this forum.
So i am trying to interface ds1307 tiny rtc module with stm32f401 nucleo via i2c1 but unable to set time to it. I am getting the uptime of rtc but whenever I am trying to set time,nothing happens.
here is some of my code:
/* i2c related----------------------------------------*/
char slave_address_lcd = 0x4E;char slave_address_clock = 0xD0;int chkflag = 0;void check_i2c_status (void);/* clock related -----------------------------------------*/uint8_t sec_rec, min_rec;void clock_init(void);struct time_t { uint8_t seconds; uint8_t minutes; uint8_t hour; uint8_t day; uint8_t date; uint8_t month; uint8_t year;}time;void set_time (uint8_t sec, uint8_t min, uint8_t hr, uint8_t d,uint8_t dt, uint8_t mon, uint8_t yr);void get_time (void);uint8_t cnvt_to_bin (uint8_t data);uint8_t cnvt_to_dec (uint8_t data);/*----------------->>>>>>>>>>>>>>>>--------------------*/
{ uint8_t high = 0; uint8_t low = 0; high = data/10; low = (data)-(high * 10); return (high<<4) | low;}/*----------------->>>>>>>>>>>>>>>>--------------------*/
uint8_t cnvt_to_dec (uint8_t data)
{ return ((data & 0x0f) + ((data>>4)&0x0f)*10);}/*----------------->>>>>>>>>>>>>>>>--------------------*/
void set_time (uint8_t sec, uint8_t min, uint8_t hr, uint8_t d,uint8_t dt, uint8_t mon, uint8_t yr)
{ uint8_t data[7], addr = 0x00; data[0] = cnvt_to_bin (sec); data[1] = cnvt_to_bin (min); data[2] = cnvt_to_bin (hr); data[3] = cnvt_to_bin (d); data[4] = cnvt_to_bin (dt); data[5] = cnvt_to_bin (mon); data[6] = cnvt_to_bin (yr); HAL_I2C_Master_Transmit (&hi2c1, slave_address_clock, &addr, 1, 100); HAL_I2C_Master_Transmit (&hi2c1, slave_address_clock, data, 7, 100);}/*----------------->>>>>>>>>>>>>>>>--------------------*/
void clock_init (void)
{ uint8_t data[2]; data[0] = 0x00; // go to address 0x00 data[1] = 0x00; // write 0 to that address thus starting rtc HAL_I2C_Master_Transmit (&hi2c1, slave_address_clock, data, 2, 100);}/*----------------->>>>>>>>>>>>>>>>--------------------*/
void get_time (void)
{ uint8_t data[7], addr = 0x00; HAL_I2C_Master_Transmit (&hi2c1, slave_address_clock, &addr, 1, 100); HAL_I2C_Master_Receive (&hi2c1, slave_address_clock|0x01, data, 7, 100); time.seconds = cnvt_to_dec (data[0]); time.minutes = cnvt_to_dec (data[1]); time.hour = cnvt_to_dec (data[2]); time.day = cnvt_to_dec (data[3]); time.date = cnvt_to_dec (data[4]); time.month = cnvt_to_dec (data[5]); time.year = cnvt_to_dec (data[6]);}/*----------------->>>>>>>>>>>>>>>>--------------------*/
void StartDefaultTask(void const * argument){/* USER CODE BEGIN 5 */
/* Infinite loop */ for(;;) { get_time (); sprintf (lcd_hour, '%02u', time.hour); lcd_send_cmd (0x80); lcd_send_string (lcd_hour); sprintf (lcd_min, '%02u', time.minutes); lcd_send_cmd (0x83); lcd_send_string(lcd_min); sprintf (lcd_sec, '%02u', time.seconds); lcd_send_cmd (0x86); lcd_send_string(lcd_sec); osDelay(100); } /* USER CODE END 5 */ }/*----------------->>>>>>>>>>>>>>>>--------------------*/
main (){ clock_init (); set_time (25, 50, 20, 2, 31, 05, 17);}#i2c #stm32f4 #i2c-ds1307 #rtos