I have a small task to program the following using C, for an ATmega48 controller:
In the beginning, all (6) LEDs are turned on. When button SW1 is pressed, LEDs are only allowed to be turned off with the respective buttons. When button SW2 is pressed, LEDs are only allowed to be turned on with the respective buttons.
Here is what I came up with:
#define F_CPU 1000000UL
#include <asf.h>
#include <util/delay.h>
volatile unsigned char r,d;
ISR(INT0_vect)
{
if (d = 0)
{
PORTB = PORTB | 1<<1;
d = 1;
}
else
{
PORTB = PORTB | 0<<2;
d = 0;
}
}
int main (void)
{
d = 0;
DDRC = 0x00;
PORTC = 0xFF;
DDRB= 0xFF;
PORTB = 0x00;
PCICR = 0b00000001;
sei();
while(1)
{
PCMSK0 = 0b00000100;
while (d == 0)
{
for (int i=0; i<6; i++)
{
if(!(PINC & (1<<i)))
PORTB = PORTB | i<<1;
}
}
PCMSK0 = 0b00000010;
while (d == 1)
{
for (int i=0; i<6; i++)
{
if(!(PINC & (1<<i)))
PORTB = PORTB | i<<0;
r = r<<1;
}
}
}
}
When I tried to simulate in Atmel AVR Studio, wrong LEDs were turning on when I pressed the respective buttons (e.g. LED2 for SW4), and interrupts never happened.
Please explain what I've done wrong, as I have already scoured several resources, and each one gives a different approach, which I fail to comprehend.



PORTB = PORTB | 0<<2;? This code does nothing! ... If you want to turn off the bit 2 you have to usePORTB = PORTB & ~(1<<2);