AVR-GCC : CODE COMPLIANCE

Posted on

AVR-GCC

On the Internet there are many examples of code for AVR-GCC , written using functions and macros that are no longer supported in newer versions of the compiler. To use these examples and understand them with the essence of the ports of the microcontroller, you need to rewrite code or use special macros.

For starters, learn programming AVR microcontrollers, this approach can be difficult. As a result, many go to other compilers and never use a cult which has become for many a development environment WinAVR. Although there is a simple way to solve all the problems and return again rejected by the developers of the functions in AVR-GCC. You can use a specially crafted AVR-GCC myROBOT PATCH for WinAVR .

Before we proceed to the description of the patch, let’s see what kind of function it returns. The latest versions of libraries avr-libc, which is part of the compiler AVR-GCC, uses a WinAVR, more functions are not supported inp , outp , sbi and CBI . Made it to the best interpretation of the source code when it is compiled and determine how to handle the port – how to register or to memory. Support for these functions is not starting from WinAVR-20050214 version. This includes no support for such a popular and convenient for many of the former bit_is_set and bit_is_clear .

Of course, nobody calls using only the “old” functions return the patch is backward-compatible programs on the AVR -GCC. Using the patch, you will not lose the ability to work with the new notation, and only return the use of excluded functions. For example, checking the work of any one found on the Internet example, you can practice by rewriting it with the new syntax.
In conclusion, let us consider some examples of possible replacements.

“Old” notation possible replacement an action to perform
outp (0xff, DDRB); DDRB = 0xff; all port pins configured as outputs B
outp (0xff, PORTB); PORTB = 0xff; all bits of Port B set to "1"
sbi (DDRB, DDB2); DDRB | = 1 <<2; configure the line 2 port B as output
cbi (DDRB, DDB2); DDRB & = ~ (1 <<2); configure the line 2 port B as input
sbi (PORTB, PB2); PORTB | = _BV (PB2); 
or simply 
PORTB | = 1 << 2; 
possible so 
PORTB | = 1 << PINB2;
set to "1" on line 2 
port B
cbi (PORTB, PB2); PORTB & = ~ _BV (PB2); 
or simply 
PORTB & = ~ (1 <<2), 
possibly because 
PORTB & = ~ 1 << PINB2;
set to "0" on Line 2 
port B
if (bit_is_set (PIND, 3)) 

... 
}
if (PIND & (1 << PIND3)) 

... 
}
check "1" on line 3 of port D
if (bit_is_clear (PIND, 3)) 

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

... 
}
check the "0" on line 3 of port D

Please note that _BV () is preferable because in this case, the compiler performs a bitwise shift and inserts the result in the compiled code. This ensures that no time spent during direct execution of code in the microcontroller.
As you can see, the possibilities of syntax in the AVR-GCC wide enough to consider him one of the most convenient and flexible means of programming microcontrollers Atmel AVR.

Gravatar Image
Robotics Technology , Tutorial and News

Leave a Reply

Your email address will not be published. Required fields are marked *