9 - Embedded Programming¶
Becoming The Master of Microcontrollers...
Tools & Assignment
Updated for 2019 evaluation standards!
Group assignment
- Compare the performance and development workflows for other architectures
Individual assignment
- Read a microcontroller data sheet.
- Program your board to do something, with as many different programming languages and programming environments as possible.
Learning outcomes
- Identify relevant information in a microcontroller data sheet.
- Implement programming protocols.
Have you?
- Documented what you learned from reading a microcontroller data-sheet.
- What questions do you have? What would you like to learn more about?
- Programmed your board
- Described the programming process/es you used
- Included your code
(Copy of my 2016 website, with spelling corrections :)
Preamble¶
I have to admit that I do have previous C/C++ experience.
My final computer vision project that I presented to the EUCYS was written in C++ giving me previous knowledge.
I had the intention of exploring a field I had no experience in, such as PSoc programming, however that did not workout to the degree I desired.
Our Eighth Lecture¶
Neil worked through the 8th week schedule page as usual discussing the various options we had.
Making it very clear that there is, in fact, no difference between programming in the Arduino IDE and conventional C programming....
Except it’s far easier for beginners.
One question I have had through-out this entire week was, why do we even use C… I mean Phyton, Java, and the 1 million more new languages have such a simple and friendly syntax....
While the majority of people, it seems, STILL like to program microcontrollers using C, but coming from me it’s a little hypocritical since I really like C.
Note from 2019
Okay, so now I understand why we use C after going through 3 years of University. Basically C is fast, and works really well with things like microcontrollers thanks to vast support. There are basically only 2 faster alternatives, Fortran and assembly language.
On top of speed, using assembly languages, we’re really getting bare-metal low-level control over the hardware, meaning that fewer libraries are needed out of the box. Therefore we can fit more meaningful code in the same amount of the (limited) space we get.
Programming¶
I wanted to learn something new, so originally I wanted to Program a PSoc board, however I figured ordering it was going to be too much of complication.
Since I had used the IDE Processing in the past, I was quite happy working around the very similar and familiar Arduino IDE; I was going to use a different IDE to try something new.
I picked Eclipse IDE which I have barely used in the past (I have programmed Java in it, to try out some simple Genetic Algorithms). This meant I was going to program in pure C/C++.
Online Video Tutorials & Links¶
While I do have knowledge with Programming, I had no experience with using Eclipse, or using Eclipse to program AVR’s, or programming AVR’s at all....
Well Emma taught us to use the Arduino IDE on Thursday but that didn’t give me any insight on using Eclipse, however it was still a really good lesson.
So I went to Google and searched for information, from this research the following links are the most valuable:
Videos¶
-> How to program the AVR microcontroller Pt 2
-> AVR_Programming Video2-Programmers Notepad & Make file creation
-> Getting Started AVR with Eclipse Development and Arduino board in Linux
-> AVR ATtiny85 Programming: Blink LED (Digital Output)
Understanding AVRdude¶
Admittedly have used the AVRdude terminal commands without having a clue of the command I was typing, mostly using avrdude -c usbtiny -p t44
to check if an ATtiny was connected…
So I felt it was about time I understood how it worked, which wasn’t difficult.
Typing avrdude
in the terminal will open up all the Avrdude options.
AVRdude options and Explanations through terminal
This allowed me to have an insight on the options I used.
-c
= Specify programmer type.
-p
= Required. Specify AVR device.
-u
= Disable safe-mode, default when running from a script.
-U
= Memory operation specification.
Using avrdude -c asdf
shows all acceptable programmer types, for us we will need usbtiny.
Eclipse Setup¶
Eclipse was created to program in Java, therefore, as expected, right out of the box Eclipse doesn’t do much of what I needed it to.
The first thing I did was install the C/C++ environment needed to write programs in C, this is a simple option in the Eclipse Installer which pops up every time you open the Eclipse IDE.
Installing the C/C++ environment for Eclipse
Having done this, it is necessary to add the AVR Eclipse Plugin which allows us to set up AVR projects.
Doing this requires several steps, the first is downloading CrossPack (for Mac) which is an AVR-GCC toolchain which adds useful libraries and other stuff we need to program our AVR chip.
CrossPack Download Page
After doing this it was necessary to install the AVR plugin, this was done following the instructions on the official AVR plugin page.
Instructions on adding the AVR Eclipse Plugin
Finding how to install AVR Plugin
Installing the AVR plugin in Eclipse
This concluded the setting up of Eclipse. We can then go ahead and create a new Eclipse project.
Creating a AVR project which allows us to build .hex
files for the ATtiny44
Programming & Reading The Datasheet¶
Next was the programming which was rather straight forward.
Creating a new C project in eclipse and selecting the right preferences was the first step.
The key was checking out the datasheet to select the exact frequencies (see Image 4# & 5#) and the write commands, which are clearly specified in the datasheet.
Creating a AVR project which allows us to build .hex
files for the ATtiny44
Selecting the microcontroller type and frequency from project setup.
then adding a new source fail main.c
where we will write the program.
Adding a Source file to the project
First thing I did was write this simple program to check if uploading to the board worked fine.
After that the major problem is finding what you need in the ocean of information it has, but after some getting used to it gets pretty easy to understand where is what.
Understanding which registers to use in my C program.
I then proceed to make different test programs which where still quite basic due to the fact they written by myself, following the huge datasheet.
The example program seen below creates a dim light. I used a comment drawing, as see at the top of the image, that helps me see on which pin every component is, following the pinout configuration.
AVR ATtiny44
Screen shot of my first program: how to make a dim light with the LED
Changing the value of delay_ms
changes the speed at which the led blinks, if it’s set to something like above (0.01
) it will be too fast for the eye to see and will appear dim, if it was instead set to 100ms
it will appear to be blinking.
Eclipse Building & Flashing The ATtiny44¶
After changing the Build Configuration to Release
building the .hex
file was as difficult as clicking a button, the hammer button on the top.
(Success is seen in the Eclipse console).
Eclipse Console output from Building.
After this I opened up the terminal, using cd
I specified where the .hex
was.
I then connected the ISP with the Echo-board attached and run the command avrdude -c usbtiny -p t44 -u -U flash:w:FabAcademy_Programming_1.hex
: FabAcademy_Programming_1.hex
being the name of the build .hex
file.
Happily enough flashing worked, and the Echo-board LED started blinking .
Successful flashing of the ATtiny using the avrdude -c usbtiny -p t44 -u -U flash:w:FabAcademy_Programming_1.hex
command.
[Update 7 Jul 2016] Eclipse can be set up to flash an ATtiny through the USBtiny without having to use the console thanks to the Eclipse AVR plugin; if you follow the linked video “Getting started with AVR with Eclipse Development..”.
2019¶
I’m not going to complicate my life this time around. That means using Arduino IDE
So, the first step is learning how to program an attiny44/45
using the Arduino IDE.
A quick google search “programming attiny44” returns 891,000 results, the first link being:
-> Program an ATtiny44/45/84/85 With Arduino
This is a fairly good guide, it also assumes you’re using an Arduino as ISP. It’s a little more tricky to get the wiring right on it, but it’s more reliable.
Since using the Fabbed ISP from electronics production week isn’t a requirement for programming, I will take the liberty to use the Arduino as an ISP. I do still have my FabISP
from 2016, but 4 years sitting on a shelf brought about it’s demise.
Not sure how I got to it, but in my searches I also found this gem:
-> Yue Siew Chin’s - 2016 - Embedded Programming
It’s a great guide and it was essential to my success.
Something To Keep In Mind
Personally, as I’ve mentioned before, I use MacOS, so this guide only applies to Mac users.
Arduino As ISP¶
On it’s own the Arduino can’t be used as an ISP, so what we can do is program it to be able to do so, this can be achieved in the following steps:
Open the Arduino ISP example sketch:
File
->Examples
->11.ArduinoISP
(Built-in Examples) ->ArduinoISP
Connect your Arduino (obviously), choose the right settings:
Tools
:
Board:
“Arduino/Genuino Uno”Port:
“usbmodem~whatever the name of your arduino”Programmer:
“AVRISP mkII”
Finally, upload the code: (ctrl + u)
or Sketch
-> Upload
or click arrow thing on top of the Sketch.
When it says done uploading
, your Arduino is ready to act as an ISP
Install Board Manager for Attiny in Arduino IDE¶
First things first we have to install all software requirements. Basically, following the guide, but for MacOs:
To use the attiny on the Arduino IDE we need get a ‘board support url’, this will let us install a board manager package, which will define the settings of, and act as wrapper for, avrdude.
On the Unofficial list of 3rd party boards support urls, we can search the page for attinty
:
(ctrl + f)
->attiny
As can be seen there are several links, most people seem to be using simply the ATtiny
link.
I’ve tried also the ATtinyCore
, which seems to be more powerful but not necessary for now.
So we just follow the herd, we copy the link under Attiny
:
-> https://raw.githubusercontent.com/damellis/attiny/ide-1.6.x-boards-manager/package_damellis_attiny_index.json
Then we open Arduino app:
In the options :
Arduino
->Preferences
Paste link in:
Additional Boards Manager URLs
While we’re here, it will be useful in the future to have both the[x] compilation
& [x] upload
check-boxes for Show verbose output during:
ticked finally select Ok
.
In the options select:
Tools
->Board
->Boards Manager...
Let the the Download platform index
at the bottom of the screen end.
Enter in search bar : attiny
Under attiny by David A. Mellis
select -> Install
Now, under attiny microcontrollers
in Tools
-> Board
, we have Attiny25\45\85
& Attiny24\44\84
, which is what we will be programming.
My Old HelloEcho Board¶
Miraculously, my old hello echo from 2016 still works, this is pretty amazing. Actually it still had the blink program in it!
I’ll do the same blinking program (the only thing it can do?) using the Arduino IDE
This old derelict doesn’t really have much info it left in the cyberspace. Hold and behold my poor documentation doesn’t include a photo of the .brd
from Eagle, so I have no idea where everything goes.
What I had to do was probe every pin with a multimeter and check what it matched to in the pin-out, works well enough, here it is:
Understanding The Arduino Pinout¶
So, what really gets confusing is the connections, I had to write it down on a sticky note because I’d always get it wrong:
- Pin 13 (PB5) -> SCK/CLK
- Pin 12 (PB4) -> MISO
- Pin 11 (PB3) -> MOSI
- Pin 10 (PB2) -> RST
Why these specfic pins? Good question, the answer is no particular reason
except, that the AtMega has hardware implementations of SCK/MISO/MOSI
on those pins.
What does that mean? Basically that the functions that are usually run over typical SCK/MISO/MOSI
pins are facilitated by hardware.
Why do we care about that, well, if we look at the datasheet, we see:
Ah ah! I know the word Serial
is about 5 weeks too early, but basically it’s the communication protocol we use to program the Attiny boards.
Hardware is faster then software, aka programming the board is faster done through these pins. Perfect.
Usually other pins have hardware defined functions, we can see that in the pin configuration
image posted above (eg. XTAL
, RESET
, etc.)
Some of these functions may only be implemented through those pins, which is why we care to use them for their predefine purposes.
Programming¶
The programming is pretty straight forward, we can open the Arduino IDE, and open the Blink
example.
File
->Examples
->01.Basics
->Blink
The code is very short and simple :
// the setup function runs once when you press reset or power the board void setup() { // initialize digital pin LED_BUILTIN as an output. pinMode(LED_BUILTIN, OUTPUT); } // the loop function runs over and over again forever void loop() { digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level) delay(1000); // wait for a second digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW delay(1000); // wait for a second }
-> Code credits
Obviously this will not work straight out of the box, considering that the function call pinMode(LED_BUILTIN, OUTPUT);
on the 4th line calls for LED_BUILTIN
which will throw some 'LED_BUILTIN' was not declared in this scope
because the board we’re trying to program doesn’t have that kind of fancy support (nor should it, because how can you assume where the LED is attached?)
Anyways, we can change those first few lines to:
led = // ??? some number void setup() { pinMode(led, OUTPUT);
The question is what value should we give to the variable led
?
Well if we look at the datasheet, we can check the IC pin to which my LED is connected, which happens to be pin 3
.
Naively, you could make the assumption that 3
is the number you’re looking for. It’s not.
In actuality, Arduino has it’s on special pin numbers that don’t match the standard layout of the IC, they actually stand for the physical header numbers on the Arduino.
We could cross reference to the AtMega328p chip datasheet that the Arduino board uses, or you could find someone who has done the job already. Out of all the possible diagrams online, my personal favourite are the one madde by SpenceKonde/ATTinyCore for both clarity and breadth.
Using his diagrams for the Attiny24/44/85, we can see that the pin we need in this case is arduino pin4
for IC pin 3
.
So that’s what we set in our code, final code:
led = 4 pinMode(led, OUTPUT); digitalWrite(led, LOW/HIGH); // turn the LED on (HIGH is the voltage level)}
I’d explain the rest of the code, but it’s pretty well commented so it would sound copy-pasty.
Wiring¶
Now things get a little fiddly, we need to make sure that everything is wired up correctly.
You’ll need:
- Dupont connecotors
- 10μF Capacitor (10 micro farads)
The reason we need the capacitor, is that the Arduino has a bad tendency to reset itself when connected to bare Attinty boards, if we insert a capacitor between the Reset
header and the GND
(ground) header, we can prevent this reset.
Capacitor Orientation
Capacitors sometimes have a polarity, which is usually the case for THT (through-hole) capacitors so beware, the negative leg (usually represented by a silver strip pointing towards it) goes to GND
.
Arduino Logic Levels
Not all IC are born equal. In fact, often enough certain IC will have 5V logic, while others have 3V logic. This means that an input pin is read as HIGH (1) when the voltage at is is 3V vs 5, for output pins it’s the same thing but they are set rather then read.
Because the Arduino uses 5V logic, it’s important that the Attiny is also capable of 5V logic, we can check the datasheet for this information:
Good, we know that at 10mA, it will work with 5V logic levels. Additionally we found out that the board is 5V tolerant for VCC, which is handy because we know we can power it with the 5V pin from the Arduino without frying it.
Following the above statement, wiring guide, etc. above, the wiring should look something like:
Uploading¶
The only thing left is (optinally burn a bootloader &) uploading the code, this is done by:
We must select the right settings in -> Tools
Board
: “ATtiny25/45/85”Processor
: “ATtiny4545”Clock
: “Internal 16 MHz”Port
: “usbmodem~>whatever/your/port/is”Programmer
: “Arduino as ISP” (NOT ArduinoISP)
Then we can select Burn bootloader
or (shift + ctrl + R)
which is Sketch
-> Uploading using programmer
The output of that should be:
Except I swapped the labels around, sorry about that…
The LED blinks, and another week is complete.
Collusion & Original Code
I understand that one of the points made in the FAQ
is:
Q : If I adjust existing code, is that now my code?
A :
- If you use someone’s else code as a starting point, remember that you must acknowledge whoever made it.
- To succeed in this project, experiment with changing the code, understand how that worked, and write your own.
So I feel like I meet those criteria, plus I’ve littered my website with original code so there is sufficient evidence with my proficiency. I would have done something more original if this board could have done anything else except blink.
Download The Files¶
-> Download The ‘main.c’ Code
-> Download The ‘blink.ino’ Code