Embedded Programming
Assignments Goals
- Read a microcontroller data sheet
- Program your board to do something, with as many different programming languages and programming environments as possible
Exploring the Datasheet
During my years at my workplace I worked on a lot of different electronic devices, and I had the pleasure of reading a plethora of datasheets, ranging from microcontrollers, integrated devices, electromechanical devices and analog devices. What I learned during this years can be summarized in a few words: every datasheet is different, and you have to take your time to carefully read it if you want to avoid errors!
Unfortunately a lot of datasheets around are poorly written, but major companies, as Atmel, Microchip and STM put a lot of effort to make them as complete as possible. For this week assignment I will explore the ATTiny44A datasheet, focusing on the sections that according to me are the most important to start developing on it.
On the first page of the datasheet we can find (in a really marketing prone way ;) ) all the main characteristics of our microcontroller, such as architecture, memories size, peripherals and clock speeds etc... This could be used as a quick reference for the main features of our microcontroller.
The next section I usually read really carefully is the Electrical Characteristics (sect. 20) one. In there are listed all the Absolute Maximum Ratings and the DC levels used. From these tables you can determine things like the maximum operating voltage (so not to burn the microcontroller, supplying a wrong voltage), and the DC current per pin, so to size the load on it correctly.
The next section I usally read is the Fuse Bytes (sect. 19.2). In there you can read the meaning of each bit, and their default value. This is extremely important when selectiong the clock source, and other functioning modes. After that, another useful section (or mainly a page) I read is the Pin Configurations (sect. 1), that lists the pinout and the functions assigned to each pin.
All these sections are important even before starting to program our board, and It's a good idea to have a look at them when designing the schematic.
The next sections I usually look at, depends on the program I'm going to write. For example if I'll use some GPIOs, I look at I/O Ports (sect. 10) and the relatives registers DDRx, PORTx and PINx. If I'll use timers I look at 8-bit or 16-bit Timers (sect. 11 & 12) and the relatives registers TTCR0x and TCNT0, and so on.
So, the most useful part in a complex datasheet is... the index!.
Sintax Highlighter
For the following sections, since I intend to show some code snippets and terminal commands, I used a free javascript plugin to highlight code listings: Prism. It's powerful, really easy to use and has a couple of useful extensions to add some nifty features!
Programming using Atmel Studio 7
Since during Week 06 I already programmed my board using WinAVR to make my board blink in Morse Code (you can find the assignment documentation here), I decided to try the official tool from Atmel for programming and flashing their microcontrollers: Atmel Studio 7.
It is a free full-blown IDE, derived from Microsoft Visual Studio, to edit, compile and flash any Atmel microcontroller in an easy and user-friendly way. Moreover, if you own a compatible debugger (I have an AVR Dragon), you can make step by step debugging and explore every register and memory block in real time.
Moreover I decided to tidy up the code I wrote last time, and to organize it in subfolders. The result was a well structured file tree. In order to debug the ATTiny, we have to use the debugWIRE interface, selecting it in the project properties and writing the fuse bit DWEN.
To organize the code and make it simple to customize, I created a separate config.h file, in which are defined all the GPIO connections and the relative registers involved.
In another file, macros.h I put all the useful macros to set, clear and toggle bits in order for example to configure pins as output or input, to set output level high or low, to enable or disable internal pull-up resistors and to enable or disable Pin Change Interrupts.
Work files links
GF_HelloMorse.zip
Programming using CrossPack
As a further exploration, I wanted to try to use CrossPack development environment on Mac OS X, and create a new Makefile to compile and build the same firmware I created with Atmel Studio.
Makefiles are not simple at all, especially if you want to create them extensible, modular and parametric. After a lot of trial and error, and after reading tons of documentation, I came up with a working Makefile. The turnkey to make it working was the use of the variable VPATH, to include source files in different folders and subfolders!
Execute make help to look at the possible make targets.
Work files links
GF_HelloMorse.zip
Programming using Arduino IDE
Since programming in C and having to learn bitwise operations is not always an easy process (especially if you haven't programmed ever...) I asked myself is it possible to use Arduino IDE and libraries with the ATTiny44?
The short answer is yes.
In order to do that, you have to install an additional package inside Arduino IDE, containing all the core libraries for the ATTiny microcontrollers. I followed this simple tutorial, and everything went smooth.
To test the process, I re-wrote the HelloMorse sketch, after looking at the naming conventions of the GPIO pins, from the image below.
The code is really simple to write using Arduino Libraries, but I ran in a lot of problems because almost all the microcontroller data memory is filled by those! In this sketch I had to remove every whistles and bells, especially when it comes to using char arrays. Now the program replies a morse code only for digits.
So the long answer is yes, but according to me unless you need to write a really tiny program, with no char arrays and few variables, then it's not suitable for the ATTiny microcontroller. As you can see from the image below, a simple sketch with just the initialization of the SoftwareSerial library and a print, uses up to 54% of total dynamic memory!
If you want to use the AVR Dragon programmer with Arduino IDE, you have to add these lines in the file Arduino\hardware\arduino\avr\programmers.txt
dragon.name=AVR Dragon ISP
dragon.communication=usb
dragon.protocol=dragon_isp
dragon.program.tool=avrdude
dragon.program.extra_params=-Pusb
After a bit of thinking, I tried to optimize my code to be able to fit it in the small memory of the Tiny44, and I came up with a kind of encoding for the morse alphabet, in which every letter and digit occupies a single byte: the most significant 3 bits, encode the length of morse sequence, the remaining 5 bits encode the sequence itself, where 0 is the dot, and the 1 is the dash.
In this way the memory used was a lot less, but I had the full alphabet in my code!
Work files links
ArduinoTiny.zip
Assignments Outcomes
- Identify relevant information in a microcontroller data sheet
- Implement programming protocols
Have you:
documented what you learned from reading a microcontroller datasheetprogrammed your board
described the programming process/es you used
included your code

