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.

Pls help me in únderstanding this code

Status
Not open for further replies.

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
}
}
.............
 

Hi

I can't understand :D found in you for loop

but I think it's so clear.
it's just an endless loop reading an input and setting the corresponding led


Salam
Hosssam
 

Hi somf0872,

there is a problem with the for loop condition you have given above. is it related to PORTB?

thanks.
 

BTW your code can be replaced by :
Code:
unsigned char keys

	for (;;)
	{
		keys = ~PIND; 		
		keys = keys & -keys;
		if(keys != 0)
			PORTB = ~keys;
	}

P.S,. really nice book to read https://www.jjj.de/fxt/fxtbook.pdf
 

    somf0872

    Points: 2
    Helpful Answer Positive Rating
Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top