Embedded Programming.

Program your board to do something, with as many different programming languages and programming environments as possible.


Jumping in at the deep end of the pool.

AVR-Eclipse plug-in provides an integrated interface for editing code, compiling and uploading to the device. And, not to be undervalued, also has a GUI to set fuses and lock bits. It generates Makefiles, so if and when you are ready to graduate from an IDE back to bare bones then you will have something familiar to work from.

This sound great but to make it work you also have to install (avr-libc, binutils-avr, gcc-avr) libraries. I found it easies to do it through the terminal with:

sudo apt-get install gcc-avr binutils-avr gdb-avr avr-libc avrdude

I also installed Package Manager for Ubuntu to able install upgrade and remove software packages (softwares called package in Ubuntu). Finally I feel I have control over what I have installed and an easy way to modify it.

I followed several tutorials on how to set up eclipse for AVR but I could not adjust my configuration for my programmer AVRDude. All I got was a warning saying AVRDude is not supported for static library projects. I spent embarrassedly many hours trying to figure it out. It was only when I went back and created my third project i saw my mistake. Turned out that I had chosen the wrong project setting, and this is something you can not change after project is created.

Choose cross target application not cross target static library!

But the celebration did not last long before I bumped in to next issue, my knowledge of C is next to zero so I had no clue where to go from here... do I create a class or a source file? ...and whats up with that .h? I decided to get out of this rabbit hole as I had spent way to much time on it.

Arduino IDE

I followed highlowtech.org tutorial on Programing aTTiny with arduino IDE. Compared to Eclipse, Arduino IDE seems like a toy. It strength but also it's weakness is it's simplicity. I have to run the Arduino IDE 1.6.0 with Sudo command to give it access to the USB ports. I can connect and upload sketches but something is of with my serial connection!

Seem to be sometining wrong with my board. Serial output acting strange.

Debugging the serial problem

In the code the serial was set to Serial.begin(9600) but in the serial monitor all bauds except 1200 gave me gibberish. This means that the clock speed was off. Turned out that when I made the fuse in the Make file in week 6 I had the wrong setting. "U lfuse:w:0x5E:m" which is 94 in decimal, and i have a crystal at 20mht so my clock speed was wrong and therefor giving me this strange result.

I Burned a bootloder through Arduino IDE with my correct settings and now I got the correct speed and serial monitor is working on the right baud.

Next up where pin confusion, Working with the Arduino IDE you can not follow the data sheet for pin reference! Below is a image of of Arduino pin layout.

Pin order in Arduino land.

Having figured out the pins I could get cracking at the code. I programed the button with state shift. It works like a charm.

