51单片机:定时器与LCD1602的运用,做一个简易时钟

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

void Delay(int time)
{
    while(time--) //while循环一次需要10us
    {
    }
}

 void Timer0Init(void)        //1毫秒@11.0592MHz
{

    TMOD &= 0xF0;        //设置定时器模式
    TMOD |= 0x01;        //设置定时器模式
    TL0 = 0x66;        //设置定时初值
    TH0 = 0xFC;        //设置定时初值
    TF0 = 0;        //清除TF0标志
    TR0 = 1;        //定时器0开始计时
    ET0 = 1;
    EA  = 1;
    PT0 = 0;
}

//定义变量
int Seconds, Minutes, Hours,count;
 
// 中断后需要执行的内容
void Timer0_motion() interrupt 1
{  
    TL0 = 0x66;        //设置定时初值
    TH0 = 0xFC;        //设置定时初值
    
       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();//计时器初始化
    LCD_Init();     //LCD1602初始化
    
    LCD_ShowString(1,2,"Clock:");
    LCD_ShowChar(2,4,':');
    LCD_ShowChar(2,7,':');
    
     while(1)
     {
          //显示小时
        LCD_ShowNum(2,2,Hours,2);
        //显示分钟
        LCD_ShowNum(2,5,Minutes,2);
        //显示秒
        LCD_ShowNum(2,8,Seconds,2);    
     }
    
    return 0;

实验现象

关于LCD1602显示屏的一些显示功能的function可以去到B站查看

猜你喜欢

转载自blog.csdn.net/weixin_52300845/article/details/124844068