AVR Fundamentals
Upcoming SlideShare
Loading in...5
×
 

AVR Fundamentals

on

  • 2,457 views

Introduction to the AVR Family of Microcontrollers

Introduction to the AVR Family of Microcontrollers

Statistics

Views

Total Views
2,457
Views on SlideShare
2,455
Embed Views
2

Actions

Likes
0
Downloads
365
Comments
0

2 Embeds 2

http://www.slashdocs.com 1
https://www.linkedin.com 1

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • 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 AVR Fundamentals Presentation Transcript

  • AVR
  • 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
  • 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
  • Processor corePeripheralsHardware Example – Polulu 3pi robotDevelopment EnvironmentsSoftware Example - PID algorithm walkthrough
  • What is Harvard Architecture?Before we can answer that…
  • Separate instruction and data pathsSimultaneous accesses to instructions & dataHardware can be optimized for access type andbus width.
  • Special instructions can access data from programspace.Data memory is more expensive than programmemoryDon’t waste data memory for non-volatile data
  • 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
  • Faster clock ratesSingle cycle instructions (20 MIPS @ 20 MHz)Better compiler optimizationTypically no divide instruction in core
  • 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
  • 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
  • Non-volatile program space storage16 Bit widthSome devices have separate lockable bootsectionAt least 10,000 write/erase cycles
  • ATmega 48 ATmega 88/168/328 0x000 0x000Application Flash Application Flash 0x1FFF 0x3FFF 0x7FF Boot Flash 0x7FFF
  • Data space storage8 Bit width
  • 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
  • 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
  • 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
  • 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
  • Directly connected to all 32 general purposeregistersOperations between registers executed within asingle clock cycleSupports arithmetic, logic and bit functionsOn-chip 2-cycle Multiplier
  • 131 instructions Arithmetic & Logic Branch Bit set/clear/test Data transfer MCU control
  • Register ↔ register in 1 cycleRegister ↔ memory in 2 cyclesBranch instruction 1-2 cyclesSubroutine call & return 3-5 cyclesSome operations may take longer for externalmemory
  • 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
  • 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
  • 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
  • Power on resetExternal resetWatchdog system resetBrown out detect (BOD) reset
  • ATmega328 has 26 reset/interrupt sources1 Reset source2 External interrupt sourcesI/O Pin state change on all 24 GPIO pinsPeripheral device events
  • 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 ...
  • 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!
  • 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
  • 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
  • Most port pins have alternate functionsInternal peripherals use the alternate functionsEach port pin can be assigned only one function ata time
  • 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)
  • Multiple Operating modes Simple timer / counter Output Compare Function Waveform generator Clear/set/toggle on match Frequency control Pulse Width Modulation (PWM)
  • DATA BUS IRQ Waveform = Generator OCnA TCCRnA OCRnA ClockTn Select TCNTn IRQ TCCRnB OCRnB Waveform = Generator OCnB DATA BUS
  • Use Clear Timer on Compare Match (CTC) ModeOCnx Toggles on Compare MatchTOP = OCRnx 0 OCnx (toggle)
  • Dynamically change duty cycle of a waveformUsed to control motor speed
  • Fast PWM Mode Counter counts from BOTTOM (0) to MAX Counter reset to 0 at MAX OCnx cleared at TOP OCnx set at BOTTOM
  • Clear on TOPMAXTOPBOTTOM Set on BOTTOMOCnx
  • 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
  • 10 Bit Successive Approximation ADC8 Channel multiplexer using port pins ADC0-7Max conversion time 260 μsec.
  • 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
  • Industry standard serial protocol forcommunication between local devicesMaster/Slave protocol3 Wire interfaceSlaves addressed via Slave Select (SS) inputs
  • SCLK Serial ClockMOSI Master Out Slave InMISO Master In Slave OutSS Slave Select
  • Industry standard serial protocol forcommunication between local devicesMaster/Slave protocol2 Wire interfaceByte oriented messagesSlave address embedded in command
  • SDA Serial DataSCL Serial Clock
  • EEPROMIO ExpandersReal Time ClocksADC & DACTemperature sensorsUltrasonic range findersCompassServo / Motor ControllerLED Display
  • Universal Synchronous and Asynchronous serialReceiver and TransmitterFull Duplex OperationHigh Resolution Baud Rate GeneratorCan provide serial terminal interface
  • 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
  • ISP (In-System Programmer)Connect to 6 or 10 pin connectorSPI interfaceSpecial cable required Can program FLASH EEPROM Some fuses Lock bits
  • 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
  • V+1 2 M3 4 V-
  • V+PD5=1 1 2 M 3 4 PD6 (OC0A) PMW V-
  • V+ 1 2 PB3 (OC2A) PMW MPD3=1 3 4 V-
  • 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
  • 1. Output: 10 μsec pulse on PC02. Input: measure decay t indicates amount of reflected light
  • ADC6: battery voltage monitorADC7: reads trimmer pot voltage
  • Buzzer: Timer1 PB2 (OC1B)User defined (can be USART) : PD0, PD1Device (ISP) Programming: PB3, PB4, PB520 MHz crystal: PB6, PB7
  • Choose a language BASCOM C/C++ Assembler (machine language)Choose a platform Linux Windows
  • Text Editor / IDETool Chain Compiler LibrariesProgrammer (cable & software)
  • Windows based BASIC compiler for AVR FamilyComplete IDEIncludes simulatorExtensive library for AVR peripheral controlIncludes ISP programmerDemo versions available – limited code space
  • Linux AVR-GCC Based on GNU toolset Open Source AVR-LIBC librariesWindows WIN-AVR Port of AVR-GCC to Windows
  • Linux Editors & command line tools Eclipse IDEWindows Atmel AVR Studio Includes ISP programming support Eclipse IDE
  • STK 200/300/400/500 Atmel AVR starter kit and development system Interfaces to AVR Studioavrdude Programming support for all memories
  • Win-AVRAVR-StudioProprietary libraries
  • PID AlgorithmPID Code Walkthrough
  • Simple Line FollowerThree sensors can provide position information left of center on center right of sensorsReact to current position only
  • Three choices:
  • Guaranteed to zig-zagback and fort h
  • Anticipates where we want to goContinuously adjusts direction to get thereAdjustable constants control behavior
  • P + Error Output Set point Σ I Σ Process(center line) – D
  • (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.
  • 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
  • // 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
  • // 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);
  • // 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;
  • // 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);
  • Open source elections prototyping platformHardware Based on Atmega (328 & others)Software Wiring language, based on c++ Boot loader Arduino IDE
  • 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
  • PDIP – Plastic Dual In-line Package Good for hobbyistsTQFP – Thin Quad Flat Pack Surface mountMLF – MicroLeadFrame 28 & 32 pin Surface mount / higher temperature
  • www.atmel.comwww.polulu.comhttp://winavr.sourceforge.netwww.avrfreaks.netwww.wrighthobbies.netwww.eclipse.orgJust Google AVR