General Timer based on RTC using stm32f103rb

Posted

in

by

This timer uses stm32 internal rtc peripheral to display time.

The initialization code is generated using CUBEMX which is embedded inside the CUBE IDE.

/* USER CODE BEGIN WHILE */
	
	RTC_TimeTypeDef readTime;	// RTC Time structure
	RTC_DateTypeDef readDate;	// RTC Date structure
	uint8_t time_hou_var[2];
	uint8_t time_min_var[2];
	uint8_t time_sec_var[2];

	while (1)
	{


    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
		/*  function to read time from RTC shadow register */
		    HAL_RTC_GetTime(&hrtc, &readTime, RTC_FORMAT_BIN);

		/* function to read date from RTC shadow register	*/
		    HAL_RTC_GetDate(&hrtc, &readDate, RTC_FORMAT_BIN);

		    itoa(readTime.Hours,(char *)time_hou_var,10);
		    textPtr = ((uint8_t *)(time_hou_var));
		    LCD_Fill(0, 40, 240, (40 + 1)+30, WHITE);
		    WriteString(10,(40*1)+10,textPtr,RED);

		    textPtr = ((uint8_t *)":");
		    WriteString(40,(40*1)+10,textPtr,GREEN);

		    itoa(readTime.Minutes,(char *)time_min_var,10);
		    textPtr = ((uint8_t *)(time_min_var));
		  //  LCD_Fill(0, 40, 240, (40 + 1)+30, WHITE);
		    WriteString(45,(40*1)+10,textPtr,RED);

		    textPtr = ((uint8_t *)":");
		    WriteString(70,(40*1)+10,textPtr,GREEN);

		    itoa(readTime.Seconds,(char *)time_sec_var,10);
		    textPtr = ((uint8_t *)(time_sec_var));
		    //   LCD_Fill(0, 40, 240, (40 + 1)+30, WHITE);
		    WriteString(75,(40*1)+10,textPtr,RED);

		    HAL_Delay(1000);

		    if(HAL_GPIO_ReadPin (GPIOA, KEY1_Pin))
		    {
		    	// Set The LED ON!
		    	HAL_GPIO_WritePin(GPIOA, LED1_Pin, GPIO_PIN_SET);

		    }
		    else
		    {
		    	// Else .. Turn LED OFF!
		    	HAL_GPIO_WritePin(GPIOA, LED1_Pin, GPIO_PIN_RESET);
		    	//HAL_GPIO_WritePin(LCD_BL_EN_GPIO_Port, LCD_BL_EN_Pin, GPIO_PIN_RESET);
		    	HAL_GPIO_TogglePin(LCD_BL_EN_GPIO_Port, LCD_BL_EN_Pin);
		    }
}

The code uses itoa() function which needs stdlib.h header file.

itoa() function in C language converts the integer into ASCII digits which are stored in a buffer.

itoa( integar_to_be_converted, Buffer_to_store_conversion, Base_system);

You can choose the base system in itoa function. For conversion to decimal number system, you enter 10

for binary, you can write 2.

for hexadecimal, you can write 16.

and so on.

Comments

Leave a Reply

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