Squirrels have fuzzy tails.
0 Members and 1 Guest are viewing this topic.
while(!(PIND & (1 << PIND2))){} // Button release detection
while(PIND & (1 << PIND2)){} // Button pressed detection
#define F_CPU 1000000UL#include <avr/io.h>#include <avr/interrupt.h>#include <util/delay.h>int main(void){ PORTD |= 1 << PORTD2; // Set PD2 (INT0) high (pull - up) TCCR1A |= 1 << COM1A1; // Set OC1A on Compare Match when upcounting, clear - when downcounting TCCR1A |= 1 << WGM11; // PWM, phase and frequency correct TCCR1B |= 1 << WGM13; // PWM, phase and frequency correct TCCR1B |= 1 << CS10; // No timer prescaling ICR1 = 3; // PWM top value OCR1A = 0; // PWM compare value; EICRA |= 1 << ISC01; // The falling edge of INT0 (PD2) generates an interrupt request EIMSK |= 1 << INT0; // Enable INT0 external interrupt SREG |= 1 << 7; // Enable global interrupt DDRB |= 1 << DDB1; // Set PB1 (OSC1A) as output while(1) {}}ISR(INT0_vect) // Routine when interrupt occours{ while(!(PIND & (1 << PIND2))){} // Button release detection if(OCR1A < 3) // If(DutyCycle < 100%) { OCR1A++; // Increase PWM compare value by 1 (increase duty cycle) } else { OCR1A = 0; // Set PWM compare value to 0 (set duty cycle to 0%) } _delay_ms(2000);}
ISR(INT0_vect) // Routine when interrupt occours{ while(!(PIND & (1 << PIND2))){} // Button release detection [ ...]}
When the switch bounces up, this loop will end and the following code will execute. Then the ISR will be called again when the switch bounces down. I would think the 2ms delay at the end of the isr would deal with that, but I've heard of switches that take longer to stop bouncing.
I understand what You say, however, the delay is NOT 2ms, it is 2s (2000ms).