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

Discussion in 'C Programming' started by ssylee, Dec 31, 2007.

  1. ssylee

    ssylee Guest

    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!
     
    ssylee, Dec 31, 2007
    #1
    1. Advertising

  2. Re: function reading hour register either in 12 or 24 hour formatsand returning properly

    On Dec 31, 8:56 am, ssylee <> 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
     
    Nick Keighley, Dec 31, 2007
    #2
    1. Advertising

  3. ssylee

    ssylee Guest

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

    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?
     
    ssylee, Dec 31, 2007
    #3
  4. >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).
     
    Gordon Burditt, Dec 31, 2007
    #4
  5. ssylee

    Thad Smith Guest

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

    Nick Keighley wrote:
    > On Dec 31, 8:56 am, ssylee <> 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
     
    Thad Smith, Jan 1, 2008
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    912
    Hal Rosser
    Jun 1, 2007
  2. Christof Warlich

    reading from either cin or file

    Christof Warlich, Mar 2, 2009, in forum: C++
    Replies:
    2
    Views:
    403
    Christof Warlich
    Mar 2, 2009
  3. deadpickle
    Replies:
    1
    Views:
    1,089
    Jens Thoms Toerring
    Nov 7, 2010
  4. Phil Tomson
    Replies:
    11
    Views:
    217
    Daniel Berger
    Apr 6, 2005
  5. Chris Gunnels
    Replies:
    4
    Views:
    188
    Robert Klemme
    Sep 16, 2010
Loading...

Share This Page