qdec_example.c File Reference


Detailed Description

QDEC example driver for AVR32 UC3.

This file provides an example for the QDEC on AVR32 UC3 devices.

Author:
Atmel Corporation: http://www.atmel.com
Support and FAQ: http://support.atmel.no/

Definition in file qdec_example.c.

#include <avr32/io.h>
#include "intc.h"
#include "board.h"
#include "compiler.h"
#include "qdec.h"
#include "gpio.h"
#include "power_clocks_lib.h"

Go to the source code of this file.

Defines

#define FCPU_HZ   60000000
#define FPBA_HZ   FCPU_HZ

Functions

int main (void)
 Main function. Execution starts here.
static void qdec_int_handler (void)

Variables

volatile unsigned char flag_qdec = 0
volatile avr32_qdec_t * qdec = &AVR32_QDEC0
System Clock Frequencies
static pcl_freq_param_t pcl_freq_param


Define Documentation

#define FCPU_HZ   60000000

Definition at line 87 of file qdec_example.c.

#define FPBA_HZ   FCPU_HZ

Definition at line 88 of file qdec_example.c.


Function Documentation

int main ( void   ) 

Main function. Execution starts here.

Definition at line 128 of file qdec_example.c.

References flag_qdec, pcl_freq_param, qdec, qdec_configure_interrupts(), qdec_init_timer_mode(), qdec_int_handler(), qdec_software_trigger(), QDEC_TSIR_DOWN, qdec_write_pc_cmp(), qdec_write_pc_cnt(), qdec_write_rc_cmp(), and qdec_write_rc_cnt().

00129 {   
00130   // Options for QDEC timer Mode
00131   static const qdec_timer_opt_t QUADRATURE_TIMER_OPT =
00132   {
00133     .upd      = DISABLED,             // Up/Down Mode Timer Disabled.
00134     .tsdir    = QDEC_TSIR_DOWN,       // Count Down Timer.
00135     .filten   = FALSE,                // Disable filtering.
00136     .evtrge   = FALSE,                // Disable event triggering.
00137     .rcce     = TRUE,                 // Enable Position Compare.
00138     .pcce     = TRUE,                 // Enable Revolution Compare.
00139   };
00140   // Options for QDEC Interrupt Management
00141   static const qdec_interrupt_t QDEC_INTERRUPT =
00142   {
00143     .cmp    = 1,                      // Enable Compare Interrupt.
00144     .cap    = 0,                      // Disable Capture Interrupt.
00145     .pcro   = 0,                      // Disable Position Roll-over Interrupt.  
00146     .rcro   = 0                       // Disable Counter Roll-over Interrupt.  
00147   };
00148     
00149   // Configure system clocks.
00150   if (pcl_configure_clocks(&pcl_freq_param) != PASS) {
00151     while(1);
00152   }  
00153 
00154   // Setup the generic clock for QDEC
00155   scif_gc_setup(AVR32_SCIF_GCLK_QDEC0, 
00156                 SCIF_GCCTRL_OSC0, 
00157                 AVR32_SCIF_GC_NO_DIV_CLOCK, 
00158                 0);  
00159   // Now enable the generic clock
00160   scif_gc_enable(AVR32_SCIF_GCLK_QDEC0);
00161   
00162   Disable_global_interrupt();
00163  
00164   // Initialize interrupt vectors.
00165   INTC_init_interrupts();
00166 
00167   // Register the QDEC interrupt handler to the interrupt controller.
00168   INTC_register_interrupt(&qdec_int_handler, AVR32_QDEC0_IRQ, AVR32_INTC_INT0);
00169   
00170   Enable_global_interrupt();
00171   
00172   // Initialization of counter value as a 32-bit counter to 0x0000FFFF (Rc=0x0000/Pc=0xFFFF)
00173   qdec_write_rc_cnt(qdec,0x0000);
00174   qdec_write_pc_cnt(qdec,0xffff);
00175   // Initialization of compare value to 0x0 as the interrupt will be generated when the counter value will be equal to 0
00176   qdec_write_rc_cmp(qdec,0);
00177   qdec_write_pc_cmp(qdec,0);
00178   
00179   // Initialize the QDEC in quadrature decoder mode.
00180   qdec_init_timer_mode(qdec,&QUADRATURE_TIMER_OPT);
00181 
00182   // Configure the QDEC interrupts.
00183   qdec_configure_interrupts(qdec,&QDEC_INTERRUPT);
00184 
00185    // Start the QDEC.
00186   qdec_software_trigger(qdec);
00187 
00188   unsigned int pc_cmp = 0;
00189   while(1)
00190   {
00191     // Compare Interrupt Flag
00192     if(flag_qdec == 1)
00193     {
00194         gpio_tgl_gpio_pin(LED0_GPIO);           // Check signal on oscilloscope.
00195         if (pc_cmp < 0xffff ) pc_cmp +=  0x100; // Increase PC CMP
00196         else pc_cmp = 0;                        // Start the signal pattern over.
00197         qdec_write_pc_cmp(qdec,pc_cmp);         // Reload Compare Flag
00198         flag_qdec = 0;                          // Reset Interrupt Flag
00199     }
00200   }
00201 }

static void qdec_int_handler ( void   )  [static]

QDEC Interrupts handler.

Definition at line 115 of file qdec_example.c.

References flag_qdec, qdec, qdec_write_pc_cnt(), and qdec_write_rc_cnt().

Referenced by main().

00116 {
00117   // Reset CNT to CNT = 0x0000FFFF
00118   qdec_write_rc_cnt(qdec,0x00);
00119   qdec_write_pc_cnt(qdec,0xffff);  
00120   // Synchronize with the main loop.
00121   flag_qdec = 1;  
00122   // Clear the comparator interrupt.
00123   qdec->scr = (1<<AVR32_QDEC_SCR_CMP_OFFSET);
00124 }


Variable Documentation

volatile unsigned char flag_qdec = 0

Definition at line 105 of file qdec_example.c.

Referenced by main(), and qdec_int_handler().

pcl_freq_param_t pcl_freq_param [static]

Initial value:

{
  .cpu_f        = FCPU_HZ,
  .pba_f        = FPBA_HZ,
  .osc0_f       = FOSC0,
  .osc0_startup = OSC0_STARTUP
}

Definition at line 92 of file qdec_example.c.

Referenced by main().

volatile avr32_qdec_t* qdec = &AVR32_QDEC0

Definition at line 102 of file qdec_example.c.

Referenced by main(), and qdec_int_handler().


Generated on Fri Feb 19 02:26:13 2010 for AVR32 - QDEC Driver by  doxygen 1.5.5