Moin Leute,
Ich versuche mich gerade an Interrupts und habe mir dazu folgenden
Beitrag durchgelesen:
https://sites.google.com/site/qeewiki/books/avr-guide/external-interrupts
Anbei mein Code:
1 | #include <avr/io.h>
|
2 | #define F_CPU 1000000
|
3 | #include <util/delay.h>
|
4 | #include <avr/interrupt.h>
|
5 |
|
6 |
|
7 | int main(void)
|
8 | {
|
9 |
|
10 | DDRB |= (1<<PB0); // PB0 declared as output pin
|
11 | DDRD |= (1<<PD6); // PD6 declared as output pin
|
12 | DDRD &= ~(1<<PD2); // Declared as input
|
13 | PORTD |= (1<<PD2); // Activate internal pull-up resistor
|
14 | sei(); // Enable interrupts
|
15 | MCUCR |= (0x00); // Sets the external interrupt INT0 to low level
|
16 | GICR |= (0x40); // External interrupt pin is enabled
|
17 |
|
18 |
|
19 | while(1) {
|
20 | PORTD &= ~(1<<PD6);
|
21 | PORTB |= (1<<PB0); // Sets output high
|
22 | _delay_ms(100);
|
23 | PORTB &= ~(1<<PB0); // Sets output low
|
24 | _delay_ms(100);
|
25 |
|
26 |
|
27 |
|
28 | }
|
29 | }
|
30 |
|
31 | ISR(INT0_vect)
|
32 | {
|
33 | PORTB &= ~(1<<PB0);
|
34 | PORTD |= (1<<PD6); // Sets PD6 high when interrupt occurs
|
35 | }
|
Im Datenblatt des Atmega8 steht nun, dass die Interruptroutine auch
ausgelöst werden kann, wenn der INT0 Pin als Ausgang definiert ist. Dazu
habe ich den Code wie folgt umgeschrieben:
1 | #include <avr/io.h>
|
2 | #define F_CPU 1000000
|
3 | #include <util/delay.h>
|
4 | #include <avr/interrupt.h>
|
5 |
|
6 |
|
7 | // volatile uint8_t test;
|
8 |
|
9 | int main(void)
|
10 | {
|
11 |
|
12 | DDRB |= (1<<PB0); // PB0 declared as output pin
|
13 | DDRD |= (1<<PD6); // PD6 declared as output pin
|
14 | /*
|
15 | DDRD &= ~(1<<PD2); // Declared as input
|
16 | PORTD |= (1<<PD2); // Activate internal pull-up resistor
|
17 | */
|
18 | DDRD |= (1<<PD2); // Declared as output pin
|
19 | sei(); // Enable interrupts
|
20 | MCUCR |= (0x00); // Sets the external interrupt INT0 to low level
|
21 | GICR |= (0x40); // External interrupt pin is enabled
|
22 |
|
23 |
|
24 | while(1) {
|
25 | PORTD &= ~(1<<PD6);
|
26 | PORTB |= (1<<PB0); // Sets output high
|
27 | _delay_ms(100);
|
28 | PORTB &= ~(1<<PB0); // Sets output low
|
29 | _delay_ms(100);
|
30 |
|
31 |
|
32 |
|
33 | }
|
34 | }
|
35 |
|
36 | ISR(INT0_vect)
|
37 | {
|
38 | PORTB &= ~(1<<PB0);
|
39 | PORTD |= (1<<PD6); // Sets PD6 high when interrupt occurs
|
40 | }
|
Leider leuchtet die LED nun nicht so wie gehofft, was bedeutet, dass ich
entweder ein gravierendes Missverständnis bezüglich
Interrupts/Datenblatt habe oder irgendwo im Code stimmt etwas nicht. Von
alleine komme ich leider nicht drauf, weshalb ich gerne um eure Hilfe
bitten möchte.
Vielen Dank.
Gruß