cancel
Showing results for 
Search instead for 
Did you mean: 

how to use external RTC

EmbeddedWithLove
Associate

I'm using STM32G070KBT6 controller.

I'm trying to use external RTC and External RTC were ( DS1341). i2c based RTC.

Here is my code

/* Private user code ---------------------------------------------------------*/

/* USER CODE BEGIN 0 */

// Convert normal decimal numbers to binary coded decimal

uint8_t decToBcd(int val)

{

 return (uint8_t)( (val/10*16) + (val%10) );

}

// Convert binary coded decimal to normal decimal numbers

int bcdToDec(uint8_t val)

{

 return (int)( (val/16*10) + (val%16) );

}

typedef struct {

uint8_t seconds;

uint8_t minutes;

uint8_t hour;

uint8_t dayofweek;

uint8_t dayofmonth;

uint8_t month;

uint8_t year;

} TIME;

TIME time;

// function to set time

void Set_Time (uint8_t sec, uint8_t min, uint8_t hour, uint8_t dow, uint8_t dom, uint8_t month, uint8_t year)

{

uint8_t set_time[7];

set_time[0] = decToBcd(sec);

set_time[1] = decToBcd(min);

set_time[2] = decToBcd(hour);

set_time[3] = decToBcd(dow);

set_time[4] = decToBcd(dom);

set_time[5] = decToBcd(month);

set_time[6] = decToBcd(year);

HAL_I2C_Mem_Write(&hi2c1, DS1341_ADDRESS_Write, 0x00, 1, set_time, 7, 1000);

}

void Get_Time (void)

{

uint8_t get_time[7];

HAL_I2C_Mem_Read(&hi2c1, DS1341_ADDRESS_Write, 0x00, 1, get_time, 7, 1000);

time.seconds = bcdToDec(get_time[0]);

time.minutes = bcdToDec(get_time[1]);

time.hour = bcdToDec(get_time[2]);

time.dayofweek = bcdToDec(get_time[3]);

time.dayofmonth = bcdToDec(get_time[4]);

time.month = bcdToDec(get_time[5]);

time.year = bcdToDec(get_time[6]);

}

/* USER CODE END 0 */

int main(void)

{

 /* MCU Configuration--------------------------------------------------------*/

 /* Reset of all peripherals, Initializes the Flash interface and the Systick. */

 HAL_Init();

 /* USER CODE BEGIN Init */

 /* USER CODE END Init */

 /* Configure the system clock */

 SystemClock_Config();

 /* Initialize all configured peripherals */

 MX_GPIO_Init();

 MX_I2C1_Init();

 MX_USART1_UART_Init();

 /* USER CODE BEGIN 2 */

 Set_Time(03, 05, 7, 5, 4, 2, 22);

 /* USER CODE END 2 */

 /* Infinite loop */

 /* USER CODE BEGIN WHILE */

 while (1)

 {

 HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_6);

 Get_Time();

printf("\n Time : %02d:%02d:%02d", time.hour, time.minutes, time.seconds);

 printf("\n Time : %02d-%02d-20%02d", time.dayofmonth, time.month, time.year);

 HAL_Delay(500);

  /* USER CODE END WHILE */

  /* USER CODE BEGIN 3 */

Here is the dock light output

Time : 10:164:13 --> 164 acts as a sec

Time : 00-00-2000

Time : 11:00:16

Time : 40-00-2000

Time : 11:02:24

Time : 00-00-2000

0693W00000Lwlc2QAB.png 

My issues are:

  1. I can't able to read the time and date from the external RTC.

Please, let me know my mistakes to learn from this.

Thanks in advance for your time and support

This discussion is locked. Please start a new topic to ask your question.
1 REPLY 1
Ala
Senior

shouldn't you enable LSE for RTC sake?