Firstly, I assume the typos aren't in the actual program (o instead of 0 and potrd).
I would guess that the reason it's not working correctly is due to the speed you're changing things. You have to allow a little time for the pins to change state. Read up on the RMW (Read, Modify, Write) problem that pics have. The other possible reason is the carry bit is getting cleared. If you incorporate a delay then make sure you preserve the carry bit.
As Mike said... Ports on modern day micro's have tons of peripherals... Consequently.. lots of protection.. The Read Modify Write latching issues can cause this sort of problem. I isn't picked up by simulation software solutions...
To make it work right.. all of the time, you can use a "ghost" register.
Ok.. The Read Modify Write issue is on many of the PIC's..
Take the statement BSF PORTB,1
The whole port is read Assume b'00001000' ( bit 3 is set )
The latch register is then updated b'0001010' ( bit 3 and bit 1 are now set )
The latch register is written to the port..
If you write the whole 8 bits then the port is cleared, updated and written to.
So
Code:
BSF PORTB,1
theoretically takes three steps
Code:
movlw 1
movwf PORTB
this is a safer way... ( still takes three steps )
Try in future to get a pic with a writable latch register... Extended PIC16's and PIC18's all have latch registers.
THE RMW problem only occurs when the source and target are a port as port pins have capacitance and loads. When you keep a memory copy (of the port) you only ever write to the port and so the RMW problem never happens.
THE RMW problem only occurs when the source and target are a port as port pins have capacitance and loads. When you keep a memory copy (of the port) you only ever write to the port and so the RMW problem never happens.
The problem is in the chip not the outside loads a shallow copy fixes that problem. It's like making the pin latch like the 18f chips have but your doing it in code.
I said capacitance AND load. The pin itself has capacitance and the LED is the load. If you have just an LED without a series resistor then the ON voltage for the pin will be 2V. A voltage of 2V on the pin, when read back, can be seen as 1 or 0 as it's between the high and low values. So, although you might write a one to a pin it may read back as a zero. In this case it is the load that is causing the problem not the capacitance.
Due to the tolerance of the chips some will work fine at 2V but others will fail. You might try 100 chips and find 50 work and 50 don't. Best way is to stay in tolerance (best solution) or use a shadow register (but chip may fail).
BTW, I wasn't angry, just emphasizing the AND.
Edit, Just remembered a project from about 10 years ago which had a 50% failure rate on the ADC. Turns out I had set the pin to output and so if the chips tolerance made that pin hi it malfunctioned. That had me baffled for a few hours.
Finally some one can explain the why matter of fact the RRF and INCF instructions behave in two different manner
If fault with chip the RRF and INCF both should be effected
If problem attached to the load and capacitance the RRF and INCF both should be effected
Finally the Shadow register solved the problem
It would be much appreciated If some one can explain in logic level operation
lease advice
Thanks in advance
When using read-modify-write instructions (ex. BCF, BSF, etc.) on a port, the value of the port pins is read, the desired operation is done to this value, and this value is then written to the port latch.
Now, the first statement seems clear, but the second is not. What instructions are included in, "etc." People have asked on forums for a full list, but I have not found that question to be answered. The usual default answer is the first quote above.
Aren't there some byte-wise operations that are affected? Microchip gives an example of such with CLRF in its 16F datasheets. Are there others?