Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Please advice bit wise command not working properly

Status
Not open for further replies.

micropad

Member
Dear All

Program 1
Code:
bsf  portc,0
bsf  portd,0

loop 
rlf  portc,f
rlf  potrd,f
goto  loop

Above code runs two three loops and PORTC rotation stops and PORTD rotation continue some time getting stuck

Then i change program as follows

Code:
bsf  portc,0
bsf  portd,0

loop 
incf  portc,f
incf  potrd,f
goto  loop

This program runs well forever

watchdog timer is off
TRIS register configuration is correct

(but both program above mention are working properly in Proteus Simulator and PIC IDE Simulator. The problem occur only my real hardware )

Please advice bit wise command not working properly

Thanks in advance
 
Last edited:
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.

Mike.
 
Firstly, I assume the typos aren't in the actual program (o instead of 0 and potrd).

Yes sorry for my typing mistake this not cut from actual program

Code:
bsf  portc,0
bsf  portd,0
 
loop 
rlf  portc,f
nop
nop
nop

rlf  potrd,f
nop
nop
nop
goto  loop

I edit program as per above in my actual program but problem is remaining unchanged ( I think this is a solution for RMW)


The other possible reason is the carry bit is getting cleared
Can you please explain this more
Thanks in advance
 
Last edited:
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.

Code:
EQU ghost_c 0x2C
EQU ghost_d 0x2E

bsf ghost_c,0
bsf ghost_d,0

loop
rlf ghost_c,f
movf ghost_c,w
movwf PORTC

rlf ghost_d,f
movf ghost_d,w
movwf PORTD

goto loop
 
Hi Dear Ian Rogers,

Really problem is solved by your advice

It would be much appreciated can you please explain the actual scenario
Thanks in advance
 
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.
 
What PIC is that? 16F family?
 
Dear Ian Rojer,

Thanks for very theoretical explanation

rlf ghost_d,f

movf ghost_d,w
movwf PORTD

As per I know the above program also following read modify write behavior . but it is working .Am in correct ?
Please advice
Thanks in advance
 
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.

Mike.
 
Last edited:
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.

Dear Pommie Thanks for reply
What do you think in my case
No any capacitance load, just LED only
Please advice
Thanks in advance
 
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.

Mike.
 
Capacitance is in the chip the way it's made the outside load adds to the problem.
I've seen it happen with almost no load driving a high ohm input.

But it's still a load.

So there both the problem use a copy of the port and problems gone The base line chips are real bad about this.
 
Dear be80be,Pommie,
Please do not anger with me, if I continue same problem again and again

INCF PORTD,F is followed by RMW behavior , But it is working fine .
is this case dose not effect the load behavior?
Please advice
Thanks in advance
 
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.

Mike.
 
Last edited:
Dear All,
Thank you so much for all

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
 
I personally think ( also with experience ) that its mainly the "bitwise" operations that cause port issues... ie... BCF and BCF

byte wise don't seem to have the same issues..

And if you ask Microchip which instructions do and don't have issues, you don't get a complete answer. You get,
All write operations are read-modify-write operations.

And
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?

John
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top