I have discovered strange behaviour on PIC18F2455. One of the output I/O ports was connected to a load that dropped voltage on the i/o pin down to around 3V (VCC = 5V). Under these cismumstance the program could not set the pin into high state. Whe the program ran freely the instruction
bsf PORTC, 1
was apparently ignored. Of course, TRIS bit was cleared and all peripherals using RC1 was disabled.
However if I set a breakpoint on this instruction and single stepped through it (I was using ICD2) then the pin was indeed set to high state, and remained high if I continued to run the program after that. Direct manipulation of the bit while the program was halted also worked fine.
I already resolved the issue, however having spent few hours tracing it I would like to hear some comments as to why it works that way.
Posts: 5416
Joined: Nov. 7, 2003
From: Morgan Hill, CA
Status: offline
Are you doing any other 'bsf' instructions to the PORTC register? If so, you may be seeing a read-modify-write problem, especially with that pin being so heavily loaded.
In 18F chips, you can avoid r-m-w problems completely by performing any 'bsf' and 'bcf' instructions to the LATC register rather than the PORTC register. The LATC contents will always reflect the last value written to the port rather than whatever happens to be read on the pins when the instruction executes.
<added I should have included this:
An R-M-W problem on a pin doesn't occur when you perform a 'bcf' or 'bsf' on that pin. It occurs when you perform any instruction that reads the port, modifies some other bit, and then writes the whole byte back to the port. If that read happens before the pin you've just set has reached a high enough voltage level, the pin you've just set will be written back to a zero.
R-M-W issues aren't only caused by 'bsf' and 'bcf' instructions. Logical operations such as 'andwf', 'iorwf', and 'xorwf' can trigger this behavior as well. /added>
While it's always good to learn from one's mistakes, it's much easier to learn from the mistakes of others. Please don't PM me with technical questions. I'll be quite happy to help (if I can) on the forums.
Yes, I'm aware of the read-modify-write isuue, it's not the case here. When program was freely runinng the pin did not switch into high state, I was monitoring it using logic analyzer.
What I can't understand is difference between single stepping and normal execution of the program....
Posts: 5416
Joined: Nov. 7, 2003
From: Morgan Hill, CA
Status: offline
Probably the most common reason for programs to act differently between single-stepping and normal execution is that, while single-stepping, the I/O pins have sufficient time between instructions to reach their final states. This frequently masks R-M-W problems that only appear when the program is run at full speed.
I'd still suggest performing any 'bsf' and 'bcf' instructions for I/O port pins to the LATx register rather than the PORTx register. Doing that guarantees that you won't have any R-M-W issues.
_____________________________
While it's always good to learn from one's mistakes, it's much easier to learn from the mistakes of others. Please don't PM me with technical questions. I'll be quite happy to help (if I can) on the forums.
Posts: 5416
Joined: Nov. 7, 2003
From: Morgan Hill, CA
Status: offline
Here's a scope picture that I took of a PIC 18F4550 output being turned on. The pin has a capacitive load.
Notice that for much of the first 10 uS (that's a LOT of instruction times) after the pin is switched to a high, its voltage has barely risen toward a volt. Any port modification instruction (bcf, bsf, andwf, iorwf, xorwf) that's executed during that time will read the pin back as a zero. When the instruction writes the modified value back, this bit will be written back to a zero.
While it's always good to learn from one's mistakes, it's much easier to learn from the mistakes of others. Please don't PM me with technical questions. I'll be quite happy to help (if I can) on the forums.
hi i am experincing the same problem regarding a lower voltage when it should be giving 5V. I try the method you all have recommended but still i got the same problem the MCU i used is PIC18F8722 and i am using the Development board from MCU. So i assume the board should be working fine (I did some testing on it).
The RMW problem is unfortunately extrememly common.
I've started to point people towards the PICList FAQ at http://massmind.org/techref/readmodwrite.htm . Does that cover it, or is there anything else I should add / anything confusing I need to take out?