1 | // Timer/Counter TCC0 initialization
|
2 | void tcc0_init(void)
|
3 | {
|
4 | unsigned char s;
|
5 | unsigned char n;
|
6 |
|
7 | // Note: the correct PORTC direction for the Compare Channels outputs
|
8 | // is configured in the ports_init function
|
9 |
|
10 | // Save interrupts enabled/disabled state
|
11 | s=SREG;
|
12 |
|
13 | // Disable interrupts
|
14 | #asm("cli")
|
15 |
|
16 | // Disable and reset the timer/counter just to be sure
|
17 | tc0_disable(&TCC0);
|
18 |
|
19 | // Clock source: Peripheral Clock/64
|
20 | TCC0.CTRLA=(TCC0.CTRLA & (~TC0_CLKSEL_gm)) | TC_CLKSEL_DIV64_gc;
|
21 |
|
22 | // Mode: Normal Operation, Overflow Int./Event on TOP
|
23 | // Compare/Capture on channel A: Off
|
24 | // Compare/Capture on channel B: Off
|
25 | // Compare/Capture on channel C: Off
|
26 | // Compare/Capture on channel D: Off
|
27 | TCC0.CTRLB=(TCC0.CTRLB & (~(TC0_CCAEN_bm | TC0_CCBEN_bm | TC0_CCCEN_bm | TC0_CCDEN_bm | TC0_WGMODE_gm))) | TC_WGMODE_NORMAL_gc;
|
28 |
|
29 | // Capture event source: None
|
30 | // Capture event action: None
|
31 | TCC0.CTRLD=(TCC0.CTRLD & (~(TC0_EVACT_gm | TC0_EVSEL_gm))) | TC_EVACT_OFF_gc | TC_EVSEL_OFF_gc;
|
32 |
|
33 | // Overflow interrupt: Disabled
|
34 | // Error interrupt: Disabled
|
35 | TCC0.INTCTRLA=(TCC0.INTCTRLA & (~(TC0_ERRINTLVL_gm | TC0_OVFINTLVL_gm))) | TC_ERRINTLVL_OFF_gc | TC_OVFINTLVL_OFF_gc;
|
36 |
|
37 | // Compare/Capture channel A interrupt: Disabled
|
38 | // Compare/Capture channel B interrupt: Disabled
|
39 | // Compare/Capture channel C interrupt: Disabled
|
40 | // Compare/Capture channel D interrupt: Disabled
|
41 | TCC0.INTCTRLB=(TCC0.INTCTRLB & (~(TC0_CCDINTLVL_gm | TC0_CCCINTLVL_gm | TC0_CCBINTLVL_gm | TC0_CCAINTLVL_gm))) | TC_CCDINTLVL_OFF_gc | TC_CCCINTLVL_OFF_gc | TC_CCBINTLVL_OFF_gc | TC_CCAINTLVL_OFF_gc;
|
42 |
|
43 | // High resolution extension: Off
|
44 | HIRESC.CTRL&= ~HIRES_HREN0_bm;
|
45 |
|
46 | // Advanced Waveform Extension initialization
|
47 | // Optimize for speed
|
48 | #pragma optsize-
|
49 |
|
50 | // Disable locking the AWEX configuration registers just to be sure
|
51 | n=MCU.AWEXLOCK & (~MCU_AWEXCLOCK_bm);
|
52 | CCP=CCP_IOREG_gc;
|
53 | MCU.AWEXLOCK=n;
|
54 | // Restore optimization for size if needed
|
55 | #pragma optsize_default
|
56 |
|
57 | // Pattern generation: Off
|
58 | // Dead time insertion: Off
|
59 | AWEXC.CTRL&= ~(AWEX_PGM_bm | AWEX_CWCM_bm | AWEX_DTICCDEN_bm | AWEX_DTICCCEN_bm | AWEX_DTICCBEN_bm | AWEX_DTICCAEN_bm);
|
60 |
|
61 | // Fault protection initialization
|
62 | // Fault detection on OCD Break detection: On
|
63 | // Fault detection restart mode: Latched Mode
|
64 | // Fault detection action: None (Fault protection disabled)
|
65 | AWEXC.FDCTRL=(AWEXC.FDCTRL & (~(AWEX_FDDBD_bm | AWEX_FDMODE_bm | AWEX_FDACT_gm))) | AWEX_FDACT_NONE_gc;
|
66 |
|
67 | // Fault detect events:
|
68 | // Event channel 0: Off
|
69 | // Event channel 1: Off
|
70 | // Event channel 2: Off
|
71 | // Event channel 3: Off
|
72 | // Event channel 4: Off
|
73 | // Event channel 5: Off
|
74 | // Event channel 6: Off
|
75 | // Event channel 7: Off
|
76 | AWEXC.FDEVMASK=0b00000000;
|
77 |
|
78 | // Make sure the fault detect flag is cleared
|
79 | AWEXC.STATUS|=AWEXC.STATUS & AWEX_FDF_bm;
|
80 |
|
81 | // Clear the interrupt flags
|
82 | TCC0.INTFLAGS=TCC0.INTFLAGS;
|
83 |
|
84 | // Set counter register
|
85 | TCC0.CNT=0x0000;
|
86 |
|
87 | // Set period register
|
88 | TCC0.PER=0xFFFF;
|
89 |
|
90 | // Set channel A Compare/Capture register
|
91 | TCC0.CCA=0x0000;
|
92 |
|
93 | // Set channel B Compare/Capture register
|
94 | TCC0.CCB=0x0000;
|
95 |
|
96 | // Set channel C Compare/Capture register
|
97 | TCC0.CCC=0x0000;
|
98 |
|
99 | // Set channel D Compare/Capture register
|
100 | TCC0.CCD=0x0000;
|
101 |
|
102 | // Restore interrupts enabled/disabled state
|
103 | SREG=s;
|
104 | }
|