AVR1600 Using the XMEGA Quadrature Decoder
qdec_signal_generator.c File Reference

Test signal generation, generates a Quadrature signal. More...

Include dependency graph for qdec_signal_generator.c:

Go to the source code of this file.

Functions

void generate_qdec_signal (PORT_t *qPort, uint8_t lineCount, uint8_t freq, bool dir)
 Initializes TCE0 to create Qadrature signal. More...
 
 ISR (TCE0_CCA_vect)
 Creates a Quadrature signal, up or down counting depending on the configured direction(dir = 1 -> CW/forward, dir = 0 -> CCW/backward). More...
 
 ISR (TCE0_CCB_vect)
 Creates a Quadrature signal, up or down counting depending on the configured direction(dir = 1 -> CW/forward, dir = 0 -> CCW/backward). More...
 
 ISR (TCE0_CCC_vect)
 Creates a Quadrature signal, up or down counting depending on the configured direction(dir = 1 -> CW/forward, dir = 0 -> CCW/backward). More...
 
 ISR (TCE0_CCD_vect)
 Creates a Quadrature signal, up or down counting depending on the configured direction(dir = 1 -> CW/forward, dir = 0 -> CCW/backward). More...
 
 ISR (TCC0_ERR_vect)
 Error interrupt routine for when index signal is used. More...
 

Variables

PORT_t * q_test_sig_Port
 The port to set the signal out on. More...
 
uint8_t test_lineCount
 Number of lines in the Quadrature encoder. More...
 

Detailed Description

Test signal generation, generates a Quadrature signal.

This file contains code to generate a Quadrature signal for testing of the system. The signal generated goes either direction given by the argument "dir" passed to generate_qdec_signal(). (1->CW/forward, 0->CCW/backward)

To use the test signal the GENERATE_TEST_SIGNAL has to be uncommented in the qdec_example.c file. The generate_qdec_signal will then be included and a signal generated. The signal output pins then has to be connected with the input pins to the Quadrature decoder.

When the signal is connected and the output portC is connected to the led’s the frequency value will be displayed in hex, pin0 LSB.

To test that the index signal is correct, when enabled, one can set a breakpoint in the timer counter C0 error interrupt routine. If the interrupt routine is run more than 1 time (first time resets the count) the index setting is wrong. If it is correct the execution should not break when the index signal is connected, but if the signal is disconnected (remove cable) the code should break.

If a scope is used the frequency measurement can be controlled, the measured frequency should be equal to the frequency on the scope given by the time between the index signal.

