I have written a simple C program to read hours and minutes and then add them together. But it is not getting added and currentHrMin
is printing only the value of minutes. However if getCurrentDate(&dateParams)
is called after printing currentHrMin
, there is no issue. I am not able to find out what is wrong with my code. Could be a silly issue. I am using MinGW C compiler.
#include <stdio.h>
#include <stdint.h>
#define BCD_TO_DEC(num) ((((num)&0xF0)>>4)*10+((num)&0x0F))
#define DEC_TO_BCD(num) ((((num)/10) << 4) | ((num) % 10))
struct RTC_TIME
{
uint8_t hours;
uint8_t minutes;
uint8_t seconds;
uint8_t twelveHourFormat:1; //1 = 12 hour format, 0=24 hour format.
uint8_t AM_0_PM_1:1;
uint8_t hours24Format;
uint8_t alarm1State:1;
uint8_t alarm2State:1;
};
struct RTC_DATE
{
uint8_t date;
uint8_t month;
uint8_t dayOfWeek;
uint8_t year;
};
void getCurrentTime(struct RTC_TIME* time)
{
printf("Enter Hour: ");
scanf("%d",&(time->hours));
printf("Enter Min: ");
scanf("%d",&(time->minutes));
}
void getCurrentDate(struct RTC_DATE* date)
{
printf("Enter Month: ");
scanf("%d",&(date->month));
}
int ar1[5]= {0x1253,0x1034,0x0804,0x1112,0x0409};
int main(void)
{
struct RTC_DATE dateParams;
struct RTC_TIME timeParams;
getCurrentTime(&timeParams);
getCurrentDate(&dateParams);
uint16_t currentHrMin = timeParams.hours*60 + timeParams.minutes;
printf("Current hour minute = %d\n",currentHrMin);
return(0);
}
After including #include <inttypes.h>
, change:
scanf("%d",&(time->hours));
to this:
scanf("%" SCNu8, &(time->hours));
in all your scanf's, so that you read for uint8_t, instead of an int.
The observation you made relates to that, you were reading with %d
specifier, which designated for type int
, which is usually 32 bits. So when you assign the read value to time->hours
, then it would "overflow" to the adjacent struct fields as well.
Next time, please enable your compiler's warning, and you should get something like this:
Georgioss-MacBook-Pro:~ gsamaras$ gcc -Wall main.c
main.c:32:16: warning: format specifies type 'int *' but the argument has type
'uint8_t *' (aka 'unsigned char *') [-Wformat]
scanf("%d",&(time->hours));
~~ ^~~~~~~~~~~~~~
%s
main.c:34:16: warning: format specifies type 'int *' but the argument has type
'uint8_t *' (aka 'unsigned char *') [-Wformat]
scanf("%d",&(time->minutes));
~~ ^~~~~~~~~~~~~~~~
%s
main.c:40:16: warning: format specifies type 'int *' but the argument has type
'uint8_t *' (aka 'unsigned char *') [-Wformat]
scanf("%d",&(date->month));
~~ ^~~~~~~~~~~~~~
%s
3 warnings generated.
I used Wall compiler flag, as I discuss in this answer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With