UP | HOME

Week 8 Assignments

Table of Contents

Week 8 - Embedded programming

This week assignment involves programming the board we created two weeks ago using several languages and enviroment combinations. I will use both Wiring/Arduino IDE and C/emacs/avrdude toolchain to program some samples that make use of both the button and/or the LED.

Setting up the environment

I upgraded my environment for this practice downloading Arduino 1.0.4 and the definition files for ATTiny Series. I extracted those files under Skectchbook/hardware directory. After a reeboot, the processor were selectable from the Board Menu. In our case, it was necessary to select the right clock, 20Mhz external.

I plugged the USB to the FabISP to the USB port and the FTDI to the new board and then connected the ISP headers together using a flat cable. This is the result:

connections.jpg

My first verification was to open "Apple Icon ->About this Mac-> More info-> System report" to check that FabISP was correctly detected. Then I had to burn the bootloader from the IDE. To do this, I selected the Serial Port from the menu, and USBTinyISP as the programmer and then clicked on "Burn the bootloader".

When I tried to burn the bootloader I got this error, because cable was plugged in the wrong direction in Arduino IDE:

inversecable.jpg

I changed the numbers from to fit the ones in my board using this conversion table:

ATtiny 44 PinArduino Pin NumberUsageUsed?
1-VCC
210
39
4-RST
58PWM
67PWM, A7*
76PWM, A6, MOSI
85PWM, A5, MISO
94A4, SCK
103A3*
112A2
121A1
130A0, AREF
14-GND

Resulting lines changed are:

// constants won't change. They're used here to 
// set pin numbers:
const int buttonPin = 3;     // Pin 10 on the ATtiny
const int ledPin =  7;      // Pin 6 on the ATtiny

After I uploaded the code, I was surprised to discover that my LED was red instead of green! As I clicked the button firmly the LED went off. I could notice some rebound issues on the button if it was not pressed right in the center or in a very strong way. I checked the source code to change the button logic and changing this line:

if (buttonState == LOW) {     
    // turn LED on:    
    digitalWrite(ledPin, HIGH);  
  } 

instead of 

if (buttonState == HIGH) {     
    // turn LED on:    
    digitalWrite(ledPin, HIGH);  
  } 

Here is a sample usage of the board. (Note that the FTDI is plugged reverse. I had to switch it to make it work for the next part) ledon.jpg

Programming on emacs and c

Next step was to program the board using my lovely emacs to compile the source code directly. I'm quite used to the Arduino IDE but I've never programmed microcontrollers directly. Time to learn something new!

C-x 4 f to open the c file in a separate buffer. C-x 3 to split window in 3, C-x o and after another C-x f, we're ready for a nice emacs screenshot. Smile! :)

emacssetup.jpg

I clicked on Tools -> compile and a new message appeared on the minibuffer: "Compile command". I typed "make -f hello.ftdi.44.echo.make program-usbtiny"

I got an error on exit code 2:

-*- mode: compilation; default-directory: "~/Documents/FabAcademy/" -*-
Compilation started at Tue Mar 19 22:18:49

make -f hello.ftdi.44.echo.c.make program-usbtiny
avr-gcc -mmcu=attiny44 -Wall -Os -DF_CPU=20000000 -I./ -o hello.ftdi.44.echo.out hello.ftdi.44.echo.c
make: avr-gcc: No such file or directory
make: *** [hello.ftdi.44.echo.out] Error 1

Compilation exited abnormally with code 2 at Tue Mar 19 22:18:49

Oops! I forgot to put sudo in front on that order, so I imagine program doesn't have enough permissions to create the file. Let's try it again with sudo. No luck.

I switched to a terminal to verify if my problem was emacs related or not. Same problem in the terminal. I double check filenames and paths as the problem seems to be the compiler is not able to create the obj file.

New try with the files downloaded again, fresh without any modification.

I check a thread on Stack Overflow, to discover avr-gcc was not found. I did some previous practices using CrossPack for AVR, so maybe it's something related to paths. I opened a terminal and then tried completing the names using tab to check if either avr-gcc or avrdude were located on the default path. I found them without problem.

Back to emacs I opened a new terminal with M-x shell. I tried completion and it failed this time.

This is my regular path file

declare -x PATH="/Users/satch/.rvm/gems/ruby-1.9.3-p194/bin:/Users/satch/.rvm/gems/ruby-1.9.3-p194@global/bin:/Users/satch/.rvm/rubies/ruby-1.9.3-p194/bin:/Users/satch/.rvm/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/CrossPack-AVR/bin:/usr/local/git/bin:/Users/wayneeseguin/.sm/bin:/Users/wayneeseguin/.sm/pkg/active/bin:/Users/wayneeseguin/.sm/pkg/active/sbin"

This is my emacs path

declare -x PATH="/usr/bin:/bin:/usr/sbin:/sbin:/Users/satch/.rvm/bin"

As you can see it's a completly different kind of beast! BTW, who is wayneeseguin and what is he doing on my default path? Exploring further it seems to be bundled in a crappy rvm installer. Why does the installer add its creator path to new installed machines it's dirty, a security problem and non-sense in my opinion.

Anyway, time to try the compilation again from a fresh new terminal were avr-gcc completion works.

New error

Afrodita:FabAcademy satch$ make -f hello.ftdi.44.echo.c.make program-usbtiny
avr-gcc -mmcu=attiny44 -Wall -Os -DF_CPU=20000000 -I./ -o hello.ftdi.44.echo.out hello.ftdi.44.echo.c
hello.ftdi.44.echo.c: In function 'main':
hello.ftdi.44.echo.c:216:19: error: variable 'message' must be const in order to be put into read-only section by means of '__attribute__((progmem))'
make: *** [hello.ftdi.44.echo.out] Error 1

I had to modify line number 216 and add const at the beggining of the line. I was able to compile successfully afterwards!

compileok.jpg

I opened Arduino IDE and opened the Serial Monitor to verify the program was working as expected. To try to learn a bit more about programming in C, I tried to make the LED as many times as the lenght of the buffer displayed on screen. I followed this tutorial by Lady Ada, on blink and incorporated some elements in the body. It was easy to obtain the lenght as there is a variable called index that stores it to prevent a buffer overflow. This is my resulting code: hello.ftdi.44.echo.blink.c Feel free to play with it or try it with your own boards!

sequence.jpg

Date: 2013-03-13 Wed

Author: César García Sáez

Org version 7.8.03 with Emacs version 24

Validate XHTML 1.0