Moin
Bisweilen etwas frustrierend, woran man(n) scheitert...
Ich möchte Timer/Interruptgesteuert eine LED im Sekundentakt blinken
lassen. Also folgendes zu Papier gebracht:
1 | #include <xc.h>
|
2 | #include <avr/interrupt.h>
|
3 |
|
4 | volatile uint32_t milli_ticks;
|
5 |
|
6 | void led1on()...
|
7 | void led1off()...
|
8 |
|
9 | ISR(TCB0_INT_vect) {
|
10 | milli_ticks++;
|
11 | }
|
12 |
|
13 | void main(void) {
|
14 |
|
15 | //Timer auf 20000 (bei 20MHz Takt -> 1 ms)
|
16 | TCB0.CCMPL = 0b00100000;
|
17 | TCB0.CCMPH = 0b01001110;
|
18 | //Enable Timer
|
19 | TCB0.CTRLA |= 1;
|
20 | //Enable Capture
|
21 | //DS: Bit 0 – CAPT Capture Interrupt Enable
|
22 | // Writing this bit to ‘1’ enables interrupt on capture.
|
23 | TCB0.INTCTRL |= 1;
|
24 |
|
25 | sei();
|
26 |
|
27 |
|
28 | while (1) {
|
29 | //0-1000 an, 1001-2000 aus
|
30 | if (milli_ticks > 100000) {
|
31 | milli_ticks = 0;
|
32 | } else if (milli_ticks > 50000) {
|
33 | led1off();
|
34 | } else {
|
35 | led1on();
|
36 | }
|
37 |
|
38 | }
|
39 | }
|
Jetzt war meine Idee, dass ich jeweils 1.000 1-ms-Ticks abwarte, und
dann ist die Sekunde um. Tatsächlich muss ich etwa 50.000 Ticks warten.
Denk- oder Programmfehler?
Gruß
Holger
Auszug aus dem DS:
By default, the TCB is in Periodic Interrupt mode. Follow these steps to
start using it:
* 1.Write a TOP value to the Compare/Capture (TCBn.CCMP) register.
* 2.Optional: Write the Compare/Capture Output Enable (CCMPEN) bit in
the Control B (TCBn.CTRLB) register to
‘1’. This will make the waveform output available on the corresponding
pin, overriding the value in the
corresponding PORT output register. The corresponding pin direction must
be configured as an output in the
PORT peripheral.
* 3.Enable the counter by writing a ‘1’ to the ENABLE bit in the Control
A (TCBn.CTRLA) register.
The counter will start counting clock ticks according to the prescaler
setting in the Clock Select (CLKSEL) bit
field in the Control A (TCBn.CTRLA) register.
* 4.The counter value can be read from the Count (TCBn.CNT) register.
The peripheral will generate a CAPT
interrupt and event when the CNT value reaches TOP.
* 4.1. If the Compare/Capture register is modified to a value lower than
the current CNT, the peripheral will
count to MAX and wrap around.
* 4.2.At MAX, an OVF interrupt and event will be generated.