Hilfe bei AVR PCINT

OP #3790809
Lesenswert?

Hallo
Ich möchte mit einem Attiny 85 den PCINT des Reset Pins nutzen. Die 
Fuses hab ich entsprechend gesetzt. Hier der Code:
1
#include <avr/io.h>
2
#include "nrf24.h"
3
#define F_CPU 1000000L
4
#include <util/delay.h>
5
#include <avr/interrupt.h>
6
uint8_t tx_address[5] = {0xE7,0xE7,0xE7,0xE7,0xE7};
7
uint8_t messages[2][8]={{0,0,126,126,126,0,0},{0,0,127,127,127,0,0}};//opened closed
8
int main() {
9
  nrf24_init();
10
    PCMSK|=(1<<5);
11
    GIMSK|=(1<<5);
12
    DDRB&= ~(1<<5);
13
    PORTB |= (1<<5);
14
    /* Channel #2 , payload length: 8 */
15
      nrf24_config(2,8);
16

17
    /* Set the device addresses */
18
      nrf24_tx_address(tx_address);
19
        sei();
20
    while(1) {
21
    }
22
        
23
    return 0;
24
}
25
ISR(PCINT0_vect){
26
  _delay_ms(50);
27
  nrf24_send(messages[(PINB & (1<<5))>>5]);
28
    while(nrf24_isSending());
29
}
Wenn ich aber PB5 an Masse lege wird die ISR immer doppelt ausgeführt. 
Wenn ich die Verbindung wieder trenne nicht. Warum ist das so?

LG
#3790863
Lesenswert?

Weil das zweite Ereignis gespeichert wir, während die erste ISR noch 
läuft.
Beim Loslassen hast du wohl tatsächlich nur eine einzige Flanke, gut 
möglich.

Deine ISR ist an sich Mist, mit delay und zeitaufwändiger Kommunikation.
Kannst ja als erste Massnahme erstmal vor Verlassen der ISR weitere 
anhängige Pinchange-Ints löschen, dann wirds funktionieren. Ist aber 
bestimmt noch nicht die endgültige Lösung :-)
OP #3790874
Lesenswert?

So gehts:
1
#include <avr/io.h>
2
#include "nrf24.h"
3
#define F_CPU 1000000L
4
#include <util/delay.h>
5
#include <avr/interrupt.h>
6
uint8_t tx_address[5] = {0xE7,0xE7,0xE7,0xE7,0xE7};
7
uint8_t messages[2][8]={{0,0,126,126,126,0,0},{0,0,127,127,127,0,0}};//opened closed
8
char state=-1;
9
int main() {
10
  nrf24_init();
11
    PCMSK|=(1<<5);
12
    GIMSK|=(1<<5);
13
    DDRB&= ~(1<<5);
14
    PORTB |= (1<<5);
15
    /* Channel #2 , payload length: 8 */
16
      nrf24_config(2,8);
17

18
    /* Set the device addresses */
19
      nrf24_tx_address(tx_address);
20
        sei();
21
    while(1) {
22
      if(state){
23
        cli();
24
        nrf24_send(messages[state-1]);
25
        while(nrf24_isSending());
26
        state=-1;
27
        sei();
28
      }
29
    }
30
        
31
    return 0;
32
}
33
ISR(PCINT0_vect){
34
  _delay_ms(50);
35
  state=(PINB & (1<<5)>>5)+1;
36
}
OP #3790893
Lesenswert?

Jetzt aber:
1
#include <avr/io.h>
2
#include "nrf24.h"
3
#define F_CPU 1000000L
4
#include <util/delay.h>
5
#include <avr/interrupt.h>
6
uint8_t tx_address[5] = {0xE7,0xE7,0xE7,0xE7,0xE7};
7
uint8_t messages[2][8]={{0,0,126,126,126,0,0},{0,0,127,127,127,0,0}};//opened closed
8
int state=-1;
9
int main() {
10
  nrf24_init();
11
    PCMSK|=(1<<5);
12
    GIMSK|=(1<<5);
13
    DDRB&= ~(1<<5);
14
    PORTB |= (1<<5);
15
    /* Channel #2 , payload length: 8 */
16
      nrf24_config(2,8);
17

18
    /* Set the device addresses */
19
      nrf24_tx_address(tx_address);
20
        sei();
21
    while(1) {
22
      if(state){
23
        cli();
24
        nrf24_send(messages[state-1]);
25
        while(nrf24_isSending());
26
        state=-1;
27
        sei();
28
      }
29
    }
30
        
31
    return 0;
32
}
33
ISR(PCINT0_vect){
34
  _delay_ms(50);
35
  if(PINB & (1<<5))state=1;
36
  else state=2;
37
}

Antwort schreiben

Bitte melde dich an, um einen Beitrag zu schreiben.

oder

Mit Google-Account einloggen

Die Registrierung ist kostenlos und dauert nur eine Minute.

Jetzt registrieren