| AVR1600 Using the XMEGA Quadrature Decoder

Macros | Functions | Variables
qdec_example.c File Reference

The XMEGA Quadrature Decoder example source code. More...

`#include "avr_compiler.h"`
`#include "qdec_driver.h"`
`#include "qdec_signal_generator.h"`
Include dependency graph for qdec_example.c:

Go to the source code of this file.

## Macros
#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. More...

## Functions
int | main (void)
| Quadrature decoding example. More...

## Variables
uint16_t | calcFreq = 0
uint16_t | calcRPM = 0
uint16_t | captureFreq = 0
| Global frequency variable. More...
bool | dir = CW_DIR
| Direction of the output signal. More...
uint8_t | freq = 60
| If GENERATE_TEST_SIGNAL is defined the system generates a test signal with frequency equal to freq (RPM = freq*60). More...
uint8_t | lineCount = 30
| Number of lines in the quadrature encoder. More...

## Detailed Description

The XMEGA Quadrature Decoder example source code.

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.

Application note:
  AVR1600: Using the XMEGA Quadrature Decoder

Documentation
  For comprehensive code documentation, supported compilers, compiler settings and supported devices see readme.html

Author
  Former Atmel Corporation: http://www.microchip.com

Revision
  7626

Date
  2017-09-07 11:28:20 +0200 (to, 07 sep 2017)

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.

## Macro Definition Documentation

## ◆ CLOCK_DIV

#define CLOCK_DIV 64

Definition at line 69 of file qdec_example.c.

Referenced by main().

## ◆ CLOCK_DIV_bm

#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 68 of file qdec_example.c.

Referenced by main().

## Function Documentation

## ◆ 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:

- PD0 - QDPH0
- PD1 - QDPH90
- PD2 - QDINDX

Peripherals used:

- PORTD[2:0] signal input.
- Event channel 0: quadrature signal.
- Event channel 1: index signal.
- TCC0: Quadrature counter.

Definition at line 98 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().

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.*/
126 generate_qdec_signal(&PORTE, lineCount, freq, dir);
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 }
CLOCK_DIV_bm
#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
freq
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
GetCaptureValue
#define GetCaptureValue(_tc)
This macro return the value of the capture register.
Definition: qdec_driver.h:72
QDEC_Total_Setup
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
lineCount
uint8_t lineCount
Number of lines in the quadrature encoder.
Definition: qdec_example.c:72
generate_qdec_signal
void generate_qdec_signal(PORT_t *qPort, uint8_t lineCount, uint8_t freq, bool dir)
Initializes TCE0 to create Qadrature signal.
Definition: qdec_signal_generator.c:94
dir
bool dir
Direction of the output signal.
Definition: qdec_example.c:63
calcRPM
uint16_t calcRPM
Definition: qdec_example.c:77
CLOCK_DIV
#define CLOCK_DIV
Definition: qdec_example.c:69
F_CPU
#define F_CPU
Define default CPU frequency, if this is not already defined.
Definition: avr_compiler.h:49
QDEC_TC_Freq_Setup
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
calcFreq
uint16_t calcFreq
Definition: qdec_example.c:76
Here is the call graph for this function:

## Variable Documentation

## ◆ calcFreq

uint16_t calcFreq = 0

Definition at line 76 of file qdec_example.c.

Referenced by main().

## ◆ calcRPM

uint16_t calcRPM = 0

Definition at line 77 of file qdec_example.c.

Referenced by main().

## ◆ captureFreq

uint16_t captureFreq = 0

Global frequency variable.

Definition at line 75 of file qdec_example.c.

## ◆ dir

bool dir = CW_DIR

Direction of the output signal.

Definition at line 63 of file qdec_example.c.

Referenced by main().

## ◆ freq

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 60 of file qdec_example.c.

Referenced by main().

## ◆ lineCount

uint8_t lineCount = 30

Number of lines in the quadrature encoder.

Definition at line 72 of file qdec_example.c.

Referenced by generate_qdec_signal(), and main().

@DOC_TITLE@