Thursday, June 24, 2010

Session 2 - Flash LED on Press of Switch


Now it is time to think about flashing LEDs on the press of switches.  The connections can be done like  8 pins of PORTA to 8 switches and 8 pins of PORTB to 8 LEDs.  The switches should be arranged in such a way that when it is pressed, a high voltage should be shorted to the other end.  The connection of LED is as in the previous session.  Our aim is to turn on the  LED when the respective switch is pressed.  That is, when we press switch 1, LED1 should flash and so on.  This can be achieved by writing a very simple code as shown below:


Program-2

Code Explained:

The code can be understood almost completely by a simple look on it.  However I am going to explain it a little.  Here our code starts with some initialization of PORTA and PORTB.  First PORTA is defined as input (DDRA=0x00;) and then PORTB as output (DDRB=0xFF;).  This time we have used the Hexadecimal format for coding.  Always keep in mind that each digit in HEX format represent 4 binary bits.  So here 0xFF means 0b11111111.

The next code used here is the 'while' loop.  I dare to explain what is a loop.  However, as its meaning indicates, it is a code used for repeated works.  What we write inside a loop, will be repeated.  The question is how much times should it repeat?  In this program the code used is 'while(1)'.  What does this '1' inside the brackets indicate?  Actually a condition is expected at the place of this '1'.  The loop will work on the basis of the result of that condition.  The result can attain two values: True (1) or False (0).  If the answer is a True then the codes written under the loop will work.  As the name of the loop is 'while' its meaning can be predicted.  That is, WHILE the condition is true, the code inside the loop will work.  Or in other words, the codes will work repeatedly until the condition become wrong.  For example take a look at the code below:

a=1;
while(a <= 10)
{
PORTA=0xFF;
a=a+1;
}

This code will work repeatedly for 10 times.
Now let us come to our example.  We have given a '1' instead of a condition and it means a TRUE result always.  Hence this loop always gives true result and the codes inside it will repeat infinitely!  This is what is called an infinite loop.  Infinite loop is an important concept in embedded programming.  We use it in many situations.

Now think about the code written inside the infinite loop which is going to be repeated infinite times (The term infinite means the time, from the processor is turned on and till it is turned off).  It is 'PORTB=PINA'.  This is the first time we use this code 'PINA'.  When a port is used as input, we use the keyword PIN to represent it.  If it is an output we use PORT.  Here the B port is used as output and A port as input and hence PORTB and PINA.  So what does this code do?  It just copies the value of PINA to PORTB.  If we press the switch (connects to HIGH value) connected to the 0th pin of A port (PA0), that value will be copied to PORTB.  So the result is when we press 0th swtich, 0th LED will flash.  When 1st switch is pressed 1st LED will flash and so on.  Since it is inside the infinite loop, the value of PINA is copied to PORTB continuously.  Thus we have reached our target!

1 comment:

  1. Next post is expected to be posted on or before 25th of July.

    ReplyDelete