2016-10-19 235 views
0

嗨即時通訊使用arduino Mega2560。 我想設置一個triger到中斷。 插針53,52,51,50和12,13。Arduino Mega2560設置中斷

void setup(){ 

PCICR |= (1 << PCIE0); 

    PCMSK0 |= (1 << PCINT0); 
    PCMSK0 |= (1 << PCINT1); 
    PCMSK0 |= (1 << PCINT2); 
    PCMSK0 |= (1 << PCINT3); 
    PCMSK0 |= (1 << PCINT6); 
    PCMSK0 |= (1 << PCINT7); 
} 

ISR(PCINT0_vect){ 
// Code when one of the pins was changed 
} 

這不適合我可以有人請幫忙嗎?

回答

1

測試,它的工作:

/* 
* Connections: 
* A0 (PF0) -> PCINT0 (PB0/53) 
* A1 (PF1) -> PCINT1 (PB1/52) 
* A2 (PF2) -> PCINT2 (PB2/51) 
* A3 (PF3) -> PCINT3 (PB3/50) 
* A4 (PF4) -> PCINT6 (PB6/12) 
* A5 (PF5) -> PCINT7 (PB7/13) 
*/ 

uint8_t pins[] = {A0,A1,A2,A3,A4,A5}; 

void setup() { 
    Serial.begin(115200); 

    // Make PF0-5 outputs: 
    DDRF = _BV(PF0) | _BV(PF1) | _BV(PF2) | _BV(PF3) | _BV(PF4) | _BV(PF5); 

    PCICR = _BV(PCIE0); 
    PCMSK0 = _BV(PCINT0) | _BV(PCINT1) | _BV(PCINT2) | _BV(PCINT3) | _BV(PCINT6) | _BV(PCINT7); 

    // Arduino sets global interrupts, so we don't have to do it here: 
    // sei(); 
} 

volatile uint8_t data = 0; 
uint8_t   last = 0; 

uint32_t change_ts = 1000; 
uint8_t  out_val = 0; 

void loop() { 

    if (data != last) { 
    Serial.print("Got: "); Serial.println(data, BIN); 
    last = data; 
    } 

    if (change_ts <= millis()) { 
    change_ts += 1000; 
    Serial.print(F("Changed to: ")); Serial.println(++out_val, BIN); 
    PORTF = out_val; 
    } 
} 

ISR(PCINT0_vect) { 
    data = PINB & PCMSK0; // read data and clear unused pins 
} 

如果不使用Arduino的框架,你必須sei()啓用全局中斷。如果從外部看不到ISR中設置的一些變量,則必須將其設置爲volatile

+0

非常感謝你的完美兄弟! –