Tamas Rudnai wrote: > 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 It *is* a (sort of) RMW error, but not a *timing* problem, a delay will not help no matter how long. All pins set as analog input (such as RA4 in this case) will always read as "0". RA5 does not have any analog function and therefore wortks OK even with the analog (comparators) enabled... It's all in the datasheet, and it's cleary documented in the "PORT" section, even in the specific code example (example 4-2). Jan-Erik. 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 >> > > > -- http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist