Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Help on Atmega48 (LED toggle using a single key )

Status
Not open for further replies.

srbombaywala

Newbie level 1
Joined
Nov 2, 2011
Messages
1
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,289
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

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.
 

You interrupt fires when it is pressed or depressed but you only use the depress action so this is fine.
The problem is probably the button bounce, a single push can give several on/off pulses before it stabilizes, have you use a denounce circuit externally?

You will find several article using google, here is one of them
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top