How to connect STM32F429I-DISC1 board to DS1307 using I2C

Posted

in

by

On the STM32F429 board, there is one I2c extension connector. This connector has eight pins. Which is used to connect to other I2C peripherals such as RTC, EEPROM and other microcontrollers etc.

DS1307 connected to I2C

In the hardware schematics, it is labelled as ACP/RF E2P connector.

The I2C3 SDA and SCL lines are pulled up via a 4.7 k ohm resistor to VCC 3.3V.

This is the basic code that I used to set/get the data in/from the DS1307 via I2c.

/* USER CODE BEGIN 4 */
struct Time{
	  uint8_t sec;
	  uint8_t min;
	  uint8_t hour;
	  uint8_t weekday;
	  uint8_t day;
	  uint8_t month;
	  uint8_t year;
	  };

/* USER CODE END 4 */

/* USER CODE BEGIN Header_StartDefaultTask */
/**
  * @brief  Function implementing the defaultTask thread.
  * @param  argument: Not used
  * @retval None
  */
/* USER CODE END Header_StartDefaultTask */
void StartDefaultTask(void const * argument)
{
  /* init code for USB_HOST */
  MX_USB_HOST_Init();
  /* USER CODE BEGIN 5 */
 char buff[30];
  uint8_t *ptr1;
  uint8_t *ptr2;
  ptr2 = (uint8_t *)buff;

  struct Time Set_time,Get_Time;

  Set_time.sec = 0;
  Set_time.min = 0;
  Set_time.hour = 0;
  Set_time.day = 0;
  Set_time.month = 04;
  Set_time.year = 0;
  Set_time.weekday = 0;

  HAL_I2C_Mem_Write(&hi2c3, 0xd0, 0, 1,&Set_time.sec, 7, 1000);



  /* Infinite loop */
  for(;;)
  {
	 
	  HAL_I2C_Mem_Read(&hi2c3, 0xD1, 0, 1, &Get_Time.sec, 7, 1000);
    osDelay(1000);
    ptr1 = (uint8_t *)"Hello\n";
    HAL_UART_Transmit(&huart1, ptr1, 6, 1000);
    sprintf(buff,"%02x:%02x:%02x - %02x - %02x/%02x/%02x \n",
    		Get_Time.hour,
			Get_Time.min,
			Get_Time.sec,
			Get_Time.weekday,
			Get_Time.day,
			Get_Time.month,
			Get_Time.year);
    HAL_UART_Transmit(&huart1, ptr2,26, 1000);
  }
  /* USER CODE END 5 */
}

In this code, I created a structure for the time, weekday and date. Which is similar to the internal registers of DS1307.

The Structure then creates two instances called set_time and Get_time. The Set_time object is filled with the values and then its location is transfered to the HAL_I2C_Mem_Write function. Which sends this data through polling to the DS1307.

Similarly the Get_time structure is used to retrieve the data from the DS1307 using the HAL_I2C_Mem_Read function. Which reads 7 bytes from the DS1307.

The retrieved time and date are then sent via the UART to display on a terminal.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *