Skip to content

9. Embedded programming

This week I compared the performance and development workflows for other architectures, read a microcontroller data sheet, programed my board to do something, with as many different programming languages, and programming environments as possible.

ATtiny Microcontroller Data Sheet

reference

In hopes of getting closer to understanding the world of embedded programming, I spent alot of time trying to ‘decode’ the data sheet.

This content is boring.. read at your own risk.... Link to my full notes on the data sheet


To begin the ATiny44 Data Sheet is a document made up of 286 pages, divided in 27 chapters. Each chapter is clearly labled and divided between its key specifications. Chapters 1-3 give general overview of the microcontroller including the pin configurations and code examples.

Pin Configurations

VCC=Supply voltage GND=Ground

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.

Block Diagram

When researching RISC architecture I found it easiest to understand when comparing to a different architecture:

From my observation the benefits of RISC design simplicity are a smaller chip, smaller pin count, and very low power consumption.

Code examples

The data sheet also contains simple code examples that briefly show how to use various parts of the device.

Assembly Code vs. C Code

  • Portability: Portability is the most important factor in language selection. If the source code is not portable then we have to pay a lot of time.

  • Maintainability: Language should be easy to understand otherwise it will take a huge amount time for small changes.

  • Availability: Compiler and IDE should be easily available in the market and most important thing is that it should be cheap.

  • Efficiency: Language has a good efficiency and bug rate should be less.

  • Development time: Take less amount of time to develop the project. reference

From what I can tell (I am no expert) one of the biggest advantages of C language is it provides portability and does not depend on the specific platform. The code which was written in C could be easily reused on a different platform. Whereas, Assembly does not provide the portability and source code specific to a processor because assembly instruction depends on the processor architecture.

Taking a closer look: Chapters 4 - 19, cover all the operating and programming aspects of the microcontroller. Chapters 20-25 contain general information of the microcontrollers properties, summarizes registers and instruction sets and offers information on ordering and packaging of the microcontroller. Chapter 26 shows errata of the datasheet (ATiny24,ATiny44,ATiny84) and chapter 27 presents the datasheet revision history.

I found the connection schematic examples to be a very helpful reference when understanding how to create my own schematics.


Programming

First I connected my FabISP (bottom board) to my computer via USB cable and connected to Hello FTDI board (top board) via 6-pin header cable.

Next I downloaded and booted the Arduino IDE and followed a Attiny Programming Tutorial (linked at the bottom of my page) to load in the ATtiny microcontroller board files.

ATtiny

First I installed the ATtiny support using the built-in boards manager After I open “Preferences” in Arduino software and selected “Additional Boards Manager URLs”

Next I pasted this URl

Then I opened the boards manager in the “Tools > Board” menu, scrolled to the bottom of the list, selected the “ATtiny” entry.

After this step an install button should appear, and I clicked it. - Lastly, I close dthe boards manager and I now see see an entry for ATtiny in the “Tools > Board” menu.

Using Arduino IDE I selected ATtiny24/44/84 board

Then I selecting ATtiny44 for the processor.

Next, I selected external 20 MHz clock, to match the clock I used on my board.

Finishing by Selecting USBtinyISP from the Tools > Programmer menu

Programming the ATtiny

I really appreciate the community and resources Arduino IDE provides. As a beginner I found the example programs to be very helpful. To program my board I used the blink code. This was very simple: first I navigated to file > examples > basics > blink and simply changed the pin numbers to match those on my board. Before sending the code I selected “Burn Bootloader” from the “Tools” menu. Once I successfully burned the bootloader I selected the check mark symbol in the top left corner to “verify” the sketch, finished by clicking the arrow symbol to the right of “verify” to “upload.”

Arduino ATTINY44 Pin-Outs

I used this diagram to translate AMTEL’s ATiny44 pinouts to Arduino’s.

int Led1=7;
int Button=3;

void setup() {

  pinMode(Led1, OUTPUT);
  pinMode(Button, INPUT);

}

// the loop function runs over and over again forever
void loop() {
  if (digitalRead(Button)==0){
  digitalWrite(Led1, HIGH);   
  delay(50);                  
  digitalWrite(Led1, LOW);    
  delay(50);    
  }
}                      