#include ]
    	SoftwareSerial Serial(0, 1); // RX, TX
    	const int ledPin = 8;
    	const int buttonPin = 7;
    	boolean lastButton = LOW;
    	boolean currentButton = LOW;
    	boolean ledOn = false;
    	void setup() {
    	  pinMode(ledPin, OUTPUT);
    	  pinMode(buttonPin, INPUT);
    	boolean debounce(boolean last){
    	  boolean current = digitalRead(buttonPin);
    	  if(last != current){
    	    current = digitalRead(buttonPin);
    	  return current;
    	void loop() {
    	  currentButton = debounce(lastButton);
    	  if(lastButton == HIGH && currentButton == LOW){
    	    ledOn = !ledOn;
    	  lastButton = currentButton;
    	  digitalWrite(ledPin, ledOn);

State change.Coded with Arduino IDE, programed with Fab ISP.

C code in Sublime Text 3 & Compile with GCC through Terminal.

The minimalist dream.

Writing C code in a text editor seems temptingly minimalistic, but at the same time a bit hard core. Sublime text 3 have a package Sublime C Improved that formats the text to C.

There is no compiler compiler within Sublime (although I think you can write some script for making it automatic) but as I had downloaded AVR-GCC (compiler) and AVRDDUDE (upload to aTTiny within44) I did it through the terminal with the following commands (big thanks to Jani Ylioja)

Compiling was done with GCC and converter it to an .elf file. "Wall"= gives warning i something wrong "-o" rename the exported file and target .c file.

  • avr-gcc -mmcu=attiny44a -Wall -Os -o button.elf button.c
  • Creat a hex file:

  • avr-objcopy -j .text -j .data -O ihex button.elf button.hex
  • Up load it to the attiny 44 with the fab USBtiny:

  • sudo avrdude -p t44 -P usb -c usbtiny -U flash:w:button.hex
  • Or better solution: with a Makefile!

    F_CPU = 20000000
    CFLAGS=-mmcu=$(MMCU) -Wall -Os -DF_CPU=$(F_CPU)
    $(PROJECT).hex: $(PROJECT).out
    	avr-objcopy -O ihex $(PROJECT).out $(PROJECT).c.hex;\
    	avr-size --mcu=$(MMCU) --format=avr $(PROJECT).out
    $(PROJECT).out: $(SOURCES)
    	avr-gcc $(CFLAGS) -I./ -o $(PROJECT).out $(SOURCES)
    program-avrisp2: $(PROJECT).hex
    	avrdude -p t44 -P usb -c avrisp2 -U flash:w:$(PROJECT).c.hex
    program-avrisp2-fuses: $(PROJECT).hex
    	avrdude -p t44 -P usb -c avrisp2 -U lfuse:w:0x5E:m
    program-usbtiny: $(PROJECT).hex
    	avrdude -p t44 -P usb -c usbtiny -U flash:w:$(PROJECT).c.hex
    program-usbtiny-fuses: $(PROJECT).hex
    	avrdude -p t44 -P usb -c usbtiny -U lfuse:w:0x5E:m
    NOTE: the hex code 0x5E in the Makefile above is not working for 20MHz!

    How to:

    Compile and upload:

  • In the first line, change the "FileName" to the your filename (leave out the extension)
  • Make sure you got the right possessor
  • Adjust to you clock speed under F_CPU
  • There are two programmers included in this Makefile. The avrisp2 and usbtiny
  • Save your modified Makefile in the same directory as your .c file. Name it Makefile, with capital M, and no file extension.
  • In the terminal go to your folder where you saved the Makefile and .c file. Run: sudo make program-usbtiny (if you are programming with the usbtiny) This will compile the code and upload the code to the micro controller.
  • If this is the first time you program you micro controller you have to make a fuse before you can start uploading code to it. This will among other things configure your micro controller with the right clock speed

    As before open a terminal window in your folder where you saved the Makefile and .c file. Run: sudo program-usbtiny-fuses. You will only have to do this once unless you change your configuration on your board.

    C Syntax PORTS and PINS

    DDRB |= BIT(2); // set as output. set bit in register. LED

    DDRA &= ~BIT(7); // set as input. clear bit in register. BUTTON

    PORTA |= BIT(0) //Turn on 0th bit

    PORTA &= ~BIT(0) //Turn off 0th bit

    PORTA ^= BIT(0) //Toggle on 0th bit

    if (PORTA & BIT(0)) //test to see if bit is set

    C code for led button.

    Code embedded with GitHub Gist.

    In week 10 I came back to interrrupts and made this C code for my hello board.

    Interrupt Button-LED.

    Sublime Text 3 & Assembly

    Following tutorial of Scott Zitek: link I downloaded a AVR assemmbler Gerd's AVR Assembler and installing it.

    Sublime Test 3 also have a package for assembly code. With the help of Francisco I wrote the assembly code and saved it as an .asm file.

    Next I compile the .asm file with gavrsm compiler. In the folder where I saved the blink.asm file. I run gavrsm (through terminal) with the command gavrsm blink.asm. (gavrasm 'filename.asm') This creates 2 files in the same folder, a .hex and a .list file

    After that I created (modify) a make file. MakeFile is a utility for automatically building executable programs and libraries from source-code. Project to the file name of my assembly file Open a terminal window in the same folder as the .hex and.list file. I run the command "make fuse" (fuse to initialize the board run once and "make program" (commands that are defined in the make file and uses avrdude) this copies the assembly code over to the ATtiny44

    Code Blocks

    Code Blocks is a free C, C++ and Fortran IDE It is designed to be very extensible and fully configurable, built around a plug-in framework. Any kind of functionality can be added by installing/coding a plug-in. For instance, compiling and debugging functionality is already provided by plug-ins!

    This worked so much better the Eclipse. Code Blocks also have a project setting for AVR I could get a project up and running with ease.

    How ever on compiling my C code I got a compiler error about #include not found. The solution to this is to install the package libc6-dev-i386. Easily done with package control I installed earlier.

    Code Blocks got a custom project setting for coding AVR.

    But that is how far I got. I was not able to upload or debug any code on my ATtiny44. This is the tutorial I followed but still could not make it work correctly. So at the moment it is just a fancy text editor and nothing more.

    Summary what I learned:

  • I never got Eclipse to work and it was overwhelming trying to figure out the software and at the same time trying to understand C.
  • Arduino is the easiest to begin with and I have come to appreciate it's user friendliness after working with the other compilers and IDE. But there is a lot of over head e.g. a digitalWrite takes 40 clock cycles in Arduino while only one in C and Assembly. Someone expressed it "like knitting with boxing gloves."
  • The Minimalistic approach to programming in Sublime really speaks to me and when I learned how to how to make the Makefile and compile through the terminal it was pretty straight forward.
  • I was pleasantly surprised with Code Blocks but frustrating that I could not get it to work as a whole system.
  • For now will go back to Sublime text 3 and Makefile. Workflow is pretty efficient but I'm missing some debugging tools.
  • Fuse Calculator Webbcast

    Programming AVR Micro controllers in C Webbcast


    Download Assets:

  • Makefile
  • Code files
  • Google+

    Follow me on Google +.

    Click me


    Twitter madness.

    Click me


    Connect with me on LinkedIn.

    Click me