Thursday, March 8, 2012

Time Display On LCD using 8051 Microcontroller

/* Displaying time on LCD using microcontroller
    To get 1sec delay timer(16bit mode) should overflow 14 times, 
 but instead i have used only 11 times because I 
 have counted prograam instructions delays and 11 
 times will increment exactly 1sec.   

 algorithm: wait in the loop for INCREMENT to be 1
      INCREMENT will be 1 only when the timer0
      overflows for 11 times(spcifically for my program)
      see my program logic, you can understand  */


#include<reg51.h>
#include "lcd_display4bit.h"

sbit ON=P1^0;
sbit SET=P1^1;
sbit incr_hrs=P1^2;
sbit incr_min=P1^3;
sbit set_hrs=P1^5;
sbit set_min=P1^4;

int sec,min,hrs;
int min_value=0,hrs_value=0,sec_value=0;
int count=0,increment=0;

void timer0() interrupt 1
{

  EA=0;
   TH0=0x00;
   TL0=0x00;
   count++;       //everytime timer overflows count is incremented
   if(count==11)  //if count is 11 then make increment=1
    {             //why 11 because if timer overflows for 11 times  
     count=0;     //then it will be 1sec
     increment=1;
    }
 else 
  increment=0;
  TF0=0;
  EA=1;
}


void main()
{
  int i;
  en=rs=0;
  ON=SET=incr_min=incr_hrs=set_hrs=set_min=1;
  P2=0x00;
  EA=1;
  ET0=1;
  TMOD=0x01;
  lcd_init();
  
   
   while(1)
    {
     while(!SET)
      {
       lcd_cmd(0x80);
       lcd_data_int(hrs_value);
       lcd_data(':');
       lcd_cmd(0x84);
       lcd_data_int(min_value);
       lcd_data(':');
       lcd_cmd(0x88);
       lcd_data_int(sec_value);

      while(!set_hrs)
       {
         lcd_cmd(0x80);
         lcd_data_int(hrs_value);
         if(!incr_hrs)
          {
           if(hrs_value==24)
              hrs_value=0;
           else
             hrs_value +=1;
         for(i=0;i<20000;i++); //delay for incrementing hrs_value by 1
    }                            //if delay is not present then, hrs_value
    }                        //will increment more than 1
            
   while(!set_min)
    {
      lcd_cmd(0x84);
      lcd_data_int(min_value);
       if(!incr_min)
        {
         if(min_value==59)
              min_value=0;
         else
              min_value +=1;
         for(i=0;i<20000;i++);
        }
    }
  }//end of While(!SET) loop


 while(!ON)
 {
   lcd_cmd(0xc0);
   lcd_dataS("hrs min sec");
   for(hrs=hrs_value;hrs<=24;hrs++)
    {
     lcd_cmd(0x80);
     lcd_data_int(hrs);
     lcd_data(':');
     for(min=min_value;min<=59;min++)
      {
       lcd_cmd(0x84);
       lcd_data_int(min);
       lcd_data(':');
       for(sec=sec_value;sec<=59;sec++)
        {
         lcd_cmd(0x88);
         lcd_data_int(sec);
         TH0=0;
         TL0=0;
         TR0=1;
         while(!increment); //wait until increment is made 1  
                            //increment will be 1 only when timer 
                            //overflows 11 times i.e.,1sec
         increment=0;      
        }
      }
    }
     }// end of while(!ON) loop
   }//end of super loop
}

1 comment: