asking for the fastest way to toggle one bit in a register

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

Hi,
I use Mega128L and I want to toggle COM1C0 in TCCR1A, and I wrote:

if ((TCCR1A & _BV(COM1C0)) == 0x00)
{
TCCR1A |= _BV(COM1C0);
}
else
{
TCCR1A &= ~(_BV(COM1C0));
}

but it just take too many clock cycles, so I want faster code.
anyone have good idea?

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

TCCR1A ^= COM1C0; //flip bit 4 in reg

Imagecraft compiler user

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

Use the exclusive OR function.

Try TCCR1A ^= _BV(COM1C0); I think ^ is the symbol for exclusive OR. Double check. This should flip the specified bit

Randy

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

I do not know what the _BV does try to make _BV(COM1C0) 0b0001 or something.
Or try looking at the assebmly code and try to make it faster in assembly.

But if you want to toggle this bit every time than try this
TCCR1A ^= _BV(COM1C0)

How many clock cycles does the code take any way?????

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

Thanks, guys

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

Doesn't anybody who hangs out at these forums do anything besides hang out at these forums?

Three virtually identical simultaneous answers!! LOL

Regards,
Steve A.

The Board helps those that help themselves.

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

I'm trying to rack up points for dropping a real tough stumper that no one can answer.

Imagecraft compiler user