Le lundi 19 octobre 2009, prateek sikka a écrit :
> We are facing an issue with serial port reception code we have written.
>  All the local variables are overwritten by the third value read from the
> serial port. 
> [...]
>  Can someone comment on it?

I've noticed some strange quirks with some code I'm writing as well (but being 
a newcomer in C, I always wonder if the quirks come from the compiler or are 
located between the keyboard and chair...) However I saw a very puzzling 
example last night in a thing that took me hours debugging - target is a 
PIC16F886. Example code :

// PIC output register
#define O_BUZ           RB3

// All the following are global variables defined in another.c file
volatile octetBits flag;
unsigned short int tuneRepeat;
unsigned short int tuneAddr;
unsigned short int tunePos;

// 2 following functions are in the main .c file
// Function that stops playing a tune
void subStopTune() {
        TMR2IE=0;
        TMR2ON=0;
        TMR2IF=0;
        O_BUZ=0;
        flag.tunePlaying=0;
        tuneRepeat=0; tuneAddr=0; tunePos=0;
}

// Function that starts playing a tune
void subPlayTune(unsigned short int address, unsigned short int repeat) {

        subStopTune();  // Stops previously playing tune, if any

        tuneRepeat=repeat;
        if (tuneRepeat>0) tuneRepeat--; // 0=1 ;-)
        tuneAddr=address;
        tunePos=address;
        flag.tunePlaying=1;
        subContTune();          // Play it
}

That looks simple enough, but what I had is that the tunes were properly 
playing, but never repeating. Debugging showed me that the "tuneRepeat" 
variable was always 0 when exiting subPlayTune(), whatever value could have 
the "repeat" parameter used when calling it :-(

After hours of debugging I found out that tuneRepeat was zero because it was 
zeroed in subStopTune(), even though subPlayTune() was setting it _after_ 
coming back from subStopTune() !

The tunePos and tuneAddr variables that seemed to be reset and set exactly the 
same way were however working properly !

Just removing "tuneRepeat=0;" from subStopTune() solved my problem, but for no 
reason that I'm able to understand - except for #me being stupid or a compiler 
quirk...

-- 
Michel Bouissou (OpenPGP ID 0xEB04D09C)

------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay 
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
Sdcc-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/sdcc-user

Reply via email to