9. Embedded programming

Mr. Rudolph has a super helpful Google Slide presentation going over the basics of c on TinkerCAD, which I used to help me understand how to program the LED to blink / turn on when a button is pressed.

I used the board from week 7 (the Hello World Board) and programmed it.

Checking the LED

Week 7’s assignment did not involve the LED on my board at all. When I tried to run the example blink code from Arduino onto my board, it didn’t blink. I checked the LED with a multimeter, and it didn’t actually light up, so realized that the LED I was using was burnt out. I switched out this LED for another one, making sure that it was a functional LED.

Arduino

I read through the ATtiny44 datasheet after reading through this Sparkfun tutorial on how to read a datasheet. I’ve skimmed datasheets before, but I never really know the setup/ordering of a datasheet until now. Reading this guide was super helpful in just understanding where to find certain pieces of information in the datasheet.

In Arduino, I programmed my board to turn on the LED when I pressed the button.

The original Arduino code:

I changed the pins in the example code to match with the pins that I was using on the ATtiny44.

My Arduino code:

const int buttonPin = 3;     // the number of the pushbutton pin
const int ledPin =  7;      // 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 == LOW) {
    // turn LED off:
    digitalWrite(ledPin, LOW);
  } else {
    // turn LED high:
    digitalWrite(ledPin, HIGH);
  }
}

Turning the LED off with the button:

Download my Arduino code

Atmel / C

Then, I used C’s bitshifting in Atmel Studio to also code my LED to turn on when a button is pressed. Wrapping my head around 8 register bitshifting was initially difficult, but I definitely understand it much better now.

I had never used Atmel or before, so even just creating a document was a new process for me.

The Atmel homepage looks like this:

I created a new document and called it “EchoButtonBlink”:

I made sure to change my device to the ATtiny 44:

I also made sure to upload using the FabTinyISP, the programmer that we’ve been using / made in week 5.

Uploaded code:

Code

Mr. Rudolph helped me write the Atmel code:

/*
 * GccApplication2.c
 *
 * Created: 3/28/2019 8:46:22 AM
 * Author : engineering
 */ 

#define F_CPU 2000000 // External crystal resonator 

#include <avr/io.h>


int main(void)
{
    DDRA |= (1<<DDA7);
    DDRA &= ~(1<<DDA3); 
    PORTA |= (1<<PA3);
    //PORTA |= (1<<PA7);


    /* Replace with your application code */
    while (1) 
    {
        if (!(PINA & (1<<PA3)))
        {
            PORTA |= 1<<PA7;
        }
        else
        {
            PORTA &= ~ (1<<PA7);
        }
    }
}

Understanding the code

I referenced this page on C operators and this Quora forum to learn about the operators and their meanings.

Operand Description
& and
I or
<< bitshift left
~ not, flips the bits, bitwise operator
! not, logic operator

I also referenced this AVR Freaks forum to better understand the C syntax.

  • PORTx controls how much voltage a pin receives
  • DDRx determines the direction of a pin (input or output)
  • PINx reads which voltage levels are low or high

x can be either A, B, C, or D, depending on which one you want to use and what type of AVR controller is being used. Some AVR controllers only have A or B, or it might only have half of a register.

I also referenced the ATtiny44 datasheet, using the datasheet-reading skills I learned earlier in the week.

Bitshifting

If I code this:

(1<<PA3)

It reads as:

00001000

It takes the 1 and moves it 3 places from the left, since I was using the bitshift left operand.


This Arduino forum helped a lot and put things in perspective. For example ~ “flips the bits.” Take 00010000. If you “not” it, it changes from:

00010000

to

11101111


    DDRA |= (1<<DDA7);

Here, this line means that the bits that are high in DDRA or DDA7 will remain high.

    DDRA &= ~(1<<DDA3);

Similarly, this line of code means that only the bits shared between DDRA and DDA3 will remain high (or at 1). The only difference between this line and the previous line is the & and the |.

Then, everything within the while loop continuously repeats.

        if (!(PINA & (1<<PA3)))
        {
            PORTA |= 1<<PA7;
        }
        else
        {
            PORTA &= ~ (1<<PA7);
        }

This is the if statement. The first part of the if statement describes what happens when the button is pressed, and the second half (after else) describes what to do when not pressed / in its normal state. Using PORTA changes the voltage that is given to the LED on pin 7, effectively turning the LED on or off.


Turning the LED on with the button:

Download my c code

Download my Atmel code

Group Work

We were supposed to

compare the performance and development workflows for other architectures

for this week’s group project.

Raspberry Pi

None of us really had much experience with anything other than Arduino, so we first went with something really simple: learning how to use a Raspberry Pi. Kai actually had some experience with Pi because he did a personal project using it to stream a video on Youtube. He also brought in his Raspberry Pi 3 Model B with Raspbian Jessie Lite and OS already installed, thanks to Kai. Without Kai’s already setup RPi, we’d have to follow this setup tutorial.

Here are some specs from the RPi website (linked above):

  • Quad Core 1.2GHz Broadcom BCM2837 64bit CPU
  • 1GB RAM
  • BCM43438 wireless LAN and Bluetooth Low Energy (BLE) on board
  • 100 Base Ethernet
  • 40-pin extended GPIO
  • 4 USB 2 ports
  • 4 Pole stereo output and composite video port
  • Full size HDMI
  • CSI camera port for connecting a Raspberry Pi camera
  • DSI display port for connecting a Raspberry Pi touchscreen display
  • Micro SD port for loading your operating system and storing data
  • Upgraded switched Micro USB power source up to 2.5A

We plugged everything in:

First, we just wanted to do something simple: get “Hello” to print.

So, we created our code document by typing sudo nano test_code.py. sudo gives you admin priviledges (necessary to write code), nano is the RPi code editor location, the name of the document was “test_code.py”, and it’s in Python (hence the .py). The first time that we called upon “test_code.py”, it creates a file with this name. The second time you type in this code, it would just open the already existing document.

Within Nano, we typed in print("Hello"). This code just told it to print out “Hello”. To save the document, we pressed Y. Then, to exit, we typed Command+X.

Then, we typed in python test_code.py. This ran the code that we just typed, and it spit this out:

We played around with it a bit:

Then, we followed this tutorial to get an LED to blink with our RPi.

Here’s our setup:

We made sure to check the RPi 3B pinout as we were plugging everything in.

Here’s our code:

We had to use the Import RPi.GPIO as GPIO in order to use GPIO pins in Python code. GPIO means “general purpose in and out”.

The LED blinking:

We also used some other commands…

sudo Raspi-config opens the configuration menu where various settings can be changed

sudo apt-get update searches the server for an update (since this RPi can connect to wifi)

Script transcript.log begins a transcript of the code entered. The info is then stored in a file called “transcript.log”. The startup proces of transcript.log is shown below:

Then, to show each line of code that has been typed, you use nano transcript.log. This opens up Nano, as before, and shows you the code that’s been typed.