Assignment 9: Embedded Programming

Assignment:
Group assignment:
- Experiment with 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

Reading the datasheet
Reading a microcontroller datasheet was a task this week. I chose to read datasheet for ATtiny 44A. It is the microcontroller we use in our boards. The datasheet is 286 pages long and contains all the necessary information for its use and the programming. Datasheet is divided in 27 chapters, which are clearly named and divided between its key properties. The first three chapters give general overview of the microcontroller including the pin configurations. ATtiny 44A pin configuration looks like:

PinConfigurations
Pinout of ATtiny44A. (Atmel Corporation, 2018)
The ATtiny 44A is a low-power CMOS 8-bit microcontroller based on the AVR enchanced RISC architecture. It contains 32 general purpose working registers.

PinConfigurations
ATtiny44A architecture (Atmel Corporation, 2018)
In the chapters 4 to 19, all the operating and programming aspects of the microcontroller are presented. Chapters 20 to 25 give general information of the microcontrollers characteristics, summarizes registers and instruction sets and offers information on ordering and packaging of the microcontroller. Chapter 26 shows errata of the datasheet and chapter 27 presents the datasheet revision history.
There can be found several code examples in Assembly and also in C, which are used to clarify the programming of the different features on microcontroller.

PinConfigurations
Code example for ATtiny44A (Atmel Corporation, 2018)
As a beginner it is hard for me to find all the important information for Fab academy assignments in the datasheet. But as I read different datasheets during the groupwork, I found out that this is one of the easier ones to read and the explanations of different things are on good level. It seems that everything is relative and you can quickly learn to look for the pins and ports and directions in the datasheet. Totally different thing is that how well they are documented in the datasheet.

Embedded programming
We started to learn about embedded programming with our local instructor Juha-Pekka Mäkelä at 15.3.2018 at noon. We went through the basics of the embedded programming and the answers were given to question what, why and how. Microcontrollers are typically dedicated to specific tasks and embedded programming is used to give the microcontroller the specific task. In the Fab academy it is necessary to use embedded programming to use different types of sensors and to be able to communicate the measurements. I made my own for 2x3 pin headers. It required a ribbon cable and 2 pieces of 2x3 female connectors. I just pressed the connectors into cable to make connection and it was done.

connector

connector
In the C-language the pin inputs and outputs are defined with different bit operations. The bit operations we used in this task involved:
DDRA |= (1<<PA7); Pin 7 of port A is an output
PORTA |= (1<<PA7); Pin 7 of port A is high
PORTA &= ~(1<<PA7); Pin 7 of port A back to low.
DDRA &= ~(1<<PA7); Pin 7 of port A is an input.
This is not the actual code but used to demonstrate operations. Since I programmed the hello board and documented the flashing process already in the Week7, I won’t go into detail, but show all the steps made here to program the button and led to work. At first, hex file is needed for the flashing. The command to create it is:
>make -f hello.ftdi.44.echo.c.make
Because I had already flashed my board, I only had to flash the microcontroller by running command
>sudo make -f hello.ftdi.44.echo.c.make program-usbtiny
Depending on your c-file name you can change the first row in the make file to your c-file name without the .c ending.

ButtonLed
At first, I just went ahead and set the led on with a simple code.
#include <avr/io.h>
int main(void){
DDRB = DDRB | (1<< PB2);
while (1) {
PORTB =PORTB | (1<<PB2);
}
}
This turns the led on and it remains on as long as the FTDI cable is connected. Then I moved to get button operated led, where pressed button lights the led and it gets dark, when the button is released. The connection for programming looks like this:

connection
In previous year Ivan made a code in C to blink the led, when the button is pressed. I only added the clock divider from Neil's code. I used this code to tryout my board, but because I wanted to understand more, I used another code as well. The code worked without any problems, so it was not a good teaching moment. I started working with Neil’s code and to modify it to work with my board. I added input port pin for the button, changed led pin to PB2, added button port, direction, pin PA7, I utilized Neil's pintest to check, whether the button is pressed. Because of the pullup resistor, the pintest has to be false. It was surprisingly easy task and the only problem I faced, was that I accidentally chose the wrong direction for led and it lit up but was very dim. When I changed the direction, everything worked perfectly. I used the following code (modified from Neil's code).

Then I moved to Arduino IDE 1.8.5. I installed ATtiny microcontroller boards for the Arduino with a guide and link from was Fab academy tutorials . It was easy and worked exactly as it was instructed . I forgot to run Arduino IDE as root at first, but I quickly noticed the error and that was the end of problems. Steps to program ATtiny 44, with Arduino IDE are as follows:
- Open Files -> Preferences and there is Additional Boards Manager URLs:

Preferences
- Add link https://raw.githubusercontent.com/damellis/attiny/ide-1.6.x-boards-manager/package_damellis_attiny_index.json, if you have multiple links separate them with comma.

Preferences
- Click ok and open boards manager in the Tools -> Board menu. In the bottom of the list you should see ATtiny. Click install button to install it.

Preferences
- When it is installed, close the boards manager and you should see ATtiny in the Tools -> Board menu.
- Select Board: ATtiny24/44/84, Processor: ATtiny 44, Clock: External 20 MHz, Port: /dev/ttyUSB0 (case related) and Programmer: USBtinyISP.

Preferences
- When all is selected press burn bootloader.

Preferences
Now everything is ready for the programming. I used an Arduino tutorial blink as an example for my code. Basically, all I had to do was to change led_pin=8 and button_pin=7. For that I used ATtiny 44A vs. Arduino Pin-Out Numbering from ATtiny Using Arduino Fab academy tutorial page.

Preferences
First I compiled the code by opening Sketch -> Verify/Compile. This can be done also with the "correct" sign positioned in the leftmost of the tool bar.

Compile
Then I opened Sketch and in there selected Upload Using Programmer, which starts programming the microcontroller.

Programming2
When the programming is done, it says Done uploading in the Arduino IDE.

Upload
The board worked as intended.

connection
Reflection on this weeks assignment
I learned a lot about embedded programming this week. I find the subject really interesting and to be in the core of the whole course. What I would like to learn more about is using interrupts in the microcontrollers. Several times there is some routine running all the time in a machine and something needs to be used to interrupt that routine immediately (for example pressing a button). If one loop is relatively long, there is lots of lag in the reaction and I think it would be a very useful skill.

Group assignment

The group work consisted of testing different microcontroller architectures. We tested Rasberry Pi 3 Model B, STM32 Nucleo-64 development board with STM32F401RE MCU and we tried to work with Atmel XMEGA-E5 Xplained, but failed to get it to do anything. The progress and testing is presented in embedded programming group page. We chose the boards together and searched information related on them, but my computer was used program the boards, so I made most of that work. It was a lot harder than I thought to get the boards to do anything (except Rasberry Pi), but luckily Arduino IDE has extensions for different boards.

Group
The files used in the assignment are shared below:
led.c Led on

buttonLed.c Led blinking based on Ivan's code

hello.ftdi.44.echo.c.make Makefile for buttonLed.c

buttonLed2.c Led blinking based on Neil's code

buttonLedArduino.ino Led blinking Arduino code

References
Atmel Corporation (2018) ATtiny24A/44A/84A datasheet, Retrieved from http://ww1.microchip.com/downloads/en/DeviceDoc/8183S.pdf , cited 23 May, 2018.