Question on setting, clearing and toggling bits in GCC

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

I'm just starting to write in C and am trying to port some code from PIC to AVR. The PIC code uses a lot of bit manipulations, which I am trying to port to AVR. Below are a couple examples:

PIC Code:
output_low(PIN0); //Clears PIN0
What is the equivalent code to clear PINB0 on the AVR?

clear_interrupt(INT_RB); //Clears interrupt flag
What is the equivalent code to clear INTF1 in GIFR? But, this shouldn't be needed if you use ISR(INT1_vect, ISR_BLOCK) correct?

output_toggle(PIN0); //Toggles PIN0
What is the fastest equivalent code to toggle PINB0? Would you have to do a Read-Modify-Write?

Any suggestions would be appreciated. Thanks.

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

Read the "Programming 101 (aka bit manipulation)" thread in the tutorials section. It will reveal all.

Regards,
Steve A.

The Board helps those that help themselves.

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

Look in your existing PIC source code for the macros or possibly function declarations.

I suspect that they will be macros. So just read, ponder and inwardly digest.

They will probably just be ugly C code for clearing a bit, calling an #asm("OPCODE"), and toggling a bit by XOR with 1.

You either look up the equivalent macros in avr-gcc, and just #define output_low bit_clear or whatever.

Or you just use the same macro definition that you found in the PIC code.

Unfortunately manipulating bits always looks a little untidy in C.

So most people cover it up with macros. You will soon get the hang of it.

David.

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

Thanks for the tips Steve and David. I'll dig throught the tutorial real quick and if that is unfruitful, I'll probably just write my own snippet to get the job done.

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

FYI... I used the CLEARBIT and SETBIT macros from the bit manipulations tutorial you pointed me to, and they worked great. I have the code completely ported and compiling with no errors or warnings. Thanks!

Clint