登录  | 立即注册

游客您好!登录后享受更多精彩

扫一扫,访问微社区

QQ登录

只需一步,快速开始

开启左侧

[寒假笔记] 使用定时器(T1)和中断实现数码管999999倒计时。

[复制链接]
发表于 2022-1-8 21:35:34 | 显示全部楼层 |阅读模式
学习笔记
学习科目: 单片机
学习安排:
开始时间: 2022-01-08
结束时间: 2022-01-08







                              使用定时器(T1)和中断实现数码管999999倒计时

     本次程序所需要定义4个全局变量,变量i用来每隔1ms刷新一个数码管,6ms刷新6个数码管。形成视觉暂留现象。变量b用来定时1s。变量a用来初始化数码管的值,初始值为999999.变量c用来消除无效位数字(就是当计时为99999最高位自动消去)

                         具体效果点击此处:
www.bilibili.com/video/BV1KY411h71t/

#include<reg52.h>
sbit ENLED = P1^1;
sbit ADDR0 = P2^0;
sbit ADDR1 = P2^1;
sbit ADDR2 = P2^2;
    unsigned char code  LedChar[] = {
    0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8,
        0x80, 0x90, 0x88, 0x83, 0xC6, 0xA1, 0x86, 0x8E
        };
    unsigned char buff[] = {
    0xFF,0xFF,0xFF,0xFF,0xFF,0xFF
        };
  unsigned char i = 0;
  unsigned int b = 0;
  unsigned long a = 999999;
  unsigned long c = 0;
void main()
{

  ENLED = 0;
  TMOD = 0x01;
  TH1 = 0xF5;                                                                                                                                                                 
  TL1 = 0x55;
  TR1 = 1;
  EA = 1;
  ET1 = 1;
  while(1)

  {

        if(b>=1000)
        {
                  b=0;
                  a--;
                          c++;
                buff[0] =LedChar[a%10];
                        buff[1] =LedChar[a/10%10];
                        buff[2] =LedChar[a/100%10];
                        buff[3] =LedChar[a/1000%10];
                        buff[4] =LedChar[a/10000%10];
                        buff[5] =LedChar[a/100000%10];
        }
       
          
                
                }
        }
void InterrupuTimer0() interrupt 1
{
  b++;
  TH1 = 0xF5;
  TL1 = 0x55;
  P0 = 0xFF;
  switch(i)
                {
                case 0:if(c<=900000){ADDR2 = 0; ADDR1 = 0; ADDR0 = 0; i++; P0 =buff[5];break;}
                case 1:if(c<=990000){ ADDR2 = 0; ADDR1 = 0; ADDR0 = 1; i++; P0 =buff[4];break;}
                case 2:if(c<=999000){ADDR2 = 0; ADDR1 = 1; ADDR0 = 0; i++; P0 =buff[3];break;}
                case 3:if(c<=999900){ ADDR2 = 0; ADDR1 = 1; ADDR0 = 1; i++; P0 =buff[2];break;}
                case 4:if(c<=999990){ ADDR2 = 1; ADDR1 = 0; ADDR0 = 0; i++; P0 =buff[1];break;}
                case 5:if(c<=999999){ ADDR2 = 1; ADDR1 = 0; ADDR0 = 1; i=0; P0 =buff[0];break;}
                         }
}












好懒~~不想说~~~
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表