org 0 Start bsf STATUS,RP0 ; select Register Page 1 clrf TRISC ; make IO PortC all output bcf STATUS,RP0 ; back to Register Page 0
bsf PORTC,0 ;Turn on led 1 bsf PORTC,1 ;Turn on led 2 bsf PORTC,2 ;Turn on led 3 bsf PORTC,3 ;Turn on led 4
goto $ ;wait here end
In this case only the last led that I switch on (led4) is on and the rest are off, after bsf PORTC,X the previous bsf PORTC,X that I wrote turns off. Is this normal? I think that all leds must be on.
Posts: 1545
Joined: Mar. 26, 2004
From: Michigan, USA
Status: offline
quote:
ORIGINAL: danielle
This is a "read-modify-write" problem, please look it up in the datasheet and search this forum for more information.
Danielle,
Are you familiar with this relatively new 20-pin device? The Port C pins are configured as Analog inputs after reset... Perhaps you should take a little time before jumping in with a quick answer?
Posts: 3510
Joined: Aug. 17, 2004
From: St. Leonards-on-Sea, E. Sussex, UK.
Status: offline
quote:
ORIGINAL: K8LH
quote:
ORIGINAL: danielle
This is a "read-modify-write" problem, please look it up in the datasheet and search this forum for more information.
Danielle,
Are you familiar with this relatively new 20-pin device? The Port C pins are configured as Analog inputs after reset... Perhaps you should take a little time before jumping in with a quick answer?
Regards, Mike
He was obviously using the debugger to step through his program. It overrides the ADC function, even if it hasn't been disabled in the code.
The fact that the LEDs were coming on and then going off is a clear indication of read-modify-write behaviour and not incorrect port initialisation!
Leon
< Message edited by leon_heller -- Feb. 19, 2006 5:03:02 AM >
In that case, read-modify-write is not the entire problem, but it is certainly half of the problem! ;)
Read-modify-write may be part of the problem, but the real problem here is that the OP didn't read the datasheet
Mike, the behavior the OP described on his board sounded to me like a r-m-w problem. If he had described different symptons, I would have suggested a different fix. Either way, my suggestion to look up the info in the datasheet would cure both problems.
ORIGINAL: danielle Either way, my suggestion to look up the info in the datasheet would cure both problems.
The tired, old "read the datashet" really isn't very helpful. Yes, both problems are referenced in the datasheet. But the RMW problem, while OP might well find it, isn't all that well described. Even if you notice it, the datasheet doesn't really help understand it.
The initialization of analog pins is even worse. Yes, you might happen to trip across the little detail that pins whiich might be analog, are analog on power up, but you are unlikely, especially if your interest is digital I/O, to figure out how to turn off the A/D's, and a lot less likely to realize that you have to. To figure that out you need to memorize the datasheet then connect the dots. In theory possible, but pretty unlikely.
Mullor, two bits of knowledge experienced PIC types have. First, it is never a good idea to do a RMW operation (such as a bit set or clear) on a port. Some special purpose registers also have restrictions on RMW operations. In some cases it is possible, but most folks maintain an in-memory copy of what the port should look like, and do the bit operations on that. The second thing is that if a pin can accept an analog input, then it is set to analog on power up. The implication is that you need to turn off the analog capability before you can use the pin for digital I/O. "Turn off" means slightly different things for A/D's and comparator inputs. But it does mean that you need to study some of the peripherals that you don't intend to use.
--McD
< Message edited by jjmcd -- Feb. 24, 2006 6:10:05 AM >
Ok just so I have it correct. I was under the impression that it was generally OK to use bsf and bcf (RMW commands) on the ports if you put a NOP in between. This gives one instruction cycle for the port to change state before being read. Isn't this acceptable as long as there is not some large capicitance on the lines that keeps the port line from changing states over an instruction period?
RE: Newbie needs help with bsf and bcf - Feb. 24, 2006 7:46:58 AM
Guest
Good explanation jjmcd. However, I disagree that the datasheet does not describe the r-m-w problem. I also disagree that the analog setup is not well covered.
The datasheet is clear about the r-m-w on ports, even including code snippets with LEDs to show the effect. It also covers that r-m-w happens as well in other registers, such as TRISC on parts that have peripherals multiplexing PORTC pins with normal pin functions. It states clearly that r-m-w instructions should not be used in TRISC else the peripherals can flip unintended TRIS bits.
The analog section always cover which setup is effective on RESET. A good practice is simply disregard RESET or power-up conditions and explicitly initialize all chip structures.
Reading the datasheet completely is a requirement. 'Memorizing' the datasheet is simply an effect of the reading process. A PIC datasheet can be read in a matter of hours. Debugging 'obscure' and 'hard to catch' chip behaviour can take weeks (or years).
< Message edited by j_doin -- Feb. 24, 2006 7:48:26 AM >
RE: Newbie needs help with bsf and bcf - Feb. 24, 2006 7:56:27 AM
Guest
There is more to r-m-w than simply line capacitance. If you have a purely resistive ideal load, then a r-m-w will always return a logic value equivalent to the least current in the load. If you have it pulled up, a high level will be read, if pulled down, a low level. Since no load is purely resistive, there is some capacitance in the system formed by pin-resistor-LED that makes it react with a (very small) RC time delay. So, sometimes a r-m-w will not turn off a LED, but most of times it will. And since board design plays a role in this, you can have a prototype that runs fine, and a production board that fails. Or worse, the product fails in the field, under different moisture and temperature levels.
Bottom line: avoid r-m-w at any costs, unless you use it as a design feature (on-purpose) as a hardware optimization point (highly unlikely, but possible).
The tired, old "read the datashet" really isn't very helpful. Yes, both problems are referenced in the datasheet. But the RMW problem, while OP might well find it, isn't all that well described. Even if you notice it, the datasheet doesn't really help understand it.
In the case of this particular datasheet, you're right, it doesn't delve into the r-m-w problem. However, the mid-range reference manual has a very detailed explanation, so I should have suggested that the OP read that as well. But I don't think there's anything "tired" and "old" about reading the datasheet - it never goes out of style!
quote:
The initialization of analog pins is even worse. Yes, you might happen to trip across the little detail that pins whiich might be analog, are analog on power up, but you are unlikely, especially if your interest is digital I/O, to figure out how to turn off the A/D's, and a lot less likely to realize that you have to. To figure that out you need to memorize the datasheet then connect the dots. In theory possible, but pretty unlikely.
Here is where I really have to disagree! The section on PORTC says in no uncertain terms that the analog registers MUST be initialized to use the digital ports - even highlighting it in a gray "Note" box. As if that wasn't enough, it even gives an exact example for initializing PORTC as all digital - clearing ANSEL and all. You don't even have to look at the analog section of the datasheet (but everyone should!).
Posts: 2924
Joined: May 25, 2005
From: PA, now MN via NJ,AZ,OR,CA
Status: offline
j_doin, I remember a post that you had a few months ago with scope traces showing the time that it took for pins to change state.
I just looked, but cannot find it easily.
Do you remember it?
It might be a good idea to take some of the pictures and text from that post and turn it into an FAQ as this seems to come up all the time.
Perhaps a 10 commandments about microcontrollers (like the 10 commandments of C).
My recommendation for #1
Thou shalt not use bit operations on ports.
_____________________________
PM personal questions and observations only. Keep technical questions to the forums where everyone can benefit. When it comes to binary, there are 10 kinds of people: those who understand, and those who don't.
RE: Newbie needs help with bsf and bcf - Feb. 24, 2006 8:36:45 AM
Guest
Commandment #2:
Thou shalt never let a voltage to pass over the VDD and VSS towers. Thou shalt place sentinels called 'diodes' to guard the CPU citadel against those infidels, or the almighty Power Supply will inflict great pain to all your transistor souls, and unleash the four-layered dormant beast of Latchup to spread Chaos and Mayhem upon thy world.
RE: Newbie needs help with bsf and bcf - Feb. 24, 2006 8:51:09 AM
Guest
I remember that thread. The nice scope photos were not placed by me, but if I recall correctly bob_barr or Dale Schisholm placed them. Maybe I have downloaded it ... no. I didn't. toobad. Next time I see a nice picture, I'll grab it.
Posts: 5416
Joined: Nov. 7, 2003
From: Morgan Hill, CA
Status: offline
quote:
I remember that thread. The nice scope photos were not placed by me, but if I recall correctly bob_barr or Dale Schisholm placed them. Maybe I have downloaded it ... no. I didn't. toobad. Next time I see a nice picture, I'll grab it.
Guilty as charged. That was me.
I'll see if I can find a copy of it. I don't remember the thread title. (If nothing else, I can always take another shot of it.)
_____________________________
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.
RE: Newbie needs help with bsf and bcf - Feb. 24, 2006 9:09:44 AM
Guest
quote:
ORIGINAL: mford
[...]Conclusion:
One NOP gives enough of a delay even at 20Mhz for low capacitive load.
Inversely, a load is required to have at least 410pF to withstand a driving logic level (if operating at 8mA) before its charge is depleted to cross the VIL or VIH level. The stray capacitance is 50pF to 80pF at best (worst). The typical junction capacitance of a LED is 45pF to 55pF, so it is likely that the LED will see not enough storage capacitance, even with a single NOP. On the other hand, slow LEDs can have a fall time of ~500ns, so we are sitting entirely on the LED type here. Since this parameter will vary with temperature, it is simply not a good practice to use r-m-w on those situations.
RE: Newbie needs help with bsf and bcf - Feb. 24, 2006 9:20:42 AM
Guest
Commandment #3:
Thou shalt always meticulously specify the destination parameter of instructions, even when thou art convinced that it is unnecessary, lest the Default leprechaun will take vengeance upon thee when thou least expect it.
(I remember bob_barr using the word surprised in that post).
There seems to be an attachment, but for some reason, I can't open it.
_____________________________
PM personal questions and observations only. Keep technical questions to the forums where everyone can benefit. When it comes to binary, there are 10 kinds of people: those who understand, and those who don't.