Fab Academy 2020
08. Embedded Programming

Assignment :

Jump to Group Assignment section below

Week 8 learning journey :

I am from an IT background, but this week lesson on “embedded programming” is a bit confusing and too overwhelming for me as I am new to the AVR microcontroller environment.

My week :
At first though, this week we do not need to fabricate anything in Fab Lab, it is mainly dealing with software programming. So I spend time doing a bit of reading, since this week assignment is about 'reading'.

The AVR tutorial listed on class site is very helpful too.

My instructor, Mr Steven, had passed to me a 'BBC micro:bit' to play around with over the weekend and it had been a useful device to get started in programming and understanding of more about programming a microcontroller.

The learning outcome are :
- Understand other type of microcontrollers and its architectures
- Understanding more on AVR microcontrollers - by reading the datasheet
- Try different ways to programming the AVR microcontroller
- Learning to program my hello world board to do something
- Learning C programming language
Misc :
- Learning to capture video and embed the (.mp4) video onto my website.
- Learn to store large video files on remotely file sharing server
- Learn to use a HTML formatter call "Prettify" to display my coding on the page


Introduction to embedded system, microprocessor and microcontroller

Computer :
A computer or a notebook contains a CPU (processor) and is the brain which runs the program and does all the computing (or making the decisions).

Computer Architecture (base on Intel® architecture) :
The CPU is connected to a 'hard drive' where the program is stored as well as the OS and the data. The harddrive is a 'non-volatile' storage media which means when the power is turn-off, the data would not be lose. When you run a program, the program is transfer from the harddrive via the 'system bus' and load onto the RAM chips (temporary storage for data/instrcutions) before sending to the CPU for processing/computing. RAM chips are volatile memory and lose the program/data when the power is switched odff.


Image from 'Computer Architecture Course Website'
@ https://www.doc.ic.ac.uk/~eedwards/compsys/


In the Fab Lab, we are using AVR microcontrollers which are low cost alternative to the costly Intel® CPU. Microcontrollers are commonly found in embedded systems, and popular among hobbyist and educational instituions. Why put a costly Intel® CPU into a vending machine, a microwave oven, or other consumer devices?

Microcontroller A microcontroller is essentially a tiny CPU with integrated memory or peripheral devices. The program is store in the non-volatile Flash memory inside the microcontroller. Microcontroller is widely used in embedded systems.
Microprocessor A microprocessor is also a tiny CPU but require (or use) external chips for memory and peripheral interfaces. A microprocessor is usually faster than a microcontroller and runs at a speed of 1GHz and above. Therefore it is used for the larger embedded system.
An embedded system is simply defined as any computing system contained within a product that is not described as a computer. Embedded system is designed to perform a specific task and cannot be use like a general computer to run other different computing tasks by other programs. Embedded systems are found in consumer electronic devices/gadgets (like microwave oven, digital timer, etc) to more complicated embedded systems (like gaming consoles) and even major factory and other industrial systems.

References :
- Understanding Embedded Systems - the basics (by electronics-notes.com)


AVR family of microcontrollers

We are "spiraling" back on the topic of electronics.

In Electronic Production week, I had fabricated a 'FabTinyISP' which used an ATTiny45 chip. In Electronic Design week, I had fabricated a 'hello world' board which used an ATTiny44A. The popular 'Arduino Uno' board is using an ATmega328P chip. All these chips belong to the AVR family of microcontrollers.

AVR Microcontroller (MCU)
ATTiny44A ATTiny45 ATmega328P
on my Hello World board
on FabTinyISP
on my Arduino Uno Rev3 board
Part No. ATTINY44A-SSU-ND Part No. ATTINY45V-10SU-ND Part No. ATMEGA328P-AU-ND
Datasheet Datasheet Datasheet
Core Processor : AVR Core Processor : AVR Core Processor : AVR
Speed : 20 MHz Speed : 10 Mhz Speed : 20 Mhz
Architecture : 8 bit Architecture : 8 bit Architecture : 8 bit
Number of I/O : 12 Number of I/O : 6 Number of I/O : 23
Flash : 4KB Flash : 4KB Flash: 32KB
SRAM : 256 SRAM : 256 SRAM : 2K
EEPROM Size : 256 EEPROM Size : 256 EEPROM Size : 1K
Price : SGD $1.10 (immediate stock) Price : SGD $2.23 (immediate stock) Price : SGD $2.05 (immediate stock)
More info on Digikey More info on Digikey More info on Digikey

