AVR1600 Using the XMEGA Quadrature Decoder
qdec_signal_generator.h File Reference
This graph shows which files directly or indirectly include this file:

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

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
@DOC_TITLE@
Generated on Thu Oct 26 2017 13:33:37 for AVR1600 Using the XMEGA Quadrature Decoder by doxygen 1.8.13