Changing several o/p pins at the same time


Closed Thread
Results 1 to 11 of 11
  1. #1
    Join Date
    Feb 2006
    Location
    Brussels, Belgium
    Posts
    104

    Default Changing several o/p pins at the same time

    One project I'm working on is a controller for a linear stepper driver. The driver has 4 configuration bits setting microstepping mode, power and direction. So if I set these as adjacent bits in a register (eg B.0 thru B.3) can I write them (and only them) in one go ? I know I can write the whole port in one go but how about a subset of the port ?

    thanks, Andrew

  2. #2
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by AndrewC View Post
    One project I'm working on is a controller for a linear stepper driver. The driver has 4 configuration bits setting microstepping mode, power and direction. So if I set these as adjacent bits in a register (eg B.0 thru B.3) can I write them (and only them) in one go ? I know I can write the whole port in one go but how about a subset of the port ?

    thanks, Andrew
    Read the port into a temp variable, change the bits, then write it back. Even if the port is an output, you'll still be able to read the last thing you output (assuming that whatever is connected to the pin isn't overdriving that pin and pulling it the wrong way).

  3. #3
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Read-Modify-Write, just like the chip does it when setting a single bit.

    Code:
    PORTB = (PORTB & %11110000) | MyNibble
    This reads the Port, saves the bits you don't want to change, then adds the new bits and writes it back to the Port.

    If there's any possibilty of bits above 3 being set in the MyNibble variable, you should use this instead ...
    Code:
    PORTB = (PORTB & %11110000) | (MyNibble & %00001111)
    <br>
    Last edited by Darrel Taylor; - 1st August 2008 at 20:30. Reason: Rat's, ya beat me ski. :(
    DT

  4. #4
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    That's amazing.
    I was going to put almost exactly what you put, then I got to thinking about R-M-W issues.
    Won't the code that you put down have that problem too?

    Well...maybe not. PBP won't necessarily do it all in one instruction...thereby avoiding the issue...

  5. #5
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    R-M-W's themselves only have a problem when a pin in Analog mode is set as an output.

    The real R-M-W problem comes when you do more than 1 R-M-W too fast, and there's capacitance on the Pin.

    For instance, if you were doing the previous example but had 2 nibbles being put to the same port, 1 to the lownib and 2 to the highnib.
    Code:
    PORTB = (PORTB & %11110000) | (Nibble1 & %00001111)
    PORTB = (PORTB & %00001111) | (Nibble2 & %11110000)
    The last thing the first line does is write the result to the port. And the first thing the next line does, is read the port.

    If there's any capacitance on those pin's, it's Very likely to cause the infamous R-M-W hiccup.
    <br>
    DT

  6. #6
    Join Date
    Feb 2006
    Location
    Brussels, Belgium
    Posts
    104


    Did you find this post helpful? Yes | No

    Default

    OK, I get that (though it took a minute or two to work out that | is a bitwise OR). So in PBP how do I set a nibble ? There isn't a variable type for nibbles - just bits, byte and words unless i've missed something. But I suppose it doesn't really matter if I use a whole byte, if the high nibble is all zeros the OR will just keep whatever it found in the Read PORTB and AND operation.

    So is it "better" programming to do it this way rather than just four consecutive single bit operations ?

  7. #7
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    PBP doesn't have a nibble variable type. But like you say, just use a byte with only the low nibble doing things.

    Doing 4 bit writes to a single port one after the other, is actually doing 4 full port R-M-W's

    If there's no capacitance, it'll be fine.
    But if there is. You may have problems.

    The method above is only 1 R-M-W at a time.
    <br>
    DT

  8. #8
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    No 'nybble' variable types in PBP. You have to break a byte up into smaller pieces then reassemble it.

    As far as 'better' programming...I think the operative phrase here might be that it's 'more reliable' programming.

    (DT - wow! I'm leaving now!)

  9. #9
    Join Date
    Feb 2006
    Location
    Brussels, Belgium
    Posts
    104


    Did you find this post helpful? Yes | No

    Default

    Is there really just one of you out there and you are two faces of a single evil genius

  10. #10
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Talking

    Quote Originally Posted by AndrewC View Post
    Is there really just one of you out there and you are two faces of a single evil genius
    No, I'm the evil, he's the genius....
    Or is he.........

  11. #11
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    I had "Who's Online" in one window, and my reply in another.

    Boy was I scrambling to beat you.
    <br>
    DT

Similar Threads

  1. Change On Interrupt, PIC16F884
    By elec_mech in forum mel PIC BASIC Pro
    Replies: 17
    Last Post: - 14th November 2008, 17:25
  2. I don't understand this code!
    By Russ Kincaid in forum mel PIC BASIC Pro
    Replies: 46
    Last Post: - 13th February 2008, 02:55
  3. Serout2/serin2 Pbp Problem
    By SOMRU in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 11th December 2006, 19:55
  4. Control unlimited servos at the same time
    By mrx23 in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 19th September 2006, 14:14
  5. Timer in real time
    By martarse in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 29th July 2005, 14:24

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts