Using ‘C’ with the STK500 AVR boards Check in the D drive that there is a gcc compiler present. This will be in a directory called either avr-gcc-3.2 or avrgcc. The actual program is located in d:\WinAVR\bin. If your computer has a different directory name then d:\WinAVR\bin will be changed to d:\avr-gcc-3.2\bin in the first line of the program below. In the second line the –mmcu=ATmega16 entry will need to be modified depending on the processor used to –mmcu=atmega163 for both the mega16 and mega163.
Create a directory where you want to save all your ‘C’ programs to.
To create a batch file to compile C programs
avr-gcc –mmcu=ATmega16 –o %1.out %1.c
avr-objcopy –O ihex %1.out %1.hex
Save this program as ccavr.bat in the directory you have just created.
To compile type
in the directory you have just created.
Up to now you have always typed in the addresses of the ports, special registers etc. However a much more elegant way is to use Header Files, these can be recognised by the ‘.h’ extension. e.g #include
PORTA = 0xaa;// outputs 10101010 to port A
c = PINC//saves the data on port C to the variable ‘c’.
GCC optimisation levels
-O0 Does not perform any optimisation. Compiles the source code in the most straightforward way possible. Each line of source code is converted directly to corresponding instructions without rearrangement. Best option when debugging.
-O1 Turns on the most common forms of optimisation that do not require any speed-space tradeoffs. Resulting executables should be smaller and faster than with -O0.
-O2 Turns on further optimisations such as instruction scheduling. Optimisations that do not require any speed-space tradeoffs are used, so executable should not increase in size. Provides maximum optimisation without increasing the executable size. Default optimisation level for GNU packages.
-O3 Turns on more expensive optimisations, such as function inlining, in addition to all the optimisations of the lower levels. The -O3 optimisation level may increase the speed of the resulting executable, but can also increase its size.
-Os Selects optimisations which reduce the size of an executable. Tries to produce the smallest possible executable, for memory constrained systems.
The benefit of optimisation must be weighed against the cost. Cost of optimisation includes more difficult debugging. Hint: use -O0 for debugging, and -O2 for final product.
Below is a simple program which just sets up the ports and outputs 10101010 to port C.
DDRA = 0x00;
DDRC = 0xff;
PORTC = 0xaa;
Notice how the header files are declared, #include .
Connect the STK500 boards to the computer, then open the text editor Notepad and type in the above program saving it as filename.c in the directory you have just created. When completed open a DOS window, change directories to the one you have created for your ‘C’ programs and the CCAVR batch file and type CCAVR filename. NOTE do not add the ‘.c’ extension. Start AVRStudio and open the download window, select the relevant hex file and download it. With any luck it will light every other LED.
Using the following delay routine write a program that waits for a push button to be pressed and then counts up in binary.