Embedded Programming

Studying 8 bit AVR microcontroller datasheet and programming it.

Assignments

  • To read data sheet of an microcontroller
  • Program the board to do some task


  • I started this week my re-visiting different terminologies covered by Neil in his lecture.

    Von Neumann Architecture

  • Same memory holds program and data
  • There is sequence number for each and every memory location and it is addressed linearly
  • Program consists of sequence of instructions and the sequence can only be changed by unconditional and conditional jump instructions.
  • Memory organization is in the hands of programmer
  • Used in desktop computers, laptops, workstations etc.
  • ARM follows this architecture

  • Harvard Architecture

  • Separate memory for data and instructions
  • Parallel access to data and memory is possible
  • Requires 2 buses
  • Used in embedded computers and DSP
  • 8051,PIC,AVR follow this architecture

  • RISC

  • Less number of instructions
  • Emphasis given of software
  • Large code size

  • CISC

  • Complex instruction set
  • Emphasis is given hardware - more transistors required for executing complex instructions
  • Some code size

  • Microprocessor

  • It is an IC which has only CPU inside them like intel i5 chip. It generally has an ALU, reister set and control circuitary to fetch decode and execute instructions.
  • They have ability to execute bigger and generic application. We can add external peripherals, RAM, storage to them.
  • Microprocessors are used to perform unspecific tasks like developing software,website etc
  • They have clock speed in GHz

  • Microcontrollers

  • They are generalized devices which contains a fixed amount of RAM, ROM, ADC and other peripherals all embedded on a single chip.
  • They are task specific devices.
  • They contain microprocessor inside them.
  • Clock speed is generally in MHz

  • Families

    I will be comparing 5 IC families known to me :

    1. 8051
      • Developed by Intel in the year 1981.
      • 8 bit microcontroller with 8 bit standard core
      • Very low cost and known for its standard.
    2. PIC - Reripheral Interface Controller
      • Developed by Microchip.
      • 8/16/32 bit core
      • Has less community support and its IDE is not freely available
    3. MSP
      • It is a Mixed signal microcontroller family from TI.
      • 16 bit core
      • Used in ultra low power application like in case of battery operated devices.
    4. AVR - It derives its name from its developers Alf-Egil Bogen and Vegard Wollan RISC microcontroller
      • Developed by Atmel Corporation in 1996
      • 8/32 bit core
      • Widely used in robotics. Has good community support and its simply to implement
    5. ARM - Advanced RISC Machines
      • ARM is basically an architecture developed by an UK base company which is now used by many big companies
      • 32/64 bit core.
      • Used in heavy applications with high end communication protocol, large memory requirement with fast processing.

    Packages

  • DIP - Dual Inline Package
  • SOT - Small Outline Transistor
  • SOIC - Small Outline Integrated Circuit
  • TSSOP - Thin Shrink Small Outline Package
  • TQFP - Thin Quad Flat Package
  • LQFP - Low profile Quad Flat package



  • Studying datasheet of 8 bit AVR microcontroller ATtiny

    This week for testing we will be using ATtiny44 which has 4KB of flash, 256 Bytes of EEPROM and SRAM.

  • ATtiny44 is a low-power CMOS 8-bit microcontroller based on the AVR enhanced RISC architecture.
  • One 8-Bit and One 16-Bit Timer/Counter with Two PWM Channels each
  • It has an On-chip Analog Comparator.
  • Its operating range is 2.7V to 5.5V
  • Works for industrial temperature range -40 to 85 degree C

  • In our lab we are using SOIC package of ATtiny 44 its pin out is as below:


    Another important diagram to go through is ICs internal block diagram. It lets us about different hardware modules available in the IC.


    You can refer it at page 4 of the datasheet. Below is the link to download datasheet

    Datasheet

    I will be using Arduino board for programming my board.

    Arduino IDE can be download from https://www.arduino.cc/en/main/software and depending on your OS select file to be downloaded.


    For using Arduino board for programming first we have to make it as ISP. For doing same follow below mention steps:

  • Connect Arduino to computer using USB
  • In tools select board as Arduino/Gennuino Uno, Port on which Arduino is connected(For same check My devices in My computer), Programmer as Arduino as ISP



  • Next step is to unplug Arduino and connect capacitor between Reset and GND before you again power ON it.


  • Next step is to download and install Atiny board libraries
  • For writing C code I will be using Atmel Studio 7. Same can be downloaded using below link

    Atmel Studio 7 - Click to download

    Open Atmel studio and create new project.



    Select GCC C Executable and give name to the project.



    Then select microcontroller as Attiny44


    Next step is to write the c code and save it

    /* * Amol_LED_blinking.c * * Created: 13-April-17 2:24:24 AM * Author : Amol Wadkar */ //#defines #define F_CPU 1000000 //Define clock speed as 1Mhz //Include Header #include //Import header file required for AVR microcontroller #include //Import header file required for delay function int main(void) { DDRA = 0b10000000; //set PA7as output & all other pins as input while (1) //Repeat the below actions continuously { if((PINA & (1<<3))==0b00000000) //When button is press this condition will be true PORTA |= (1<<7);// Set PA7 high (Make LED ON) else PORTA &= ~(1<<7);// Set PA7 low (Make LED OFF) } }

    Now plug back Arduino to PC connecting the capacitor between reset and ground

    Connect the Arduino to the board. Follow below sequence



    Burn the bootloader using Arduino IDE

  • Open Arduino IDE
  • Select board ATtiny44
  • CLock as External 20MHz
  • Programmer : USBtiny ISP
  • Burn bootloader


  • Then click on burn bootloader
  • Open the C code , compile the code and upload it to board.

    Its done - LED blinks on pressing the button.

    Modified the code to blink the LED slow and fast on button press

  • LED blinking fast on pressing the button
  • /* * Amol_Input_device * * Created: 27-April-17 8:28:27 AM * Author : Amol Wadkar */ //#defines #define F_CPU 1000000 //Define clock speed as 1Mhz //Include Header #include //Import header file required for AVR microcontroller #include //Import header file required for delay function int main(void) { unsigned long i, j ; DDRA = 0b10000000; //set PA7as output & all other pins as input while (1) //Repeat the below actions continuously { if((PINA & (1<<3))==0b00000000) //When magnetic sensor is in NC { PORTA |= (1<<7);// Set PA7 high (Make LED ON) for( i = 0; i < 1 ; i++ ) { for(j = 0; j < 90000; j++) { } } PORTA &= ~(1<<7); for( i = 0; i < 1 ; i++ ) { for(j = 0; j < 90000; j++) { } } } else PORTA &= ~(1<<7);// Set PA7 low (Make LED OFF) } }
  • I modified the code to keep LED ON when button is not press and blink slow when button is pressed.
  • /* * Amol_Input_device * * Created: 27-April-17 8:28:27 AM * Author : Amol Wadkar */ //#defines #define F_CPU 1000000 //Define clock speed as 1Mhz //Include Header #include //Import header file required for AVR microcontroller #include //Import header file required for delay function int main(void) { unsigned long i,j; DDRA = 0b10000000; //set PA7as output & all other pins as input while (1) //Repeat the below actions continuously { if((PINA & (1<<3))==0b00000000) //When magnetic sensor is in NC { PORTA &= ~(1<<7);// Set PA7 low (Make LED OFF) for( i = 0; i < 5 ; i++ ) { for(j = 0; j < 90000; j++) { } } PORTA |= (1<<7);// Set PA7 high (Make LED ON) for( i = 0; i < 5 ; i++ ) { for(j = 0; j < 90000; j++) { } } } else PORTA |= (1<<7);// Set PA7 high (Make LED ON) } }



    Output files

  • C code
  • C code - Fast blinking
  • C code - Slow blinking
  • Attiny 44 datasheet