srbombaywala
Newbie level 1
Hi there,
I decided to toggle the leds by using PCINT interrupts.
but now the problem is that as i use a swtich it interrupts twice. Once when the key is pressed and other when released and so the toggle become two times on one press.
I included a condition in the ISR but nothing seems to work.
what i want is that if the key is kept pressed; nothing should happen and the led's should not toggle unless its released.
the code is as below
I wont be using int0 and int1 level triggers as they are needed to be used for other functions.
Kindly help.
I decided to toggle the leds by using PCINT interrupts.
but now the problem is that as i use a swtich it interrupts twice. Once when the key is pressed and other when released and so the toggle become two times on one press.
I included a condition in the ISR but nothing seems to work.
what i want is that if the key is kept pressed; nothing should happen and the led's should not toggle unless its released.
the code is as below
Code:
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#define SETBIT(ADDRESS,BIT) (ADDRESS |= (1<<BIT))
#define CLEARBIT(ADDRESS,BIT) (ADDRESS &= ~(1<<BIT))
#define FLIPBIT(ADDRESS,BIT) (ADDRESS ^= (1<<BIT))
#define CHECKBIT(ADDRESS,BIT) (ADDRESS & (1<<BIT))
#define WRITEBIT(RADDRESS,RBIT,WADDRESS,WBIT) (CHECKBIT(RADDRESS,RBIT) ? SETBIT(WADDRESS,WBIT) : CLEARBIT(WADDRESS,WBIT))
volatile int i=0;
int main(void)
{
DDRB &= ~(1 << DDB0); // Clear the PB0 pin
// PB0 (PCINT0 pin) is now an input
PORTB |= (1 << PORTB0); // turn On the Pull-up
// PB0 is now an input with pull-up enabled
DDRD = 0xFF;
PORTD = 0xF9;
PCICR |= (1 << PCIE0); // set PCIE0 to enable PCMSK0 scan
PCMSK0 |= (1 << PCINT0); // set PCINT0 to trigger an interrupt on state change
sei(); // turn on interrupts
while(1)
{
if(i==3)
{
i=0;
SETBIT(PORTD,0);
CLEARBIT(PORTD,1);
CLEARBIT(PORTD,2);
}
else
if(i==1)
{
SETBIT(PORTD,1);
CLEARBIT(PORTD,2);
CLEARBIT(PORTD,0);
}
else
{
SETBIT(PORTD,2);
CLEARBIT(PORTD,0);
CLEARBIT(PORTD,1);
}
}
}
ISR (PCINT0_vect)
{
if(PORTB & (1 << PB0))
{
i++;
}
else
{
i=i;
}
}
I wont be using int0 and int1 level triggers as they are needed to be used for other functions.
Kindly help.