cancel
Showing results for 
Search instead for 
Did you mean: 

COSMIC function prototyping

pete3
Associate II
Posted on October 17, 2006 at 14:44

COSMIC function prototyping

3 REPLIES 3
pete3
Associate II
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.

luca239955_st
Associate III
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)

pete3
Associate II
Posted on October 17, 2006 at 14:44

My bad.

Thanks Luca