RTC Date and Time Display Issues on Tera Term with Format and Pointer Errors

Good day everyone, my aim is to showcase the RTC date and time on Tera Term; however, I am facing errors that are explained in the code below. Furthermore, no information is appearing on Tera Term. My approach involves using pointers according to SetDate and GetDate declarations. Nonetheless, there have been warnings such as: 1. format '%d' expects a matching 'int' argument [-Wformat=] 2. passing argument 2 of 'HAL_RTC_GetDate' from incompatible pointer type [-Wincompatible-pointer-types] 3. passing argument 2 of 'HAL_RTC_SetDate' from incompatible pointer type [-Wincompatible-pointer-types] Here’s the code I'm working with:
#include "main.h"
#include "stdio.h"

//uint8_t Time[6]="HH:MM:SS";
//uint8_t Date[6]="DD/MM/YY";

int main(void)
{
/* USER CODE BEGIN 1 */
typedef struct
{
uint8_t Month = 0x03;
uint8_t Date = 0x24;
uint8_t Year = 0x21;
}Date_struct;

uint8_t *Date;
Date = &Date_struct; // Error: Expected expression before Date_struct

HAL_RTC_SetDate(&hrtc, &Date, RTC_FORMAT_BCD);

while (1)
{
HAL_RTC_GetDate(&hrtc,&Date, RTC_FORMAT_BCD);
HAL_Delay(1000);
}
}
#include "main.h"
#include "stdio.h"

//uint8_t Time[6]="HH:MM:SS";
//uint8_t Date[6]="DD/MM/YY";

int main(void)
{
/* USER CODE BEGIN 1 */
typedef struct
{
uint8_t Month = 0x03;
uint8_t Date = 0x24;
uint8_t Year = 0x21;
}Date_struct;

uint8_t *Date;
Date = &Date_struct; // Error: Expected expression before Date_struct

HAL_RTC_SetDate(&hrtc, &Date, RTC_FORMAT_BCD);

while (1)
{
HAL_RTC_GetDate(&hrtc,&Date, RTC_FORMAT_BCD);
HAL_Delay(1000);
}
}
How can I resolve these errors and warnings?
2 Replies
UC GEE
UC GEE3mo ago
Hey @Sterling In C in your project, you cannot initialize structure members directly within the structure definition. Instead, you should define the structure and then create an instance of it. Another error that's occurring in your project,You're trying to use a pointer to a uint8_t to point to a structure (Date_struct) . This error is causing the warnings about incompatible pointer types. Here’s a corrected version of your code that addresses the above issues:
#include "main.h"
#include "stdio.h"

// Define a structure for the date
typedef struct
{
uint8_t Month;
uint8_t Date;
uint8_t Year;
} Date_struct;

int main(void)
{
// Initialize the RTC handle (assuming it is defined elsewhere)
RTC_HandleTypeDef hrtc;

// Create an instance of Date_struct
Date_struct date = {0x03, 0x24, 0x21}; // March 24, 2021

// Set the date in RTC
HAL_RTC_SetDate(&hrtc, (RTC_DateTypeDef*)&date, RTC_FORMAT_BCD);

while (1)
{
// Get the date from RTC
HAL_RTC_GetDate(&hrtc, (RTC_DateTypeDef*)&date, RTC_FORMAT_BCD);

// Here you can print the date using Tera Term or any other method
// Example: printf("Date: %02d/%02d/%02d\n", date.Date, date.Month, date.Year);

HAL_Delay(1000);
}
}
#include "main.h"
#include "stdio.h"

// Define a structure for the date
typedef struct
{
uint8_t Month;
uint8_t Date;
uint8_t Year;
} Date_struct;

int main(void)
{
// Initialize the RTC handle (assuming it is defined elsewhere)
RTC_HandleTypeDef hrtc;

// Create an instance of Date_struct
Date_struct date = {0x03, 0x24, 0x21}; // March 24, 2021

// Set the date in RTC
HAL_RTC_SetDate(&hrtc, (RTC_DateTypeDef*)&date, RTC_FORMAT_BCD);

while (1)
{
// Get the date from RTC
HAL_RTC_GetDate(&hrtc, (RTC_DateTypeDef*)&date, RTC_FORMAT_BCD);

// Here you can print the date using Tera Term or any other method
// Example: printf("Date: %02d/%02d/%02d\n", date.Date, date.Month, date.Year);

HAL_Delay(1000);
}
}
Sterling
Sterling3mo ago
Woah, that’s loaded… thanks @UC GEE
Want results from more Discord servers?
Add your server