AVR-GCC code compatibility

AVR-GCC compiler is changing during the time. There are so many things changed from recent versions of WinAVR to now. C language itself doesn't change much, but functions that deal with hardware all the time. Earlier even set up interrupt service routines would take some brain, now it is simplified to just one simple function ISR(). All necessary job is handled in macro level.

Another very important issue is operations with I/O bits. Functions with bits have changed since old versions of WinAVR. Compiler just throws errors that functions cannot be found. One solution is to write your own macros to support them or change the code with new syntax. Or use special Patch program which returns old functions to new WinAVR. You can download this patch from myrobot.ru. This patch returns functions that are not supported since WinAVR20050214. This is handy when you are trying to compile older source versions and do not want to change the code and do not want to download older WinAVR tools.

Patch returns inp, outp, sbi and cbi functions. According to these functions also there are no more bit_is_set and bit_is_clear logical tests. Dont wary, patch only returns these functions and leaves other as it is.

Table shows how these functions changed from old to new version:





DDRB = 0xff;

All port B pins as output


PORTB = 0xff;

All port pins set to “1”

sbi(DDRB, DDB2);

DDRB |= 1<<DDB2;

Set port D pin 2 as output

cbi(DDRB, DDB2);

DDRB &= ~(1<<DDB2);

Set port D pin 2 as input


PORTB |= _BV(PB2); or

PORTB |= 1<<2; or

PORTB |= 1<<PINB2;

Set port B pin 2 to “1”


PORTB &= ~_BV(PB2); or

PORTB &= ~(1<<2); or

PORTB &= ~1<<PINB2;

Set port B pin 2 to “0”

if (bit_is_set(PIND,3))

if (PIND & (1<<PIND3))

Check if port D pin 3 equal to “1”

if (bit_is_clear(PIND,3))

if (!(PIND & (1<<PIND3)))

Check if port D pin 3 equal to “0”

As you see in table, there is always several ways to write same sentence. Version with PORTB |= 1<<PINB2; have been always supported and working as shift syntax is C standard bitwise operation, but it is no the most optimal solution as _BV() is more optimal as compiler itself shifts bits before writing result to compiled code. So this saves performance as microcontroller do not have to perform shift operation during execution - it just takes results that have been prepared by compiler during preprocessing.

As we can see WinAVR toolset is changing all the time. Some people like this some not. But for me WinAVR AVR-GCC toolset gives flexibility with open possibilities to write, modify, adapt microcontroller code.

Source: http://myrobot.ru