9. Embedded programming

  1. Group assignment http://fabacademy.org/2019/labs/lakazlab/assignments/week09/

    Compare the performance and development workflows for other architectures

  2. Individual assignment

    Read a microcontroller data sheet. Program your board to do something, with as many different programming languages and programming environments as possible.

Objectives

  • [x] Documented what you learned from reading a microcontroller datasheet.
  • [x] What questions do you have? What would you like to learn more about?
  • [x] Programmed your board
  • [x] Described the programming process/es you used
  • [x] Included your code
  • [x] Add link to the group assignment.
  • [x] Screenshots and description of the information you extracted from the micro-controller’s data sheet.
  • [x] Describe the programming of the board and all the actions you programmed the board to perform
  • [x] Photos or videos of your board working.
  • [x] Include questions you may have about the board or the microcontroller? What would you like to learn more about?
  • [x] Include all the files you created for download.

Reading the Microcontroller Datasheet

I will research the Atmel ATtiny45A Microcontroller datasheet for relevant information within our scope. The datasheet can be downloaded here ATtiny24A/44A/84A - Atmel

The important stuff:

The pin layout that the ATtiny shares between the 24, 44 and 84 and the 2,4 & 8 stands for the amount of kb’s memory on the chip. That aside they are exactly the same.

Pinout ATtiny24/44/84

ATtiny24/44/84 Pinout

Pin description

Block Diagram

Block Diagram

Memory types

Signals & Ovverrides

Alternate Functions

Summary of the ATtiny

  • Pin Count: 14
  • Max. Operating Freq. (MHz): 20 MHz
  • Max I/O Pins: 12
  • TWI (I2C): 1
  • Self Program Memory: Yes
  • External Bus Interface: 0
  • Temp. Range (deg C): -40 to 85
  • Operating Voltage (Vcc): 1.8 to 5.5
  • Timers: 2
  • PWM Channels: 4
  • PB3:PB0 is a 4-bit bi-directional I/O port with internal pull-up resistors
  • RESET (PB3); If given low level to this pin for longer than the minimum pulse length, it will generate a reset.
  • PA7:PA0 is a 8-bit bi-directional I/O port with internal pull-up resistors. These pins have alternate functions as analog inputs for the ADC, analog comparator, timer/counter, SPI and pin change interrupt.
  • CPU(Central Processing Unit) will ensure correct program execution by accessing memories, performing calculations, controlling peripherals, and handling interrupts.

There are three types of memory.
  • Flash stores the compiled program even when the power is off.
  • SRAM saves temporarily variables while calculating.
  • EEPROM is for data storage.
  • Analog-Digital converters \ ATtiny44A features successive approximation Analog-to-Digital Converter (ADC). It generates a 10-bit result which is presented in the ADC Data Registers, ADCH and ADCL. ADCL presents only the low byte of the ADC conversion result and ADCH presents only the high byte. ADCL is read first, then ADCH. When ADCH is read, ADC access to the ADCH and ADCL Registers is re-enabled and a new result is provided.
  • RESET (PB5): Reset input. If the pin drops to zero (0) volts for longerthan a pulse length it will generate a reset so MCU will be in programming mode. Thus allowing firmware uploads.

Programming the Microcontroller

Programming the Microcontroller can be done with an ISP (In-system programming), the FabISP we build in week 4 of Fabacademy. It is performed using only four lines, data is transferred through two lines only and is shifted bit by bit.

  • MOSI (data input) line, with a clock cycle between each bit and the next (on the SCK (clock input) line).
  • MISO (data output) line is used for reading and for code verification, it is only used to output the code from the FLASH memory of the microcontroller.
  • The RESET pin to activate the serial Programming, bring the chip into programming mode, It is thus used to enable the three pins (MOSI, MISO and SCK) on the microcotroller.

Serial Programming

Serial Programming

Serial Programming on Datasheet

Programming Pins

Programming with Arduino IDE

First i will use the Arduino IDE with the FabISP we built in week 5 to program the ATtiny on the Hello Board.

