Topics Topics Edit Profile Profile Help/Instructions Help Member List Member List  
Search Last 1|3|7 Days Search Search Tree View Tree View  

Read-modify-write

:: EPE Chat Zone ­:: ­Radio Bygones Message Board :: » EPE Forum Archives 2005-2006 » Archive through 18 November, 2006 » Read-modify-write « Previous Next »

  Thread Last Poster Posts Pages Last Post
  ClosedClosed: New threads not accepted on this page        

Author Message
Top of pagePrevious messageNext messageBottom of page Link to this message

Pat
Frequent Contributor
Username: Pat

Post Number: 62
Registered: 05-2005

Rating: N/A
Votes: 0 (Vote!)

Posted on Wednesday, 08 November, 2006 - 12:35 pm:   Edit Post Delete Post Print Post

Reference the problem "Read-modify-write", I cannot understand the explanation given in the Microchip data sheets. Could someone kindly give a simpler explanation of what it is, what are the implications and how to avoid being caught out?.

Kind regards,
Pat
Top of pagePrevious messageNext messageBottom of page Link to this message

Epithumia
Frequent Contributor
Username: Epithumia

Post Number: 156
Registered: 06-2006

Rating: N/A
Votes: 0 (Vote!)

Posted on Wednesday, 08 November, 2006 - 12:54 pm:   Edit Post Delete Post Print Post

The most obvious example would be something like:
MOVLW 0x10
XORWF PORTA, F

The XOR instruction reads PORTA, modifies the data by exclusive ORing with W (in this case causing bit 4 to be inverted), then writes the modified data back to PORTA. Result, output Port A4 should toggle.

The gotcha here is when you read PORTA you're actually reading the voltage on the I/O pin, not the value in the output register. If Port A4 is heavily loaded then it may read 0 even when the output register is 1. In that case the XOR instruction would always set A4, not toggle it.

I picked A4 because it is a open collector output requiring an external pullup to reach a high voltage, and so is very easy to overload.

I fell into this trap years ago with the EPE PIC tutorial series, because A4 was used to drive a tone into an earpiece and my earpiece had too low an impedance to allow A4 to ever go high - result, no tone.

Rob
If you need me, Neil and me will be hanging out with the Dream King. - Tori Amos
Top of pagePrevious messageNext messageBottom of page Link to this message

Mikehibbett
Frequent Contributor
Username: Mikehibbett

Post Number: 581
Registered: 04-2005

Rating: N/A
Votes: 0 (Vote!)

Posted on Wednesday, 08 November, 2006 - 01:37 pm:   Edit Post Delete Post Print Post

And it is a complete pain, and will keep tripping you up year after year :o)

That's one of the reasons why I like the PIC18F family; they have introduced a new register associated with each port, called LAT. It holds a copy of whatever is written to the port, so a command like

bsf LATA, 4

works as intended.

The alternative on the PIC16F family (and below) is to hold a copy of what you write to the port in a variable, and do your modifications to that variable. eg

PortA_copy EQU 0x45

...

bsf PortA_copy, 4
movfw PortA_copy
movwf PORTA


It's ugly code, no matter how you dress it up!

Mike
-----
www.drivesentinel.co.uk - Home build GPS Speed Camera Detectors
Top of pagePrevious messageNext messageBottom of page Link to this message

Violin_m
Frequent Contributor
Username: Violin_m

Post Number: 113
Registered: 05-2005

Rating: N/A
Votes: 0 (Vote!)

Posted on Wednesday, 08 November, 2006 - 06:23 pm:   Edit Post Delete Post Print Post

Quote ---Pins become cleared unexpectedly

Issue
Sometimes a high output pin on a port become cleared when doing a bit set on a different port pin. For example, bsf PORTA,2 sets PORTA bit 2 but clears PORTA bit 4 that was set earlier.

Solution
This behaviour happens because a BSF instruction causes a read-modify-write (R-M-W) operation. When the processor executes a BSF PORTA,2 instruction it reads PORTA, sets bit 2 of the data that was read, and writes the data back to PORTA. In many cases the data read from PORTA is not the same as the data that was written to the port. For example, analog pins will read as zero even if they are high outputs, input pins will read the pin state regardless of what was written, open drain outputs may be low even if a one was written to the pin, and pins shorted to Vss read zero.

The solution for PIC18 parts is to write to the LATx registers instead of the PORTx registers because they always read what was written to the output latch. As a general rule, always use LAT registers for outputting to ports and always use PORT registers for reading from ports. The intention with a BSF PORTA,2 bit operation is to write, not read, so use BSF LATA,2 instead.

For PIC16 parts, there are several solutions. Make sure that pins used for digital I/O are not configured as analog. If you know that a pin will read back differently because it has an external load or is tri-state or open drain, then avoid bit operations by writing all eight bits at once. You may need to use a general purpose register to keep track of the state of the pins and copy this register value to the port. --------End quote

Hi. This was taken from the Microchip site it might help to understand it better. I think it’s a good example of R-M-W

