AVR Fundamentals

13,637 views
13,489 views

Published on

Introduction to the AVR Family of Microcontrollers

Published in: Self Improvement
0 Comments
6 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
13,637
On SlideShare
0
From Embeds
0
Number of Embeds
10
Actions
Shares
0
Downloads
1,402
Comments
0
Likes
6
Embeds 0
No embeds

No notes for slide
  • 11/07/12 20:08 © 2007 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
  • 11/07/12 20:08 © 2007 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
  • 11/07/12 20:08 © 2007 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
  • 11/07/12 20:08 © 2007 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
  • AVR Fundamentals

    1. 1. AVR
    2. 2. Modified Harvard architecture 8-bit RISC singlechip microcontrollerComplete System-on-a-chip On Board Memory (FLASH, SRAM & EEPROM) On Board PeripheralsAdvanced (for 8 bit processors) technologyDeveloped by Atmel in 1996First In-house CPU design by Atmel
    3. 3. 8 Bit tinyAVR Small package – as small as 6 pins8 Bit megaAVR Wide variety of configurations and packages8 / 16 Bit AVR XMEGA Second Generation Technology32 Bit AVR UC3 Higher computational throughput
    4. 4. Processor corePeripheralsHardware Example – Polulu 3pi robotDevelopment EnvironmentsSoftware Example - PID algorithm walkthrough
    5. 5. What is Harvard Architecture?Before we can answer that…
    6. 6. Separate instruction and data pathsSimultaneous accesses to instructions & dataHardware can be optimized for access type andbus width.
    7. 7. Special instructions can access data from programspace.Data memory is more expensive than programmemoryDon’t waste data memory for non-volatile data
    8. 8. Reduced Instruction Set ComputerAs compared to Complex Instruction SetComputers, i.e. x86Assumption: Simpler instructions execute fasterOptimized most used instructionsOther RISC machines: ARM, PowerPC, SPARCBecame popular in mid 1990s
    9. 9. Faster clock ratesSingle cycle instructions (20 MIPS @ 20 MHz)Better compiler optimizationTypically no divide instruction in core
    10. 10. 32 8 Bit registersMapped to address 0-31 in data spaceMost instructions can access any register andcomplete in one cycleLast 3 register pairs can be used as 3 16 bit indexregisters32 bit stack pointer
    11. 11. 7 0 addr R0 0x00 R1 0x01 R3 0x02 R4 0x03 R5 0x04 R6 0x05 ••• R26 0x1A x register low byte R27 0x1B x register high byte R28 0x1C y register low byte R29 0x1D y register high byte R30 0x1E z register low byte R31 0x1F z register high byte
    12. 12. Non-volatile program space storage16 Bit widthSome devices have separate lockable bootsectionAt least 10,000 write/erase cycles
    13. 13. ATmega 48 ATmega 88/168/328 0x000 0x000Application Flash Application Flash 0x1FFF 0x3FFF 0x7FF Boot Flash 0x7FFF
    14. 14. Data space storage8 Bit width
    15. 15. 0x0000 – 0x001F 32 Registers 0x0020 – 0x005F 64 I/O Registers 0x00060– 0x00FF160 External I/O Reg 0x0100 Internal SRAM(512/1024/2048x8) 0x04FF/0x6FF/0x8FF External SRAM
    16. 16. Electrically Erasable Programmable Read OnlyMemory8 bit widthRequires special write sequenceNon-volatile storage for program specific data,constants, etc.At least 100,000 write/erase cycles
    17. 17. DEVICE FLASH EEPROM SRAMATmega48A 4K Bytes 256 Bytes 512 BytesATmega48PA 4K Bytes 256 Bytes 512 BytesATmega88A 8K Bytes 512 Bytes 1K BytesATmega88PA 8K Bytes 512 Bytes 1K BytesATmega168A 16K Bytes 512 Bytes 1K BytesATmega168PA 16K Bytes 512 Bytes 1K BytesATmega328 32K Bytes 1K Bytes 2K BytesATmega328P 32K Bytes 1K Bytes 2K Bytes
    18. 18. I/O registers visible in data space I/O can be accessed using same instructions as data Compilers can treat I/O space as data accessBit manipulation instructions Set/Clear single I/O bits Only work on lower memory addresses
    19. 19. Directly connected to all 32 general purposeregistersOperations between registers executed within asingle clock cycleSupports arithmetic, logic and bit functionsOn-chip 2-cycle Multiplier
    20. 20. 131 instructions Arithmetic & Logic Branch Bit set/clear/test Data transfer MCU control
    21. 21. Register ↔ register in 1 cycleRegister ↔ memory in 2 cyclesBranch instruction 1-2 cyclesSubroutine call & return 3-5 cyclesSome operations may take longer for externalmemory
    22. 22. Clock control module generates clocks formemory and IO devicesMultiple internal clock sourcesProvisions for external crystal clock source (max20 MHz)Default is internal RC 8 MHz oscillator with ÷ 8prescale yielding 1 MHz CPU clockDefault is only 5-10% accurate
    23. 23. Timer/Counter Timer/ Oscillator Counters IO Crystal Modules Oscillator ADC System AVR External Clock Clock Clock Mux Prescaler Clock CPU Core ControlLow Freq Crystal ÷8 Oscillator RAM Calibrated RC FLASH & Oscillator EEPROM
    24. 24. Multiple power down modes Power down mode Wake on external reset or watchdog reset Power save mode Wake on timer events Several standby modesUnused modules can be shut down
    25. 25. Power on resetExternal resetWatchdog system resetBrown out detect (BOD) reset
    26. 26. ATmega328 has 26 reset/interrupt sources1 Reset source2 External interrupt sourcesI/O Pin state change on all 24 GPIO pinsPeripheral device events
    27. 27. Each vector is a 2 word jump instructionVectors start at program memory address 0Reset vector is at address 0Sample vector table:Address Labels Code Comments0x0000 jmp RESET ; Reset Handler0x0002 jmp EXT_INT0 ; IRQ0 Handler0x0004 jmp EXT_INT1 ; IRQ1 Handler0x0006 jmp PCINT0 ; PCINT0 Handler0x0008 jmp PCINT1 ; PCINT1 Handler ...
    28. 28. Fuses configure system parameters Clock selection and options Boot options Some IO pin configurations Reset optionsThree 8 bit fuse registersUse caution! Some configurations can put thedevice in an unusable state!
    29. 29. 23 General Purpose IO BitsTwo 8 bit & one 16 bit timer/countersReal time counter with separate oscillator6 PWM Channels6 or 8 ADC channels (depends on package)Serial USARTSPI & I2C Serial InterfacesAnalog comparatorProgrammable watchdog timer
    30. 30. Three 8 Bit IO Ports Port B, Port C & Port D Pins identified as PBx, PCx or PDx (x=0..7)Each pin can be configured as: Input with internal pull-up Input with no pull-up Output low Output high
    31. 31. Most port pins have alternate functionsInternal peripherals use the alternate functionsEach port pin can be assigned only one function ata time
    32. 32. 8/16 Bit register Increments or decrements on every clock cycle Can be read on data bus Output feeds waveform generatorClock Sources Internal from clock prescaler External Tn Pin (Uses 1 port pin)
    33. 33. Multiple Operating modes Simple timer / counter Output Compare Function Waveform generator Clear/set/toggle on match Frequency control Pulse Width Modulation (PWM)
    34. 34. DATA BUS IRQ Waveform = Generator OCnA TCCRnA OCRnA ClockTn Select TCNTn IRQ TCCRnB OCRnB Waveform = Generator OCnB DATA BUS
    35. 35. Use Clear Timer on Compare Match (CTC) ModeOCnx Toggles on Compare MatchTOP = OCRnx 0 OCnx (toggle)
    36. 36. Dynamically change duty cycle of a waveformUsed to control motor speed
    37. 37. Fast PWM Mode Counter counts from BOTTOM (0) to MAX Counter reset to 0 at MAX OCnx cleared at TOP OCnx set at BOTTOM
    38. 38. Clear on TOPMAXTOPBOTTOM Set on BOTTOMOCnx
    39. 39. Timer 1 has capture modeCapture can be triggered by ICP1 pin or ACO fromanalog comparatorCapture event copies timer into input captureregister ICR1Can be used to time external events or measurepulse widthsRange finders generate pulse width proportionalto distance
    40. 40. 10 Bit Successive Approximation ADC8 Channel multiplexer using port pins ADC0-7Max conversion time 260 μsec.
    41. 41. Compares voltage between pins AIN0 and AIN1Asserts AC0 when AIN0 > AIN1AC0 can trigger timer capture function Range finders indicate distance with pulse with Timer capture mode can compute pulse width
    42. 42. Industry standard serial protocol forcommunication between local devicesMaster/Slave protocol3 Wire interfaceSlaves addressed via Slave Select (SS) inputs
    43. 43. SCLK Serial ClockMOSI Master Out Slave InMISO Master In Slave OutSS Slave Select
    44. 44. Industry standard serial protocol forcommunication between local devicesMaster/Slave protocol2 Wire interfaceByte oriented messagesSlave address embedded in command
    45. 45. SDA Serial DataSCL Serial Clock
    46. 46. EEPROMIO ExpandersReal Time ClocksADC & DACTemperature sensorsUltrasonic range findersCompassServo / Motor ControllerLED Display
    47. 47. Universal Synchronous and Asynchronous serialReceiver and TransmitterFull Duplex OperationHigh Resolution Baud Rate GeneratorCan provide serial terminal interface
    48. 48. Some chips have JTAG interface Industry standard for debugging chips in circuit Connect to special JTAG signals Can program FLASH EEPROM All fuses Lock bits
    49. 49. ISP (In-System Programmer)Connect to 6 or 10 pin connectorSPI interfaceSpecial cable required Can program FLASH EEPROM Some fuses Lock bits
    50. 50. CW: PD5 & PD6 PWM mode (OC0B, OC0A)CCW: PD3 & PB3 PWM mode (OC2B, OC2A)Motor interface uses H-Bridge controllerSpeed and direction controlled by port bitsController standby controlled by PC6
    51. 51. V+1 2 M3 4 V-
    52. 52. V+PD5=1 1 2 M 3 4 PD6 (OC0A) PMW V-
    53. 53. V+ 1 2 PB3 (OC2A) PMW MPD3=1 3 4 V-
    54. 54. PC5 enables the IR emittersOne port pin for each of 5 sensors, PC0-4Port pin used as input and output Set pin as output high for 10 μsec to charge RC circuit Set pin as input Measure time for voltage to drop (using TCNT2) Time indicates level of light
    55. 55. 1. Output: 10 μsec pulse on PC02. Input: measure decay t indicates amount of reflected light
    56. 56. ADC6: battery voltage monitorADC7: reads trimmer pot voltage
    57. 57. Buzzer: Timer1 PB2 (OC1B)User defined (can be USART) : PD0, PD1Device (ISP) Programming: PB3, PB4, PB520 MHz crystal: PB6, PB7
    58. 58. Choose a language BASCOM C/C++ Assembler (machine language)Choose a platform Linux Windows
    59. 59. Text Editor / IDETool Chain Compiler LibrariesProgrammer (cable & software)
    60. 60. Windows based BASIC compiler for AVR FamilyComplete IDEIncludes simulatorExtensive library for AVR peripheral controlIncludes ISP programmerDemo versions available – limited code space
    61. 61. Linux AVR-GCC Based on GNU toolset Open Source AVR-LIBC librariesWindows WIN-AVR Port of AVR-GCC to Windows
    62. 62. Linux Editors & command line tools Eclipse IDEWindows Atmel AVR Studio Includes ISP programming support Eclipse IDE
    63. 63. STK 200/300/400/500 Atmel AVR starter kit and development system Interfaces to AVR Studioavrdude Programming support for all memories
    64. 64. Win-AVRAVR-StudioProprietary libraries
    65. 65. PID AlgorithmPID Code Walkthrough
    66. 66. Simple Line FollowerThree sensors can provide position information left of center on center right of sensorsReact to current position only
    67. 67. Three choices:
    68. 68. Guaranteed to zig-zagback and fort h
    69. 69. Anticipates where we want to goContinuously adjusts direction to get thereAdjustable constants control behavior
    70. 70. P + Error Output Set point Σ I Σ Process(center line) – D
    71. 71. (P) Proportional Where we are relative to where we want to be.(I) Integral Sum of previous errors. History of where we were.(D) Derivative Rate of change. Affects how fast we react to changes.
    72. 72. Useful functions read_line Returns a value between 0 – 4000 0 – 1000: robot is far right of line 1000 – 3000: robot is approximately centered 3000 – 4000: robot is far left of line set_motors Set speed and direction of both motors -255 – 0 : reverse direction 0 – 255: forward direction
    73. 73. // Introductory messages. The "PROGMEM" identifier causes the data to// go into program space.const char welcome_line1[] PROGMEM = " Pololu";const char welcome_line2[] PROGMEM = "3xf7 Robot";const char demo_name_line1[] PROGMEM = "PID Line";const char demo_name_line2[] PROGMEM = "Follower";// A couple of simple tunes, stored in program space.const char welcome[] PROGMEM = ">g32>>c32";const char go[] PROGMEM = "L16 cdegreg4";A
    74. 74. // Auto-calibration: turn right and left while calibrating the// sensors.for(counter=0;counter<80;counter++){ if(counter < 20 || counter >= 60) set_motors(40,-40); else set_motors(-40,40); // This function records a set of sensor readings and keeps // track of the minimum and maximum values encountered. The // IR_EMITTERS_ON argument means that the IR LEDs will be // turned on during the reading, which is usually what you // want. calibrate_line_sensors(IR_EMITTERS_ON); // Since our counter runs to 80, the total delay will be // 80*20 = 1600 ms. delay_ms(20);}set_motors(0,0);
    75. 75. // Get the position of the line. Note that we *must* provide// the "sensors" argument to read_line() here, even though we// are not interested in the individual sensor readings.unsigned int position = read_line(sensors,IR_EMITTERS_ON);// The "proportional" term should be 0 when we are on the line.int proportional = ((int)position) - 2000;// Compute the derivative (change) and integral (sum) of the position.int derivative = proportional - last_proportional;integral += proportional;// Remember the last position.last_proportional = proportional;// Compute the difference between the two motor power settings,// m1 - m2. If this is a positive number the robot will turn// to the right. If it is a negative number, the robot will// turn to the left, and the magnitude of the number determines// the sharpness of the turn.int power_difference = proportional/20 + integral/12000 + derivative*7/4;
    76. 76. // Compute the actual motor settings. We never set either motor// to a negative value.const int max = 250;if(power_difference > max) power_difference = max;if(power_difference < -max) power_difference = -max;// One Motor will always be full speedif(power_difference < 0) set_motors(max+power_difference, max);else set_motors(max, max-power_difference);
    77. 77. Open source elections prototyping platformHardware Based on Atmega (328 & others)Software Wiring language, based on c++ Boot loader Arduino IDE
    78. 78. ATmega 48/88/168/328 What we have been talking aboutATmega 164/324/644/1284 JTAG interface All 4 IO Ports A,B,C & D More memory
    79. 79. PDIP – Plastic Dual In-line Package Good for hobbyistsTQFP – Thin Quad Flat Pack Surface mountMLF – MicroLeadFrame 28 & 32 pin Surface mount / higher temperature
    80. 80. www.atmel.comwww.polulu.comhttp://winavr.sourceforge.netwww.avrfreaks.netwww.wrighthobbies.netwww.eclipse.orgJust Google AVR

    ×