Codeblocks is a stand-alone IDE. It is not dedicated to the Arduino, Atmel or microcontrollers. We will have to prepare the computer by installing the avr-gcc toolchain.
Install AVR-GCC toolchain
Ubuntu operating systems do not normally come with the avr-gcc toolchain installed. It does come with the gcc toolchain so that you can write software for the linux operating system.
Open a terminal and enter the command
$ sudo apt-get install binutils gcc-avr avr-libc uisp avrdude flex byacc bison
This will install the gcc-avr toolchain, avr-libc is the library of C functions for AVR, avrdude is the uploader program and binutils adds features such as make to your computer. If a package is already on your computer then it will be skipped.
Install Codeblocks application
Open the Synaptics package manager and select all of the codeblocks packages.
You need codeblocks-contrib to better handle programming of microcontroller devices. This will require space on your computer hard drive.
Start Codeblocks Program
The first time Codeblocks runs on your computer it will scan and find the compiler toolchains available for programming. The default is for the regular GNU GCC Compiler to be selected. This is for writing programs on your host Linux computer.
Because we have already installed AVR-GCC it is found and listed. Set as the default compiler and click OK. Next you see Codeblocks IDE with no open files or projects.
Start a new Project
Start a new Project using the File->New->Project. Let's look at the many options in the New from template dialog. We are going to select AVR Project. We could program for ARM, or Arduino if they are set up. We could write programs for Linux, MINGW compiler toolchain allows us to program windows applications.
Yes. You do want to tick the box that says Skip this page next time.
Select a name for the project and a location for the files. I have selected a folder cb and a project name of uno.
Make sure Codeblocks is going to use the GNU GCC Compiler for AVR (avr-gcc). If another compiler is selected then change to AVR. Other defaults are okay.
On the next screen select the atmega328p MCU microcontroller unit that is in the Arduino Uno board. Look through the options, the defaults are okay but notice that F_CPU is defined here as 16000000UL.
Click Finish and you will see Codeblocks has a project uno. Look in Sources and you will see two files main.c and fuse.c. Double click on main.c to see the default code. This is a C program for Arduino that performs no function. You can compile to check the IDE setup. Go to the Build menu and try the Build command.
If the codeblocks-contrib package is missing then fuse.c will not be generated and you may not find all the processors. You can open fuses.c and see that it instructs the compiler to program the default fuse values for the Atmega328p MCU.
Select all the text in file main.c and replace with this C language program. It will program an Arduino Uno board to blink its built-in LED about once a second. Notice the line #define F_CPU 16000000 is commented. If we do not comment this line it will conflict with the F_CPU value from Codeblocks and generate a warning.
// PORTB is an 8bit number at address 0x24
// #define F_CPU 16000000
#include <avr/io.h>
#include <util/delay.h>
int main(void){ DDRB = 0xFF; // PORTB is output
while(1){PORTB++; // increment PORTB
_delay_ms(100); }}
Go to the Build pull down menu. There are quite a few commands. Let's try Build or use the keyboard ctrl-F9.
Build failed, that doesn't sound very good
I get a build failed warning. The message is a warning that because Compiler optimizations are disabled; functions from <util/delay.h> won't work as designed.
But looking at the Build Log it does not look that serious. It shows us how a Makefile was executed, line by line in order to build object modules and link them together into an executable output. The error does not sound that bad, it's even shown in blue, not red.
If we look in the project folder at cb\uno\bin\Debug we see all of the files that we expected to generate.
Open uno.hex with a text editor and it also looks like what we expect. This is the intel hex format and was developed to store programs on punch-cards, paper-tape and cassettes. There are hex editor programs that convert binary into AVR machine code.
Maybe check the compiler
Once again, Codeblocks is not dedicated to AVR programming and it normally will configure to use the GCC toolchain so that you can write software for your PC.
Go to the Settings tab and select Compiler Settings->Compiler. It is correctly set to use the GNU GCC Compiler for AVR avr-gcc. We can use this panel to change the toolchain and use XC8, for example.
How about we upload to a board?
We have a compiled hex binary file that looks okay. Our failed build has warning messages but no show-stoppers, messages describing a complete failure. We could run the hex file on a simulator like Simavr application. Why don't we just upload to an Arduino board and see what happens?
Go to the Tools menu and Configure a new tool named uno. Executable is the avrdude program that uploads hex files to the uno board. We installed avrdude in our first steps of the project. The parameters tell the program where to find the hex file and what name it will have.
-p m328p -c arduino -P /dev/ttyACM0 -U flash:w:$(PROJECT_DIR)bin/Debug/$(PROJECT_NAME).hex:i
Go back to Tools and select uno. A black terminal screen should open showing progress of the avrdude flash programmer. The uno board should show Tx and Rx leds blink as the program is upload. Uno board should now blink its built-in led about once a second.
Mine works but the clock speed may not be accurate. When a compiler optimizes your code it takes out what you have programmed and substitutes code that it thinks is better.
Example of optimization: you may program a delay by using the NOP no operation instruction, your compiler may decide that doing nothing is something that can be removed from your program. Without the NOP to slow the program down the LED may appear on steady.
Comments