Blinking a led using timer in Mode 0.

 

  Code 

#include<reg51.h>                            //  Include 8051 library.
void delay(unsigned int t);                 // Delay for turning ON and OFF LED,
sbit led = P1^0;                                 //  Set the port where the led is connected.
void main()                                        // Main function starts
{
         led = 1;                                       // Led turned OFF.
while(1) // Infinite loop.
{
led = 0;                          // Led turned ON.
delay(1000);                 // Delay function calling for 8sec = 1000 * 8millisec
. led = 1;                         // Led turned OFF.
delay(1000);                 // Delay function calling for 8sec = 1000 * 8millisec.
}
}

void delay(unsigned int t)        // Delay function
{
int i;                                        // Assigning integer i.
TMOD = 0X00;                       // Setting the Timer 0 in Mode 0 config. 
for(i=0;i<t;i++)                     // For loop for delay
{
TH0 = 0X19;                          // Assigning 19hex value to TH for 8millisec delay.
TL0 = 0X13;                          // Assigning 13hex value to TL for 8millisec delay.
TR0 = 1;                                // Initializing Timer 0. 
while(TF0==0);                    // Waiting for timer to complete 8millisec.
        TF0 = 0;                                // Timer overflow flag reset.
        }
}

     :  Circuit Diagram     

        

           Led is connected in sinking mode. Sinking mode is when cathode of  LED is connected to Port of 8051 and anode is connected to Vcc.

            A ballast resistor is added in series with led to limit the flow of current through the led and prevent excess current that can blow the led. 






Blinking Led Result   : 

   Initially we make the port(P1.0) high for the led to turn off.

  As soon as port(P1.0) becomes low the led forward biases and it turns on.

Here the delay is of 8 millisec but since the delay is very small , we cannot see the led switch states . So we have passed 1000 into the delay function to get a delay of 8 sec.

( gif just for reference - not accurate )---------->



                                                                 




Comments