Mike. Out of curiosity, why did you use --movfw PortA_copy-- movfw is not an instruction, but a directive AFAIK excepted only by MPLAB IDE . Properly written it should be --movf PortA_copy,W--. I only mentioned this because I would imagine people leaning assembly would be a bit confused by this wording. Regards. Violin.
Top of pagePrevious messageNext messageBottom of page Link to this message

Atferrari
Frequent Contributor
Username: Atferrari

Post Number: 273
Registered: 05-2005


Rating: N/A
Votes: 0 (Vote!)

Posted on Thursday, 09 November, 2006 - 09:13 am:   Edit Post Delete Post Print Post

Whas this awful thing, common for other micros?

I don't recall of something like that before PICs.
Agustín Tomás - Buenos Aires - Argentina
Top of pagePrevious messageNext messageBottom of page Link to this message

Epithumia
Frequent Contributor
Username: Epithumia

Post Number: 157
Registered: 06-2006

Rating: N/A
Votes: 0 (Vote!)

Posted on Thursday, 09 November, 2006 - 09:40 am:   Edit Post Delete Post Print Post

In the old days we'd use something like a 6821 I/O port chip with an 8 bit micro. The 6821 had 2 ports, A and B. If you read port B you'd get back the value in the output register. If you read port A you'd get back the value on the pin.

So yes, you could get the same problem with old micros.

Rob
If you need me, Neil and me will be hanging out with the Dream King. - Tori Amos
Top of pagePrevious messageNext messageBottom of page Link to this message

Mikehibbett
Frequent Contributor
Username: Mikehibbett

Post Number: 582
Registered: 04-2005

Rating: N/A
Votes: 0 (Vote!)

Posted on Thursday, 09 November, 2006 - 11:46 am:   Edit Post Delete Post Print Post

Voilin - you'r right, it was lazyness on my part. movfw is accepted by GPASM, the assembler I use.

Mike
-----
www.drivesentinel.co.uk - Home build GPS Speed Camera Detectors
Top of pagePrevious messageNext messageBottom of page Link to this message

Pat
Frequent Contributor
Username: Pat

Post Number: 63
Registered: 05-2005

Rating: N/A
Votes: 0 (Vote!)

Posted on Thursday, 09 November, 2006 - 04:53 pm:   Edit Post Delete Post Print Post

Violin.m that was the most detailed answer so far and already I am beginning to understand the problem. First I will paraphrase to make sure I understand exactly what you said and at this moment I refer only to a PIC16F84.

1. A digital input can only have 2 states +v or 0v such as a switch for example. Same goes for digital output implying the load is neglible.
An analogue pin is an output with a current consuming load.

2.The read-modify-write problem only occurs with port output pins.

3.An instruction to an analogue outpin like bsf portb,5 or bcf portb,5 will definitely set or clear that pin as appropriate but at that point the PIC can become confused as to the actual state of the port pins but it will not actually change their state.

4. If you later in effect ask the PIC "What is the state of portb,4" by the instruction "btfsc portb,4" then if pin 4 is an input then the PIC actually reads the port 4 pin state and thus you get a correct answer. However the similar instruction "btfsc portb,5" may give an incorrect answer because it is an analogue output. This of course is an unacceptable state of affairs and what you say is the solution is to avoid bit operations by writing all eight bits at once or alternatively use a general purpose register to keep track of the state of the pins and copy this register value to the port. Thankyou all for your replies.

Afterrari by the look of it most micros suffer to some degree from the problem including the 8051 seris. Go to Google and enter "read-modify-write" to read all about it.

Kind regards,
Pat
Top of pagePrevious messageNext messageBottom of page Link to this message

John_becker
:::: Super User ::::
Username: John_becker

Post Number: 769
Registered: 05-2005

Rating: N/A
Votes: 0 (Vote!)

Posted on Thursday, 09 November, 2006 - 05:48 pm:   Edit Post Delete Post Print Post

MOVFW might be found elsewhere too - I wrote TK3 to recognise and act upon it, and similar for a few other Microchip "shorthand" commands

PORTB analog ?? Since when - purely digital so far as I know for all PICs I've encountered. And I've never heard of an analog pin being an output - would that PICs could provide D-A as well as A-D, the latter being common on PORTA & E of the 16F87x series for instance.

J
Top of pagePrevious messageNext messageBottom of page Link to this message

Epithumia
Frequent Contributor
Username: Epithumia

Post Number: 158
Registered: 06-2006

Rating: N/A
Votes: 0 (Vote!)

Posted on Thursday, 09 November, 2006 - 07:14 pm:   Edit Post Delete Post Print Post

We're more likely to have this problem on Port A than Port B because that's where the analogue functions are usually found.

3. Not quite. bsf and bcf should always set the selected port to the appropriate state, but they might also inadvertently change one of the other outputs as well, if the PIC cannot determine what state the output was in to start with.

4. PICs don't have analogue outputs. They do have analogue inputs. The problem is that if the analogue input function isn't turned off then btfsc porta, n will always see '0' regardless of what voltage is on the pin.

An example circuit which will fool the PIC is this:
NPN

