Xmega Application Note


qdec_example.c

Go to the documentation of this file.
00001 /* This file has been prepared for Doxygen automatic documentation generation. */
00051 #include "avr_compiler.h"
00052 #include "qdec_driver.h"
00053 #include "qdec_signal_generator.h"
00054 
00058 //#define GENERATE_TEST_SIGNAL
00059 
00061 uint8_t freq = 60;
00062 
00064 bool    dir  = 0;
00065 
00069 #define CLOCK_DIV_bm  TC_CLKSEL_DIV64_gc
00070 #define CLOCK_DIV     64
00071 
00073 uint8_t lineCount = 30;
00074 
00076 uint16_t captureFreq  = 0;
00077 uint16_t calcFreq     = 0;
00078 uint16_t calcRPM      = 0;
00079 
00099 int main( void )
00100 {
00101         /* Set up port C as output. */
00102         PORTC.DIRSET = 0xFF;
00103 
00104         /* Setup PORTD with pin 0 as input for QDPH0, dont invert IO pins.
00105          *
00106          * Setup event channel 0, pin 0 on PORTD as input, don't enable index.
00107          * if index used then state 00 is set as the index reset state.
00108          *
00109          * Setup TCC0 with Event channel 0 and lineCount.
00110          */
00111         QDEC_Total_Setup(&PORTD,                    /*PORT_t * qPort*/
00112                          0,                         /*uint8_t qPin*/
00113                          false,                     /*bool invIO*/
00114                          0,                         /*uint8_t qEvMux*/
00115                          EVSYS_CHMUX_PORTD_PIN0_gc, /*EVSYS_CHMUX_t qPinInput*/
00116                          false,                     /*bool useIndex*/
00117                          EVSYS_QDIRM_00_gc,         /*EVSYS_QDIRM_t qIndexState*/
00118                          &TCC0,                     /*TC0_t * qTimer*/
00119                          TC_EVSEL_CH0_gc,           /*TC_EVSEL_t qEventChannel*/
00120                          lineCount);                /*uint8_t lineCount*/
00121 
00122         /* Setup TCD0 with Event channel 2 for same pin as QDPH0 and clk div 64. */
00123         QDEC_TC_Freq_Setup(&TCD0, TC_EVSEL_CH2_gc, EVSYS_CHMUX_PORTD_PIN0_gc, CLOCK_DIV_bm);
00124 
00125 #ifdef GENERATE_TEST_SIGNAL
00126         /* Initialize and start outputting quadrature signal.*/
00127         generate_qdec_signal(&PORTE, lineCount, freq, dir);
00128 
00129         /* Enable low level interrupt.*/
00130         PMIC.CTRL |= PMIC_LOLVLEN_bm;
00131 
00132         /* Enable global interrupts.*/
00133         sei();
00134 #endif
00135         
00136         /* Display the frequency of rotation on LEDs */
00137         while (1) {
00138 
00139                 if ((TCD0.INTFLAGS & TC0_CCAIF_bm) !=  0) {
00140                         /* Period of counter ticks are 1/(F_CPU/clk_div)
00141                          * Real tick count is 4 times captured value
00142                          * (when used with quadratur signal).
00143                          * Real frequency is then (F_CPU/clk_div)/(capture_value * linecount)
00144                          * For output in RPM multiply frequency by 60 (60 sec per min).*/
00145                         calcFreq = (F_CPU / CLOCK_DIV) /
00146                                    ((GetCaptureValue(TCD0) & 0xFFFC) * lineCount );
00147                         calcRPM = calcFreq*60;
00148                         PORTC.OUT = ~(calcFreq);
00149                 }
00150         }
00151 }
@DOC_TITLE@
Generated on Wed Jul 30 09:22:36 2008 for AVR1600 Using the XMEGA Quadrature Decoder by doxygen 1.5.5