this code is written in C language using Ubuntu

The test was unsuccessful and my LED did not blink. I used a multimeter to check the continuity and voltage and everything seemed good. Then I checked the diodes and discovered it was not properly measuring so I removed the solder and the component and noticed there was an un- needed line.

After cutting out the line and re-soldering the components, the LED blinked!!!!

See week 16 where I program with python language.

Learning C++

codecademy

input:

#include <iostream>

int main()
{

  std::cout << "Hello World!\n";

}

output:

Hello World!

C++ program structure:

std::cout << "Hello World!\n";
  • std::cout is the “character output stream”. It is pronounced “see-out”.

  • << is an operator that comes right after it.

  • “Hello World!\n” is what’s being outputted here. You need double quotes around text. The \n is a special character that indicates a new line.

  • ; is a punctuation that tells the computer that you are at the end of a statement. It is similar to a period in a sentence.

output:

Hello World!

You can also output multiple lines by adding more std::cout statements:

std::cout << "Hello\n";
std::cout << "Goodbye\n";

output:

Hello
Goodbye

input:

#include <iostream>

int main()
{

  std::cout << "       1\n";
  std::cout << "     2 3\n";
  std::cout << "   4 5 6\n";
  std::cout << "7 8 9 10\n";

}

output:

    1
    2 3
    4 5 6
    7 8 9 10

CODE->SAVE->COMPILE->EXECUTE

C++ is a compiled language. That means that to get a C++ program to run, you must first translate it from a human-readable form to something a machine can “understand.” That translation is done by a program called a compiler.

  • Code — writing the program
  • Save — saving the program
  • Compile — compiling via the terminal
  • Execute — executing via the terminal

Compile & Execute

Compile: A computer can only understand machine code. A compiler can translate the C++ programs that we write into machine code. To compile a file, you need to type g++ followed by the file name in the terminal:

g++ hello.cpp

The compiler will then translate the C++ program hello.cpp and create a machine code file called a.out.

Execute: To execute the new machine code file, all you need to do is type ./ and the machine code file name in the terminal:

./a.out

The executable file will then be loaded to computer memory and the computer’s CPU (Central Processing Unit) executes the program one instruction at a time.

Compile and Execute (Naming Executables)

Compile: Sometimes when you compile, you want to give the output executable file a specific name. To do so, the compile command is slightly different. You still need to write g++ and the file name in the terminal. After that, there should be -o and then the name that you want to give to the executable file:

g++ hello.cpp -o hello

The compiler will then translate the C++ program hello.cpp and create a machine code file called hello.

Execute: To execute the new machine code file, all you need to do is type ./ and the machine code file name in the terminal:

./hello

The executable file will then be loaded to computer memory and the computer’s CPU will execute the program one instruction at a time.

Comments

As you write a C++ program, you can write comments in the code that the compiler will ignore as our program runs. These comments exist just for human readers.

Comments can explain what the code is doing, leave instructions for developers using the code, or add any other useful annotations.

There are two types of code comments in C++:

A single line comment will comment out a single line and is denoted with two forward slashes // preceding it:

// Prints "hi!" to the terminal
std::cout << "hi!";

You can also use a single line comment after a line of code:

std::cout << "hi!";  // Prints "hi!"

A multi-line comment will comment out multiple lines and is denoted with / to begin the comment, and / to end the comment: /*

/* This is all commented.
std::cout << "hi!";
None of this is going to run! */

Variables

Introduction to Variables

The “Hello World!” program just writes to the screen. It does not read anything, calculate anything, or allow for user input.

To read something from the keyboard, you first need somewhere in the computer’s memory to store data. There is where variables come in.

A variable is simply a name that represents a particular piece of your computer’s memory that has been set aside for you to store, retrieve, and use data.

C++’s basic data types (some):

  • int: integer numbers
  • double: floating point numbers
  • char: individual characters
  • string: sequence of characters
  • bool: true/false values

Every variable has a type, which represents the kind of information you can store inside of it. It tells your compiler how much memory to set aside for the variable and it defines exactly what you can do with the variable.

