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.

Read Modify Write explanation needed.

Status
Not open for further replies.

Suraj143

Active Member
Can somebody find me & give the tutorial that explains RMW (read modify write)
I searched in microchip but I couldn’t find that one.

This is regarding PORTA, PORTB SFR. Until now I’m using directly like this.

Code:
bsf	PORTA,1
How can I use an image file to this? Without accessing directly.
Can somebody show me how to use an image file?

Thanks
 
When you want the PIC to change bit(s) on the port pins, the PIC has to first read the level of the port pins, then apply the change in its ALU before outputting the result back to the port.

Problem arise when one is changing different bits in "successive" instructions using a "very fast" PIC clock frequency. Because of possible external loading on the port pin, its voltage level may take time to match with the output logic level that the programmer wants. Before the voltage reaches this level, if another port change instruction is executed within this moment, then some of the pins' level will be wrong.

This behavior will never show up in the simulator. Be warned.

Solution is to assign and use an image or shadow register. Do all the changes on this memory location, then output its content to the port after all changes are made.
 
toodles said:
Is this still a problem if LATA is written to instead of PORTA?

No, Microchip introduced the LAT registers in 18F chips to overcome the problem.

The problem is also explained on the piclist

For example, say you use a shadow reg, you may want to set a port bit or do some logical operation on the port. You do everything you want using your shadow reg instead of the port, then read the shadow reg into W and write W to the port.
 
My 2 pence's worth on the subject

As eblc1388 said the problem arises due to capacitance on a pin

The Pic works like this when altering a port

1 it reads the port
2 it alters that value
3 it writes it back

now if you write to a pin then on the next instruction or 2 you write to another pin on the same port. It will as I said read/modify/ and write the value back.

Should the line have a high capacitance it will have held the line low as the pic charges the line, a fast rewrite will see the line as not set and consequently will alter the value it has then write it back, obviously now its written back a low when you meant it to be high.

The solution's round it are
1 as stated below keep a copy your self modify it and write it back in one go
2 place a delay between writing to the port it may take only a from a few nops it all depends on the capacitance
3 move up to an 18 series and use the LAT regs

BTW I use ISIS and yes it does emulate the Read/Modify/Write issue like in real life
 
Suraj143 said:
Can somebody show me a simple example to turn ON PORTA,1 with an image register.

Thanks

You just do all your processing on the image and then write it to the port.
Code:
	bsf	ImageA,3
	bsf	ImageA,4
	bcf	ImageA,7
	movfw	ImageA
	movwf	PORTA

Mike.
 
Wow now I clearly understand what the effects of RMW when accessing to ports are. Earlier I haven’t notice about this problem.

May be this is one reason my PIC is not resetting properly. It’s a 2 seven segment display counter. Two segments driving through two transistors. Commands I use

bsf PORTA,1
bcf PORTA,2

Thanks a lot
elbc1388
toodles
picasm
blueroomelectronics
Tim B

Code:
	bsf	ImageA,3
	bsf	ImageA,4
	bcf	ImageA,7
	movfw	ImageA
	movwf	PORTA
Thanks Mike this is the one I just wondering.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top