Assignment 8: Embedded Programming

Tools and material used:
- Atmel Studio 7 software
- avrdude software
- Arduino software
- usbtiny_signed_8 driver
- ATtiny44A datasheet

Useful link to understand how bits are manipulated:
http://www.avrfreaks.net/forum/tut-c-bit-manipulation-aka-programming-101?name=PNphpBB2&file=viewtopic&t=37871

ATtiny44A:

ATtiny44A is a high performance, low power AVR® 8-bit microcontroller with for example the following features:
• Advanced RISC Architecture, in which most of the 120 instructions are executed in one clock cycle, and the
ATtiny24A/44A/84A achieves throughputs approaching 1 MIPS per MHz
• 32 general purpose working registers
• One 8-bit and One 16-bit Timer/Counter with Two PWM Channels, Each
• 10-bit ADC
• Internal and External Interrupt Sources
• 8-bit and 4-bit data I/O ports



At first I tested the newly built hardware with the example program hello.ftdi.44.echo.c. I opened the example in Atmel Studio and let it build solution.
File->New Project-> GCC C Executable Project -> Device Family ATtiny->ATtiny44A.
Paste code, or Atmel Studio opens .c-files automatically.
Build->Build Solution

Then I installed the usbtiny_signed_8 driver for the programmer made in assignment 4.

Then I programmed my board using my own usbtinyISP-programmer powered with the FTDI-cable, because the programmer doesn't provide power for the board to be programmed.

In the first time I used avrdude directly from the command line.
>  avrdude -p t44 -P usb -c  usbtiny -U flash:w:hello.hex


I checked the programming  of fuses from here. And then programmed the fuses too..
> avrdude -p t44 -P usb -c avrisp2 -U lfuse:w:0x5E:m


The operation looked like this:


Programming using command line felt uncomfortable. So, I installed avrdude to Atmel Studio as an external tool following this guide. The arguments box contains the same options as the original avrdude command and reference to the target directory and target name, -p t44 -P usb -c  usbtiny -U flash:w:$(TargetDir)$(TargetName).hex:i.
Now I can program my board directly from Atmel Studio.


I also tested the interrupt based version with Atmel Studio. At first I got a warning "F_CPU not defined for <util/delay.h>". This definition is in the makefile. So, I added it in the beginning of the code:
#define F_CPU 20000000UL
and the build succeeded.



With Jani I wrote a small program read the state of the button connected to I/O pin PA7 to control the state of the LED connected to I/O pin PB2. ATTiny44A has one 8-bit port (PORTA) and one 4-pin port (PORTB). The bits of a port are named PA7-PA0 and PB3-PB0, or PINA7-PINA0 or PINB3-PINB0. The direction of a pin is defined by registers DDRA and DDRB (Data Direction register). If a bit in the DDR is set, that pin is an output, otherwise the pin is an input. The state of an input pin is read with registers PINA and PINB. Data is written to an output pin using registers PORTA and PORTB.

/*
 * Fabhellobutton.c
 *
 * Created: 11.3.2016 8:46:20
 * Author : anttim
 */

#include <avr/io.h>

int main(void)
{
    DDRB |= (1 << PB2);        // with OR-operation set bit number 2 in DDRB register, make it output
    DDRA &= ~(1 << PA7);    // clear bit number 7 in DDRA register,  make it input, shift 1 left by seven, 10000000, AND with one's complement 01111111
   
    /* Replace with your application code */
    while (1)
    {
        if (PINA & (1 << PINA7)){    // with AND-operation check if bit 7 is  set
            PORTB |= (1 << PB2);    // with OR-operation set bit number 2 in PORTB
        } else  {
            PORTB &= ~(1 << PB2);    // with AND-operation clear bit number 2  in PORTB
        }
    }
}


Arduino:
Then I tested Arduino as a programming tool for ATtiny. Following this guide I installed ATtiny support in Arduino.
The link https://raw.githubusercontent.com/damellis/attiny/ide-1.6.x-boards-manager/package_damellis_attiny_index.json has to be added to Additional Boards Manager URLs.


Then open Boards Manager.


In boards manager scroll down until you find ATtiny, or use the Filter your search... box. Select attiny and press Install.


 I used an example File->Examples->02.Digital->Button in Arduino and edited the pins according to the guide to match my board.
Arduino ATtiny44/84
Arduino
VCC
1
14
GND
Pin 10
2
13
Pin 0 (Analog input 0, AREF
Pin 9
3
12
Pin 1 (Analog input 1)
Reset
4
11
Pin 2 (Analog input 2)
(PWM) Pin 8
5
10
Pin 3 (Analog input 3)
(PWM, Analog input 7) Pin 7
6
9
Pin 4 (Analog input 4, SCK)
(PWM, Analog input 6) Pin 6
7
8
Pin 5 (Analog input 5, PWM, MISO)

The picture below shows the Board, Processor, clock and Programmer settings. With the command Tools->Burn Bootloader the appropriate fuses in the ATtiny are programmed to accept programming with Arduino.


However, the upload process is not reliable. Most of the time I get an error like this:


But, when it succeeds, it looks like this:



Update:
After reprogramming my usbtiny programmer it started working perfectly in Arduino!

/*
  Button

 Turns on and off a light emitting diode(LED) connected to digital
 pin 8(=5 in ATTiny44A), when pressing a pushbutton attached to pin 7(=6 in ATtiny44A).
 
 created 2005
 by DojoDave <http://www.0j0.org>
 modified 30 Aug 2011
 by Tom Igoe

 This example code is in the public domain.

 http://www.arduino.cc/en/Tutorial/Button
 */

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

Conclusion: I have set up my programming environment so, that I can program my boards directly from Atmel Studio using avrdude as an external tool, or by using Arduino.

Files:
FabHelloButton
Arduino_LED_Button

Home