Docstoc

Timers and PWM

Document Sample
Timers and PWM Powered By Docstoc
					ATXMEGA Programming
                    ATXMEGA vs. ATTINY


Feature               Tiny85                       Xmega256A3
Flash                 8K                           256K
SRAM                  512 bytes                    16K
ADC                   4x @ 10-bit, 15KSPS          16x @ 12-bit, 2MSPS
DAC                   No                           2x @ 12-bit, 1MSPS
Timers                2 8-bit (1 sort of 16-bit)   7 16-bit
USART / SPI / I2C     1 shared                     7 (3) USART/SPI, 2 I2C
DMA                   No                           4
Voltage/Speed         8MHz@3V, 16MHz@5V            12MHz@1.6V, 32MHz@3V
Pins (Usable)         8 (5)                        64 (50)
        Understanding the XMEGA series

 Two Datasheets
   Part-Specific – specifies the modules and pinout of a chip

   A series manual – details on modules for any XMEGA A chip



 Lower Voltage Limits
   No 5V allowed! 1.8V – 3.3V only

   Use modern chips or level-shifters (a la TXS0104EDR)



 Very new chips
   Still have kinks – check the ERRATA in part-specific datasheet

   Key errata – best ADC/DAC with VCC > 2.7V, AREF < 2.4V
             Programming the XMEGAs

 Uses the same toolchain as ATTINY
 Modules (PORTs, ADC, etc.) work differently
 Very different syntax
   _BV() isn’t needed anymore: _bm, _bp, _gc

   Modules are now structs – very handy for programming!



 Many, many new constants and #defines
   #include <avr/io.h> is still all you need

   The exhaustive list of defines for xmega256A3 is here:
    <drive>\WinAVR-XXX\avr\include\avr\iox256a3.h
                       Modules

 Tables are in
  Part Datasheet
 Pins are still
  overloaded
 Modules have
  different names
  but many are
  identical!
 Written correctly,
  code for USARTD0
  can work on
  USARTD1, C0, or
  C1…
               PORT Modules

PORTs are a
 little more
 complicated
 than they
 used to be…
    But I already knew how PORTs worked!

 They’re better, we promise
 They still work the old way, if you really want
   DDR        DIR
   PORT       OUT
   PIN        IN
 But now there are better registers
   DIRSET, DIRCLR, DIRTGL           easier bit twiddling
   OUTSET, OUTCLR, OUTTGL           ditto
   PINxCTRL  pin settings – no more trickery for pull-ups

 So DIRA, OUTA, INA?
   Not exactly…

   There is PORTA_DIR, and PORTA_OUT, but don’t use them
                 Structures and You

 AVR-libc now has structures for each module
   PORT_t , ADC_t, USART_t

   <avr/io.h> defines all of these
                                  /* I/O Ports */
                                  typedef struct PORT_struct
                                  {
                                      register8_t DIR; /* I/O Port Data Direction */

 Access via struct names
                                      register8_t DIRSET; /* I/O Port Data Direction Set */
                                      register8_t DIRCLR; /* I/O Port Data Direction Clear */
                                      register8_t DIRTGL; /* I/O Port Data Direction Toggle */
                                      register8_t OUT; /* I/O Port Output */

   PORTA.DIR                         register8_t OUTSET; /* I/O Port Output Set */
                                      register8_t OUTCLR; /* I/O Port Output Clear */
                                      register8_t OUTTGL; /* I/O Port Output Toggle */

   PORTC.IN
                                      register8_t IN; /* I/O port Input */
                                      register8_t INTCTRL; /* Interrupt Control Register */
                                      register8_t INT0MASK; /* Port Interrupt 0 Mask */
                                      register8_t INT1MASK; /* Port Interrupt 1 Mask */
                                      register8_t INTFLAGS; /* Interrupt Flag Register */
                                      register8_t reserved_0x0D;
                                      register8_t reserved_0x0E;


 Code portability
                                      register8_t reserved_0x0F;
                                      register8_t PIN0CTRL; /* Pin 0 Control Register */
                                      register8_t PIN1CTRL; /* Pin 1 Control Register */
                                      register8_t PIN2CTRL; /* Pin 2 Control Register */

   Pass around module pointers
                                      register8_t PIN3CTRL; /* Pin 3 Control Register */
                                      register8_t PIN4CTRL; /* Pin 4 Control Register */
                                      register8_t PIN5CTRL; /* Pin 5 Control Register */
                                      register8_t PIN6CTRL; /* Pin 6 Control Register */

   One function  many modules       register8_t PIN7CTRL; /* Pin 7 Control Register */
                                  } PORT_t;
                                    Example: PORT code

