COSMIC function prototyping
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2006-10-17 5:44 AM
3 REPLIES 3
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2006-10-17 3:51 AM
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.Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2006-10-17 4:24 AM
Posted on October 17, 2006 at 13:24Peter, 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)
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2006-10-17 5:44 AM
Posted on October 17, 2006 at 14:44
My bad.
Thanks Luca