somf0872
Member level 5
- Joined
- Oct 14, 2003
- Messages
- 92
- Helped
- 1
- Reputation
- 2
- Reaction score
- 0
- Trophy points
- 1,286
- Activity points
- 917
Can somebody help me that how the for loop is working in this code? I need some step by step explanation.
Kind regards,
--X--
.............
static volatile uint8_t led; // use volatile when variable is accessed from interrupts
int main(void)
{
unsigned char keys;
DDRB = 0xff; // use all pins on PortB for output
PORTB = 0xff;
DDRD = 0x00; // use all pins on port D for input
PORTD = 0xff; // activate internal pull-up
led = 1; // init variable representing the LED state
for (; { // loop forever
keys = ~PIND; // read input port with keys (active-low)
if ( keys & 1 )
led = 1;
else if ( keys & 2 ) // priority encoder: if multiple keys are pressed,
led = 2; // only the lowest key is recognized
else if ( keys & 4 )
led = 4;
else if ( keys & 8 )
led = 8;
else if ( keys & 0x10 )
led = 0x10;
else if ( keys & 0x20 )
led = 0x20;
else if ( keys & 0x40 )
led = 0x40;
else if ( keys & 0x80 )
led = 0x80;
if ( keys )
PORTB = ~led; // Set corresponding LED if key pressed
}
}
.............
Kind regards,
--X--
.............
static volatile uint8_t led; // use volatile when variable is accessed from interrupts
int main(void)
{
unsigned char keys;
DDRB = 0xff; // use all pins on PortB for output
PORTB = 0xff;
DDRD = 0x00; // use all pins on port D for input
PORTD = 0xff; // activate internal pull-up
led = 1; // init variable representing the LED state
for (; { // loop forever
keys = ~PIND; // read input port with keys (active-low)
if ( keys & 1 )
led = 1;
else if ( keys & 2 ) // priority encoder: if multiple keys are pressed,
led = 2; // only the lowest key is recognized
else if ( keys & 4 )
led = 4;
else if ( keys & 8 )
led = 8;
else if ( keys & 0x10 )
led = 0x10;
else if ( keys & 0x20 )
led = 0x20;
else if ( keys & 0x40 )
led = 0x40;
else if ( keys & 0x80 )
led = 0x80;
if ( keys )
PORTB = ~led; // Set corresponding LED if key pressed
}
}
.............