Tuesday, January 4, 2011

Session 3 - Automatic blinking LEDs

We are going to achieve something more interesting in this session.  In the first session, the blinking of LED was out of our control.  In the second one, it was in control of switches.  Now controlled, automated switching is going to happen.  Here no need to connect any switches as in the previous case.  Just connect all the pins of B Port to 8 LEDs.  The aim of this session is to prepare a program which can make the LEDs blink automatically for a nominal time interval.  The following program does this job:


Most of the commands in this program are familiar through the previous sessions.  A new comer is the 'for' loop. 'For loop' is a very effective looping tool which can be used in variety of situations.  Here we have used this loop to acquire a delay between the blinking of LEDs.  Let us discuss how this task is accomplished by 'for' loop.  The syntax of 'for' loop is a little lengthy.  It looks like shown below:

From the first look, we can see a control variable named 'j' as main actor in this loop.  Inside the 'for loop', there are three parts.  The first one is familiar in the previous programs.  That is: 'int j=0'.  This statement assigns value '0' to the variable j.  The working of the loop depends on the value of this variable and so it is called the control variable.  The next part is the condition part.  Here it is 'j less than 20000'.  Compiler checks whether the value of the variable 'j' is less than 20000.  If it is true the statements written under the for loop statement will be executed.  (In this case, the value of 'j' is declared as zero.  So the condition is true).  Here the statement written under 'for loop' is PORTB=0x00;.  After executing this statement the third part of the 'for loop' statement will work out.  Here the third part is: 'j++'.  This statement tells the compiler to add one to the value of j.  That is, the statement PORTB=0x00 is executed once and the value of 'j' is incremented to '1' from '0'(0+1=1).  Then the condition is checked again (j less than 20000).  This cycle will repeat until the condition become false (Here when j=20000).

So let us think about the advantage of using this looping statement.  We could execute the statement PORTB=0x00 for 20,000 times without writing 20,000 commands!  We can include more than one statement below the for loop statement by using an opening and closing braces ({}), in the starting and ending of the statements below the 'for loop' statement.

Now let us come to our program.  Why do we need to execute the same command (PORTB=0x00) for 20,000 times?  When this statement is executed once, all the pins of 'B Port' will be in logic 0.  If we execute the statement 20,000 times repeatedly, there is no remarkable change in the output value.  Then why do we need to do this?  The answer is 'Delay'.
We may get a more clarified answer by looking at the next command.  It is another 'for loop' which executes the statement 'PORTB=0xFF'.  When this statement is executed, all the pins of 'B Port' will become logic 1.  And this is again repeated 20,000 times.  Now let us assume that the compiler will take one second to execute the same command for 20,000 times.  If it is so, the logic value of the pins will be altered between 0 and 1 in an interval of 1 second.  The result is: LEDs connected to these pins will be turned on and off in 1 second interval.  Here 1 second is assumed.  Time can be adjusted by calibrating the limiting value of variable 'j' with the output.

Concatenating programs with real time can be done accurately by doing some theoretical calculations.  It is related to the clock frequency of the processor, the time taken by the processor to execute each instruction cycle, etc.  Anyway we can discuss those matters later and now it is desirable to do a calibration technique to achieve one second time delay.

Output of this program is the automated blinking of LEDs in desired time interval.  Thus we have reached our target.  In upcoming sessions we can step further.

2 comments:

  1. hey when is the next session..? btw last three sessions were awesome..:)

    ReplyDelete
  2. Next post is expected to be published soon... The background work is going on...
    And thanks for your comment...

    ReplyDelete