Xmega Application Note


qdec_driver.c File Reference


Detailed Description

The XMEGA Quadrature Decoder driver source file.

This file contains the function prototypes and enumerator definitions for various configuration parameters for the XMEGA Quadrature Decoder.

The driver is not intended for size and/or speed critical code. The driver is intended for rapid prototyping and documentation purposes for getting started with the XMEGA Quadrature Decoder.

For size and/or speed critical code, it is recommended to copy the function contents directly into your application instead of making a function call.

Several functions use the following construct: "some_register = ... | (some_parameter ? SOME_BIT_bm : 0) | ..." Although the use of the ternary operator ( if ? then : else ) is discouraged, in some occasions the operator makes it possible to write pretty clean and neat code. In this driver, the construct is used to set or not set a configuration bit based on a boolean input parameter, such as the "some_parameter" in the example above.

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_driver.c.

#include "qdec_driver.h"

Include dependency graph for qdec_driver.c:

Go to the source code of this file.

Functions

bool QDEC_EVSYS_Setup (uint8_t qEvMux, EVSYS_CHMUX_t qPinInput, bool useIndex, EVSYS_QDIRM_t qIndexState)
 This function configure the event system for quadrature decoding.
uint8_t QDEC_Get_Direction (TC0_t *qTimer)
 This function return the direction of the counter/QDEC.
bool QDEC_Port_Setup (PORT_t *qPort, uint8_t qPin, bool useIndex, bool invIO)
 This function set up the needed configuration for the port used for the quadrature decoding.
void QDEC_TC_Dec_Setup (TC0_t *qTimer, TC_EVSEL_t qEventChannel, uint8_t lineCount)
 This function set up the needed configuration for the Timer/Counter to handle the quadrature decoding from the event system.
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 measurement from the event system.
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.


Function Documentation

bool QDEC_EVSYS_Setup ( uint8_t  qEvMux,
EVSYS_CHMUX_t  qPinInput,
bool  useIndex,
EVSYS_QDIRM_t  qIndexState 
)

This function configure the event system for quadrature decoding.

Parameters:
qEvMux Which event channel to use. Only 0, 2 and 4 is available.
qPinInput The pin input of QDPH0 to the EVSYS.CHMUX .
useIndex True if an optional Index pin is used.
qIndexState In which state the index signal will trigger.
Returns:
bool True if setup was ok, false if any errors.

Definition at line 159 of file qdec_driver.c.

Referenced by QDEC_Total_Setup().

00163 {
00164         switch (qEvMux){
00165                 case 0:
00166                     
00167                 /* Configure event channel 0 for quadrature decoding of pins. */
00168                 EVSYS.CH0MUX = qPinInput;
00169                 EVSYS.CH0CTRL = EVSYS_QDEN_bm | EVSYS_DIGFILT_2SAMPLES_gc;
00170                 if(useIndex){
00171                         /*  Configure event channel 1 as index channel. Note
00172                          *  that when enabling Index in channel n, the channel
00173                          *  n+1 must be configured for the index signal.*/
00174                         EVSYS.CH1MUX = qPinInput + 2;
00175                         EVSYS.CH1CTRL = EVSYS_DIGFILT_2SAMPLES_gc;
00176                         EVSYS.CH0CTRL |= (uint8_t) qIndexState | EVSYS_QDIEN_bm;
00177 
00178                 }
00179                 break;
00180                 case 2:
00181                 EVSYS.CH2MUX = qPinInput;
00182                 EVSYS.CH2CTRL = EVSYS_QDEN_bm | EVSYS_DIGFILT_2SAMPLES_gc;
00183                 if(useIndex){
00184                         EVSYS.CH3MUX = qPinInput + 2;
00185                         EVSYS.CH3CTRL = EVSYS_DIGFILT_2SAMPLES_gc;
00186                         EVSYS.CH2CTRL |= (uint8_t) qIndexState | EVSYS_QDIEN_bm;
00187                 }
00188                 break;
00189                 case 4:
00190                 EVSYS.CH4MUX = qPinInput;
00191                 EVSYS.CH4CTRL = EVSYS_QDEN_bm | EVSYS_DIGFILT_2SAMPLES_gc;
00192                 if(useIndex){
00193                         EVSYS.CH5MUX = qPinInput + 2;
00194                         EVSYS.CH5CTRL = EVSYS_DIGFILT_2SAMPLES_gc;
00195                         EVSYS.CH4CTRL |= (uint8_t) qIndexState | EVSYS_QDIEN_bm;
00196                 }
00197                 break;
00198                 default:
00199                 return false;
00200         }
00201         return true;
00202 }

uint8_t QDEC_Get_Direction ( TC0_t *  qTimer  ) 

This function return the direction of the counter/QDEC.

Parameters:
qTimer The timer used for QDEC.
Return values:
CW_DIR if clockwise/up counting,
CCW_DIR if counter clockwise/down counting.

Definition at line 256 of file qdec_driver.c.

References CCW_DIR, and CW_DIR.

