Week 09. Embedded Programming¶
This week we learnt about embedded programming.
And the assignment includes:
- individual assignment:
(1) read a micro-controller data sheet, for mine, I read the data sheet of ATtiny44.
(2) program your board (aka. the board I made last week) to do something with as many different programming languages and programming environments as possible.
- group assignment:
compare the performance and development workflows for other architectures.
Read the data sheet¶
I read the data sheet of ATtiny44, which is consisted of 286 pages.
Here is the main point that I learned from reading the data sheet.
(Pinout of ATtiny24A/44A/84A, image credit: Atmel)
From the pinout part of the Datasheet, I learned that the pins for my button is Pin2, and LED is Pin3, which will be used in the code when I define my input and output pins.
Program the board¶
This is the board I designed in Week 07 Electronics Design. And this week, I am going to program it!
ATtiny using Arduino IDE¶
I connected my board to the USBtiny and then connected to my MacBook.
Open the Arduino IDE.
I already added ATtiny as the board in Week 12 Output Devices, so I do have it by default now.
I chose the right board, processor, clock and programmer (as shown in the image below).
And click “Tool => Burn bootloader”, it was successful!
By referring to the code of Seeed Grove Button, I wrote the code to use button to control the LED. I checked my design file and the datasheet, my button is connected to pin PA2, and LED is connected to pin PA3 of ATtiny44.
So I used the following code in Arduino IDE and upload.
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); } }
It was uploaded succesfully, and when I pressed the button, the LED lighted up, when I released the button, the LED was off. You can see the video below.
ATtiny using Make and Makefiles¶
First of all, I downloaded the hello.ftdi.44.echo.c and the hello.ftdi.44.echo.c.make to the desktop of my laptop.
Now I connect my hello echo board with the USBtinyISP and the laptop.
I opened my terminal and started programming.
- STEP 1: locate the above-downloaded files by putting the following code in the terminal and press ‘enter/return’ key. (My files are on my desktop, if your files are in a different location, you need to change the following code accordingly).
cd Desktop/
- STEP 2: put in the following code, and press ‘enter/return’ key
make -f hello.ftdi.44.echo.c.make
You can see the screenshot below for STEP 1 & 2.
- STEP 3: put in the following code, and press ‘enter/return’ key, and it will require you to enter your system’s password.
sudo make -f hello.ftdi.44.echo.c.make program-usbtiny-fuses
It was successful!
I also tried to program the board to make it blink 3 times when push the button once. Basically by changing the code a little bit, and I can use the button to control the LED differently, including the number of blinks, the duration of LED on, the time between each blink etc.
One push, 3 blinks: each blink‘s duration is different
I used the following code.
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); delay(100); digitalWrite(ledPin, LOW); delay(10); digitalWrite(ledPin, HIGH); delay(200); digitalWrite(ledPin, LOW); delay(10); digitalWrite(ledPin, HIGH); delay(300); digitalWrite(ledPin, LOW); delay(10); } else { // turn LED off: digitalWrite(ledPin, LOW); } }
And you can see the results in the video below.
One push, 3 blinks: each blink‘s duration is the same
I used the following code.
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); delay(500); digitalWrite(ledPin, LOW); delay(100); digitalWrite(ledPin, HIGH); delay(500); digitalWrite(ledPin, LOW); delay(100); digitalWrite(ledPin, HIGH); delay(500); digitalWrite(ledPin, LOW); delay(100); } else { // turn LED off: digitalWrite(ledPin, LOW); } }
And you can see the results in the videos below (one with long durations and the other with slightly shorter blinking durations).