PortA,0 controls T2 via a current limit resistor. Reading PortA,0 returns the correct value. PortA,4 is an open collector pin (the only such pin on the PIC) so needs a pullup resistor instead of a series resistor. But T1 base-emitter junction clamps A4 to 0.6V, so Port A4 always reads '0' even when it is programmed high.

Rob
If you need me, Neil and me will be hanging out with the Dream King. - Tori Amos
Top of pagePrevious messageNext messageBottom of page Link to this message

Violin_m
Frequent Contributor
Username: Violin_m

Post Number: 114
Registered: 05-2005

Rating: N/A
Votes: 0 (Vote!)

Posted on Friday, 10 November, 2006 - 01:16 pm:   Edit Post Delete Post Print Post

Quote------- An analogue pin is an output with a current consuming load.

I’m not really understanding in what you mean about this. As already stated by others, a Pic microcontroller doesn’t have an DA converter. Only AD converters and this doesn’t apply to 16f84/a

Quote-----Make sure that pins used for digital I/O are not configured as analogue. If you know that a pin will read back differently because it has an external load or is tri-state or open drain,

This means for example, using the 16f877/a. set all analogue pins as digital. Traditionally any analogue pins are on porta of all pic microcontrollers.
This by – movlw 0x06—movwf ADCON1-- which this SFR (special-function-register) is in bank one of this particular device/s

I will try and just finish it off with this.

An example of BCF/BSF

A --BSF PORTA,2 --- instruction performs what is known as a Read-Modify-Write operation. That means it first reads what has already been set to that particular port of 0-7bits –(or how many bits that particular port consists off), then modifies it to PORTA,2 = set/1 - and then writes back to PORTA with bit 2 now set/1. = R-M-W

In the process of reading back from this particular port it may mistakenly read back because of various affects on that particular port, for the reasons already explained. A different reading may now be obtained. It may now have now cleared PORTA,1. Which was previously set within the code.

As Mike has already stated. I don’t think of this R-M-W too much now, as my device of preference at the moment is the 18f452 so I use the LAT registers for this operation.

I hope this helps more. Regards. Violin.
Top of pagePrevious messageNext messageBottom of page Link to this message

Pat
Frequent Contributor
Username: Pat

Post Number: 64
Registered: 05-2005

Rating: N/A
Votes: 0 (Vote!)

Posted on Friday, 10 November, 2006 - 09:01 pm:   Edit Post Delete Post Print Post

Violin m., having only dealt with the PIC 16F84 I was unaware it had any analogue inputs or outputs but I was not about to argue with an expert and thus giving you the benefit of the doubt I paraphrased on the assumption that whilst nothing is ever perfect perhaps you were loosely referring to an analogue output as one being when the voltage may be pulled down to a voltage closer to 2.1v than the more normal +5v. However that has now been clarified; you were indeed talking about analogue in the strictest sense as found in such pics as the 16f627 and the 16F87x series......

Interesting Epithumia. So it is not just a case of the pic being confused as to the state of its pins but whilst obeying an instruction on one pin it might actually change the state of another pin without being told to? That might explain my latest unruly circuit which seems to have a mind of its own and starts and stops the motor when it deems the time is right!!!!

Also whilst I was aware RA4 has an open collector I had the impression all inputs needed an external base resistor to be provided. Whilst on that subject I would have thought that without a base resistor RA4 would not only read 0 but would be destroyed by an excessive current flow. When used as an input do any other 16f84 pins have a built-in base resistor?

Kind regards,
Pat
Top of pagePrevious messageNext messageBottom of page Link to this message

Magnum4
Frequent Contributor
Username: Magnum4

Post Number: 324
Registered: 04-2005


Rating: N/A
Votes: 0 (Vote!)

Posted on Friday, 10 November, 2006 - 09:33 pm:   Edit Post Delete Post Print Post

Thanks guy`s. I think.:-) I was working on something using the 16f87. I presume this the same? I was getting odd outputs from port B. I have been away for a while, but I think this could be my problem.:-)
Regards,
Jim
Top of pagePrevious messageNext messageBottom of page Link to this message

Epithumia
Frequent Contributor
Username: Epithumia

Post Number: 160
Registered: 06-2006

Rating: N/A
Votes: 0 (Vote!)

Posted on Friday, 10 November, 2006 - 09:42 pm:   Edit Post Delete Post Print Post

Despite all that was said earlier, the 16F88 does indeed have analogue functions on some of port B, alsthough the very similar 16F87 claims not to implement them.
If you need me, Neil and me will be hanging out with the Dream King. - Tori Amos
Top of pagePrevious messageNext messageBottom of page Link to this message

Violin_m
Frequent Contributor
Username: Violin_m

Post Number: 116
Registered: 05-2005

Rating: N/A
Votes: 0 (Vote!)

Posted on Saturday, 11 November, 2006 - 01:02 pm:   Edit Post Delete Post Print Post

Quote. - having only dealt with the PIC 16F84 I was unaware it had any analogue inputs or outputs

Earlier Quote from me.---- Only AD converters and this doesn’t apply to 16f84/a

This really meant that the 16f84/a doesn’t have any AD converters.

Sorry you miss understood me. Regards. Violin

Administration Administration Log Out Log Out   Previous Page Previous Page Next Page Next Page