Embedded Programming - Starting from the bottom

The challenge:
Program your board to do something, with as many different programming languages as possible.

Plan of attack:
Last week I designed and milled up a speaker board. This week I’ll attempt to make it place some sounds. I also have an RGB board that I milled as a test so will have a crack at making that do pretty things too.

First I uploaded the example code to ensure my board actually worked.
sudo apt-get install flex byacc bison gcc libusb-dev avrdude gcc-avr avr-libc libc6-dev
apt-get install
wget http://www.avr-asm-tutorial.net/gavrasm/v34/gavrasm_lin_i386_x64_en_34.zip
wget gavrasm
unzip gavrasm_lin_i386_x64_en_34.zip -d software/gavrasm
cd software/gavrasm/
sudo chmod 777 gavrasm
sudo mv gavrasm /usr/bin/
gavrasm
install gavrasm
back in home folder
gavrasm hello.RGB.45.asm
gavrsm works
compiled hello assembly file
nano hello.RGB.45.hex
hexfile
sudo avrdude -c usbtiny -p attiny45 -P usb -U flash:w:hello.RGB.45.hex
fail upload
Woops, had the ISP plugged in backwards.
success upload

Read the datasheet

Almost every electrical component has a datasheet and while the quality can vary wildly between manafactures they are the first place to start when working with a new component. Atmel produces some great datasheets that contain heaps of information about their chips.

Unfortunately the datasheet for the attinyX5 series is 234 pages long. This makes for some pretty dense reading so the way I’ve been working with its to read the basic summary info at the start.

This has really useful information like the operating voltage ranges for the chip.

As well as the physical location of the pins avaliable and the key features they support.
From there I jump to the end of the data sheet some 220 pages and check out the index to look up the particular bit of information im after.

I was interested in using a 20Mhz crystal in one of my boards so I first found that it was supported by reading the summary information. I then jumped to the section about using external oscillators to find out how to wire it up and what voltage range I could use to run at that speed.

From reading the datasheet I also found that attiny family has an internal oscillator which can be calibrated to be accurate to 1%. I would like to learn what the process is for achiving this. If I get a chance in the coming weeks I might ditch the external crystal and give that a go.

Update:

I did get a change to have a play round with calibrating the interal oscillator check out my final page for details.

Next up I attempt to write some assembly code. But first grab some syntax highlighting support for gedit.
wget http://www.carminebenedetto.net/_downloads/asm-intel.lang
sudo cp asm-intel.lang /usr/share/gtksourceview-3.0/language-specs/

Assembly is very low level and allows for presise control over the microcontroller, allowing us to write very effishent code. But the closeness of it to the hardware layers mean we are required to do a lot of manual setup.

I started off by reading through Scott Zitek’s blog and trying to apply his code to my attiny45 driven rgb led. It wasn’t working as his code is desiged to work with a button that my board didnt have and there was quite a bit of an understanding gap so I had a look at the example code. It used PWM to cross fade between colours which looked really pretty but didnt do much to increase my understanding. So finally I checked out AVR beginners, their basic tutorial attempts to provide even more of a break down of what each line is doing and provides a bit of context for each section of the code.

After following through the tutorial, I attemped to compile my code and was greeted with 9 errors.
fail to compile
It turns out that the .include "tn45def.inc" line is actually really important, despite avrdude perviously stating that “Include defs not necessary” when compileing the examples. This got my basic code uploaded but nothing happened on my board.
I figured this was probably due to the slightly random pin selection I had used to wire up my LED. http://www.atmel.com/Images/Atmel-2586-AVR-8-bit-Microcontroller-ATtiny25-ATtiny45-ATtiny85_Datasheet-Summary.pdf

I guessed that it was probably due to the way my board was wired and changed the port it was switching.
sbi PortB, 2 set bit regiester, aka turn on the led attached to PB2.
cbi PortB, 2 clear bit register, turn it off again.
Blinks

AVR in the Arduino enviroment

“Arduino is an open-source electronics platform based on easy-to-use hardware and software. It’s intended for anyone making interactive projects” For our perpouses I’ll be focusing on the arduino IDE as a development platform because we’ll be developing our own boards for atmel chips and using them through out the duration of Fab Academy.

The Arduino IDE is basically a series of libaries and nice programing interface designed to make it easier to write code of embeded devices. Under the hood it still uses the same avr-gcc, arm-gcc and avrdude that I had to setup to work in assembly.

Setting started with Arduino is super easy, just head over to their website and download the latest version.

Arduino setup

Straight out of the box Arduino has support of quite a number of boards, but most of these are baised on the atmega 168 or 328 chips which are often a massive over kill for our entry level projects. So the first thing we need to do is tell Arduino about the boards we do plan on using. Instead we can add support of the attiny family by downloading the core from google code and placing it in the hardware folder. On windows this hides in C:\Users\username\Documents\Arduino\hardware as well as in the arduino install directory C:\Program Files\Arduino\hardware. Living in that same arduino folder is the libraries directory. Where we can add other libaries to further extend the fuctionality of our board.`

Working with sound

Last week I developed a board with a speaker driven via a fet off one of the PWM pins on a attiny45. The goal this week was to make the speaker actually make some noise. I took two different approaces to try this, playing short shounds using PCM(Pulse-code modulation) and playing tones using the tone libary.

PCM is a way of a converting analog signal into a digital repersentation. It works by sampling the audio signal at a regular rate and then assigning it a value. This process is used for storing audio as a WAV or AIFF file. To implement it on my attiny I used a sample that had already been taken and stored it as one giant array. This eats up most of the micro-controllers memory and is the main limitation with using this methord. We then cycle through the samples in the array at the same speed as the samples were taken to reporduice the sound.

Tone is a function included with the core arduino enviroment since 0018. It attempts to geneorate sounds at a specifice frequency. Playing a tone this way is as easy as calling tone(pin, frequency). The library supports 1, 8 and 16 Mhz clock cycles.

Update

Since writing this post arduino version 1.6.2 has been released which introduces the “Boards and Libraries Manager” allowing some of the setup steps to be completed from within the Arduino GUI. See the cores guide for more information.

embedded programming - Weekly resources