Embedded Programming


Group assignment

Individual assignment

Learning outcomes:

Have you:

Week workflow

Week 8 workflow diagram


Tools used


Software Used


Group Assignment

Go to the group assignment page

Introduction

Read the Datasheet

A few thoughts about it:

On a more serious note, I really think this assignment is one of the hardest. I'm stuck because of this datasheet.

I will jump to programming and use it only for reference. While I do, I will highlight the important sections below:

ATTINY44A pin configurations ATTINY44A pin configurations


Programming

Because I was a bit confused about how to do it, I decided to list all steps in detail and have some sort of template for my next projects. Where applicable, I fill in the gaps after each step and do it while documenting, and that is what follows below:

  1. Burn bootloader
    1. Identify programmer

      - avrdude or Atmel Studio 7 (Windows only)

    2. Identify bootloader and makefile

      - For that, I start by pointing out that I want to program an ATTINY44A

      - Download the makefile

      - The bootloader is not going to be used here, since there is no USB

    3. Download bootloader

      - Not applicable

    4. Identify commands to burn bootloader

      - Nope

    5. Burn bootloader

      - Nope


  2. Burn firmware
    1. Identify example code:

      - There is this code from Prof. Neil

    2. Identify commands to compile and burn the firmware

      - With all files previously downloaded into one directory, in Linux I rename the file to makefile and run the script to create the firmware (I will repeat this step every time I want to modify the code):

      mv hello.ftdi.44.echo.c.make makefile
      make

      Renaming to makefile and compiling Renaming to makefile and compiling

    3. Compile source code

      - Done in the previous step

    4. Adapting the makefile to work with my programmer

      Editing makefile to work with programmer I had to edit the makefile to work with my programmer. That was done by replacing the entry usbtiny with USBasp 2 times, once for burning the firmware and once for burning the fuses

    5. Burn firmware

      Run:

      make program-usbasp

      Burning firmware Burning firmware

    6. Identify what fuses to burn

      - That was already done in the makefile

    7. Burn fuses

      Run:

      make program-usbasp-fuses

      Burning fuses Burning fuses


Programming with Arduino IDE

I decided to try the Arduino IDE as well to program my board.

In order to do it, there are a few steps that should work regardless of the OS

  1. Add an additional board to the Boards Manager:

    Paste the following into (Preferences)-(Additional Boards Manager URLs:)

    https://raw.githubusercontent.com/damellis/attiny/ide-1.6.x-boards-manager/package_damellis_attiny_index.json

    Adding board to Arduino IDE

  2. Choose your newly added board, in my case ATtiny44

    Choosing ATtiny44 board

  3. Choose your processor, same as before

    Choosing processor

  4. Select the clock. Because I am using an external 20 MHz oscillator, I chose "External 20 MHz"

    Selecting clock

  5. Because I want to do some serial monitoring on this board, I chose the best match for my FTDI, which is "/dev/cu.usbserial-A7032T9C"

    Selecting serial port

  6. The last step before compiling and burning was to select the programmer, in my case "USBasp"

    Selecting programmer


First test

Burning Neil's code "hello.ftdi.44.echo.c"

Here, instead of using the terminal and avrdude, I used the Arduino IDE, as it has a built-in serial monitor.

The results are as follows in the video


Program something else

Now I will try to program it to blink as I press the button.

  1. Determine the pin where the switch is connected

    PCB schematics

    After looking into the PCB schematics, I determined the button is connected to pin 10

  2. Determine the pin where the LED is connected

    After looking into the PCB schematics, I determined the LED is connected to pin 6

  3. Code

    In human language, I want something like this:

    1. Wait for the button
    2. If the button is pressed, blink the LED 3 times
    3. If not, go back to the beginning

The problem is I never really coded from scratch, only changed code, so that will be a challenge...


1st iteration

How I did it

I first tried to customize the Blink code from the Arduino IDE with my pin numbers. That did not work

Code without comments below:

void setup() {

  pinMode(LED_BUILTIN, OUTPUT);
  
}

void loop() {

  digitalWrite(LED_BUILTIN, HIGH);   
  delay(1000);                       
  digitalWrite(LED_BUILTIN, LOW);    
  delay(1000); 
  
}