Differences between three AVR microcontrollers are in 'bold' text.
Brief specs and price are from DigiKey website @ https://www.digikey.sg.



Reading ATTiny44A Datasheet

Before you use a microcontroller or any electronic component, the very first step is to have a look at the datasheet of that component. The datasheet is like an instruction manual and explain what the features available, what they does and how to (correctly) use them.

The ATTiny44A datasheet is a large manual of 286 pages long.

This is my first time reading a datasheet. I briefly fliped throught the pages and list down some of the interesting information available inside the datasheet :

Summary Page (pg 1) :
First page is usually has a general description or provide a summary of the features available. Here also mentioned the version number "Rev. 8183F–AVR–06/12", which the vendor might update from time to time (but very rare).

I compared the features between ATTiny44A and the ATTiny45. There are only slight differences as seen from the summary sheet of both AVR chips; like number of I/O pins and speed.

   

Pin Layout (pg 2 & 3) :
Page 2 shows the 'Pin Layout' diagram. There are 14 pins in total. But only 14 pins can be use by us for I/O.
My Hello World Connection Diagram : I had printed out this Pin Layout diagram with my own markings in red color for reference, showing pin 5 (PB2) is connected to the LED, and pin 6 (PA7) is connected to the Switch.



Architecture Overview (pg 8) :


Memories (pg 15) :
Three type of memories are available in the ATTiny44A.
- Flash memory (also known as 'Program Memory Space')
for ISP Programming and which store the program
- SRAM memory (also known as 'Data Memory Space')
- EEPROM memory (a separate data memory space)

The datasheet provide information on operating condition example below :
Note : During periods of low VCC, the EEPROM data can be corrupted because the supply voltage is too low for the CPU and the EEPROM to operate properly. These issues are the same as for board level systems using EEPROM, and the same design solutions should be applied.
Clock System (pg 24) :
- Calibrated Internal 8 MHz Oscillator
AVR chip comes with its own internal oscilators as clock source. Total there are three of them. The default clock source for ATTIny44A with a longest start-up time and running an initial system clock prescaling of 8, (8 Mhz divided by 8) resulting in 1 MHz system clock.

I/O Ports (pg 53) :
- switching Between Input and Output (pg 56)
- C Code Example (pg 57)
- Alternate Functions of Port A (PB0 to PA7 pins)
- Alternate Functions of Port B (PB0 to PB3 pins)

Pulse Width Modulator? (pg 68) :
- 8-bit Timer/Counter0 is a general purpose 8-bit Timer/Counter module, with two independent Output Compare Units, and with PWM support.??
- The 16-bit Timer/Counter unit allows accurate program execution timing (event management), wave generation, and signal timing measurement. (pg 85)
Timer/Counter Timing Diagrams (pg 101) :
Timer/Counter Prescaler (pg 113) :
- Prescaler Reset
- External Clock Source
Analog to Digital Converter (pg 132) :
Self-Programming the Flash (pg 152) :
Reading Fuse Bits from Firmware (pg 154) :
- Fuse Low Byte (FLB)
- Fuse High Byte (FHB)
- Fuse Extended Byte (FEB)
Preventing Flash Corruption (pg 156) :
- During periods of low VCC, the Flash program can be corrupted because the supply voltage is too low for the CPU and the Flash to operate properly. These issues are the same as for board level systems using the Flash, and the same design solutions should be applied.

A Flash program corruption can be caused by two situations when the voltage is too low. First, a regular write sequence to the Flash requires a minimum voltage to operate correctly. Secondly, the CPU itself can execute instructions incorrectly, if the supply voltage for executing instructions is too low.

Lock Bit (pg 158) :
This section set the security to protect your program in the Flash memory.
Example : LB Mode 1 : No memory lock feature enable
Example : LB Mode 2 : Programing to Flash and EEPROM is disabled
Example : LB Mode 3 : Programing and verification to Flash and EEPROM is disabled
Fuse Bytes (pg 159) :
This portion state the three types of fuses and how to set these three fuses. The three Fuse bytes are called HIGH, LOW and EXTENDED.

