PDA

View Full Version : Changing several o/p pins at the same time



AndrewC
- 1st August 2008, 19:53
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

skimask
- 1st August 2008, 20:24
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).

Darrel Taylor
- 1st August 2008, 20:28
Read-Modify-Write, just like the chip does it when setting a single bit.


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 ...
PORTB = (PORTB & %11110000) | (MyNibble & %00001111)
<br>

skimask
- 1st August 2008, 20:32
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...

Darrel Taylor
- 1st August 2008, 20:54
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.


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>

AndrewC
- 1st August 2008, 20:58
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 ?

Darrel Taylor
- 1st August 2008, 21:05
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>

skimask
- 1st August 2008, 21:06
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! :D I'm leaving now!)

AndrewC
- 1st August 2008, 21:09
Is there really just one of you out there and you are two faces of a single evil genius :)

skimask
- 1st August 2008, 21:12
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.........

Darrel Taylor
- 1st August 2008, 21:14
I had "Who's Online" in one window, and my reply in another.

Boy was I scrambling to beat you. :D
<br>