The reason the first code did not work is that I was using code built for the Arduino platform, but on my own board. Arduino boards usually have a built-in LED connected to pin 13, not my board! The constant the Arduino platform uses for the built-in LED is:

LED_BUILTIN 

In the first code I tried to use that constant and failed. In the second code I found empirically that setting a number instead of the constant would turn the LED on.

The right code for the ATtiny44 would need to have the pin declared at the beginning with the following code:

const int ledPin =  07;

I realized that after trial and error. Now I understand that the pin number on the data sheet does not correspond to the Arduino pin number, which is also not the same as the Arduino platform constant. Of course, after all, this is a customized board, but that's the price you pay for being a noob!

So after empirically finding the right pin number, I programmed the board and got it to blink!

Here is the customized code without comments:

const int ledPin =  07;

void setup()    {

  pinMode(ledPin, OUTPUT);
  
}

void loop()     {

  digitalWrite(ledPin, HIGH);
  delay(1000);
  digitalWrite(ledPin, LOW);
  delay(1000);

}
        

No more empirically finding pins. Here is the ATtiny44 Arduino pinout diagram

ATtiny44 Arduino pinout diagram

In order to have the button recognized by the board, I needed to initialize it too, with the following code at the beginning of the sketch:

const int buttonPin = "the pin number here"

Next I had to declare a variable to hold the state of the pin, with the following code:

int buttonState = 0; 

The <0> in the above code could also be written as <LOW>, another constant in Arduino programming. See the Arduino website on constants.

After searching for the ATtiny44 Arduino pins, and with the right pin numbers in hand, I replaced the pins in the code and it worked! Here is the code. (Corrected later: the original file had if (buttonState, HIGH) and misplaced braces, so it did not compile.)

const int buttonPin = 3;
const int ledPin =  7;

int buttonState = 0; 

void setup() {

  pinMode(ledPin, OUTPUT);

  pinMode(buttonPin, INPUT);
}

void loop() {

  buttonState = digitalRead(buttonPin);

  if (buttonState == HIGH) {

  digitalWrite(ledPin, HIGH);
  } else {

    digitalWrite(ledPin, LOW);
  }
}
    
Schema

Because I lost this board many years ago, I can only show how to connect the programmer on a similar board, just like I did in Electronics Design week.

Programmer FabISP and USBasp

Here I show how I used the programmers in Output Devices. For another method without the Arduino IDE, check Embedded Programming

USBasp programmerUSBasp

Programming an ATtiny44 board with Arduino using a FabTinyISP or USBtinyISP that I made.

FabISP programmerFabISP

In order to change the behavior of the button, I simply replaced the constant <HIGH> with the constant <LOW> here:

if (buttonState == LOW) {

This way the logic of the code is inverted, meaning that when the button is released the LED is off.

Now I inverted when the LED turns on and off. Here is the code without comments:

const int buttonPin = 3;
const int ledPin =  7;

int buttonState = 0;

void setup() {

  pinMode(ledPin, OUTPUT);

  pinMode(buttonPin, INPUT);
}

void loop() {

  buttonState = digitalRead(buttonPin);

  if (buttonState == LOW) {

    digitalWrite(ledPin, HIGH);   
  }else {

    digitalWrite(ledPin, LOW);
    }
    }

Let's see some proof...


Blink 3 times

With a simple addition to the code, I changed the behaviour so it blinks 3 times.

Code for blinking 3 timesDownload the code (Corrected later: the loop only set the LED HIGH three times, so it never blinked; it now turns the LED off and waits between blinks. The picture shows the original code.)


Hacking the board

I was a bit annoyed that my board would not let me know when it was powered, so I decided to solder a red LED and a 22 Ohm resistor, so if I got the connections wrong I would know and switch them. (Note: at 5 V, 22 Ohms lets about 145 mA through the LED, far above the 20-30 mA it can take; about 150 Ohms would be right.)

Here's the before and after

PCB before modification Before

PCB after modification After

PCB after modification, with the red LED on After

← (Mar 18) Computer-controlled Machining · all weeks · Input Devices (Apr 01) →