Hi, I think you have are facing to a so called RMW error. With PIC when you are dealing with port bits, the controller first reads out the entire port, then modifies the bit and then writes it back to the port --> Read-Modify-Write. The problem is that you set one bit high, and immediately after you set another bit. For each PIC reads the entire port but the previous state may not yet settled. The easiest workaround is to put a little delay in between the two port modification or better to use a shadow register - so you make modification to the shadow and then write the entire shadow on the porta. Tamas On Sun, Nov 23, 2008 at 11:56 PM, Robert Kiely wrote: > Hi all, > > I've just begun using PIC's and am using the following code to flash > the LED's connected to RA4 and RA5 on and then off by using timer0. As > you can see from the code the LED connected to RA4 is turned on first > and then the program uses timer0 to flash the LED's. When I program > this onto the PIC however it doesn't work even though it simulates > correctly in MPLAB SIM. After some time I decided to swap the code > around by changing all references to porta.4 to porta.5 and vice > verse. This meant that the LED connected to RA5 turned on first. This > worked perfectly first time. I can't understand why this is and was > hoping that somebody might be able to shed some light on this for me > as it's been annoying me for days ! I'm using MPLAB IDE with the > BoostC PIC16 C compiler and a PicKit 1 flash starter kit for > programming with the supplied PicKit1 Classic programming software. > > Rgds, > Rob > > > #include > #pragma DATA 0x2007, _INTRC_OSC_NOCLKOUT & _WDT_OFF // set > configuration byte > > void main(void){ > option_reg = 135; // set timer0 mode and prescaler > trisa = 207; // set porta input/output modes > porta = 0; // turn off porta outputs > tmr0 = 0; // set timer0 value > intcon = 0; // clear all flags and disable all > interrupt (just in case) > porta.4 = 1; // turn on led d0 > intcon.5 = 1; // enable timer0 interrupts > intcon.7 = 1; // enable interrupts > while(1); // wait for timer0 overflow > } > > void interrupt(void){ > if (porta.4 == 0){ // if led d1 is on > porta.5 = 0; // turn it off and > porta.4 = 1; // turn on led d0 > } else { > porta.4 = 0; // turn of led d0 > porta.5 = 1; // turn on led d1 > } > intcon.2 = 0; // clear timer0 flag > return; > } > -- > http://www.piclist.com PIC/SX FAQ & list archive > View/change your membership options at > http://mailman.mit.edu/mailman/listinfo/piclist > -- Rudonix DoubleSaver http://www.rudonix.com -- http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist