difficult bit-twiddling problem

Go To Last Post
12 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi all,

I'm having trouble with something that I feel must be easy, but it's confounding me.

I want to clear certain bits of a port without touching others, and without setting a PORT pin to a 0 as an intermediate step if it's supposed to be a 1 at the end of the assignment. The problem is the middle of the port cannot be touched (input pins, TXD, etc). Example:

DDRC = 0b11000011;
// PORTC might be      0b11xxxx11
// new value should be 0x10xxxx01
 

thereby setting bit 6 and 1 to zero while leaving 7 and 0 intact and not writing to the input bits at all.

I can't figure out a way to do it without clearing pin values and then resetting with

PORTC |= (1<<n);

and a mask. Is there a better way than a mask and four |= assignments? One other thing, I only have to clear bits, not set them.

Can anyone set me on the right path?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Why does

PORTC = PORTC & ~CLEAR_THESE_BITS;

not work? All untouchable bits are written out as they were. What am I missing?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

AND the 1's compliment.

PORTB &= ~(0x01 << 7);
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
PORTC |= (1<<7 | 1<<6 | 1<<1 | 1<<0);

or you can read a port into a variable, clear/set bits then write it out.

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

So the one's complement idea seems to make sense, and working it out on paper, I can see how it would work. I was reluctant to use that because I thought that would still write to the forbidden "x" bits. Is it okay to write to such bits, so long as I preserve their values? If so, then it's all clear to me...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Because of the x AND 1 operation, x will always hold its value.

You shouldn't have any problems re-writing 1's to your "untouchables." Check the datasheet to be sure, though. Some bits do crazy things when they are written to.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks to all -- I'll give this a try once I confirm it further on paper. :)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Better yet, experiment with 8 LEDs

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
Is it okay to write to such bits, so long as I preserve their values?
Yes. When the pin is set as input, he PORT bits control whether or not the pull-up resistor is enabled.

Regards,
Steve A.

The Board helps those that help themselves.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

When finalized, we'll name it the Helmick Maneuver.

Lee

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Helmick Maneuver indeed ;) Would be riotously funny if it was the first time I'd heard that. ;)

Seriously, thanks to everyone for the help. I almost have it working now.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

theusch wrote:
When finalized, we'll name it the Helmick Maneuver.

Lee


That's one way to twiddle your bits.

Four legs good, two legs bad, three legs stable.