简体   繁体   中英

C - atmega328p - I don't understand what is wrong

i do not unterstand why my code isnt working(doISR). What its supposed to do:

-there is a running light(6 LEDs) and 3 Buttons. I need to press the button at the right time(Button 1, if LED 1 or 2 is on....etc). if its correct: increase the speed, if not: reset.

i bet its a blunder, but i dont get it :)

   void wait(void) {
   while (!(TIFR1 & (1 << OCF1A))) // wait
   ;
   TIFR1 |= (1 << OCF1A); // delete Interrupt flag 
}

volatile bool ISRhasHappend = false;

ISR(PCINT0_vect) {
    ISRhasHappend = true;
}

int main(void) {
    OCR1A = 3125 - 1; //2 seconds
    TCCR1B |= (1 << WGM12); //turn on CTC mode
    TCCR1B |= (1 << CS12) | (1 << CS10); //Prescalemode 101 -> 1024 Takt
    DDRB = (1 << PCINT5); 
    DDRC = 0x3f; 
    PCICR = 0x3f; //Pin Change Interrupt Control Register
    PCMSK0 = (1 << PCINT0) | (1 << PCINT2) | (1 << PCINT4); 

   sei();
   doRunningLight();
   if (ISRhasHappend == true) {
        doISR();
   }
}

void doISR() {

    //while timee >0
    for(int x=3125;x>0;x=x-250){
        //if LEDs 0+1 on, and button0 pressed ...etc                                            
        if ((PORTC & (0b00000011)) && (PINB & (1 << PINB0)) || (PORTC & 
        (0b00001100)) && (PINB & (1 << PINB2)) || (PORTC & (0b00110000)) && 
      (PINB & (1 << PINB4))) {
               //All led lights up
               PORTC |= 0x3f;
               wait();
               //reduce timer if catched the light
               OCR1A = x;
           }
           else {
               //turn signal
               for (int y = 1; y < 5; y++) {
                   PORTB ^= (1 << PCINT5);
                   wait();
               }
                //back to 3124 if failed
                OCR1A = 3125 - 1;
           }
    }
    ISRhasHappend = false;
}

void doRunningLight(){

   while(1) {
       for (int i = 0; i<6; i++){
           PORTC |= (1<<i);
           wait();
           PORTC  &= ~(1<<i);
       }
   }
}

The function doRunningLight() never returns, so the code that you wrote after it to check ISRhasHappend and call doIsr will never run. You will need to think of a different way to handle your events, probably without using blocking delay loops.

I would also question whether you actually need a pin-change interrupt. Button presses are slow enough that your microcontroller should be able to detect them by reading the pin in the main loop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM