我如何使用计时器1以特定速度闪烁2个LED,如果我按下按钮,闪烁速度会增加吗?

问题描述 投票:-1回答:1

大家好,我使用atmega328p并试图使LED闪烁,当按下按钮时,它会增加到显示的点,而实际上它会快速闪烁代码如下]

        uint32_t lastTime = 0;
        int selectedTime = 20;
        unsigned long previousMillis = 0;        // will store last time LED was updated
        long interval = 1000;
        ISR(TIMER1_OVF_vect) {
          LEDswitch();
        }
 void btn(){
 }

        void setup() {
          Serial.begin(9600);
          PORTD |= BUTTONS_MASKS;
          DDRD &= ~ BUTTONS_MASKS;
          DDRB |= (1 << PINB2) | (1 << PINB3); // set all pins to 0 except on pin 6
          OCR1A =counterval;
          TCCR1A=0x00;
          TCCR1B=0x00;
          TCCR1B |= (1 <<CS10);
          TIMSK1 |= (1<<TOIE1);
          sei();
        }
    void LEDswitch()
    {
      int counter= 0;
      switch(counter){
        counter++;
        case 1:
          PORTB ^= 1 << PINB2;
          counter++;
          break;
          case 2:
            PORTB ^= 1 << PINB3;

    }}

大家好,我使用atmega328p并试图使LED闪烁,当按下按钮时,它会增加到出现的点,而其实际快速闪烁的代码如下...

c++ avr arduino-uno
1个回答
0
投票

您可以使用静态计数器来切换LED

© www.soinside.com 2019 - 2024. All rights reserved.