root / TL16C754_training.X / main.c @ master
Historique | Voir | Annoter | Télécharger (18,304 ko)
1 | d2006889 | Enzo Niro | /*
|
---|---|---|---|
2 | * File: newmain.c
|
||
3 | * Author: eniro
|
||
4 | *
|
||
5 | * Created on May 27, 2024, 3:32 PM
|
||
6 | */
|
||
7 | |||
8 | |||
9 | #define F_CPU 24000000UL |
||
10 | |||
11 | #include <xc.h> |
||
12 | #include <math.h> |
||
13 | #include <stdlib.h> |
||
14 | #include <util/delay.h> |
||
15 | #include <avr/interrupt.h> |
||
16 | #include <avr/eeprom.h> |
||
17 | #include "macros_def.h" |
||
18 | #include "TL16C_addr.h" |
||
19 | #include "TL16C_CFG.h" |
||
20 | |||
21 | |||
22 | //OSC. setup
|
||
23 | #define FREQSEL 0x7 //16MHz |
||
24 | #define _FREQSEL_REG_WR ((FREQSEL) << 2) |
||
25 | |||
26 | |||
27 | |||
28 | //////////////////////////////////////
|
||
29 | //prototypes functions
|
||
30 | |||
31 | void initUartA(uint8_t busSpeed, uint8_t dataBits, uint8_t parity, uint8_t stopBit);
|
||
32 | void initUartB(uint8_t busSpeed, uint8_t dataBits, uint8_t parity, uint8_t stopBit);
|
||
33 | void initUartC(uint8_t busSpeed, uint8_t dataBits, uint8_t parity, uint8_t stopBit);
|
||
34 | void initUartD(uint8_t busSpeed, uint8_t dataBits, uint8_t parity, uint8_t stopBit);
|
||
35 | |||
36 | void writePortA(uint8_t reg, uint8_t addr);
|
||
37 | void writePortB(uint8_t reg, uint8_t addr);
|
||
38 | void writePortC(uint8_t reg, uint8_t addr);
|
||
39 | void writePortD(uint8_t reg, uint8_t addr);
|
||
40 | uint8_t readPortA(uint8_t addr); |
||
41 | uint8_t readPortB(uint8_t addr); |
||
42 | uint8_t readPortC(uint8_t addr); |
||
43 | uint8_t readPortD(uint8_t addr); |
||
44 | |||
45 | void initExtClock(uint8_t prescaler, uint16_t cnt); //set an external clock on PA0 |
||
46 | void setWait(uint16_t cnt); //fast delay |
||
47 | |||
48 | |||
49 | uint8_t DLL_DEBUG, DLH_DEBUG, LCR_DEBUG; |
||
50 | |||
51 | //////////////////////////////////////
|
||
52 | |||
53 | |||
54 | |||
55 | |||
56 | int main(void) { |
||
57 | |||
58 | uint8_t ctrlReg; |
||
59 | uint8_t bufferA[64], bufferB[64], bufferC[64], bufferD[64]; |
||
60 | uint8_t counterA = 0, counterB = 0, counterC = 0, counterD = 0; |
||
61 | |||
62 | setWait(5000);
|
||
63 | |||
64 | |||
65 | for(int i = 0; i < 64; i++) |
||
66 | { |
||
67 | bufferA[i] = 0;
|
||
68 | } |
||
69 | for(int i = 0; i < 64; i++) |
||
70 | { |
||
71 | bufferB[i] = 0;
|
||
72 | } |
||
73 | for(int i = 0; i < 64; i++) |
||
74 | { |
||
75 | bufferC[i] = 0;
|
||
76 | } |
||
77 | for(int i = 0; i < 64; i++) |
||
78 | { |
||
79 | bufferD[i] = 0;
|
||
80 | } |
||
81 | |||
82 | _PROTECTED_WRITE(CLKCTRL.OSCHFCTRLA, _FREQSEL_REG_WR); //switch to 24 MHz
|
||
83 | |||
84 | //initExtClock(DIV1, 0x0000); //Set clock out with prescaler
|
||
85 | |||
86 | //Enable pins (Port direction)
|
||
87 | ADDR_ENABLE; //Address pins
|
||
88 | ENABLE_WR_RD_PINS; //Read and Write pins
|
||
89 | ENABLE_CS_PINS; //Chip select pins
|
||
90 | ENABLE_RESET_PIN; //Reset pin
|
||
91 | |||
92 | //////////////////////////////////////////////////////////////
|
||
93 | //Remember to put high level on cs pins to disable devices...
|
||
94 | SETCSA; |
||
95 | SETCSB; |
||
96 | SETCSC; |
||
97 | SETCSD; |
||
98 | //Also don't forget IOR and IOW
|
||
99 | SETWR; |
||
100 | SETRD; |
||
101 | //////////////////////////////////////////////////////////////
|
||
102 | |||
103 | |||
104 | /*ENABLE_POW_SUPPLY_PIN;
|
||
105 | POWER_OFF;
|
||
106 | _delay_ms(500);
|
||
107 | POWER_ON;
|
||
108 | _delay_ms(500);*/
|
||
109 | |||
110 | |||
111 | |||
112 | |||
113 | ////////////////////////////////////////
|
||
114 | //init TL16C IC
|
||
115 | SETRST; |
||
116 | //_delay_ms(10);
|
||
117 | setWait(100);
|
||
118 | CLRRST; |
||
119 | ////////////////////////////////////////
|
||
120 | |||
121 | asm("nop"); //wait a little bit for reset sequence back on boot |
||
122 | |||
123 | |||
124 | //uint16_t wordTemp;
|
||
125 | /*uint8_t regVal; //temp value for reading register and change it's value without clearing it
|
||
126 | regVal = readPortA(LCR_REG);
|
||
127 | writePortA(0x9D, LCR_REG); //set LCR[7] = 1 -> enable divisor latch mode
|
||
128 | //Set bus speed (according of 8MHz OSC. -> DL = Fosc/(16*Fbauds*DIV));
|
||
129 | //DIV = 1 or 4 (see TL16C datasheet p.14), we use here DIV = 1
|
||
130 | //wordTemp = busSpeed >> 8;
|
||
131 | writePortA(0, DLH_REG); //Always 0x00 in our cfg...
|
||
132 | LCR_DEBUG = readPortA(DLH_REG);
|
||
133 | writePortA(104, DLL_REG); //Set speed...
|
||
134 | LCR_DEBUG = readPortA(DLL_REG);
|
||
135 | _delay_ms(1);
|
||
136 | writePortA(3, LCR_REG);
|
||
137 | LCR_DEBUG = readPortA(LCR_REG);
|
||
138 | _delay_ms(1);
|
||
139 | writePortA(0x01, FCR_REG); //enable FIFO
|
||
140 | _delay_ms(250); //wait a bit
|
||
141 | */
|
||
142 | |||
143 | initUartA(BAUDS_19200, DATA_8BITS, NO_PARITY, ONE_STOPBIT); |
||
144 | initUartB(BAUDS_19200, DATA_8BITS, NO_PARITY, ONE_STOPBIT); |
||
145 | initUartC(BAUDS_19200, DATA_8BITS, NO_PARITY, ONE_STOPBIT); |
||
146 | initUartD(BAUDS_19200, DATA_8BITS, NO_PARITY, ONE_STOPBIT); |
||
147 | //ctrlReg = readPortA(LSR_REG);
|
||
148 | //ctrlReg = readPortA(RHR_REG);
|
||
149 | /*ctrlReg = readPortB(LSR_REG);
|
||
150 | ctrlReg = readPortB(RHR_REG);
|
||
151 | ctrlReg = readPortC(LSR_REG);
|
||
152 | ctrlReg = readPortC(RHR_REG);
|
||
153 | ctrlReg = readPortD(LSR_REG);
|
||
154 | ctrlReg = readPortD(RHR_REG);*/
|
||
155 | //_delay_ms(1000);
|
||
156 | setWait(500);
|
||
157 | ctrlReg = 0;
|
||
158 | |||
159 | while(1) |
||
160 | { |
||
161 | //Tx send
|
||
162 | /*writePortA(0x42, THR_REG);
|
||
163 | writePortA(0x55, THR_REG);
|
||
164 | writePortA(0x43, THR_REG);
|
||
165 | writePortA(0x60, THR_REG);
|
||
166 | writePortA(0x97, THR_REG);
|
||
167 | writePortA(0x54, THR_REG);
|
||
168 | writePortA(0x26, THR_REG);
|
||
169 | writePortA(0x08, THR_REG);*/
|
||
170 | |||
171 | |||
172 | ////////////////////////////////////////////////////////
|
||
173 | //Read UART A
|
||
174 | |||
175 | ctrlReg = readPortA(LSR_REG); |
||
176 | |||
177 | if((ctrlReg & 0x01) == 0x01) //Check if one byte available |
||
178 | { |
||
179 | bufferA[counterA] = readPortA(RHR_REG); |
||
180 | counterA++; |
||
181 | if(counterA >= 64) |
||
182 | counterA = 0;
|
||
183 | |||
184 | if(bufferA[counterA] == 0xF8) |
||
185 | asm("nop"); |
||
186 | |||
187 | ctrlReg = 0;
|
||
188 | } |
||
189 | |||
190 | |||
191 | ////////////////////////////////////////////////////////
|
||
192 | //Read UART B
|
||
193 | |||
194 | ctrlReg = readPortB(LSR_REG); |
||
195 | |||
196 | if((ctrlReg & 0x01) == 0x01) //Check if one byte available |
||
197 | { |
||
198 | bufferB[counterB] = readPortB(RHR_REG); |
||
199 | counterB++; |
||
200 | if(counterB >= 64) |
||
201 | counterB = 0;
|
||
202 | |||
203 | if(bufferB[counterB] == 0xF8) |
||
204 | asm("nop"); |
||
205 | |||
206 | ctrlReg = 0;
|
||
207 | } |
||
208 | |||
209 | |||
210 | ////////////////////////////////////////////////////////
|
||
211 | //Read UART C
|
||
212 | |||
213 | ctrlReg = readPortC(LSR_REG); |
||
214 | |||
215 | if((ctrlReg & 0x01) == 0x01) //Check if one byte available |
||
216 | { |
||
217 | bufferC[counterC] = readPortC(RHR_REG); |
||
218 | counterC++; |
||
219 | if(counterC >= 64) |
||
220 | counterC = 0;
|
||
221 | |||
222 | if(bufferC[counterC] == 0xF8) |
||
223 | asm("nop"); |
||
224 | |||
225 | ctrlReg = 0;
|
||
226 | } |
||
227 | |||
228 | ////////////////////////////////////////////////////////
|
||
229 | //Read UART D
|
||
230 | |||
231 | ctrlReg = readPortD(LSR_REG); |
||
232 | |||
233 | if((ctrlReg & 0x01) == 0x01) //Check if one byte available |
||
234 | { |
||
235 | bufferD[counterD] = readPortD(RHR_REG); |
||
236 | counterD++; |
||
237 | if(counterD >= 64) |
||
238 | counterD = 0;
|
||
239 | |||
240 | if(bufferD[counterD] == 0xF8) |
||
241 | asm("nop"); |
||
242 | |||
243 | ctrlReg = 0;
|
||
244 | } |
||
245 | |||
246 | |||
247 | |||
248 | if(LCR_DEBUG == 0x00) |
||
249 | asm("nop"); |
||
250 | |||
251 | if(DLL_DEBUG == 0x00) |
||
252 | asm("nop"); |
||
253 | |||
254 | if(DLH_DEBUG == 0x00) |
||
255 | asm("nop"); |
||
256 | |||
257 | |||
258 | setWait(10);
|
||
259 | //_delay_ms(1);
|
||
260 | } |
||
261 | |||
262 | |||
263 | return 0; |
||
264 | } |
||
265 | |||
266 | |||
267 | |||
268 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||
269 | //functions
|
||
270 | |||
271 | |||
272 | //////////////////////////////////////
|
||
273 | //High level functions
|
||
274 | |||
275 | void initUartA(uint8_t busSpeed, uint8_t dataBits, uint8_t parity, uint8_t stopBit)
|
||
276 | { |
||
277 | |||
278 | uint8_t regVal; //temp value for reading register and change it's value without clearing it
|
||
279 | //void (*callFuncWR)();
|
||
280 | //uint8_t (*callFuncRD)();
|
||
281 | //uint16_t addrWriteFunc[] = {&writePortA, &writePortB, &writePortC, &writePortD};
|
||
282 | //uint16_t addrReadFunc[] = {&readPortA, &readPortB, &readPortC, &readPortD};
|
||
283 | uint8_t wordValues[] = {DATA_5BITS, DATA_6BITS, DATA_7BITS, DATA_8BITS}; |
||
284 | uint8_t baudsValues[] = {DL_9600_BAUDS, DL_19200_BAUDS, DL_38400_BAUDS}; |
||
285 | uint8_t parityValues[] = {PARITY_NONE, PARITY_ODD, PARITY_EVEN}; |
||
286 | uint8_t stopBitValues[] = {STOPBIT_ONE, STOPBIT_TWO}; |
||
287 | |||
288 | //callFuncWR = uart_type > 3 ? writePortD : addrWriteFunc[uart_type];
|
||
289 | //callFuncRD = uart_type > 3 ? writePortD : addrReadFunc[uart_type];
|
||
290 | |||
291 | |||
292 | //Read register first...
|
||
293 | regVal = readPortA(LCR_REG); |
||
294 | //_delay_ms(1);
|
||
295 | setWait(20);
|
||
296 | regVal |= DLAB_ERF_EN_BIT; //set LCR[7] = 1 -> enable divisor latch mode
|
||
297 | writePortA(regVal, LCR_REG); //switch Mode
|
||
298 | //_delay_ms(1);
|
||
299 | setWait(20);
|
||
300 | //Set bus speed (according of 8MHz OSC. -> DL = Fosc/(16*Fbauds*DIV));
|
||
301 | //DIV = 1 or 4 (see TL16C datasheet p.14), we use here DIV = 1
|
||
302 | writePortA(0x00, DLH_REG); //Always 0x00 in our cfg... MAX 12MHz |
||
303 | //_delay_ms(1);
|
||
304 | setWait(20);
|
||
305 | writePortA(busSpeed > 2 ? DL_38400_BAUDS : baudsValues[busSpeed], DLL_REG);
|
||
306 | //_delay_ms(1);
|
||
307 | setWait(20);
|
||
308 | |||
309 | regVal = readPortA(LCR_REG); |
||
310 | regVal &= ~(DLAB_ERF_EN_BIT); //set LCR[7] = 0 -> disable divisor latch mode
|
||
311 | |||
312 | regVal |= dataBits > 3 ? DATA_8BITS : wordValues[dataBits]; //set 8bits mode |
||
313 | regVal |= (stopBit > 1 ? STOPBIT_TWO : stopBitValues[stopBit]) << 2; //set stop bits |
||
314 | regVal |= (parity > 2 ? PARITY_EVEN : parityValues[parity]) << 3; //set stop bits |
||
315 | writePortA(regVal, LCR_REG); |
||
316 | //_delay_ms(1);
|
||
317 | setWait(20);
|
||
318 | writePortA(0x01, FCR_REG); //enable FIFO |
||
319 | //_delay_ms(250); //wait a bit
|
||
320 | setWait(20);
|
||
321 | } |
||
322 | |||
323 | |||
324 | void initUartB(uint8_t busSpeed, uint8_t dataBits, uint8_t parity, uint8_t stopBit)
|
||
325 | { |
||
326 | |||
327 | uint8_t regVal; //temp value for reading register and change it's value without clearing it
|
||
328 | //void (*callFuncWR)();
|
||
329 | //uint8_t (*callFuncRD)();
|
||
330 | //uint16_t addrWriteFunc[] = {&writePortA, &writePortB, &writePortC, &writePortD};
|
||
331 | //uint16_t addrReadFunc[] = {&readPortA, &readPortB, &readPortC, &readPortD};
|
||
332 | uint8_t wordValues[] = {DATA_5BITS, DATA_6BITS, DATA_7BITS, DATA_8BITS}; |
||
333 | uint8_t baudsValues[] = {DL_9600_BAUDS, DL_19200_BAUDS, DL_38400_BAUDS}; |
||
334 | uint8_t parityValues[] = {PARITY_NONE, PARITY_ODD, PARITY_EVEN}; |
||
335 | uint8_t stopBitValues[] = {STOPBIT_ONE, STOPBIT_TWO}; |
||
336 | |||
337 | //callFuncWR = uart_type > 3 ? writePortD : addrWriteFunc[uart_type];
|
||
338 | //callFuncRD = uart_type > 3 ? writePortD : addrReadFunc[uart_type];
|
||
339 | |||
340 | |||
341 | //Read register first...
|
||
342 | regVal = readPortB(LCR_REG); |
||
343 | setWait(20);
|
||
344 | regVal |= DLAB_ERF_EN_BIT; //set LCR[7] = 1 -> enable divisor latch mode
|
||
345 | writePortB(regVal, LCR_REG); //switch Mode
|
||
346 | setWait(20);
|
||
347 | //Set bus speed (according of 8MHz OSC. -> DL = Fosc/(16*Fbauds*DIV));
|
||
348 | //DIV = 1 or 4 (see TL16C datasheet p.14), we use here DIV = 1
|
||
349 | writePortB(0x00, DLH_REG); //Always 0x00 in our cfg... MAX 12MHz |
||
350 | setWait(20);
|
||
351 | writePortB(busSpeed > 2 ? DL_38400_BAUDS : baudsValues[busSpeed], DLL_REG);
|
||
352 | setWait(20);
|
||
353 | |||
354 | regVal = readPortB(LCR_REG); |
||
355 | regVal &= ~(DLAB_ERF_EN_BIT); //set LCR[7] = 0 -> disable divisor latch mode
|
||
356 | |||
357 | regVal |= dataBits > 3 ? DATA_8BITS : wordValues[dataBits]; //set 8bits mode |
||
358 | regVal |= (stopBit > 1 ? STOPBIT_TWO : stopBitValues[stopBit]) << 2; //set stop bits |
||
359 | regVal |= (parity > 2 ? PARITY_EVEN : parityValues[parity]) << 3; //set stop bits |
||
360 | writePortB(regVal, LCR_REG); |
||
361 | setWait(20);
|
||
362 | writePortB(0x01, FCR_REG); //enable FIFO |
||
363 | setWait(20);
|
||
364 | } |
||
365 | |||
366 | |||
367 | void initUartC(uint8_t busSpeed, uint8_t dataBits, uint8_t parity, uint8_t stopBit)
|
||
368 | { |
||
369 | |||
370 | uint8_t regVal; //temp value for reading register and change it's value without clearing it
|
||
371 | //void (*callFuncWR)();
|
||
372 | //uint8_t (*callFuncRD)();
|
||
373 | //uint16_t addrWriteFunc[] = {&writePortA, &writePortB, &writePortC, &writePortD};
|
||
374 | //uint16_t addrReadFunc[] = {&readPortA, &readPortB, &readPortC, &readPortD};
|
||
375 | uint8_t wordValues[] = {DATA_5BITS, DATA_6BITS, DATA_7BITS, DATA_8BITS}; |
||
376 | uint8_t baudsValues[] = {DL_9600_BAUDS, DL_19200_BAUDS, DL_38400_BAUDS}; |
||
377 | uint8_t parityValues[] = {PARITY_NONE, PARITY_ODD, PARITY_EVEN}; |
||
378 | uint8_t stopBitValues[] = {STOPBIT_ONE, STOPBIT_TWO}; |
||
379 | |||
380 | //callFuncWR = uart_type > 3 ? writePortD : addrWriteFunc[uart_type];
|
||
381 | //callFuncRD = uart_type > 3 ? writePortD : addrReadFunc[uart_type];
|
||
382 | |||
383 | |||
384 | //Read register first...
|
||
385 | regVal = readPortC(LCR_REG); |
||
386 | setWait(20);
|
||
387 | regVal |= DLAB_ERF_EN_BIT; //set LCR[7] = 1 -> enable divisor latch mode
|
||
388 | writePortC(regVal, LCR_REG); //switch Mode
|
||
389 | setWait(20);
|
||
390 | //Set bus speed (according of 8MHz OSC. -> DL = Fosc/(16*Fbauds*DIV));
|
||
391 | //DIV = 1 or 4 (see TL16C datasheet p.14), we use here DIV = 1
|
||
392 | writePortC(0x00, DLH_REG); //Always 0x00 in our cfg... MAX 12MHz |
||
393 | setWait(20);
|
||
394 | writePortC(busSpeed > 2 ? DL_38400_BAUDS : baudsValues[busSpeed], DLL_REG);
|
||
395 | setWait(20);
|
||
396 | |||
397 | regVal = readPortC(LCR_REG); |
||
398 | regVal &= ~(DLAB_ERF_EN_BIT); //set LCR[7] = 0 -> disable divisor latch mode
|
||
399 | |||
400 | regVal |= dataBits > 3 ? DATA_8BITS : wordValues[dataBits]; //set 8bits mode |
||
401 | regVal |= (stopBit > 1 ? STOPBIT_TWO : stopBitValues[stopBit]) << 2; //set stop bits |
||
402 | regVal |= (parity > 2 ? PARITY_EVEN : parityValues[parity]) << 3; //set stop bits |
||
403 | writePortC(regVal, LCR_REG); |
||
404 | setWait(20);
|
||
405 | writePortC(0x01, FCR_REG); //enable FIFO |
||
406 | setWait(20);
|
||
407 | } |
||
408 | |||
409 | |||
410 | |||
411 | void initUartD(uint8_t busSpeed, uint8_t dataBits, uint8_t parity, uint8_t stopBit)
|
||
412 | { |
||
413 | |||
414 | uint8_t regVal; //temp value for reading register and change it's value without clearing it
|
||
415 | //void (*callFuncWR)();
|
||
416 | //uint8_t (*callFuncRD)();
|
||
417 | //uint16_t addrWriteFunc[] = {&writePortA, &writePortB, &writePortC, &writePortD};
|
||
418 | //uint16_t addrReadFunc[] = {&readPortA, &readPortB, &readPortC, &readPortD};
|
||
419 | uint8_t wordValues[] = {DATA_5BITS, DATA_6BITS, DATA_7BITS, DATA_8BITS}; |
||
420 | uint8_t baudsValues[] = {DL_9600_BAUDS, DL_19200_BAUDS, DL_38400_BAUDS}; |
||
421 | uint8_t parityValues[] = {PARITY_NONE, PARITY_ODD, PARITY_EVEN}; |
||
422 | uint8_t stopBitValues[] = {STOPBIT_ONE, STOPBIT_TWO}; |
||
423 | |||
424 | //callFuncWR = uart_type > 3 ? writePortD : addrWriteFunc[uart_type];
|
||
425 | //callFuncRD = uart_type > 3 ? writePortD : addrReadFunc[uart_type];
|
||
426 | |||
427 | |||
428 | //Read register first...
|
||
429 | regVal = readPortD(LCR_REG); |
||
430 | setWait(20);
|
||
431 | regVal |= DLAB_ERF_EN_BIT; //set LCR[7] = 1 -> enable divisor latch mode
|
||
432 | writePortD(regVal, LCR_REG); //switch Mode
|
||
433 | setWait(20);
|
||
434 | //Set bus speed (according of 8MHz OSC. -> DL = Fosc/(16*Fbauds*DIV));
|
||
435 | //DIV = 1 or 4 (see TL16C datasheet p.14), we use here DIV = 1
|
||
436 | writePortD(0x00, DLH_REG); //Always 0x00 in our cfg... MAX 12MHz |
||
437 | setWait(20);
|
||
438 | writePortD(busSpeed > 2 ? DL_38400_BAUDS : baudsValues[busSpeed], DLL_REG);
|
||
439 | setWait(20);
|
||
440 | |||
441 | regVal = readPortD(LCR_REG); |
||
442 | regVal &= ~(DLAB_ERF_EN_BIT); //set LCR[7] = 0 -> disable divisor latch mode
|
||
443 | |||
444 | regVal |= dataBits > 3 ? DATA_8BITS : wordValues[dataBits]; //set 8bits mode |
||
445 | regVal |= (stopBit > 1 ? STOPBIT_TWO : stopBitValues[stopBit]) << 2; //set stop bits |
||
446 | regVal |= (parity > 2 ? PARITY_EVEN : parityValues[parity]) << 3; //set stop bits |
||
447 | writePortD(regVal, LCR_REG); |
||
448 | setWait(20);
|
||
449 | writePortD(0x01, FCR_REG); //enable FIFO |
||
450 | setWait(20);
|
||
451 | } |
||
452 | |||
453 | |||
454 | |||
455 | |||
456 | |||
457 | |||
458 | |||
459 | //set an external clock on PA0
|
||
460 | void initExtClock(uint8_t prescaler, uint16_t cnt)
|
||
461 | { |
||
462 | TCA0.SINGLE.CMP0 = cnt; //set number of count that define period
|
||
463 | TCA0.SINGLE.CTRLA = 0x01 | ((prescaler & 0x7) << 1) ; //enable with DIVn prescaler |
||
464 | TCA0.SINGLE.CTRLB = 0x11; //Freq mode and enable CMP0 bit for waveform output |
||
465 | PORTA.DIR |= 0x01; // PA0 OUTPUT |
||
466 | } |
||
467 | |||
468 | |||
469 | |||
470 | ///////////////////////////////////////////////////////
|
||
471 | //write functions
|
||
472 | |||
473 | |||
474 | |||
475 | void writePortA(uint8_t reg, uint8_t addr)
|
||
476 | { |
||
477 | DATADIR(0xFF); //set I/O on write mode |
||
478 | ADDRWRITE(addr); //search uartn register
|
||
479 | DATAWRITE(reg); //write data
|
||
480 | CLRCSA; //enable uartA call
|
||
481 | setWait(3);
|
||
482 | CLRWR; //enable write mode
|
||
483 | setWait(3);
|
||
484 | SETWR; //disable write mode
|
||
485 | setWait(3);
|
||
486 | SETCSA; //disable uartA call
|
||
487 | DATADIR(0x00); //set input mode (to reduce power consumption) |
||
488 | } |
||
489 | |||
490 | void writePortB(uint8_t reg, uint8_t addr)
|
||
491 | { |
||
492 | DATADIR(0xFF); //set I/O on write mode |
||
493 | ADDRWRITE(addr); //search uartn register
|
||
494 | DATAWRITE(reg); //write data
|
||
495 | CLRCSB; //enable uartA call
|
||
496 | setWait(3);
|
||
497 | CLRWR; //enable write mode
|
||
498 | setWait(3);
|
||
499 | SETWR; //disable write mode
|
||
500 | setWait(3);
|
||
501 | SETCSB; //disable uartA call
|
||
502 | DATADIR(0x00); //set input mode (to reduce power consumption) |
||
503 | } |
||
504 | |||
505 | void writePortC(uint8_t reg, uint8_t addr)
|
||
506 | { |
||
507 | DATADIR(0xFF); //set I/O on write mode |
||
508 | ADDRWRITE(addr); //search uartn register
|
||
509 | DATAWRITE(reg); //write data
|
||
510 | CLRCSC; //enable uartA call
|
||
511 | setWait(3);
|
||
512 | CLRWR; //enable write mode
|
||
513 | setWait(3);
|
||
514 | SETWR; //disable write mode
|
||
515 | setWait(3);
|
||
516 | SETCSC; //disable uartA call
|
||
517 | DATADIR(0x00); //set input mode (to reduce power consumption) |
||
518 | } |
||
519 | |||
520 | void writePortD(uint8_t reg, uint8_t addr)
|
||
521 | { |
||
522 | DATADIR(0xFF); //set I/O on write mode |
||
523 | ADDRWRITE(addr); //search uartn register
|
||
524 | DATAWRITE(reg); //write data
|
||
525 | CLRCSD; //enable uartA call
|
||
526 | setWait(3);
|
||
527 | CLRWR; //enable write mode
|
||
528 | setWait(3);
|
||
529 | SETWR; //disable write mode
|
||
530 | setWait(3);
|
||
531 | SETCSD; //disable uartA call
|
||
532 | DATADIR(0x00); //set input mode (to reduce power consumption) |
||
533 | } |
||
534 | |||
535 | |||
536 | ///////////////////////////////////////////////////////
|
||
537 | //read functions
|
||
538 | |||
539 | |||
540 | uint8_t readPortA(uint8_t addr) |
||
541 | { |
||
542 | uint8_t result; |
||
543 | DATADIR(0x00); //set I/O on read mode |
||
544 | ADDRWRITE(addr); //search uartn register
|
||
545 | CLRCSA; //enable uartA call
|
||
546 | setWait(3);
|
||
547 | CLRRD; //enable read mode
|
||
548 | setWait(3);
|
||
549 | result = PORTD.IN; //set I/O on read mode
|
||
550 | setWait(3);
|
||
551 | SETRD; //disable read mode
|
||
552 | setWait(3);
|
||
553 | SETCSA; //hang up uartA call
|
||
554 | return result;
|
||
555 | } |
||
556 | |||
557 | uint8_t readPortB(uint8_t addr) |
||
558 | { |
||
559 | uint8_t result; |
||
560 | DATADIR(0x00); //set I/O on read mode |
||
561 | ADDRWRITE(addr); //search uartn register
|
||
562 | CLRCSB; //enable uartB call
|
||
563 | setWait(3);
|
||
564 | CLRRD; //enable read mode
|
||
565 | setWait(3);
|
||
566 | result = PORTD.IN; //set I/O on read mode
|
||
567 | setWait(3);
|
||
568 | SETRD; //disable read mode
|
||
569 | setWait(3);
|
||
570 | SETCSB; //hang up uartB call
|
||
571 | return result;
|
||
572 | } |
||
573 | |||
574 | uint8_t readPortC(uint8_t addr) |
||
575 | { |
||
576 | uint8_t result; |
||
577 | DATADIR(0x00); //set I/O on read mode |
||
578 | ADDRWRITE(addr); //search uartn register
|
||
579 | CLRCSC; //enable uartC call
|
||
580 | setWait(3);
|
||
581 | CLRRD; //enable read mode
|
||
582 | setWait(3);
|
||
583 | result = PORTD.IN; //set I/O on read mode
|
||
584 | setWait(3);
|
||
585 | SETRD; //disable read mode
|
||
586 | setWait(3);
|
||
587 | SETCSC; //hang up uartC call
|
||
588 | return result;
|
||
589 | } |
||
590 | |||
591 | uint8_t readPortD(uint8_t addr) |
||
592 | { |
||
593 | uint8_t result; |
||
594 | DATADIR(0x00); //set I/O on read mode |
||
595 | ADDRWRITE(addr); //search uartn register
|
||
596 | CLRCSD; //enable uartD call
|
||
597 | setWait(3);
|
||
598 | CLRRD; //enable read mode
|
||
599 | setWait(3);
|
||
600 | result = PORTD.IN; //set I/O on read mode
|
||
601 | setWait(3);
|
||
602 | SETRD; //disable read mode
|
||
603 | setWait(3);
|
||
604 | SETCSD; //hang up uartD call
|
||
605 | return result;
|
||
606 | } |
||
607 | |||
608 | |||
609 | |||
610 | |||
611 | |||
612 | |||
613 | |||
614 | void setWait(uint16_t cnt)
|
||
615 | { |
||
616 | uint16_t k = 0;
|
||
617 | while(k < cnt)
|
||
618 | { |
||
619 | asm("nop"); |
||
620 | k++; |
||
621 | } |
||
622 | } |