The steps taken are:ows the step-by-step using Arduino IDE.

  • Open your Arduino IDE (or install it from Arduino IDE)
  • Install ATtiny support into Arduino at ‘File> Preferences> Additional Boards Manager URLs:’. Paste https://raw.githubusercontent.com/damellis/attiny/ide-1.6.x-boards-manager/package_damellis_attiny_index.json.

  • Next go to tool > Boards > Board Manager > Search for Attiny and Install the drivers
  • Connect the FabISP straight to the ISP port Hello board.
  • VCC _ VCC ,
  • GND _ GND ,
  • RST _ RST ,SCK _ SCK ,
  • MOSI _ MOSI ,
  • MISO _ MISO ,
  • Burn the bootloader \ The bootloader It is the firmware runs when the micro-controller board starts up, or after the button is pressed, kinda like the BIOS on a PC. When the computer is not uploading any sketch/program, the chip will run the program stored in memory. That program will than continuously run so as long as the board is powered. \ To burn the bootloader in Arduino IDE do:
    • Go to >>Tools>>Board>> and select ATtiny
    • Go to >>Tools>>Processor>>ATtiny44
    • Go to >>Tools>Clock>>20 MHz (external)
    • Go to >>Tools>>Programmer>>USBtinyISP

  • Burn the bootloader >>Tools>>Burn Bootloader
  • When finished and succeeded, the following message shall be seen.

Check the message log for errors. We experienced a lot of “Signature mismatch” or “Device not configured” errors because of faulty jumper cables between the FabISP and the ISP ports.

Often changing to better cables or applying some pressure on the cables near the headers solves this problem. If that doesn’t solve it measure the voltage at the reset jumper to observe if the voltage there really drops below 1V shortly. If not there might also be a current leakage in your reset circuit or even in the whole board.

Upload your code

Finally it’s time to load your sketch of choice and upload it to your ATtiny. In this case i just uploaded a simple button and led blink sketch to the board.

// FabAcademy week 9 - test sketch //

// constants won't change. They're used here to set pin numbers:
const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  8;      // 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);
  }
}

See it working here

Programming with CrossStudio for AVR

Since there’s no Mac version of Atmel Studio i will try another app for Mac that promises the same functionality. CrossStudio for AVR from Rowley Associates is commercial software and downloadable here https://www.rowley.co.uk/avr/index.htm

For this test i opted in for a 30 day trial license just to see how it works.

Select your support packages>> next then it automatically adds the support packages for your MCU

Trying to build my first project i realized they have NO FabISP support.

After intensive Googling and support Q’s i still have no clarification or solution so i will park this route for now.

PlatformIO / Atom

Another development route is actually one of my favorite IDE’s from the Windows PC age Atom running PlatformIO plugin on it. So lets try that

Install Atom

$ brew cask install atom

Open the plugings and look for PlatformIO go to >> Settings >> Install Package and search for PLatformio IDE

Do the required settings in Platformio.ini file in each project folder. In our case we added the Atmel config to it

Lets go bling with blink!

CrossPack Command-line compilation and flashing

Atmel studio uses avr-gcc and arm-none-eabi-gcc (and presumably an avr32 gcc) to do the actual compilation, all of which are available for other platforms. As are downloading tools for their bootloaders, and ISP and SWD/JTAG tools like avrdude, OpenOCD, etc.

Also the Atmel Software Framework of chip-specific source code libraries is available as a huge zip file download independent of Atmel studio.

Installing CrossPack for Mac gives you the full set of tools mentioned less the UI.

Install by cloning from git https://github.com/obdev/CrossPack-AVR

$ git clone https://github.com/obdev/CrossPack-AVR

$ cd CrossPack-AVR

$ ./mkdist.sh

Questions i have

  • How to make my own special purpose Micro Controller
  • Communicate over universal bus between microcontrollers
  • Use them in a SCADA-like industrial automation environment

Downloads

Week 9 files

Containing Attiny test code

References

No Atmel Studio for mac https://electronics.stackexchange.com/questions/387302/atmel-studio-for-mac

Cross Studio for Atmel https://www.rowley.co.uk/avr/index.htm