<<<<<<<=Go Back To Home <<<<<=Previous Week || Next Week>>>>>

Week 9 : Embedded Programming



Objectives

  • Read ATtiny44 microcontroller data sheet.
  • Program your board to do something, with as many different programming languages and programming environments as possible.
  • ATtiny 44 Board

    ATtiny 44 Data Sheet

    ATtinyPinouts14 image

    The chip has two ports , PORT A and PORT B. Port A has 8 pins and Port B has 4 pins. In my connection led is connected to PIN 10(PA3) and switch is connected to PIN 11 (PA2).

    Attiny44pins.
    This image above really explains the Pin Structure, taken from Ines Ariza Page

    Few Characteristics of ATtiny44 are :

  • Low-power CMOS 8-bit microcontrollers
  • 8K byte of In-System Programmable Flash, 128/256/512 bytes EEPROM, 128/256/512 bytes SRAM, 12 general purpose I/O lines, 32 general purpose working registers
  • Attiny 44 is based on the AVR enhanced RISC architecture.
  • It could achieve nearly 1 MIPS per MHz
  • Ten times faster than Conventional CISC microcontrollers.
  • Architectural Overview

    architecture image

    In order to maximize performance and parallelism, the AVR uses a Harvard architecture with separate memories and buses for program and data. Instructions in the Program memory are executed with a single level pipelining. While one instruction is being executed, the next instruction is pre-fetched from the Program memory. This concept enables instructions to be executed in every clock cycle. The Program memory is In-System Reprogrammable Flash memory.

    Changes to my board

    This week we have to try different programs on the ATtiny 44 microcontroller involving the LED and the Switch in the board. I first tried the LED blink program with my USBtiny. But the LED was not blinking, i was afraid that there is a problem with the circuit. The previous week of Electronic Design, it took a lot of time on hacking my board to make it work.

    echoboardhacked image board image

    So i sit with my board for hours to solve the issue. My circuit was okay and all the connections were correct. Then I tried changing my jumper wires to lookout for voltage leaks.

    board image

    Atlast i found the problem, actually the LED blinking program was working and the LED was blinking even from the start, only thing is that the LED was blinking with very little voltage to be exact just 1.2 V. So out of 5V only 1V is only gets to my LED, and thats when i noticed that i had an orange LED. Someone had placed an orange LED between the Red Leds in the pack at our storing place and i have placed a 499 ohm Resistor corresponding to the Red Led. So now as i have an orange LED, i have to change my Resistor value, i choose 100 ohm Resistor this time. And atlast my Led was blinking, and i felt happy and relief.

    Embedded Programming

  • Basics
  • Using Arduino
  • C Programming Using AVR GCC
  • Atmel Studio
  • Assembly Language
  • Some Basics I learned

    Some C operators

  • | is bitwise OR.
    Eg. 10100111 | 11000101 = 11100111
  • & is bitwise AND.
    Eg. 10100111 & 11000101 = 10000101
  • ~ is bitwise NOT.
    Eg. ~10100110 = 01011001
  • << is shift left and >> is shift right.
  • I/O

  • There are 3 registers that control the I/O pins: DDR, PORT and PIN.
  • Each port has its own registers. Hence, port A has registers DDRA, PORTA, PINA; port B has registers DDRB, PORTB, PINB; and so on.
  • DDR, PORT and PIN serve different functions.
  • DDR (Data Direction Register)

  • DDR decides whether the pins of a port are input pins or output pins.
  • If the pin is input, then the voltage at that pin is undecided until an external voltage is applied.
  • If the pin is output, then the voltage at that pin is fixed to a particular value (5V or 0).
  • Setting Register Values

  • DDR is an 8 bit register. Each bit corresponds to a particular pin on the associated port.
  • DDRA

    Interpretation of DDR values

  • If a bit on the DDR register is 0, then the corresponding pin on the associated port is set as input.
  • Similarly, if the bit is 1, then the pin is set as output.
  • PORT Register

  • PORT is also an 8 bit register. The bits on the PORT register correspond to the pins of the associated port in the same manner as in the case of the DDR register.
  • PORT is used to set the output value.
  • If the pin is set as output, then a PORT value of 1 will set voltage at that pin to 5V. If PORT value is 0, then voltage is set to 0.
  • PIN Register

  • PIN is a register whose value can be read, but cannot be changed inside the program.
  • It gives the value of the actual voltage at a particular pin. 5V corresponds to 1, and 0 corresponds to 0.
  • DDRAPIN

    LED Blink Program using C

    I have my LED connected to PA3 and Switch to PA2

    ledblink

    Download Program File

    Programming using Arduino

    First download and install Arduino IDE

    Now change the preferences and add the below link to include ATtiny details in the additional board manager library

    https://raw.githubusercontent.com/damellis/attiny/ide-1.6.x-boards-manager/package_damellis_attiny_index.json

    boardselect

    Go to the Tools> Boards> Boards Manager and search for attiny, and download the ATtiny 44 files to the library

    boardselect

    After installing the Attiny files, go to the Tools> Boards > and select Attiny 44 from the list

    boardselect

    We have to now provide other details of our Board, in Tools select the programmer as USBtiny , the processor as ATtiny44 and the clock as External 20 Hz

    programmer

    clock

    Now verify your program, then compile it and select upload the program using Programmer

    programmerupload

    LED Blinking Program

    ledblink

    Download Program File

    See the video below


    Switch and LED Program

    switchled image

    Download Program File

    See the below video

    Download Video

    Programming using AVR GCC Compiler

    Open the terminal from your C file folder and type the following commands with your program name preciding the .c , .o , .elf and .hex

    avr-gcc -g -Os -mmcu=attiny44a -c abyled.c
    avr-gcc -g -mmcu=attiny44a -o abyled.elf abyled.o
    avr-objcopy -j .text -j .data -O ihex abyled.elf abyled.hex

    program image

    folder image

    Test image

    Programming C in Atmel Studio

    First download and install Atmel Studio Atmel Studio

    Now go to Tools > Extenal Tools and add the USBTiny Debug by filling the following

  • Title USBTiny ISP Debug.
  • Command avrdude.exe
  • Arguments -c usbtiny -p attiny44 -U flash:w:$(ProjectDir)Debug\$(TargetName).hex:i
  • Open a new project and select the preferences

    newproject

    Then add the Attiny 44

    attiny44

    Type your program in the tab

    Program for toggle switch

    #include < avr/io.h> #define F_CPU 20000000 #include < util/delay.h> int main(void) { DDRA =0b00000100; PORTA =0b00001000; while(1) { if(!(PINA & (1<<3)) { PORTA |= (1<<2); } else { PORTA &= (0<<2); } } }

    Download Program File

    Now go to Build > Build Solutions to Compile and get the Hex file

    build

    Go to Tools > USBTiny Debug and Program the board

    debug

    Assembly Language Programming

    Toggle Switch and LED

    ;Author Aby Michael 20-03-2018
    ;Led is connected to PortA 3
    ;Switch is connected to PORTA 2
    .device attiny44
    .org 0
    sbi DDRA,3 ;Set bit , 3rd bit of DDRA is set , so PA3 is output
    cbi DDRA,2 ;Clear bit , 2nd bit of DDRA is set , so PA2 is input
    sbi PORTA, 2 ; the input bit is internally pulled up
    main:
    SBIC PINA,2 ;
    cbi PORTA,3 ;
    SBIC PINA, 2 ;
    RJMP main ;jump to main
    sbi PORTA,3 ;set the 3rd bit ,means digital high at output pin
    RJMP main ;jump to main

    Download Program File

    assembly

    Group Assignment

    Comparing the performance and development workflows for other architectures

    Raspberry pi

    Raspberry pi is a small single board computer that is designed to teach basic computer skills for school students in developing countries. They are used in various fields with wide range of applications.

  • Quad Core 1.2GHz Broadcom BCM2837 64bit CPU
  • 1GB RAM
  • BCM43438 wireless LAN and Bluetooth Low Energy (BLE) on board
  • 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
  • Micro SD port for loading your operating system and storing data
  • Upgraded switched Micro USB power source up to 2.5A
  • Programming

    pie

    ATmega328/P

    The Atmel ATmega328/P is a low-power 8-bit microcontroller based on the AVR enhanced RISC architecture. By executing powerful instructions in a single clock cycle, the ATmega328/P achieves throughputs close to 1MIPS per MHz( very efficient than ATTiny44). This empowers system designer to optimize the device for power consumption versus processing speed.

    assembly

    Features

    High Performance, Low Power Atmel AVR 8-Bit Microcontroller Family

  • Advanced RISC Architecture
  • 131 Powerful Instructions
  • Most Single Clock Cycle Execution
  • 32 x 8 General Purpose Working Registers
  • Fully Static Operation
  • Up to 20 MIPS Throughput at 20MHz
  • On-chip 2-cycle Multiplier
  • High Endurance Non-volatile Memory Segments
  • 32KBytes of In-System Self-Programmable Flash program
  • Memory

  • 1KBytes EEPROM
  • 2KBytes Internal SRAM
  • Write/Erase Cycles: 10,000 Flash/100,000 EEPROM
  • Data Retention: 20 years at 85°C/100 years at 25°C
  • Comparing ATtiny44 and Atmel mega 328

  • Pin Number - ATtiny 44 has 14 pins where 12 I/O pins whereas Atmel 328 has 28 or 32 pins where 23 I/O pins.
  • Architecture - Both ATtiny 44 and Atmel 328 have enhanced RISC Architecture
  • Flash size for Attiny 44 is 8 Kb whereas Atmel 328 is upto 32Kb
  • Maximum operating frequency is 20 for both
  • Conclusions

    The Week was very good, i could almost go through and study a wide range of programming languages and softwares.

    Creative Commons License
    This work by Aby Michael is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.