Hallo,
habe ein kleines Programm geschrieben um Compare und Overflow zu testen.
Die Ports überwache ich mit einem Logic Analyzer (s.Bild).
Meine Frage ist nun, warum der OVF schon beim Start des Timers
ausgeführt wird.
1 | /* PA0 Taster
|
2 | * Channel 0 PA1 Timer0 Start
|
3 | * Channel 1 PA7 OC0B PWM
|
4 | * Channel 2 PA2 TCNT0 Wert
|
5 | * Channel 3 PA3 ISR Compare
|
6 | * Channel 4 PA4 ISR OVF
|
7 | */
|
8 |
|
9 | //-------------------------------------------------------------------------
|
10 |
|
11 | #define F_CPU 1000000UL // ATtiny44-20 with 1Mhz
|
12 |
|
13 | #define Timer0_stop TCCR0B &= ~((1<<CS00)|(1<<CS01)|(1<<CS02))
|
14 | #define Timer0_start TCCR0B |= (1<<CS01)
|
15 |
|
16 | #include <avr/io.h>
|
17 | #include <avr/interrupt.h>
|
18 | #include <util/delay.h>
|
19 |
|
20 | //-------------------------------------------------------------------------
|
21 |
|
22 | ISR(TIM0_OVF_vect)
|
23 | {
|
24 | PORTA |= (1<<PA4);
|
25 | _delay_us(100);
|
26 | PORTA &= ~(1<<PA4);
|
27 | }
|
28 |
|
29 | //-------------------------------------------------------------------------
|
30 |
|
31 | ISR(TIM0_COMPB_vect)
|
32 | {
|
33 | PORTA |= (1<<PA3);
|
34 | }
|
35 |
|
36 | //-------------------------------------------------------------------------
|
37 |
|
38 | int main(void)
|
39 | {
|
40 | cli(); // clear Interrupt
|
41 |
|
42 | DDRA &= ~(1<<PA0); // PA0 Input
|
43 | PORTA |= (1<<PA0); // Pullup on
|
44 |
|
45 | DDRA |= (1<<PA1)|(1<<PA2)|(1<<PA3)|(1<<PA4); // PA1 - PA4 Output
|
46 | DDRA |= (1<<PA7); // PA7 OC0B
|
47 | TCCR0A = (1<<COM0B1)|(1<<WGM00)|(1<<WGM01); // fast PWM
|
48 | TCCR0B = (1<<WGM02); // fast PWM
|
49 |
|
50 | OCR0A = 250; // Top
|
51 | OCR0B = 125; // Compare
|
52 |
|
53 | TIMSK0 = (1<<OCIE0B)|(1<<TOIE0); // Compare und OVF
|
54 |
|
55 | sei(); // set Interrupt
|
56 |
|
57 | //---------------------------------------------------------------------------
|
58 |
|
59 | while(1)
|
60 |
|
61 | {
|
62 | if(!(PINA & (1<<PA0)))
|
63 | {
|
64 | _delay_ms(1000);
|
65 | TCNT0 = 0;
|
66 | PORTA |= (1<<PA1);
|
67 | Timer0_start;
|
68 | }
|
69 |
|
70 | if(TCNT0 == 125)
|
71 | {
|
72 | PORTA |= (1<<PA2);
|
73 | }
|
74 | }
|