Xmega Application Note


qdec_signal_generator.c File Reference


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 a define the CW_DIR_SIG gives a signal that counts in clockwise direction. The CCW_DIR_SIG define gives a signal in counter clockwise direction.

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: http://www.atmel.com
Support email: avr@atmel.com
Revision
1699
Date
2008-07-30 09:20:10 +0200 (on, 30 jul 2008)

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.

#include "avr_compiler.h"
#include "qdec_signal_generator.h"

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.
 ISR (TCC0_ERR_vect)
 Error interrupt routine for when index signal is used.
 ISR (TCE0_CCD_vect)
 Creates a Quadrature signal, up or down counting depending on CW_DIR_SIG or CCW_DIR_SIG.
 ISR (TCE0_CCC_vect)
 Creates a Quadrature signal, up or down counting depending on CW_DIR_SIG or CCW_DIR_SIG.
 ISR (TCE0_CCB_vect)
 Creates a Quadrature signal, up or down counting depending on CW_DIR_SIG or CCW_DIR_SIG.
 ISR (TCE0_CCA_vect)
 Creates a Quadrature signal, up or down counting depending on CW_DIR_SIG or CCW_DIR_SIG.

Variables

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


Function Documentation

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:
qPort The port to set the signal out on.
lineCount The number of lines in the Quadrature encoder.
freq The frequency that is put out, (time between index signal/one rotation).
dir The direction of the signal to be generated, clockwise or counterclockwise signal.

Definition at line 95 of file qdec_signal_generator.c.

References F_CPU, q_test_sig_Port, and test_lineCount.

Referenced by main().

00096 {
00097         uint16_t ticks, quarter, half_quarter;
00098 
00099         /* The following code calculates the upper boundary of the timer and the
00100          * interrupt positions to get a correct Quadrature signal of the given frequency.
00101          *
00102          * The different compare interrupts sets the phase0 and phase90 signals.
00103          * Compare A interrupt sets phase0 and clears phase90
00104          * Compare B interrupt sets phase0 and phase90
00105          * Compare C interrupt clears phase0 and sets phase90
00106          * Compare D interrupt clears phase0 and phase90.
00107          *
00108          * Ccompare A interrupt also sets the index signal when one round has passed.
00109          */
00110 
00111         /* Calculates upper boundary of timer to get desired frequency.*/
00112         ticks = F_CPU / (freq * lineCount);
00113         quarter = ticks/4;
00114         half_quarter = ticks/8;
00115 
00116         if(dir == 1){
00117                 TCE0.CCA = half_quarter;
00118                 TCE0.CCB = half_quarter+quarter;
00119                 TCE0.CCC = half_quarter+(2*quarter);
00120                 TCE0.CCD = half_quarter+(3*quarter);
00121         }else{
00122                 TCE0.CCA = half_quarter+(3*quarter);
00123                 TCE0.CCB = half_quarter+(2*quarter);
00124                 TCE0.CCC = half_quarter+quarter;
00125                 TCE0.CCD = half_quarter;
00126         }
00127 
00128         TCE0.PER = ticks;
00129         TCE0.CTRLA = TC_CLKSEL_DIV1_gc;
00130 
00131         /* Enable low level interrupt on CCA, CCB, CCC and CCD.*/
00132         TCE0.INTCTRLB = TC0_CCAINTLVL0_bm | TC0_CCBINTLVL0_bm |
00133                         TC0_CCCINTLVL0_bm | TC0_CCDINTLVL0_bm;
00134         TCC0.INTCTRLA = TC0_ERRINTLVL0_bm;
00135 
00136         qPort->DIRSET = 0xFF;
00137         q_test_sig_Port = qPort;
00138 
00139         test_lineCount = lineCount;
00140 }

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 206 of file qdec_signal_generator.c.

References q_test_sig_Port.

00207 {
00208         static uint8_t j = 0;
00209         j++;
00210 
00211         /* Since index needs to initialize, one error will happen first round.
00212          * If output is desired at every error, remove if statement or set (j>0).
00213          */
00214         if(j>2){
00215 
00216                 /* To test if index works, set breakpoint here.
00217                  * It should NOT break when index is correct.
00218                  */
00219                 q_test_sig_Port->OUT = q_test_sig_Port->OUT ^ 0x40;
00220                 j=0;
00221         }
00222 }

ISR ( TCE0_CCD_vect   ) 

Creates a Quadrature signal, up or down counting depending on CW_DIR_SIG or CCW_DIR_SIG.

Definition at line 193 of file qdec_signal_generator.c.

References q_test_sig_Port.

00194 {
00195         /* Clear pin0 and pin1, phase0 and phase90 signal.*/
00196         q_test_sig_Port->OUT = (q_test_sig_Port->OUT & ~0x03);
00197 }

ISR ( TCE0_CCC_vect   ) 

Creates a Quadrature signal, up or down counting depending on CW_DIR_SIG or CCW_DIR_SIG.

Definition at line 183 of file qdec_signal_generator.c.

References q_test_sig_Port.

00184 {
00185         /* Set pin1 phase90 signal. Clear pin0 phase0 signal.*/
00186         q_test_sig_Port->OUT = (q_test_sig_Port->OUT & ~0x03) | 0x02;
00187 }

ISR ( TCE0_CCB_vect   ) 

Creates a Quadrature signal, up or down counting depending on CW_DIR_SIG or CCW_DIR_SIG.

Definition at line 173 of file qdec_signal_generator.c.

References q_test_sig_Port.

00174 {
00175         /* Set pin0 and pin1 phase0 and phase90 signal.*/
00176         q_test_sig_Port->OUT = (q_test_sig_Port->OUT & ~0x03) | 0x01 | 0x02;
00177 }

ISR ( TCE0_CCA_vect   ) 

Creates a Quadrature signal, up or down counting depending on CW_DIR_SIG or CCW_DIR_SIG.

Definition at line 146 of file qdec_signal_generator.c.

References q_test_sig_Port, and test_lineCount.

00147 {
00148         static uint16_t i = 0;
00149         
00150         /* Set pin0 Phase0 signal.*/
00151         q_test_sig_Port->OUT = (q_test_sig_Port->OUT & ~0x03) | 0x01; 
00152 
00153         i++;
00154 
00155         /* Clear index.*/
00156         q_test_sig_Port->OUT = (q_test_sig_Port->OUT & ~0x04);
00157 
00158         /*  When (i = test_lineCount) one round has passed.
00159          *  Includes a check for i "bigger than" for error handling.
00160          */
00161         if(i>=test_lineCount){
00162 
00163                 /* Set index. Lasts 4 states.*/
00164                 q_test_sig_Port->OUT |= 0x04;
00165                 i = 0;
00166         }
00167 }


Variable Documentation

PORT_t* q_test_sig_Port

The port to set the signal out on.

Definition at line 76 of file qdec_signal_generator.c.

Referenced by generate_qdec_signal(), and ISR().

uint8_t test_lineCount

Number of lines in the Quadrature encoder.

Definition at line 80 of file qdec_signal_generator.c.

Referenced by generate_qdec_signal(), and ISR().

@DOC_TITLE@
Generated on Wed Jul 30 09:22:38 2008 for AVR1600 Using the XMEGA Quadrature Decoder by doxygen 1.5.5