function reading hour register either in 12 or 24 hour formats andreturning properly

S

ssylee

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).

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?

Thank you in advance for the comments!
 
N

Nick Keighley

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);
 
S

ssylee

Thanks for your reply Nick. I just have one more question on your
reply. What are the pros and cons of passing two pointers in the
parameters vs. using a custom-defined struct?
 
G

Gordon Burditt

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).

You could return a pointer to a static buffer (or return in a caller-supplied
buffer) a string like "08" or "8 AM" or "16". Since you're not doing
that, and this is an embedded machine apparently, you need to decide
what format you want the results in.

Why is a parameter called "adr" not a pointer?
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.

Decide what format you need.
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?

That's not the only way, but it's a good way. You probably need a 3-way
indicator (display "AM", display "PM", don't display either).
 
T

Thad Smith

Nick said:
so your function would look something like this:

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



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);

My preference would be to always return the time in 24 hour format and let
the caller format in 12 hour, if desired.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top