Re: function reading hour register either in 12 or 24 hour formats and returning properly



On Dec 31, 8:56 am, ssylee <sta***...@xxxxxxxxx> wrote:

I want to write a function that would return the hour of the time in
either 12 or 24 hour format. The hour data is accessed through reading
a register on a Real-time clock by a function:

unsigned char oneByteRead(unsigned char adr, short ramCal);

where as adr is the address (let's call the address location to be
passed HOURADR), and ramCal is whether the register is in Calendar
data or custom RAM data (let's call the calendar data RTCCLOCK).

so your function would look something like this:

unsigned char readHour (void)
{
return oneByteRead (HOURADR, RTCCLOCK);
}


Basically my function would determine the format that the hour is in
and return the hour. I don't see any problem returning the 24 hour
format. The only problem is returning the AM/PM hour data to complete
the function call. Would I need to pass a separate pointer parameter
in order to return 2 things at a time, one being the hour, the other
being AM/PM, if the function determines that the byte is stored in 12
hour format?

you are basically trying to return two values from a function.
In C you are left with two reasonable choices (unreasonable choices
would be global data (ug!) or static data (non-reentrant code)).

1. pass two pointers
void getHour (unsigned char *hour, unsigned char *flag24);

2. use a struct
typedef struct
{
unsigned char hour;
unsigned char flag24;
} HourType;

void getHour (HourType *hourType);


--
Nick Keighley
.