24
24
25
25
// the prescaler is set so that timerb3 ticks every 64 clock cycles, and the
26
26
// the overflow handler is called every 256 ticks.
27
- uint16_t microseconds_per_timerb3_overflow ;
27
+ volatile uint16_t microseconds_per_timerb3_overflow ;
28
+ volatile uint16_t microseconds_per_timerb3_tick ;
28
29
29
30
uint32_t F_CPU_CORRECTED = F_CPU ;
30
31
31
- #define TIME_TRACKING_TIMER_PERIOD 0xFF
32
- #define TIME_TRACKING_TICKS_PER_OVF (TIME_TRACKING_TIMER_PERIOD + 1) /* Timer ticks per overflow of TCB3 */
33
- #define TIME_TRACKING_TIMER_DIVIDER 64 /* Clock divider for TCB3 */
34
- #define TIME_TRACKING_CYCLES_PER_OVF (TIME_TRACKING_TICKS_PER_OVF * TIME_TRACKING_TIMER_DIVIDER)
35
-
36
32
// the whole number of milliseconds per timerb3 overflow
37
33
uint16_t millis_inc ;
38
34
@@ -41,7 +37,6 @@ uint16_t fract_inc;
41
37
#define FRACT_MAX (1000)
42
38
43
39
// whole number of microseconds per timerb3 tick
44
- uint16_t microseconds_per_timerb3_tick ;
45
40
46
41
volatile uint32_t timerb3_overflow_count = 0 ;
47
42
volatile uint32_t timerb3_millis = 0 ;
@@ -329,83 +324,6 @@ void init()
329
324
/* Apply calculated value to F_CPU_CORRECTED */
330
325
F_CPU_CORRECTED = (uint32_t )cpu_freq ;
331
326
332
- /***************************** TIMERS FOR PWM *********************************/
333
-
334
-
335
-
336
- /* TYPE A TIMER */
337
-
338
- /* PORTMUX setting for TCA -> all outputs [0:2] point to PORTB pins [0:2] */
339
- PORTMUX .TCAROUTEA = PORTMUX_TCA0_PORTB_gc ;
340
-
341
- /* Setup timers for single slope PWM, but do not enable, will do in analogWrite() */
342
- TCA0 .SINGLE .CTRLB = TCA_SINGLE_WGMODE_SINGLESLOPE_gc ;
343
-
344
- /* Period setting, 16 bit register but val resolution is 8 bit */
345
- TCA0 .SINGLE .PER = PWM_TIMER_PERIOD ;
346
-
347
- /* Default duty 50%, will re-assign in analogWrite() */
348
- TCA0 .SINGLE .CMP0BUF = PWM_TIMER_COMPARE ;
349
- TCA0 .SINGLE .CMP1BUF = PWM_TIMER_COMPARE ;
350
- TCA0 .SINGLE .CMP2BUF = PWM_TIMER_COMPARE ;
351
-
352
- /* Use DIV64 prescaler (giving 250kHz clock), enable TCA timer */
353
- TCA0 .SINGLE .CTRLA = (TCA_SINGLE_CLKSEL_DIV64_gc ) | (TCA_SINGLE_ENABLE_bm );
354
-
355
-
356
- /* TYPE B TIMERS */
357
-
358
- /* PORTMUX alternate location needed for TCB0 & 1, TCB2 is default location */
359
- PORTMUX .TCBROUTEA |= (PORTMUX_TCB0_bm | PORTMUX_TCB1_bm );
360
-
361
- /* Start with TCB0 */
362
- TCB_t * timer_B = (TCB_t * )& TCB0 ;
363
-
364
- /* Timer B Setup loop for TCB[0:2] */
365
- do {
366
- /* 8 bit PWM mode, but do not enable output yet, will do in analogWrite() */
367
- timer_B -> CTRLB = (TCB_CNTMODE_PWM8_gc );
368
-
369
- /* Assign 8-bit period */
370
- timer_B -> CCMPL = PWM_TIMER_PERIOD ;
371
-
372
- /* default duty 50%, set when output enabled */
373
- timer_B -> CCMPH = PWM_TIMER_COMPARE ;
374
-
375
- /* Use TCA clock (250kHz) and enable */
376
- /* (sync update commented out, might try to synchronize later */
377
- timer_B -> CTRLA = (TCB_CLKSEL_CLKTCA_gc )
378
- //|(TCB_SYNCUPD_bm)
379
- |(TCB_ENABLE_bm );
380
-
381
- /* Increment pointer to next TCB instance */
382
- timer_B ++ ;
383
-
384
- /* Stop when pointing to TCB3 */
385
- } while (timer_B < (TCB_t * )& TCB3 );
386
-
387
-
388
-
389
- /* Stuff for synchronizing PWM timers */
390
- // /* Restart TCA to sync TCBs */
391
- // /* should not be needed */
392
- // TCA0.SINGLE.CTRLESET = TCA_SINGLE_CMD_RESTART_gc;
393
- // TCA0.SINGLE.CTRLECLR = TCA_SINGLE_CMD_RESTART_gc;
394
- //
395
- // timer_B = (TCB_t *)&TCB0;
396
- //
397
- // /* TCB are sync to TCA, remove setting */
398
- // for (uint8_t digitial_pin_timer = (TIMERB0 - TIMERB0);
399
- // digitial_pin_timer < (TIMERB3 - TIMERB0);
400
- // digitial_pin_timer++)
401
- // {
402
- // /* disable sync with tca */
403
- // timer_B->CTRLA &= ~ (TCB_SYNCUPD_bm);
404
- //
405
- // /* Add offset to register */
406
- // timer_B++;
407
- //
408
- // }
409
327
410
328
/********************************* ADC ****************************************/
411
329
0 commit comments