Atmel Avr Basics

download Atmel Avr Basics

of 13

Transcript of Atmel Avr Basics

  • 7/30/2019 Atmel Avr Basics

    1/13

    10/5/11

    1

    University of Florida, EEL 5666 Dr. A. Antonio Arroyo

    1

    Atmel AVR Basics

    AVR Studio 5 & XMegaA. A. Arroyo

    University of Florida, EEL 5666 Dr. A. Antonio Arroyo

    2

    OverviewBasic AVR KnowledgeAVR StudioProgramming

  • 7/30/2019 Atmel Avr Basics

    2/13

    10/5/11

    2

    University of Florida, EEL 5666 Dr. A. Antonio Arroyo

    3

    AVR BasicsThe AVR microcontrollers are divided into

    three groups: 1.tinyAVR 2.AVR (Classic AVR) 3.mega AVR 4. Xmega AVR

    The difference between these devices lies inthe available features. The tinyAVRC areusually devices with lower pin-count or

    reduced feature set compared to themegaAVR's. All AVR devices have the sameinstruction set and memory organization.

    University of Florida, EEL 5666 Dr. A. Antonio Arroyo

    4

    AVR Basics AVR's contain SRAM, EEPROM, External SRAMinterface, ADC, Hardware Multiplier, UART, USART,etc. A tinyAVRand a megaAVRstripped off all theperipheral modules, leaves us with the AVR Core the same for all AVRs.

    Datasheets are complete technical documents areference on how a given peripheral/feature works.

    1.One PageKey information and Feature List2. Architectural Overview3.Peripheral Descriptions4.Memory Programming5.Characteristics6.Register Summary7.Instruction Set Summary8.Packaging Information

  • 7/30/2019 Atmel Avr Basics

    3/13

    10/5/11

    3

    University of Florida, EEL 5666 Dr. A. Antonio Arroyo

    5

    Common Peripherals UART

    Provides serial communication interface Typically RS232

    Analog/Digital Channels Many sensor are analog 4 channels minimum

    SPI/I2C/CAN interfaces Synchronous serial, very common interfaces

    JTAG Allows in circuit debugging and programming

    ISP

    In system programming, serial communication Timers

    The more the better PWM / frequency generation modules

    Allows hardware generation of motor driving signals

    University of Florida, EEL 5666 Dr. A. Antonio Arroyo

    6

    ATMegaMost popular here at UFKitchen sink of peripherals

    UARTS, multi-channel A/D, multiple countersand PWM channels, SPI/I2C/CAN, ISP, JTAG

    RISC based 8Bit core16/32MIPS @ 16/32MhzDevelopment Software free from AtmelLarge Hobbyist user base on the internet

  • 7/30/2019 Atmel Avr Basics

    4/13

    10/5/11

    4

    University of Florida, EEL 5666 Dr. A. Antonio Arroyo

    Download doc8067.pdf and doc8077.pdf

    Go to www.atmel.com and search for xMega64A1

    University of Florida, EEL 5666 Dr. A. Antonio Arroyo

    xMega128A1 Shows available

    peripheraldevices and the

    ports they use

    In IMDL, weextensively use

    A/Ds, Timers,and USARTs.

  • 7/30/2019 Atmel Avr Basics

    5/13

    10/5/11

    5

    University of Florida, EEL 5666 Dr. A. Antonio Arroyo

    9

    AVR Studio.

    Step 1: Download and Install AVR Studio:http://www.atmel.com/microsite/avr_studio_5/default.asp?

    category_id=163&family_id=607

    Get AVR 5 with all of the options

    Step 2: Read the documentation:http://www.atmel.com/dyn/products/devices.asp?

    category_id=163&family_id=607&subfamily_id=1965&source=left_nav

    University of Florida, EEL 5666 Dr. A. Antonio Arroyo

    10

    AVR Studio.Step 1: Create a New ProjectStep 2: Configure Project Settings

    What kind of project we want to create, and

    setting up filenames and locations.

    Step 3: Write your code

  • 7/30/2019 Atmel Avr Basics

    6/13

    10/5/11

    6

    University of Florida, EEL 5666 Dr. A. Antonio Arroyo

    Therearetwowaysengineerswritecodetocontrolaperipheralmoduleonthechip:

    1:Copysomeoneelsescode.Thisisfineinthisclassbutwhenitdoesntwork,yourestuck.

    2:Writeitfromscratchbyreadingeverywordofthedatasheet.Thismethodgivesyoualotmorecontrolbutcanbefrustratingwhenyoure

    unsurehowitshouldwork.{ThisisthewayDr.Arroyodevelopscode.Thisisanotherexampleofbeingthetortoiseandnotthehare}

    Using Peripherals

    University of Florida, EEL 5666 Dr. A. Antonio Arroyo

    IdeallyinIMDLyourcodeshouldbeacombinationofbothmethods.Ifyoucanfindsomecodetogiveyouastartingpoint,itcansavealotoftime.

    Otherwise,dontbeafraidtodigthroughthedatasheet.

    Wewillelaborateonhowtogetinformationfromthedatasheet

    Using Peripherals

  • 7/30/2019 Atmel Avr Basics

    7/13

    10/5/11

    7

    University of Florida, EEL 5666 Dr. A. Antonio Arroyo

    The three parts you are interested in Overview Description/Features Register Map!

    Using Peripherals

    University of Florida, EEL 5666 Dr. A. Antonio Arroyo

    Make sure you understand how the peripheralis supposed to work, what pins it uses, whatresources it uses (Timing channels, Ports, etc)

    The register map is where you actually seewhich bits to set to configure the peripheral,and which registers to use to interact with the

    peripheral

    Using Peripherals

  • 7/30/2019 Atmel Avr Basics

    8/13

    10/5/11

    8

    University of Florida, EEL 5666 Dr. A. Antonio Arroyo

    Register name is:

    PeripheralName_RegisterName

    Peripheral Name from doc8067 Register Name from doc8077

    Examples: TCC0_CTRLA, PORTB_OUT,PORTB_DIR

    Register Names

    University of Florida, EEL 5666 Dr. A. Antonio Arroyo

    Here is the register summary for the USART. Wecan see that serial communication uses 7 regs

    1 for Data 1 for status 3 for control 2 for baud rate

    Register Names

  • 7/30/2019 Atmel Avr Basics

    9/13

    10/5/11

    9

    University of Florida, EEL 5666 Dr. A. Antonio Arroyo

    TypexmegausartintoGoogle

    The first result is a nice 7-page documentexplaining EXACTLY how to set up the serialUSART on the Xmega. The source code for this

    example is easily found on the internet.

    Find Examples

    University of Florida, EEL 5666 Dr. A. Antonio Arroyo

    Important concept: There are two ways towrite/read a pin.

    Direct pin access Using a peripheral.

    Whenusingdirectpinaccess,usePORTX_INtoreadandPORTX_OUTtowrite.Remembertosetdatadirectionregister,PORTX_DIR.

    Sample Code

  • 7/30/2019 Atmel Avr Basics

    10/13

  • 7/30/2019 Atmel Avr Basics

    11/13

    10/5/11

    11

    University of Florida, EEL 5666 Dr. A. Antonio Arroyo

    21

    Assembly Code Sample

    University of Florida, EEL 5666 Dr. A. Antonio Arroyo

    22

    Assembly Code Sample

  • 7/30/2019 Atmel Avr Basics

    12/13

    10/5/11

    12

    University of Florida, EEL 5666 Dr. A. Antonio Arroyo

    23

    Assembly Code Sample

    University of Florida, EEL 5666 Dr. A. Antonio Arroyo

    RobotsinIMDLcanhavemanydifferentstylesofcodeandprogramdesign.

    Thisprogrammodelispurelyasuggestion,butithasbeenproventobereliable.

    Add#includesandfunctiondefinitions. Insidemain,startbyinitializingallperipheralsyouwill

    use.Onlydothisonce.

    Haveamainloopthatrunsasfastaspossible(neverslowerthan1Hz,~20Hzisideal)

    Writeextrafunctionsoutsideofmain()forspecificbehaviors

    Writing C Programs

  • 7/30/2019 Atmel Avr Basics

    13/13

    10/5/11

    13

    University of Florida, EEL 5666 Dr. A. Antonio Arroyo

    #includes

    main(){

    Initialize LCD, Servos, PWM, A/D, Serial,

    DIR, variables

    while(1){

    read sensors

    interpret sensors

    function1()

    update LCD } // while

    } // main

    function1() {

    stuff } // function 1

    Writing C Programs