Xmega Application Note | |||||
This file contains example code that demonstrate the quadrature decoder. It shows how to set up the event system and the Timer/Counter to decode the angle of rotation from a quadrature encoder.
Copyright (c) 2008, Atmel Corporation All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
3. The name of ATMEL may not be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Definition in file qdec_example.c.
#include "avr_compiler.h"
#include "qdec_driver.h"
#include "qdec_signal_generator.h"
Go to the source code of this file.
Defines | |
#define | CLOCK_DIV 64 |
#define | CLOCK_DIV_bm TC_CLKSEL_DIV64_gc |
Defines the clock division for the timer used. if changed both defines NEED to be changed correspondingly. | |
Functions | |
int | main (void) |
Quadrature decoding example. | |
Variables | |
uint16_t | calcFreq = 0 |
uint16_t | calcRPM = 0 |
uint16_t | captureFreq = 0 |
Global frequency variable. | |
bool | dir = 0 |
Direction of the output signal. | |
uint8_t | freq = 60 |
If GENERATE_TEST_SIGNAL is defined the system generates a test signal with frequency equal to freq (RPM = freq*60). | |
uint8_t | lineCount = 30 |
Number of lines in the quadrature encoder. |
#define CLOCK_DIV 64 |
#define CLOCK_DIV_bm TC_CLKSEL_DIV64_gc |
Defines the clock division for the timer used. if changed both defines NEED to be changed correspondingly.
Definition at line 69 of file qdec_example.c.
Referenced by main().
int main | ( | void | ) |
Quadrature decoding example.
This is the main example code demonstrating the setup and configuration to make the quadrature decoder work. The example use the event system and a Timer/Counter to decode the signals and the counter value in the Timer/Counter will reflect the angle of rotation. The direction bit in the TC reflect the current direction of rotation.
Hardware setup:
Peripherals used:
Definition at line 99 of file qdec_example.c.
References calcFreq, calcRPM, CLOCK_DIV, CLOCK_DIV_bm, dir, F_CPU, freq, generate_qdec_signal(), GetCaptureValue, lineCount, QDEC_TC_Freq_Setup(), and QDEC_Total_Setup().
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 }
uint16_t calcFreq = 0 |
uint16_t calcRPM = 0 |
uint16_t captureFreq = 0 |
bool dir = 0 |
uint8_t freq = 60 |
If GENERATE_TEST_SIGNAL is defined the system generates a test signal with frequency equal to freq (RPM = freq*60).
Output frequency when system generates a signal
Definition at line 61 of file qdec_example.c.
Referenced by main().
uint8_t lineCount = 30 |
Number of lines in the quadrature encoder.
Definition at line 73 of file qdec_example.c.
Referenced by main().
Generated on Wed Jul 30 09:22:37 2008 for AVR1600 Using the XMEGA Quadrature Decoder by ![]() |