51 microcontroller: use of timer and LCD1602 to make a simple clock

#include "regx52.h"
#include "LCD1602.h"

void Delay(int time)
{     while(time--) //The while loop takes 10us     {     } }



 void Timer0Init(void) //1 millisecond @11.0592MHz
{

    TMOD &= 0xF0; //Set the timer mode
    TMOD |= 0x01; //Set the timer mode
    TL0 = 0x66; //Set the initial timer value
    TH0 = 0xFC; //Set the initial timer value
    TF0 = 0; //Clear TF0 Flag
    TR0 = 1; //Timer 0 starts timing
    ET0 = 1;
    EA = 1;
    PT0 = 0;
}

//Define variables
int Seconds, Minutes, Hours,count;
 
//What needs to be executed after the interrupt
void Timer0_motion() interrupt 1
{  
    TL0 = 0x66; //Set the initial timing value
    TH0 = 0xFC; //Set the initial timing value
    
       count++;
   if(count == 1000)
   {    
           Seconds++; 
        if(Seconds>=60)
        {             Seconds=0;             Minutes++;             if(Minutes>=60)             {                 Minutes=0;                 Hours++;                 if(Hours>=24)                 {                     Hours=0;                 }             }         }












           
          count=0;
   }

}

int main()
{     int KeyNum;     Timer0Init();//Timer initialization     LCD_Init(); //LCD1602 initialization     LCD_ShowString(1,2,"Clock:");     LCD_ShowChar(2,4,':');     LCD_ShowChar( 2,7,':');      while(1)      {           //Display hours         LCD_ShowNum(2,2,Hours,2);         //Display minutes         LCD_ShowNum(2,5,Minutes,2);         //Display seconds         LCD_ShowNum( 2,8,Seconds,2);          }     return 0;

    
    


    



    









    

Experimental phenomena

 

For some display functions of the LCD1602 display, you can go to Station B to view them.

Guess you like

Origin blog.csdn.net/weixin_52300845/article/details/124844068