00257 {
00258         if (qTimer->CTRLFSET & TC0_DIR_bm){
00259                 return CW_DIR;
00260         }else{
00261                 return CCW_DIR;
00262         }
00263 }

bool QDEC_Port_Setup ( PORT_t *  qPort,
uint8_t  qPin,
bool  useIndex,
bool  invIO 
)

This function set up the needed configuration for the port used for the quadrature decoding.

Parameters:
qPort The port to use.
qPin The first input pin (QDPH0) to use (0 - 5/6).
useIndex True if an optional Index pin is used.
invIO True if IO pins should be inverted.
Returns:
bool True if setup was ok, false if any errors.

Definition at line 119 of file qdec_driver.c.

Referenced by QDEC_Total_Setup().

00120 {
00121         /* Make setup depending on if Index signal is used. */
00122         if(useIndex){
00123                 if(qPin > 5){
00124                         return false;
00125                 }
00126                 qPort->DIRCLR = (0x07<<qPin);
00127 
00128                 /* Configure Index signal sensing. */
00129                 PORTCFG.MPCMASK = (0x04<<qPin);
00130                 qPort->PIN0CTRL = (qPort->PIN0CTRL & ~PORT_ISC_gm) | PORT_ISC_BOTHEDGES_gc
00131                                   | (invIO ? PORT_INVEN_bm : 0);
00132 
00133 
00134         }else{
00135                 if(qPin > 6){
00136                         return false;
00137                 }
00138                 qPort->DIRCLR = (0x03<<qPin);
00139         }
00140 
00141         /* Set QDPH0 and QDPH1 sensing level. */
00142         PORTCFG.MPCMASK = (0x03<<qPin);
00143         qPort->PIN0CTRL = (qPort->PIN0CTRL & ~PORT_ISC_gm) | PORT_ISC_LEVEL_gc
00144                           | (invIO ? PORT_INVEN_bm : 0);
00145 
00146         return true;
00147 }

void QDEC_TC_Dec_Setup ( TC0_t *  qTimer,
TC_EVSEL_t  qEventChannel,
uint8_t  lineCount 
)

This function set up the needed configuration for the Timer/Counter to handle the quadrature decoding from the event system.

Parameters:
qTimer The timer to use for QDEC.
qEventChannel The event channel to listen to.
lineCount The number of lines in the quadrature encoder.

Definition at line 211 of file qdec_driver.c.

Referenced by QDEC_Total_Setup().

00212 {
00213         /* Configure TC as a quadrature counter. */
00214         qTimer->CTRLD = (uint8_t) TC_EVACT_QDEC_gc | qEventChannel;
00215         qTimer->PER = (lineCount * 4) - 1;
00216         qTimer->CTRLA = TC_CLKSEL_DIV1_gc;
00217 }

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 measurement from the event system.

Note:
The real frequency of rotation can be calculated from the capture register by using the folowing function. FREQ = ( F_CPU / clk_div ) / ( CAPTURE * lineCount )
Parameters:
qTimer The timer to use for QDEC.
qEventChannel The event channel to listen to.
qPinInput The pin input of QDPH0 to the EVSYS.CHMUX.
clksel The clk div to use for timer.

Definition at line 232 of file qdec_driver.c.

Referenced by main().

00236 {
00237         /* Configure channel 2 to input pin for freq calculation. */
00238         EVSYS.CH2MUX = qPinInput;
00239         EVSYS.CH2CTRL = EVSYS_DIGFILT_4SAMPLES_gc;
00240 
00241         /* Configure TC to capture frequency. */
00242         qTimer->CTRLD = (uint8_t) TC_EVACT_FRW_gc | qEventChannel;
00243         qTimer->PER = 0xFFFF;
00244         qTimer->CTRLB = TC0_CCAEN_bm;
00245         qTimer->CTRLA = clksel;
00246 }

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.

This function combines the following functions for a total setup: QDEC_Port_Setup, QDEC_EVSYS_Setup and QDEC_TC_Dec_Setup.

Parameters:
qPort The port to use.
qPin The first input pin (QDPH0) to use (0 - 5/6).
invIO True if IO pins should be inverted.
qEvMux Which event channel to use. Only 0, 2 and 4 is available.
qPinInput The pin input of QDPH0 to the EVSYS.CHMUX .
useIndex True if an optional Index pin is used.
qIndexState In which state the index signal will trigger.
qTimer The timer to use for QDEC.
qEventChannel The event channel to listen to.
lineCount The number of lines in the quadrature encoder.
Returns:
bool True if setup was ok, false if any errors.

Definition at line 88 of file qdec_driver.c.

References QDEC_EVSYS_Setup(), QDEC_Port_Setup(), and QDEC_TC_Dec_Setup().

Referenced by main().

00098 {
00099         if( !QDEC_Port_Setup(qPort, qPin, useIndex, invIO) )
00100                 return false;
00101         if( !QDEC_EVSYS_Setup(qEvMux, qPinInput, useIndex, qIndexState ) )
00102                 return false;
00103         QDEC_TC_Dec_Setup(qTimer, qEventChannel, lineCount);
00104 
00105         return true;
00106 }

Here is the call graph for this function:

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