Microchip Technology
Welcome to www.microchip.com
Search: Click here to Search Microchip.com
Forums Home Register LoginLog Out Inbox Address Book My Subscription Member List Search My Profile FAQ
Newbie needs help with bsf and bcf

Newbie needs help with bsf and bcf

 
View related threads: (in this forum | in all forums)

Logged in as: Guest
Users viewing this topic: none
  Printable Version
All Forums >> [Microcontroller Discussion Group] >> Getting started with pic's, architecture and overview section >> Newbie needs help with bsf and bcf Page: [1] 2   next >   >>
Login
Message << Older Topic   Newer Topic >>
Newbie needs help with bsf and bcf - Feb. 17, 2006 1:35:56 PM   
Mullor

 

Posts: 4
Joined: Feb. 12, 2006
From: 0
Status: offline
Hi, I am trying to switch on my pickit2's leds with bsf and bcf, I wrote next code:

    #include <p16F690.inc>
     __config (_INTRC_OSC_NOCLKOUT & _WDT_OFF & _PWRTE_OFF & _MCLRE_OFF & _CP_OFF & _BOD_OFF & _IESO_OFF & _FCMEN_OFF)

    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.

ty
Post #: 1
RE: Newbie needs help with bsf and bcf - Feb. 17, 2006 1:54:29 PM   
danielle

 

Posts: 268
Joined: Dec. 8, 2005
Status: offline
This is a "read-modify-write" problem, please look it up in the datasheet and search this forum for more information.

(in reply to Mullor)
Post #: 2
RE: Newbie needs help with bsf and bcf - Feb. 17, 2006 2:45:32 PM   
Mullor

 

Posts: 4
Joined: Feb. 12, 2006
From: 0
Status: offline
Thank you, I am reading about it.

(in reply to danielle)
Post #: 3
RE: Newbie needs help with bsf and bcf - Feb. 18, 2006 7:45:34 PM   
K8LH
Experienced Hobbyist

 

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?

Regards, Mike

(in reply to danielle)
Post #: 4
RE: Newbie needs help with bsf and bcf - Feb. 19, 2006 3:43:58 AM   
ric
5+ years with MCHP products


Posts: 12848
Joined: Nov. 7, 2003
From: Australia, Melbourne
Status: offline
In that case, read-modify-write is not the entire problem, but it is certainly half of the problem! ;)

_____________________________

---—≡≡≡
(All users, please read the "forum guidelines" before posting a question.)

(in reply to K8LH)
Post #: 5
RE: Newbie needs help with bsf and bcf - Feb. 19, 2006 4:42:15 AM   
leon_heller
5+ years with MCHP products


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 >


_____________________________

Leon Heller
G1HSM
leon355@btinternet.com

(in reply to K8LH)
Post #: 6
RE: Newbie needs help with bsf and bcf - Feb. 20, 2006 6:41:30 AM   
danielle

 

Posts: 268
Joined: Dec. 8, 2005
Status: offline
quote:

ORIGINAL: ric

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.

(in reply to ric)
Post #: 7
RE: Newbie needs help with bsf and bcf - Feb. 24, 2006 6:08:25 AM   
jjmcd

 

Posts: 846
Joined: Apr. 1, 2005
From: Michigan, U.S.A.
Status: offline
quote:

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 >

(in reply to danielle)
Post #: 8
RE: Newbie needs help with bsf and bcf - Feb. 24, 2006 7:38:23 AM   
mford

 

Posts: 98
Joined: Feb. 11, 2005
Status: offline
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?

(in reply to jjmcd)
Post #: 9
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 >

(in reply to jjmcd)
  Post #: 10
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).


(in reply to mford)
  Post #: 11
RE: Newbie needs help with bsf and bcf - Feb. 24, 2006 8:07:10 AM   
danielle

 

Posts: 268
Joined: Dec. 8, 2005
Status: offline
quote:


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!).

(in reply to jjmcd)
Post #: 12
RE: Newbie needs help with bsf and bcf - Feb. 24, 2006 8:23:24 AM   
jspaarg

 

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.

(in reply to danielle)
Post #: 13
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.

(in reply to jspaarg)
  Post #: 14
RE: Newbie needs help with bsf and bcf - Feb. 24, 2006 8:40:39 AM   
mford

 

Posts: 98
Joined: Feb. 11, 2005
Status: offline
Ok since I have used BSF and BCF on ports in the past with NOPs in between I figured I should look at the numbers.

Given:
PIC 18F2331

Vol = 0.6V @ 8.5mA
Vil = 0.8 V max

Voh = 4.3v @ 3mA
Vih= 2v

A 20 Mhz gives Tinstruction = 200nSec.




Therefore:

For a high to low transition the output must swings from 5v to 0.8v in 200nSec with a drive current of 8.5mA.

Using I = C dv/dt 


C =  8.5mA / ((5v-0.8v)/200nSec) = 405 pF.  







For a lowto high transition the output must swings from 0v to 2v in 200nSec with a drive current of 3mA.

C =  3mA / (2v/200nSec) = 300 pF.




Conclusion:

One NOP gives enough of a delay even at 20Mhz for low capacitive load. 




(in reply to danielle)
Post #: 15
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.

(in reply to jspaarg)
  Post #: 16
RE: Newbie needs help with bsf and bcf - Feb. 24, 2006 9:05:05 AM   
bob_barr
5+ years with MCHP products

 

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.

(in reply to Guest)
Post #: 17
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.


(in reply to mford)
  Post #: 18
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.

(in reply to jspaarg)
  Post #: 19
RE: Newbie needs help with bsf and bcf - Feb. 24, 2006 9:22:19 AM   
jspaarg

 

Posts: 2924
Joined: May 25, 2005
From: PA, now MN via NJ,AZ,OR,CA
Status: offline
I believe that I found the thread, but the pictures seem to be MIA

http://forum.microchip.com/tm.aspx?m=118409&mpage=1&key=surp𜻉

(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.

(in reply to Guest)
Post #: 20
Page:   [1] 2   next >   >>
All Forums >> [Microcontroller Discussion Group] >> Getting started with pic's, architecture and overview section >> Newbie needs help with bsf and bcf Page: [1] 2   next >   >>
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts


  Site Index  |  Legal Information  |  microchipDIRECT  |  Samples  |  Technical Support  |  Investor Information  |  Careers at Microchip  |  Contact Us  |  RSS Feeds ©2009 Microchip Technology Inc.  
  Shanghai ICP Recordal No.09049794  
Forum Software © ASPPlayground.NET Advanced Edition 2.5.5 Unicode

0.250