2017-05-13 1 views
0

J'essaye actuellement de créer un programme simple pour un émulateur ATMega48 qui a un commutateur externe de PCINT2 écoutant sur l'entrée de PORTD et changeant la production basée sur sa valeur.ATMega48 - comment enregistrer un commutateur externe PCINT2?

est ici le code de l'interrupteur:

unsigned int d; // a variable that has to change based on the input 

ISR(PCINT2_vect) { 
    if (PORTD % 2 == 0) { 
    if (d <= 8000) { 
     d += 500; 
    } 
    } else { 
    if (d >= 1000) { 
     d -= 500; 
    } 
    } 
} 

la fonction principale():

int main(void) 
{ 
    DDRD = 0x00; // set PORTC for input 
    DDRB = 0xFF; // set PORTB for output 
    PORTB = 0x00; // Initial value is 0 
    PCMSK0 = 0b00000100; 
    d = 4000; 
    sei(); 

    while (1) { 
    // Enable\Disable the LED with an initial delay 
    if (PIND == 0b00000000) { 
     PORTB = 0b00100000; 
    } else { 
     PORTB = 0b00000000; 
    } 

    // Delay for N seconds (determined by interrupt) 
    delay_stuff(d); 
    } 

    return 1; 
} 

Actuellement, il est de ne pas appeler l'interrupteur, peu importe ce qui se passe à un port, mon hypothèse est que je Je ne suis pas en train d'enregistrer des auditeurs ATMega magiques pour appeler l'interrupteur. Comment enregistrer l'interruption alors, qu'est-ce qui me manque ici?

Répondre

1

Selon datasheet la page 75-76, vous devez activer Pin Modifier Interrompre dans PCICR registre et sélectionner la broche sera activée sur l'IO PCMSK2 pour PCINT2 (PCMSK0 est pour PCINT0, i.e. PINB) correspondant.

int main(void) 
{ 
    DDRD = 0x00; // set PORTC for input 
    DDRB = 0xFF; // set PORTB for output 
    PORTB = 0x00; // Initial value is 0 

    PCICR |= _BV(PCIE2); //or, PCICR |= 0x04; 
    PCMSK2 |= 0xFF;  //any changes in PIND will trigger interrupt 
    d = 4000; 
    sei(); 

    //other codes... 
}