We had used the 'Make Fuses' command in week 5 to burn the 'fuses' for our FabISP. This section provide the detailed information. Eg. H-Fuse: bit no.6: Enable Serial program downloading (SPI)



Serial Programming (pg 162) :
As in week 7, we can briefly touched on how to program our 'hello world' board. To program the AVR chip, we need to map to these six pins (VCC, GND, MOSI, MISO, RESET and SCK). We program the AVR chip by using the ISP 6-pin (2x3) cable.


Electrical Characteristics (pg 172):
Here show the 'absolute maximum rating' that the AVR chip can withstand before being damaged.

Page 183 to 265 a whole bunch of charts?
These charts show the performance vs. various criterias...
Ordering Information (pg 270) :
Under 'ordering information', you will find a list of every variation of the ATTiny44A. Our chip used is 'ATtiny24A-SSU', package code is 14S1

Packaging Information for 14S1 (pg 273) :
Here it show the physical dimenstion in mm and in inches.

Table of Contents (pg 281 to 285)
~ End of datasheet ~





AVR programming

In order to program my hello world to do something, I need to write the code(program) for the AVR ATTiny44A microcontroller chip on the hello world board. There are a few ways to do so.

Ways to programming AVR chip :
(1) AVR-GCC (Avrdude) toolchain :

 
Source code
Compiler
(avr-gcc)
Flash Programming
(avrdude)
AVR chip starts
running code

(2) Arduino IDE (via a FabTinyISP Programmer)

 
Download ATtiny
microcontroller
support
for the
Arduino IDE
Write source
code in
Aurdino IDE
Aurdino
Bootloader
Flash Programming
(via avrdude)
AVR chip starts
running code

(3) Atmel Studio 7 IDE

Hardware require :
Programmer (ISP) : FabTinyISP
Target Board : Hello World board
Cable : ISP 6-pin (2x3) cable



Pre-programming checking :
Always run "avrdude -c usbtiny -p t44" in the 'command line' to ensure 'avrdude' can detect both ISP (programmer) and target board. Example, if 'hello world' board is not detectable, you can immediate do some troubleshooting using a multimeter or check the programmer.


(1) AVR programming using Arduino IDE :

I deccided to start with Arduino IDE (Integrated Development Environment), which is the easier to learn for a beginner like me. However I am using a non-Arduino ISP, but my FabTinyISP as the programmer.






If you are using Arduino IDE to program your board, you need to burn the bootloader to the target board. You only need to do it one time, which is the first time you programming the board.

"The bootloader is basically a .hex file that runs when you turn on the board. It is very similar to the BIOS that runs on your PC. It does two things. First, it looks around to see if the computer is trying to program it. If it is, it grabs the program from the computer and uploads it into the ICs memory (in a specific location so as not to overwrite the bootloader). That is why when you try to upload code, the Arduino IDE resets the chip. This basically turns the IC off and back on again so the bootloader can start running again. If the computer isn't trying to upload code, it tells the chip to run the code that's already stored in memory. Once it locates and runs your program, the Arduino continuously loops through the program and does so as long as the board has power." ~ Sparkfun: Installing an Arduino Bootloader.


My code :

/*
  Title: blink_led.ino
  Author: Lucas Lim
  Date Created: 19/03/2020
  Last Modified: 19/03/2020

  Purpose: Press the switch button to turn on LED
*/

// variable declaration
// const value will not change
const int led = 8;    // LED connected to PB2 of tiny44
const int sw = 7;     // switch connected to PA7 of tiny44
int buttonstate;

// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin LED as an output and pin SW as an input.
  pinMode(led, OUTPUT);
  pinMode(sw, INPUT);
}

// the loop function runs over and over again forever
void loop() {
  /*
  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);               // wait for a second
  */
  buttonstate = digitalRead(sw);
  if (buttonstate == HIGH)
    digitalWrite(led, LOW);   // turn LED on
  else
    digitalWrite(led, HIGH);  // turn LED off
}
    

Note :