Step 1: Declare a Variable

Every variable in C++ must be declared before it can be used

Before you can use a variable, you must declare, or create, it. To declare a variable, you need to provide two things:

A type for the variable. A name for the variable. So to declare an integer variable called score, we need to write:

int score;
  • The int is the type of the variable.
  • The score is the name of the variable.
  • The ; is how we end a statement.

In C++, variable names consist only of upper/lower case letters, digits, and/or underscores.

Once you have declared an int variable called score, to set it to 0, we can simply write:

score = 0;
  • The score is the name of the variable.
  • The = indicates assignment.
  • The 0 is the value you want to store inside the variable.

In C++, a single equal sign = does not really mean “equal”. It means “assign”. In the code above, we are assigning the score variable a value of 0.

You can both declare and assign a value to a variable in a single initialization statement.

Suppose you have these two lines:

// Declare a variable
int score;

// Initialize a variable
score = 0;

You can actually combine these two lines into a single line of code:

int score = 0;

This means we are declaring an integer called score and setting it equal to 0.

Arithmetic Operators

Computers are incredible at doing calculations. Now that you have declared variables, let’s use them and arithmetic operators to calculate things!

Here are some arithmetic operators:

  • addition
  • subtraction
  • multiplication / division % modulo (divides and gives the remainder)
int score = 0;
// score is 0

score = 4 + 2;
// it is now 6

score = 4 - 2;
// it is now 2

score = 4 * 2;
// it is now 8

score = 4 / 2;
// and now 2

score = 5 % 2;
// and now 1

The order of operations can be specified using parentheses. For example, the use of parentheses in score = 5 * (4 + 3) sets score equal to 5 * 7 rather than 20 + 3.

// You can output the value by adding this code underneath:

std::cout << score << "\n";

Chaining

Now that you have outputted a variable and have also outputted things using multiple couts. Let’s take a closer look at cout again.

input:

int age = 28;

std::cout << "Hello, I am ";
std::cout << age;
std::cout << " years old\n";
It will output:

output:

Hello, I am 28 years old

Notice how we use quotes around the characters in “Hello, I am ” but not in age.

  • You use quotes when we want a literal string.
  • You don’t use quotes when we refer to the value of something with a name (like a variable). So now, is it possible to write the cout statements within a single line?

Yep! You can use multiple << operators to chain the things you want to output.

For the same code above you can also do: This is chaining

int age = 28;

std::cout << "Hello, I am " << age << " years old\n";

User input

Another way to assign a value to a variable is through user input. A lot of times, we want the user of the program to enter information for the program.

We have cout for output, and there is something called cin is used for input!

std::cout << "Enter your password: ";
std::cin >> password;

The name cin refers to the standard input stream (pronounced “see-in”, for character input). The second operand of the >> operator (“get from”) specifies where that input goes.

Example: Temperature Part 1

Converting a temperature from Fahrenheit (F) to Celsius (C).

The formula is the following:

C = (F - 32) / 1.8C=(F−32)/1.8

Temperature Part 2

This time, instead of giving tempf a value of the current temperature in New York:

tempf = 83;

Ask the user what the temperature is using cin!

The body mass index (BMI) is commonly used by health and nutrition professionals to estimate human body fat in populations.

It is computed by taking the individual’s weight in kilograms (kg) and dividing it by the square of their height in meters (m²):

bmi = (weight)/(height^2)

CONDITIONALS & LOGIC

MICROCONTROLLER FEATURES

Building an ALU from Scratch

Assembly or C?

Arduino_Linux

Attiny_Arduino

Attiny Programming Tutorial

This week was defiantly one of the more challenging ones for me, programming in specific. The example sketches Arduino IDE provides are defiantly helpful, however I hope in the future to be able to write my own programs or at least know what to look for and compile my own. My instructor Luciano made a great analogy comparing writing programs to Frankenstein; through investigating existing code and manipulation it is possible to create new programs. I really liked this way of looking at writing program and I thought it simplified the process, however in order to compile new programs using existing code, I still need to be able to know what I am looking for and how to use what I find.


Class Index