9. Embedded programming
In this week, we worked in a group to test and compare the performance of different microcontrollers’ architectures. Individually, I read the datasheet of the microcontroller I used, which is ATtiny44 and programmed it to do some tasks using different Programming environments.
Softwares used:
This week assignment content:
Arduino IDE Resources:
Atmel Studio 7 Resources:
9.1. Group assignment.
The group assignment of this week is to compare the performance and development workflows for different microcontroller families. Documentation of the group assignment is available on the group assignment page.
Click here to enter the group assignment page.
9.2. Individual assignment.
For this assignment, I have used the microcontroller board that I have created in week 7, which contains ATtiny44.
9.2.1. Read the datasheet.
When dealing with electronics components, first thing to do is reading its datasheet. There are summary and complete datasheets. For most important and common inforamtion, you can read the summary one. For more deatils you may need to read the complete one.
In order to design my circuit board, I refered to the ATtiny44 pinout provided in the datasheet. There are two pinout configurations available for this microcontroller chip, PDIP/SOIC and QFN/MLF; The one I used is the first one. It has 14 pins, containing one power supply pin and ground pin. The remaining pins are for the two ports available, port A (8 bits) and port B (4 bits) (i.e. some other micro chips have more ports than ATtiny44). There are certain pins dedicated for serical communication, which are SCK, MISO, MOSI, and RESET.
9.2.2. Programming using different programming environments.
9.2.2.1. Arduino IDE.
I programmed my board first using Arduino IDE, which uses C++ programming language. The programmer I used to program my circuit is Arduino UNO. I couldn't use the programmer I created in week 5 because I used SAMD11, which has different communication interface than my PCB that uses ATtiny44.
To set Arduino UNO as the programmer:
1- Connect it to your PC USB port.
2- Set your Board option to Arduino UNO and choose the correct Port.
3- Click on File > Examples > 11.ArduinoISP > ArduinoISP .
4- You will get a code, upload it to the Arduino UNO.
Now the Arduino is ready to be used as a programmer !
The next step is to connect the ATtiny44 board ISP connector to the Arduino UNO pins as following:
- Arduino Pin 13 ---> SCK
- Arduino Pin 12 ---> MISO
- Arduino Pin 11 ---> MOSI
- Arduino Pin 10 ---> RESET
- Arduino Ground ---> GND
- Arduino 5V ---> VCC
The ATtiny44 should be added to the boards list as arduino IDE does not have it by default. This step is explained in week 7.
Then, change the tools settings as following:
The code I want to upload is a simple push button code that controls the LED on my board. To do that:
1- Add button library from Tools > Library Manager > search for Button library and install it. This library will give more stable response of the button and it handles debouncing automatically.
2- Upload the following code:
// constants won't change. They're used here to set pin numbers:
const int buttonPin = PA2; // the number of the pushbutton pin
const int ledPin = PA3; // the number of the LED pin
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}
void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
} else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
}
Button is now working as expected:
After that, I tried writing another code and uploaded it using Arduino IDE. I used the same micorcontroller board, which is ATtiny44 and used Arduino UNO as a programmer. I wrote a fading code that changes the brightness through the loop every second.
I have connected the LED to pin 8 of ATtiny44, which is "PA5" because I need a "PWM" pin to apply the fading code. Pin 8 is the same pin of "MISO", so I have conneted using external wire on a breadboard.
Fading code:
// constants won't change. They're used here to set pin numbers:
const int buttonPin = PA2; // the number of the pushbutton pin
const int ledPin = PA5; // the number of the LED pin
int brightness = 0; // how bright the LED is
int FadingLED = 4; // how many points to fade the LED by
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
}
void loop() {
analogWrite(ledPin, brightness);
brightness = brightness + FadingLED;
if (brightness == 0 || brightness == 255)
{
FadingLED = -FadingLED ;
}
delay(1000); // delay 50 milliseconds.
}
Video of the operating code:
9.2.2.2. Atmel Studio.
In this stage, I have used the same code (PushButton) created before, but instead of using Arduino IDE, I used Atmel Studio 7 to upload it to my PCB. To do that follow the below steps:
Create an External Tool from Tools top menu.
Add a new external tool and set as the following:
- Title: the title you want.
- Command: click “…” and navegate to your arduino installation avrdude.exe file.
- Arguments: copy and paste the following:
avrdude -c stk500v1 -b 19200 -p attiny44 -P COM8 -e -u -U flash:w:\Users\Maryam\AppData\Local\Temp\arduino_build_893397/ButtonCode.ino.hex:i
You could change (attiny44) to the avr chip that you are using and (COM8) to the USB port that you are connecting to. While the part after (w:) can be copied from the arduino IDE after uploading the code.
You can see the new tool now from the Tools menu.
Click on the tool name and the code should be uploaded now when you see this messgae: