AVR1600 Using the XMEGA Quadrature Decoder
qdec_example.c
Go to the documentation of this file.
1 /* This file has been prepared for Doxygen automatic documentation generation. */
50 #include "avr_compiler.h"
51 #include "qdec_driver.h"
52 #include "qdec_signal_generator.h"
53 
57 //#define GENERATE_TEST_SIGNAL
58 
60 uint8_t freq = 60;
61 
63 bool dir = CW_DIR;
64 
68 #define CLOCK_DIV_bm TC_CLKSEL_DIV64_gc
69 #define CLOCK_DIV 64
70 
72 uint8_t lineCount = 30;
73 
75 uint16_t captureFreq = 0;
76 uint16_t calcFreq = 0;
77 uint16_t calcRPM = 0;
78 
98 int main( void )
99 {
100  /* Set up port C as output. */
101  PORTC.DIRSET = 0xFF;
102 
103  /* Setup PORTD with pin 0 as input for QDPH0, dont invert IO pins.
104  *
105  * Setup event channel 0, pin 0 on PORTD as input, don't enable index.
106  * if index used then state 00 is set as the index reset state.
107  *
108  * Setup TCC0 with Event channel 0 and lineCount.
109  */
110  QDEC_Total_Setup(&PORTD, /*PORT_t * qPort*/
111  0, /*uint8_t qPin*/
112  false, /*bool invIO*/
113  0, /*uint8_t qEvMux*/
114  EVSYS_CHMUX_PORTD_PIN0_gc, /*EVSYS_CHMUX_t qPinInput*/
115  false, /*bool useIndex*/
116  EVSYS_QDIRM_00_gc, /*EVSYS_QDIRM_t qIndexState*/
117  &TCC0, /*TC0_t * qTimer*/
118  TC_EVSEL_CH0_gc, /*TC_EVSEL_t qEventChannel*/
119  lineCount); /*uint8_t lineCount*/
120 
121  /* Setup TCD0 with Event channel 2 for same pin as QDPH0 and clk div 64. */
122  QDEC_TC_Freq_Setup(&TCD0, TC_EVSEL_CH2_gc, EVSYS_CHMUX_PORTD_PIN0_gc, CLOCK_DIV_bm);
123 
124 #ifdef GENERATE_TEST_SIGNAL
125  /* Initialize and start outputting quadrature signal.*/
127 
128  /* Enable low level interrupt.*/
129  PMIC.CTRL |= PMIC_LOLVLEN_bm;
130 
131  /* Enable global interrupts.*/
132  sei();
133 #endif
134 
135  /* Display the frequency of rotation on LEDs */
136  while (1) {
137 
138  if ((TCD0.INTFLAGS & TC0_CCAIF_bm) != 0) {
139  /* Period of counter ticks are 1/(F_CPU/clk_div)
140  * Real tick count is 4 times captured value
141  * (when used with quadratur signal).
142  * Real frequency is then (F_CPU/clk_div)/(capture_value * linecount)
143  * For output in RPM multiply frequency by 60 (60 sec per min).*/
144  calcFreq = (F_CPU / CLOCK_DIV) /
145  ((GetCaptureValue(TCD0) & 0xFFFC) * lineCount );
146  calcRPM = calcFreq*60;
147  PORTC.OUT = ~(calcFreq);
148  }
149  }
150 }
#define CLOCK_DIV_bm
Defines the clock division for the timer used. if changed both defines NEED to be changed correspondi...
Definition: qdec_example.c:68
uint8_t freq
If GENERATE_TEST_SIGNAL is defined the system generates a test signal with frequency equal to freq (R...
Definition: qdec_example.c:60
This file implements some macros that makes the IAR C-compiler and avr-gcc work with the same code ba...
#define GetCaptureValue(_tc)
This macro return the value of the capture register.
Definition: qdec_driver.h:72
uint16_t captureFreq
Global frequency variable.
Definition: qdec_example.c:75
The XMEGA Quadrature Decoder driver header file.
bool QDEC_Total_Setup(PORT_t *qPort, uint8_t qPin, bool invIO, uint8_t qEvMux, EVSYS_CHMUX_t qPinInput, bool useIndex, EVSYS_QDIRM_t qIndexState, TC0_t *qTimer, TC_EVSEL_t qEventChannel, uint8_t lineCount)
Wrapperfunction to set up all parameters for the quadrature decoder.
Definition: qdec_driver.c:87
uint8_t lineCount
Number of lines in the quadrature encoder.
Definition: qdec_example.c:72
void generate_qdec_signal(PORT_t *qPort, uint8_t lineCount, uint8_t freq, bool dir)
Initializes TCE0 to create Qadrature signal.
bool dir
Direction of the output signal.
Definition: qdec_example.c:63
uint16_t calcRPM
Definition: qdec_example.c:77
#define CLOCK_DIV
Definition: qdec_example.c:69
#define CW_DIR
Definition: qdec_driver.h:63
#define F_CPU
Define default CPU frequency, if this is not already defined.
Definition: avr_compiler.h:49
int main(void)
Quadrature decoding example.
Definition: qdec_example.c:98
void QDEC_TC_Freq_Setup(TC0_t *qTimer, TC_EVSEL_t qEventChannel, EVSYS_CHMUX_t qPinInput, TC_CLKSEL_t clksel)
This function set up the needed configuration for a Timer/Counter to handle the frequency/speed measu...
Definition: qdec_driver.c:231
uint16_t calcFreq
Definition: qdec_example.c:76
@DOC_TITLE@
Generated on Thu Oct 26 2017 13:33:37 for AVR1600 Using the XMEGA Quadrature Decoder by doxygen 1.8.13