AVR1600 Using the XMEGA Quadrature Decoder
qdec_signal_generator.c
Go to the documentation of this file.
1 /* This file has been prepared for Doxygen automatic documentation generation. */
71 #include "avr_compiler.h"
72 #include "qdec_signal_generator.h"
73 
75 PORT_t * q_test_sig_Port;
76 
77 
80 
81 
94 void generate_qdec_signal(PORT_t * qPort, uint8_t lineCount, uint8_t freq, bool dir)
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 }
140 
141 
145 ISR(TCE0_CCA_vect)
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 }
167 
168 
172 ISR(TCE0_CCB_vect)
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 }
177 
178 
182 ISR(TCE0_CCC_vect)
183 {
184  /* Set pin1 phase90 signal. Clear pin0 phase0 signal.*/
185  q_test_sig_Port->OUT = (q_test_sig_Port->OUT & ~0x03) | 0x02;
186 }
187 
188 
192 ISR(TCE0_CCD_vect)
193 {
194  /* Clear pin0 and pin1, phase0 and phase90 signal.*/
195  q_test_sig_Port->OUT = (q_test_sig_Port->OUT & ~0x03);
196 }
197 
198 
205 ISR(TCC0_ERR_vect)
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 }
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.
This file implements some macros that makes the IAR C-compiler and avr-gcc work with the same code ba...
ISR(TCE0_CCA_vect)
Creates a Quadrature signal, up or down counting depending on the configured direction(dir = 1 -> CW/...
uint8_t lineCount
Number of lines in the quadrature encoder.
Definition: qdec_example.c:72
void generate_qdec_signal(PORT_t *qPort, uint8_t lineCount, uint8_t freq, bool dir)
Initializes TCE0 to create Qadrature signal.
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