Application note:
AVR1600: Using the XMEGA Quadrature Decoder
Documentation
For comprehensive code documentation, supported compilers, compiler settings and supported devices see readme.html
Author
Atmel Corporation, now Microchip Technology Inc.: http://www.microchip.com
Revision
7623
Date
2017-09-06 14:57:02 +0200 (on, 06 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_signal_generator.c.

Function Documentation

◆ generate_qdec_signal()

void generate_qdec_signal ( PORT_t *  qPort,
uint8_t  lineCount,
uint8_t  freq,
bool  dir 
)

Initializes TCE0 to create Qadrature signal.

Calculates the values to create a Quadrature signal that has a frequency given from the freq parameter.

Parameters
qPortThe port to set the signal out on.
lineCountThe number of lines in the Quadrature encoder.
freqThe frequency that is put out, (time between index signal/one rotation).
dirThe direction of the signal to be generated, clockwise or counterclockwise signal.

Definition at line 94 of file qdec_signal_generator.c.

References F_CPU, lineCount, q_test_sig_Port, and test_lineCount.

Referenced by main().

95 {
96  uint16_t ticks, quarter, half_quarter;
97 
98  /* The following code calculates the upper boundary of the timer and the
99  * interrupt positions to get a correct Quadrature signal of the given frequency.
100  *
101  * The different compare interrupts sets the phase0 and phase90 signals.
102  * Compare A interrupt sets phase0 and clears phase90
103  * Compare B interrupt sets phase0 and phase90
104  * Compare C interrupt clears phase0 and sets phase90
105  * Compare D interrupt clears phase0 and phase90.
106  *
107  * Compare A interrupt also sets the index signal when one round has passed.
108  */
109 
110  /* Calculates upper boundary of timer to get desired frequency.*/
111  ticks = F_CPU / (freq * lineCount);
112  quarter = ticks/4;
113  half_quarter = ticks/8;
114 
115  if(dir == 1){
116  TCE0.CCA = half_quarter;
117  TCE0.CCB = half_quarter+quarter;
118  TCE0.CCC = half_quarter+(2*quarter);
119  TCE0.CCD = half_quarter+(3*quarter);
120  }else{
121  TCE0.CCA = half_quarter+(3*quarter);
122  TCE0.CCB = half_quarter+(2*quarter);
123  TCE0.CCC = half_quarter+quarter;
124  TCE0.CCD = half_quarter;
125  }
126 
127  TCE0.PER = ticks;
128  TCE0.CTRLA = TC_CLKSEL_DIV1_gc;
129 
130  /* Enable low level interrupt on CCA, CCB, CCC and CCD.*/
131  TCE0.INTCTRLB = TC0_CCAINTLVL0_bm | TC0_CCBINTLVL0_bm |
132  TC0_CCCINTLVL0_bm | TC0_CCDINTLVL0_bm;
133  TCC0.INTCTRLA = TC0_ERRINTLVL0_bm;
134 
135  qPort->DIRSET = 0xFF;
136  q_test_sig_Port = qPort;
137 
139 }
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
uint8_t test_lineCount
Number of lines in the Quadrature encoder.
uint8_t lineCount
Number of lines in the quadrature encoder.
Definition: qdec_example.c:72
bool dir
Direction of the output signal.
Definition: qdec_example.c:63
PORT_t * q_test_sig_Port
The port to set the signal out on.
#define F_CPU
Define default CPU frequency, if this is not already defined.
Definition: avr_compiler.h:49

◆ ISR() [1/5]

ISR ( TCE0_CCA_vect  )

Creates a Quadrature signal, up or down counting depending on the configured direction(dir = 1 -> CW/forward, dir = 0 -> CCW/backward).

Definition at line 145 of file qdec_signal_generator.c.

References q_test_sig_Port, and test_lineCount.

146 {
147  static uint16_t i = 0;
148 
149  /* Set pin0 Phase0 signal.*/
150  q_test_sig_Port->OUT = (q_test_sig_Port->OUT & ~0x03) | 0x01;
151 
152  i++;
153 
154  /* Clear index.*/
155  q_test_sig_Port->OUT = (q_test_sig_Port->OUT & ~0x04);
156 
157  /* When (i = test_lineCount) one round has passed.
158  * Includes a check for i "bigger than" for error handling.
159  */
160  if(i>=test_lineCount){
161 
162  /* Set index. Lasts 4 states.*/
163  q_test_sig_Port->OUT |= 0x04;
164  i = 0;
165  }
166 }
uint8_t test_lineCount
Number of lines in the Quadrature encoder.
PORT_t * q_test_sig_Port
The port to set the signal out on.

◆ ISR() [2/5]

ISR ( TCE0_CCB_vect  )

Creates a Quadrature signal, up or down counting depending on the configured direction(dir = 1 -> CW/forward, dir = 0 -> CCW/backward).

Definition at line 172 of file qdec_signal_generator.c.

References q_test_sig_Port.

173 {
174  /* Set pin0 and pin1 phase0 and phase90 signal.*/
175  q_test_sig_Port->OUT = (q_test_sig_Port->OUT & ~0x03) | 0x01 | 0x02;
176 }
PORT_t * q_test_sig_Port
The port to set the signal out on.

◆ ISR() [3/5]

ISR ( TCE0_CCC_vect  )

Creates a Quadrature signal, up or down counting depending on the configured direction(dir = 1 -> CW/forward, dir = 0 -> CCW/backward).

Definition at line 182 of file qdec_signal_generator.c.

References q_test_sig_Port.

183 {
184  /* Set pin1 phase90 signal. Clear pin0 phase0 signal.*/
185  q_test_sig_Port->OUT = (q_test_sig_Port->OUT & ~0x03) | 0x02;
186 }
PORT_t * q_test_sig_Port
The port to set the signal out on.

◆ ISR() [4/5]

ISR ( TCE0_CCD_vect  )

Creates a Quadrature signal, up or down counting depending on the configured direction(dir = 1 -> CW/forward, dir = 0 -> CCW/backward).

Definition at line 192 of file qdec_signal_generator.c.

References q_test_sig_Port.

193 {
194  /* Clear pin0 and pin1, phase0 and phase90 signal.*/
195  q_test_sig_Port->OUT = (q_test_sig_Port->OUT & ~0x03);
196 }
PORT_t * q_test_sig_Port
The port to set the signal out on.

◆ ISR() [5/5]

ISR ( TCC0_ERR_vect  )

Error interrupt routine for when index signal is used.

This interrupt happens if the count value is not at BOTTOM when the index signal comes. The interrupt also happens if the count value passes the BOTTOM value.

Definition at line 205 of file qdec_signal_generator.c.

References q_test_sig_Port.

206 {
207  static uint8_t j = 0;
208  j++;
209 
210  /* Since index needs to initialize, one error will happen first round.
211  * If output is desired at every error, remove if statement or set (j>0).
212  */
213  if(j>2){
214 
215  /* To test if index works, set breakpoint here.
216  * It should NOT break when index is correct.
217  */
218  q_test_sig_Port->OUT = q_test_sig_Port->OUT ^ 0x40;
219  j=0;
220  }
221 }
PORT_t * q_test_sig_Port
The port to set the signal out on.

Variable Documentation

◆ q_test_sig_Port

PORT_t* q_test_sig_Port

The port to set the signal out on.

Definition at line 75 of file qdec_signal_generator.c.

Referenced by generate_qdec_signal(), and ISR().

◆ test_lineCount

uint8_t test_lineCount

Number of lines in the Quadrature encoder.

Definition at line 79 of file qdec_signal_generator.c.

Referenced by generate_qdec_signal(), and ISR().

@DOC_TITLE@
Generated on Thu Oct 26 2017 13:33:37 for AVR1600 Using the XMEGA Quadrature Decoder by doxygen 1.8.13