ATMEGA8515

Go To Last Post
5 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I have 8 push buttons in my board with atmega8515.I am trying to cycle the LEDS upto the pressed switch. But for some reason it is not entering the switch cases in my code. I want to cycle the LEDS untill the pressed switch. What is wrong in my code? Even it is not cycling untill the pressed switch.Where I am making mistake? Could someone look into my ISR(SIG_INTERRUPT0) routine. I have added the code for cycling in the 0x04 (switch case).Because atmega 8515 has INT0 IS WITH 2ND SWITCH

#include 
#include 
#include 

#define BIT(n) (1 << n) 

volatile uint8_t led, k, switches, i = 1 , j = 1;
  			/* uint8_t is the same as unsigned char */
int main(void)
{



	DDRB = 0xff;				/* use all pins on PortB for output */
	DDRD = 0x00;

	led = 1;					/* init variable representing the LED state */
	PORTB = 0XFF;
	
	cli( );

	TCCR0|=(1<<CS02) |(1<<CS00);

 //Enable Overflow Interrupt Enable
   TIMSK|=(1<<TOIE0);

 //Initialize Counter
   TCNT0=0;


   GICR = BIT(INT0);
    MCUCR = BIT(ISC01) | BIT(ISC00);

   sei( );

	for ( ; ;);
	
	

}


ISR(TIMER0_OVF_vect)
{
switches  = PIND;
  
  }		



ISR(SIG_INTERRUPT0)
{
	 
	 	  switch (switches)
		{
		case 0x01:
		case 0x02:
		case 0x04: 
					PORTB = ~led;			/* invert the output since a zero means: LED on */
					led <<= 1;				/* move to next LED */
					if (led == 0x04)				/* overflow: start with Pin B0 again */
					{
						led = 1;
					}
					break;
		case 0x08:
		case 0x10:
		case 0x20:
		case 0x40:
		case 0x80:
			
			break;
		
}
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

is it compile/Build without error??
quick look show your missing a " } ".

#include 
#include 
#include 

#define BIT(n) (1 << n)

volatile uint8_t led, k, switches, i = 1 , j = 1;
           /* uint8_t is the same as unsigned char */
int main(void)
{



   DDRB = 0xff;            /* use all pins on PortB for output */
   DDRD = 0x00;

   led = 1;               /* init variable representing the LED state */
   PORTB = 0XFF;
   
   cli( );

   TCCR0|=(1<<CS02) |(1<<CS00);

 //Enable Overflow Interrupt Enable
   TIMSK|=(1<<TOIE0);

 //Initialize Counter
   TCNT0=0;


   GICR = BIT(INT0);
    MCUCR = BIT(ISC01) | BIT(ISC00);

   sei( );

   for ( ; ;);
   
   

}


ISR(TIMER0_OVF_vect)
{
switches  = PIND;
 
  }      



ISR(SIG_INTERRUPT0)
{
   
         switch (switches)
      {
      case 0x01:
      case 0x02:
      case 0x04:
               PORTB = ~led;         /* invert the output since a zero means: LED on */
               led <<= 1;            /* move to next LED */
               if (led == 0x04)            /* overflow: start with Pin B0 again */
               {
                  led = 1;
               }
               break;
      case 0x08:
      case 0x10:
      case 0x20:
      case 0x40:
      case 0x80:
       
         break;
     }    //******************************* missing??
} 

P.Ashok Kumar

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

1. Show schematics or post a link to a description of the board (or at least a name).

2. Testing switches like you will often not work. You need to isolate the bit representing the individual switch you want to test for. Go over to the Tutorials forum and read the tutorial on "Bit Manipulation".

3. The whole interrupt setup seems strange. You poll the switches on a timer interrupt, but actually (try to) decode the pin values on a pin interrupt. The polling based on a timer makes little sense as it stands right now. You could just as well read the PIND value in the INTERRUPT0 ISR.

4. But if you do that, you will immediately be hit by "switch bounce". Search this site for "switch bounce", "debounce" etc to read a lot on this issue and the possible solutions. (One often used solution is to poll switches based on a timer, so you're back to that, and then look for several such polls where a switch is in a steady state. This debounce algorithm places the transitions to a new steady state in some variable where the main loop of your application can "consume" them. In that scenario the INTERRUPT0 usage is no longer meaningful. Indeed, if you do those searches you will find good arguments as to why it is very unwise to base switch decoding on an pin change interrupt. Do those searches..

EDIT:
5. Since your LEDs are "active low", it might be the same with the switches? (Is this by any chance on an STK-500 board? If so, then the switches are definitively "active low" and my point #2 above definitively applies.)

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks . I look into those.

Since Atmega8515 doesn't support pin change interrupt. I am using INT0. I am polling the switches using the timer. When portd.2 switch is pressed , I am trying to cycle that LEds upto the 2nd switch.

JohanEkdahl wrote:
1. Show schematics or post a link to a description of the board (or at least a name).

2. Testing switches like you will often not work. You need to isolate the bit representing the individual switch you want to test for. Go over to the Tutorials forum and read the tutorial on "Bit Manipulation".

3. The whole interrupt setup seems strange. You poll the switches on a timer interrupt, but actually (try to) decode the pin values on a pin interrupt. The polling based on a timer makes little sense as it stands right now. You could just as well read the PIND value in the INTERRUPT0 ISR.

4. But if you do that, you will immediately be hit by "switch bounce". Search this site for "switch bounce", "debounce" etc to read a lot on this issue and the possible solutions. (One often used solution is to poll switches based on a timer, so you're back to that, and then look for several such polls where a switch is in a steady state. This debounce algorithm places the transitions to a new steady state in some variable where the main loop of your application can "consume" them. In that scenario the INTERRUPT0 usage is no longer meaningful. Indeed, if you do those searches you will find good arguments as to why it is very unwise to base switch decoding on an pin change interrupt. Do those searches..

EDIT:yes
5. Since your LEDs are "active low", it might be the same with the switches? (Is this by any chance on an STK-500 board? If so, then the switches are definitively "active low" and my point #2 above definitively applies.)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

No compilation err. When I did the cut copy paste , I would have forgot copy the last line.

ashokok wrote:
is it compile/Build without error??
quick look show your missing a " } ".

#include 
#include 
#include 

#define BIT(n) (1 << n)

volatile uint8_t led, k, switches, i = 1 , j = 1;
           /* uint8_t is the same as unsigned char */
int main(void)
{



   DDRB = 0xff;            /* use all pins on PortB for output */
   DDRD = 0x00;

   led = 1;               /* init variable representing the LED state */
   PORTB = 0XFF;
   
   cli( );

   TCCR0|=(1<<CS02) |(1<<CS00);

 //Enable Overflow Interrupt Enable
   TIMSK|=(1<<TOIE0);

 //Initialize Counter
   TCNT0=0;


   GICR = BIT(INT0);
    MCUCR = BIT(ISC01) | BIT(ISC00);

   sei( );

   for ( ; ;);
   
   

}


ISR(TIMER0_OVF_vect)
{
switches  = PIND;
 
  }      



ISR(SIG_INTERRUPT0)
{
   
         switch (switches)
      {
      case 0x01:
      case 0x02:
      case 0x04:
               PORTB = ~led;         /* invert the output since a zero means: LED on */
               led <<= 1;            /* move to next LED */
               if (led == 0x04)            /* overflow: start with Pin B0 again */
               {
                  led = 1;
               }
               break;
      case 0x08:
      case 0x10:
      case 0x20:
      case 0x40:
      case 0x80:
       
         break;
     }    //******************************* missing??
} 

P.Ashok Kumar