cancel
Showing results for 
Search instead for 
Did you mean: 

How To get internal RTC time using STM8L050J3

Cpg2
Associate II

Hi All, 

 

We are using the STM8L050J3 chip and trying to get time using an internal RTC with LSI. We are getting some responses but are not sure what it is. Here is our result (TIME: 3108:4877:2644) and code. We are using the cosmic compiler. Could anyone help me make it correct and point out where the issue is? All support is hugely appreciated. 

Code: 

#include "stm8l15x.h"
#include <stdio.h>
 
#ifdef _COSMIC_
 #define ASM _asm
#endif
#ifdef _IAR_
 #define ASM asm
#endif
#define STARTUP_SWIM_DELAY_5S \
 { \
 ASM(" PUSHW X \n" \
 " PUSH A \n" \
 " LDW X, #0xFFFF \n" \
 "loop1: LD A, #50 \n" \
 \
 "loop2: DEC A \n" \
 " JRNE loop2 \n" \
 \
 " DECW X \n" \
 " JRNE loop1 \n" \
 \
 " POP A \n" \
 " POPW X " );\
 }
 
  uint8_t i;
// Simple delay
void delay_ms(uint16_t ms) {
uint32_t i;
    while (ms--) {
        for (i = 0; i < 1600; i++);
    }
}
 
// Redirect printf to UART
 
void UART1_Init_Custom(void) {
 
 CLK_PeripheralClockConfig(CLK_Peripheral_USART1, ENABLE);
 USART_Init(USART1, 9600, USART_WordLength_8b, USART_StopBits_1,USART_Parity_No,(USART_Mode_TypeDef)(USART_Mode_Rx |USART_Mode_Tx));
 USART_Cmd(USART1, ENABLE);
}
 
void RTC_Init_Custom(void) {
 
RTC_InitTypeDef rtc_init;
    RTC_TimeTypeDef time;
    RTC_DateTypeDef date;
 
CLK_SYSCLKDivConfig(CLK_SYSCLKDiv_1 );
    CLK_RTCClockConfig(CLK_RTCCLKSource_LSI, CLK_RTCCLKDiv_1);
    CLK_PeripheralClockConfig(CLK_Peripheral_RTC, ENABLE);
 
    RTC_WakeUpCmd(DISABLE);
    RTC_EnterInitMode();
 
 
 
     // Configure RTC prescalers for LSI (~38 kHz)
    RTC_StructInit(&rtc_init);
    rtc_init.RTC_HourFormat = RTC_HourFormat_24;
 
    // LSI ~38kHz ? we need to divide to get 1Hz
    // 38,000 / (PrescalerA + 1)(PrescalerS + 1) = 1
    // Try 37 * 1024 = 37888 ~ 1Hz
    rtc_init.RTC_AsynchPrediv = 36;  // divide by 37
    rtc_init.RTC_SynchPrediv  = 1023; // divide by 1024
 
    RTC_Init(&rtc_init);
 
    // Set time: 12:34:56
    RTC_TimeStructInit(&time);
    time.RTC_Hours = 12;
    time.RTC_Minutes = 34;
    time.RTC_Seconds = 56;
    RTC_SetTime(RTC_Format_BIN, &time);
 
    // Set date: 11 April 2025 (Friday)
    RTC_DateStructInit(&date);
    date.RTC_WeekDay = 5;
    date.RTC_Date = 11;
    date.RTC_Month = 4;
    date.RTC_Year = 2025;
    RTC_SetDate(RTC_Format_BIN, &date);
 
 
    RTC_ExitInitMode();
    RTC_WaitForSynchro();
}
void UART1_SendStr(char* str)
{
    while (*str)
    {
        USART_SendData8(USART1, *str++);
        while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
delay_ms(1);
    }
}
 
void print_current_datetime(void) {
 
    RTC_TimeTypeDef time;
    RTC_DateTypeDef date;
 
    char buffer[128];
 
    RTC_GetTime(RTC_Format_BIN, &time);
    RTC_GetDate(RTC_Format_BIN, &date);
 
    printf("Date: 20%02d-%02d-%02d\n", date.RTC_Year, date.RTC_Month, date.RTC_Date);
 
sprintf(buffer, "\r\nTIME: %02d:%02d:%02d\r\n",(uint8_t)time.RTC_Hours,(uint8_t)time.RTC_Minutes,(uint8_t)time.RTC_Seconds);
   UART1_SendStr(buffer);
     //USART_SendData8(time.RTC_Hours);
//  USART_SendData8('\r');
 
    printf("Time: %02d:%02d:%02d\n", time.RTC_Hours, time.RTC_Minutes, time.RTC_Seconds);
}
 
void main(void) {
 
STARTUP_SWIM_DELAY_5S;
//delay_ms(1000);
 
GPIO_Init(GPIOC, GPIO_Pin_5, GPIO_Mode_Out_PP_High_Fast); // TxD: output
   GPIO_Init(GPIOA, GPIO_Pin_3, GPIO_Mode_Out_PP_High_Fast); // 
 
  CLK_SYSCLKDivConfig(CLK_SYSCLKDiv_1);
 
    UART1_Init_Custom();
    RTC_Init_Custom();
 
    printf("RTC Initialized.\n");
 
    while (1) {
        USART_SendData8(USART1,0x41);
        delay_ms(1000);
 
 
print_current_datetime();
 
    }
}
 
 
 
 
 
/*

and output is: 

////OUTPUT /////


TIME: 3107:13069:2644
A
TIME: 3107:14861:2644
A
TIME: 3108:1293:2644
A
TIME: 3108:3085:2644
A
TIME: 3108:4877:2644

*/

3 REPLIES 3
AA1
Senior III

printf %d expects an integer argument and RTC_Hours, RTC_Minutes and RTC_Seconds are character variables.
Try this:

printf("Time: %02d:%02d:%02d\n", (uint16_t)time.RTC_Hours (uint16_t)time.RTC_Minutes, (uint16_t)time.RTC_Seconds);

 

Cpg2
Associate II

Thanks for the reference. 

there was a conversion issue same we solved. Now we have encountered another issue of accuracy. we are using internal clock LSI. what should be expectations and how we can bring the best possible accuracy?

See AN3133 page 20: "LSI accuracy is not suitable for calendar application". RTC clock usually is LSE 32.768 KHz.