Hallo Freunde !! Ich versuche eine LED im Sekundentakt blinken zu lassen und die andere LED soll alle 5 sec. toggeln. Ich bekomme das mit den 5 sec. einfach nicht in den Griff, bestimmt habe ich mich schon selber in meinen Codefehler blind gesehen ;)
1 | #define F_CPU 3686400UL
|
2 | #include <avr/io.h> |
3 | #include <avr/interrupt.h> |
4 | |
5 | |
6 | /*====================================================================*/
|
7 | /* */
|
8 | /* MCU = ATmega 8 */
|
9 | /* */
|
10 | /* */
|
11 | /*====================================================================*/
|
12 | // OCR1A = (F_CPU/256)-1
|
13 | |
14 | |
15 | #define LED0 (1<<PB0) // LED auf Portpin B0
|
16 | #define LED1 (1<<PB1) // LED auf Portpin B1
|
17 | #define OCRWERT (F_CPU/256)-1;
|
18 | unsigned int sec= 0; |
19 | |
20 | void ctc_init() |
21 | {
|
22 | DDRB |= (1<<PB0); // Portpin B0 = output |
23 | DDRB |= (1<<PB1); // Portpin B1 = output |
24 | //PORTB = 0xff;
|
25 | TCCR1B |= (1 << CS12); // Vorteiler auf 256 und Timer start |
26 | TCCR1B |= (1 << WGM12); // Mode 4, CTC on OCR1A |
27 | OCR1A = OCRWERT; // Compare match auf OCRWERT setzen |
28 | // = (F_CPU/256)-1
|
29 | |
30 | TIMSK |= (1 << OCIE1A); // Interrupt bei compare match aktivieren |
31 | |
32 | sei(); // enable interrupts |
33 | |
34 | }
|
35 | |
36 | int main(void) |
37 | {
|
38 | ctc_init(); |
39 | |
40 | while(1) |
41 | {
|
42 | if (sec==5) |
43 | {
|
44 | PORTB ^= LED1; // toggle LED ..ist NICHT OK |
45 | sec= 0; // sec auf 0 setzen |
46 | }
|
47 | }
|
48 | }
|
49 | |
50 | ISR (TIMER1_COMPA_vect) // Timer1 Compare Match Interrupt |
51 | {
|
52 | PORTB ^= LED0; // LED im Sekundentakt toggeln lassen = OK !! |
53 | sec = sec+1; // sec um 1 erhöhen |
54 | |
55 | }
|
Was mache ich da nur falsch ?? Gruss und Dank Gerhard