cancel
Showing results for 
Search instead for 
Did you mean: 

stm32 RTC Time Setting and Recalling

ark2
Associate III
case 1
time_t Timestamp;
Timestamp = gettime();
uint8_t *data_ptr = &body_q[1];
for (int i = 1; i <= 4; i++) {
body_q[i] = (Timestamp >> (8 * (4 - i))) & 0xFF;
}
            break;
        case 2: 
        memset(time_check, 0x0, sizeof(time_check));
            for(int k = 0; k < 4; k++){
            time_check[k] = body_q[k + 1];
            }
                settime(time_check);
            /*
                // UNIX 타임스탬프 정보를 가져옵니다.
                uint32_t set_Timestamp = 0;
 
            for (int check = 0; check < 4; check++) {
            set_Timestamp = (set_Timestamp << (8 * check)) | body_q[check + 1];
            }
 
                // UNIX 타임스탬프를 RTC 시간/날짜 형식으로 변환합니다.
                struct tm timeinfo;
                time_t rawtime = (time_t) set_Timestamp;
                localtime_r(&rawtime, &timeinfo);
                RTC_TimeTypeDef sTime;
                RTC_DateTypeDef sDate;
 
                // 한국 시간에 맞춰 조정
                timeinfo.tm_hour += 9;
                sTime.Hours = timeinfo.tm_hour;
                sTime.Minutes = timeinfo.tm_min;
                sTime.Seconds = timeinfo.tm_sec;
                sDate.Date = timeinfo.tm_mday;
                sDate.Month = timeinfo.tm_mon + 1;  // tm_mon은 0부터 시작하므로 1을 더함.
                sDate.Year = timeinfo.tm_year - 100;
 
                // 시간 및 날짜 설정
                HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BIN);
                HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BIN);
                */
            break;
 
 
uint32_t gettime(){
RTC_TimeTypeDef gTime;
RTC_DateTypeDef gDate;
struct tm get_timeinfo;
time_t get_Timestamp;
 
// STM32의 RTC에서 현재 로컬 시간 및 날짜 정보를 가져옵니다.
HAL_RTC_GetTime(&hrtc, &gTime, RTC_FORMAT_BIN);
HAL_RTC_GetDate(&hrtc, &gDate, RTC_FORMAT_BIN); // 날짜를 가져오지 않으면 시간 정보가 업데이트되지 않습니다.
 
// struct tm 형식으로 변환
get_timeinfo.tm_hour = gTime.Hours - 9;  // UTC로 변환
get_timeinfo.tm_min = gTime.Minutes;
get_timeinfo.tm_sec = gTime.Seconds;
get_timeinfo.tm_mday = gDate.Date;
get_timeinfo.tm_mon = gDate.Month - 1;  // tm_mon은 0부터 시작
get_timeinfo.tm_year = gDate.Year + 100;  // 1900을 기준으로 함
 
// UNIX 타임스탬프로 변환
get_Timestamp = mktime(&get_timeinfo);
return get_Timestamp;
}
 
 
 
void settime(uint8_t time_check[]){
    // UNIX 타임스탬프 정보를 가져옵니다.
    uint32_t set_Timestamp = 0;
 
for (int check = 0; check < 4; check++) {
set_Timestamp = (set_Timestamp << 😎 | time_check[check];
}
 
    // UNIX 타임스탬프를 RTC 시간/날짜 형식으로 변환합니다.
    struct tm set_timeinfo;
    time_t rawtime = (time_t) set_Timestamp;
    localtime_r(&rawtime, &set_timeinfo);
    RTC_TimeTypeDef sTime;
    RTC_DateTypeDef sDate;
 
    // 한국 시간에 맞춰 조정
    set_timeinfo.tm_hour += 9;
    sTime.Hours = set_timeinfo.tm_hour;
    sTime.Minutes = set_timeinfo.tm_min;
    sTime.Seconds = set_timeinfo.tm_sec;
    sDate.Date = set_timeinfo.tm_mday;
    sDate.Month = set_timeinfo.tm_mon + 1;  // tm_mon은 0부터 시작하므로 1을 더함.
    sDate.Year = set_timeinfo.tm_year - 100;
 
    // 시간 및 날짜 설정
    HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BIN);
    HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BIN);
}
 
I made it like this, and when I set it up, I sent the value {64}{A7}{86}{A1} and when I read it, it called {AF}{61}{05}{23}

I understand the slight error, but it's too different

When I set it up, I checked that 64A786A1 went to set_Timestamp properly and 64A786A1 came out, but it was like that

Even if I use the setup time function without using it, I get another value, how can I get the right value?

I did it because I brought a code that says it works
What should I do
1 REPLY 1

Read out and check the RTC registers' content.

> timeinfo.tm_hour += 9;

You can't just add time like that, this is likely to overflow. I you want to adjust local time to UTC and vice versa, add seconds to the epoch-based time (time_t rawtime).

JW