STM32-RTC-MainLoop&DisplayUpdate

by Marwen Maghrebi
int main(void)
{
    /* MCU Configuration--------------------------------------------------------*/

    /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
    HAL_Init();

    /* Configure the system clock */
    SystemClock_Config();

    /* Initialize all configured peripherals */
    MX_GPIO_Init();
    MX_SPI1_Init();
    MX_RTC_Init();
    MX_USART1_UART_Init();

    /* USER CODE BEGIN 2 */
    ST7735_Init();  // Initialize the TFT display
    ST7735_Backlight_On();  // Turn on the display backlight

    // Set the display orientation
    uint8_t rotation = 1;  // 1 for portrait or 0 for landscape, based on preference
    ST7735_SetRotation(rotation);  

    // Fill the screen with a background color
    ST7735_FillRoundRect(1, 1, 160, 128, 2, ST7735_BLUE);  // Blue background

    // Draw screen borders
    for (int x = 0; x < ST7735_GetWidth(); x++) {
        ST7735_DrawPixel(x, 0, ST7735_WHITE);  // Top border
        ST7735_DrawPixel(x, ST7735_GetHeight() - 1, ST7735_WHITE);  // Bottom border
    }
    for (int y = 0; y < ST7735_GetHeight(); y++) {
        ST7735_DrawPixel(0, y, ST7735_WHITE);  // Left border
        ST7735_DrawPixel(ST7735_GetWidth() - 1, y, ST7735_WHITE);  // Right border
    }

    /* USER CODE END 2 */

    /* Infinite loop */
    while (1)
    {
        // Get the current time and date from the RTC
        HAL_RTC_GetTime(&hrtc, &sTime, RTC_FORMAT_BIN);
        HAL_RTC_GetDate(&hrtc, &sDate, RTC_FORMAT_BIN);

        // Format the time and date into strings for display
        snprintf(time, sizeof(time), "Time: %02d:%02d:%02d", sTime.Hours, sTime.Minutes, sTime.Seconds);
        snprintf(date, sizeof(date), "Date: %02d/%02d/20%02d", sDate.Date, sDate.Month, sDate.Year);

        // Clear previous text (black background) and display the updated time and date
        ST7735_WriteString(5, 5, time, Font_7x10, ST7735_WHITE, ST7735_BLACK);  // Display time
        ST7735_WriteString(5, 20, date, Font_7x10, ST7735_WHITE, ST7735_BLACK);  // Display date

        // Send the time and date over UART for logging/debugging
        snprintf(uart_buf, sizeof(uart_buf), "%02d:%02d:%02d - %02d/%02d/20%02d\n\r",
                 sTime.Hours, sTime.Minutes, sTime.Seconds, sDate.Date, sDate.Month, sDate.Year);
        HAL_UART_Transmit(&huart1, (uint8_t*)uart_buf, strlen(uart_buf), HAL_MAX_DELAY);

        // Delay for 1 second before updating again
        HAL_Delay(1000);
    }
}
Are you sure want to unlock this post?
Unlock left : 0
Are you sure want to cancel subscription?
-
00:00
00:00
Update Required Flash plugin
-
00:00
00:00