【51单片机】定时器(第六节)

8463e0775636498b88d52ce93501d3d4.png

9f15f994a5994a82b18cb52292bbed58.png

 d0ca67d97939478bb066fd5c152f6183.png

6780e5478f2f4a3cabf6952ec97c43ae.png

 7df9fee693b14b66b6097d71e0517ab1.png

 d323ee25e6bd41c89bba46b66b89294f.png

 20c05c5ebd934bab8ab6baf58cebbfac.png

d6bd022844dc45e694a9ed21d48dc23e.png

 

使用定时器实现LED的闪烁

#include <REGX52.H>

void Timer0Init(void)
{
    TMOD &= 0xF0;        //设置定时器模式
    TMOD |= 0x01;        //设置定时器模式
    TL0 = 0x18;        //设置定时初值                                           Timer.c
    TH0 = 0xFC;        //设置定时初值
    TF0 = 0;        //清除TF0标志
    TR0 = 1;        //定时器0开始计时
    ET0=1;
    EA=1;
    PT0=0;
}

定时器中断函数模板
void Timer0_Routine() interrupt 1
{
    static unsigned int T0Count;
    TL0 = 0x18;        //设置定时初值
    TH0 = 0xFC;        //设置定时初值
    T0Count++;
    if(T0Count>=1000)
    {
        T0Count=0;
        P0_0=~P0_0;
    }
}

 

使用定时器实现流水灯

#include <REGX52.H>
#include "Timer0.h"
#include "Key.h"
#include <INTRINS.H>

unsigned char KeyNum,LEDMode;

void main()
{
    P2=0xFE;
    Timer0Init();
    while(1)
    {
        KeyNum=Key();        //获取独立按键键码
        if(KeyNum)            //如果按键按下
        {
            if(KeyNum==1)    //如果K1按键按下
            {
                LEDMode++;    //模式切换
                if(LEDMode>=2)LEDMode=0;
            }
        }
    }
}

void Timer0_Routine() interrupt 1
{
    static unsigned int T0Count;
    TL0 = 0x18;        //设置定时初值
    TH0 = 0xFC;        //设置定时初值
    T0Count++;        //T0Count计次,对中断频率进行分频
    if(T0Count>=500)//分频500次,500ms
    {
        
        if(LEDMode=T0Count=0;=0)            //模式判断
            P2=_crol_(P2,1);    //LED输出
        if(LEDMode==1)
            P2=_cror_(P2,1);
    }
}
 

定时器时钟

#include <REGX52.H>
#include "Delay.h"
#include "LCD1602.h"
#include "Timer0.h"

unsigned char Sec=55,Min=59,Hour=23;

void main()
{
    LCD_Init();
    Timer0Init();
    
    LCD_ShowString(1,1,"Clock:");    //上电显示静态字符串
    LCD_ShowString(2,1,"  :  :");
    
    while(1)
    {
        LCD_ShowNum(2,1,Hour,2);    //显示时分秒
        LCD_ShowNum(2,4,Min,2);
        LCD_ShowNum(2,7,Sec,2);
    }
}

void Timer0_Routine() interrupt 1
{
    static unsigned int T0Count;
    TL0 = 0x18;        //设置定时初值
    TH0 = 0xFC;        //设置定时初值
    T0Count++;
    if(T0Count>=1000)    //定时器分频,1s
    {
        T0Count=0;
        Sec++;            //1秒到,Sec自增
        if(Sec>=60)
        {
            Sec=0;        //60秒到,Sec清0,Min自增
            Min++;
            if(Min>=60)
            {
                Min=0;    //60分钟到,Min清0,Hour自增
                Hour++;
                if(Hour>=24)
                {
                    Hour=0;    //24小时到,Hour清0
                }
            }
        }
    }
}

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先需要配置定时器的工作模式和计数值,然后在定时器中断服务程序中实现流水灯的效果。 以下是基本的汇编代码实现: ``` ; 定义常量 DELAY_TIME equ 20000 ; 延时时间,单位为微秒 ; 定义变量 org 0 ; 程序入口地址 ljmp main ; 跳转到主程序 org 0x23 ; 定时器0中断向量地址 timer0_isr: push acc ; 保存寄存器 push psw ; 保存程序状态字 ; 流水灯效果 mov P1, #0xFE ; 点亮第一盏灯 acall delay ; 延时 mov P1, #0xFD ; 点亮第二盏灯 acall delay ; 延时 mov P1, #0xFB ; 点亮第三盏灯 acall delay ; 延时 mov P1, #0xF7 ; 点亮第四盏灯 acall delay ; 延时 mov P1, #0xEF ; 点亮第五盏灯 acall delay ; 延时 mov P1, #0xDF ; 点亮第六盏灯 acall delay ; 延时 mov P1, #0xBF ; 点亮第七盏灯 acall delay ; 延时 mov P1, #0x7F ; 点亮第八盏灯 acall delay ; 延时 pop psw ; 恢复程序状态字 pop acc ; 恢复寄存器 reti ; 返回中断 ; 延时函数 delay: mov r2, #DELAY_TIME / 256 mov r1, #DELAY_TIME % 256 L1: djnz r1, L1 ; 延时 djnz r2, L1 ; 延时 ret main: ; 定时器0工作模式 mov TMOD, #0x01 ; Timer 0, mode 1 mov TH0, #0xFF ; 65535 - 20000 = 45535,计数值为45535 mov TL0, #0xDF ; 低8位为0xDF ; 使能中断 setb ET0 ; 定时器0中断使能 setb EA ; 总中断使能 ; 循环 loop: sjmp loop ; 无限循环 ``` 在这个代码中,我们使用了定时器0的工作模式1,计数值为45535,延时时间为20毫秒。在定时器0中断服务程序中,我们依次点亮8盏LED灯,并延时20毫秒,实现流水灯的效果。 需要注意的是,这个代码中使用的是8051单片机的内部晶振,如果使用外部晶振需要根据实际情况修改计数值。同时,延时函数的精度可能不够高,需要根据实际情况进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值