Skip to main content
Visitor II
October 17, 2006
Question

COSMIC function prototyping

  • October 17, 2006
  • 3 replies
  • 795 views
Posted on October 17, 2006 at 14:44

COSMIC function prototyping

    This topic has been closed for replies.

    3 replies

    pete3Author
    Visitor II
    October 17, 2006
    Posted on October 17, 2006 at 12:51

    I have found using the 16k compiler that to pass an unsigned long variable to a function I have to prototype the function properly or the value passed to the function is not always correct.

    Sadly I have no meaningful debug on this circuit or I would investigate further. The code is as follows:

    void delay(unsigned long len){

    unsigned long tstart = 0;

    unsigned char tnew = 0;

    for(tstart = 0; tstart < len; tstart++){

    tnew = 0;

    for(tnew = 0; tnew < 2; tnew++);

    WDGCR = 0xff;

    for(tnew = 0; tnew < sizeof(len); tnew++){

    SCI1DR = (unsigned char) (len >> (tnew * 8));

    while(!(SCI1SR & 0x40)) WDGCR = 0xff;

    }

    }

    PDDR &= 0xef;

    }

    This gets locked in an endless loop unless I include the following line at the start of the program:

    void delay(unsigned long);

    The clue is that the final line before returning (i.e. outside both for loops) is not executed so I presume the test tstart < len always fails.

    I have sent the contents of the variable along RS232. When I send decimal 15 I get 26 00 00 00 when I left-shift the variable and CD 4F 06 00 when I right-shift. Either way, the test fails. When the function is properly protoyped the test passes and the code executes as I would expect.

    Visitor II
    October 17, 2006
    Posted on October 17, 2006 at 13:24

    Peter,

    if you don't prototype a function the compiler will assume the arguments are int, hence the wrong behavoiur when they are not. I think this is standard C, not specific to Cosmic.

    You should always prototype functions: the compiler offers a switch (+strict) to generate an error when you forget to do so.

    Regards,

    Luca (Cosmic)

    pete3Author
    Visitor II
    October 17, 2006
    Posted on October 17, 2006 at 14:44

    My bad.

    Thanks Luca