Where do functions with no input or output return variables?

This is a discussion on Where do functions with no input or output return variables? within the C Programming forums, part of the General Programming Boards category; Hi i have this piece of code and was wondering where it returns the value? Code: void ReadPOT(void) { ADCON0bits.GO ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    13

    Question Where do functions with no input or output return variables?

    Hi i have this piece of code and was wondering where it returns the value?

    Code:
    void ReadPOT(void)
    {
        ADCON0bits.GO = 1;              // Start AD conversion
        while(ADCON0bits.NOT_DONE);     // Wait for conversion
        return;
    }//end ReadPOT
    i just wanted to check also that this means that the number 1 is put into the member named GO.

    I'm not sure how NOT_DONE as a member would detect whether the AD conversion has completed - does anyone have any idea? how can using a dot operator here check a bit inside the ADCON0 register?

    Thanks

  2. #2
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    ADCON0bits would have to be globaly declared within the same source file (or by extern).

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,855
    It would probably need to be declared 'volatile' as well in order to make sure the bit was tested over and over.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by phoenix23
    Hi i have this piece of code and was wondering where it returns the value?

    Code:
    void ReadPOT(void)
    {
        ADCON0bits.GO = 1;              // Start AD conversion
        while(ADCON0bits.NOT_DONE);     // Wait for conversion
        return;
    }//end ReadPOT
    i just wanted to check also that this means that the number 1 is put into the member named GO.

    I'm not sure how NOT_DONE as a member would detect whether the AD conversion has completed - does anyone have any idea? how can using a dot operator here check a bit inside the ADCON0 register?
    A guess is that ADCON0bits is declared as a structure with bitfields, and some of the names of these bitfields are GO and NOT_DONE. And I would further guess that some implementation specific method is used to absolutely place this structure atop the location of a status and control register for A/D conversion. And as Salem mentioned, it is also likely volatile qualified to keep the optimizer away from the idle loop code. All of this magic is probably found in some platform-specific header.

    So this:
    Code:
    ADCON0bits.GO = 1;              // Start AD conversion
    is used to set that particular bit in the register (which starts the A/D).

    Then this:
    Code:
    while(ADCON0bits.NOT_DONE);     // Wait for conversion
    is an idle loop that waits for the A/D conversion to complete, at which time the bit corresponding to NOT_DONE is cleared -- by the micro.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can someone help me with these errors please code included
    By geekrockergal in forum C Programming
    Replies: 7
    Last Post: 02-10-2009, 02:20 PM
  2. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 04:42 PM
  3. need help program crashing
    By tunerfreak in forum C++ Programming
    Replies: 14
    Last Post: 05-22-2006, 12:29 PM
  4. Why only 32x32? (OpenGL) [Please help]
    By Queatrix in forum Game Programming
    Replies: 2
    Last Post: 01-23-2006, 02:39 PM
  5. opengl program as win API menu item
    By SAMSAM in forum Game Programming
    Replies: 1
    Last Post: 03-03-2003, 07:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21