Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > function reading hour register either in 12 or 24 hour formats andreturning properly

Reply
Thread Tools

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

 
 
ssylee
Guest
Posts: n/a
 
      12-31-2007
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!
 
Reply With Quote
 
 
 
 
Nick Keighley
Guest
Posts: n/a
 
      12-31-2007
On Dec 31, 8:56*am, ssylee <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
ssylee
Guest
Posts: n/a
 
      12-31-2007
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?
 
Reply With Quote
 
Gordon Burditt
Guest
Posts: n/a
 
      12-31-2007
>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).

 
Reply With Quote
 
Thad Smith
Guest
Posts: n/a
 
      01-01-2008
Nick Keighley wrote:
> On Dec 31, 8:56 am, ssylee <(E-Mail Removed)> 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);


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

--
Thad
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Passing an array from FORTRAN to C then passing it within C andReturning it to FORTRAN deadpickle C Programming 1 11-07-2010 02:38 PM
Is MKV formats better than other video formats? helena68 Software 3 11-26-2008 07:57 AM
Detailed Analysis of the file formats for M$ Office 2007 -"Microsoft Office XML Formats? Defective by design" Jonathan Walker NZ Computing 1 08-26-2007 03:43 AM
CyberLink Supports UDF 2.5/2.6 Formats For Blu-ray and HD DVD next generation of disc formats. Allan DVD Video 0 07-15-2005 07:43 PM
24 hour <--> 12 hour vcr clock fred@nospam.com Computer Support 4 08-13-2004 01:52 PM



Advertisments