// sets a pin to a given output                       // sets a pin to a given output
// port is a PORT_t pointer                           // port is a PORT_t pointer
// which is a PINx_bm                                 // which is a PINx_bm
// value is 1 or 0                                    // value is 1 or 0
void setOutput(PORT_t * port, int which, int value)   void setDirection(PORT_t * port, int which, int value)
{                                                     {
     if(value)                                             if(value)
     {                                                     {
                  port->OUTSET = which;                                 port->DIRSET = which;
     }                                                     }
     else                                                  else
     {                                                     {
                  port->OUTCLR = which;                                 port->DIRCLR = which;
     }                                                     }
}                                                     }

void main()                                           void main()
{                                                     {
     init();                                               init();

    setOutput(&PORTA, PIN3_bm, 1);                        setDirection(&PORTA, PIN3_bm, 1);
    setOutput(&PORTC, PIN1_bm,0);                         setOutput(&PORTA, PIN3_bm,1);

    while(1);                                             while(1);
}                                                     }
                                               Clocks

 Clock defaults to 2MHz
…but it has a hardware PLL with up to 16x multiplier

void initClock()
{
     OSC.CTRL |= _BV(OSC_RC32MEN_bp);          // turn on 32MHz internal RC oscillator
     while(!(OSC.STATUS & OSC_RC32MRDY_bm));   // wait for it to be ready
     CCP= CCP_IOREG_gc;                        // allow modification of protected register
     CLK.CTRL |= CLK_SCLKSEL_RC32M_gc;         // change from 2MHz to 32MHz
}



 Built in RTC as well
   1-32 KHz operation, independent of system clock

 Peripheral Clock up to 128MHz in certain situations
                          Interrupts

 Similar to before
   Various peripherals can trigger interrupts at different times

   Write an ISR() to handle the different interrupts

   Interrupts queue as before

 Now three-levels of interrupts
   LO, MED, HI

   Higher levels will automatically interrupt lower ones

 Interrupt behavior now configurable
   Queued, round-robin, fixed priority
                             DMA

 Direct Memory Access
   Very common in things that aren’t 8-bit microcontrollers

   Transfers memory from one area to another without CPU

   High data rate transfers between peripherals




                             versus




   CPU controlled transfer                         DMA
                                            Events

 Similar to Interrupts
      Generated by peripherals like interrupts
      But don’t trigger the CPU
      Triggers DMA or other peripherals instead


 Examples:
      Fixed frequency sampling:
       1.     Set up a timer to overflow at a given rate
       2.     Use overflow event to start ADC conversion
       3.     Wake CPU with ADC complete interrupt when done
           Automatic data shuffling
       1.     Set up UART to capture data from GPS
       2.     Set up SPI to write to SD card
       3.     When a character is received on UART, event triggers DMA
       4.     DMA moves character from UART to SPI
       5.     SPI automatically writes to SD
       6.     CPU wakes up when needed to write to card / change address
              Atmel Application Notes

 Incredibly useful examples
   Direct from Atmel

   Includes source code and everything

 Link: Relevant App Notes for Different Modules


 Doing things from scratch is also OK

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:37
posted:11/5/2011
language:English
pages:14