首页 > 其他分享 >数码管动态显示(定时器中断方式)+小数点+高位为0不显示

数码管动态显示(定时器中断方式)+小数点+高位为0不显示

时间:2023-02-01 21:58:46浏览次数:69  
标签:动态显示 定时器 temp void unsigned LedBuf 数码管 GPIO

1 仿真现象

2 程序设计

2.1 主程序

#include <REG52.H>
#include "DisplaySmg.h"
#include "Timer0.h"

unsigned int  adc_result = 1234;  

void disp_num(void)            //显示四位十进制数
{
    if(adc_result>=1000)        //千位
    {
        LedBuf[0]= adc_result/1000;
    }
    else
    {
        LedBuf[0]=23;        //不显示
    }
    
    if(adc_result>=100)        //百位
    {
        LedBuf[1]= adc_result/100%10;
    }
    else
    {
        LedBuf[1]=23;        //不显示
    }
    
    if(adc_result>=10)        //十位
    {
        LedBuf[2]= adc_result/10%10;
    }
    else
    {
        LedBuf[2]=23;        //不显示
    }
    
    LedBuf[3]= adc_result%10;    //个位
}

void main()
{
    Timer0_Init();        //定时/计数器T0初始化
    EA=1;                //中断总开关
    DotDig1=1;            //点亮第二个数码管的小数点
    while(1)
    {
        disp_num();        //显示数据
    }
}

void Timer0_ISR(void) interrupt 1 
{
    TR0=0;                //关闭定时器
    DisplaySmg();        //每过1ms,刷新一次数码管显示函数
    TL0 = 0x66;            //设置定时初始值,定时1ms
    TH0 = 0xFC;            //设置定时初始值,定时1ms
    TR0=1;                //打开定时器
}

2.2 数码管显示函数模块

#ifndef __DisplaySmg_H__
#define __DisplaySmg_H__

#include <REG52.H>

#define GPIO_SEG P0        //段选端
#define GPIO_SEL P2        //位选端

extern unsigned char LedBuf[];    //外部变量声明
extern unsigned char DotDig0,DotDig1,DotDig2,DotDig3;

void DisplaySmg(void);

#endif
#include "DisplaySmg.h"

unsigned char code LedData[]={    //共阴型数码管的段码表,字符,序号
                0x3F,  //"0",0
                0x06,  //"1",1
                0x5B,  //"2",2
                0x4F,  //"3",3
                0x66,  //"4",4
                0x6D,  //"5",5
                0x7D,  //"6",6
                0x07,  //"7",7
                0x7F,  //"8",8
                0x6F,  //"9",9
                0x77,  //"A",10
                0x7C,  //"B",11
                0x39,  //"C",12
                0x5E,  //"D",13
                0x79,  //"E",14
                0x71,  //"F",15
                0x76,  //"H",16
                0x38,  //"L",17
                0x37,  //"n",18
                0x3E,  //"u",19
                0x73,  //"P",20
                0x5C,  //"o",21
                0x40,  //"-",22
                0x00,  //熄灭 23
                         };
unsigned char DotDig0=0,DotDig1=0,DotDig2=0,DotDig3=0;    //小数点控制位
unsigned char code LedAddr[]={0xfe,0xfd,0xfb,0xf7};        //数码管位选
unsigned char LedBuf[]={22,22,22,22};    //显示缓存区

void DisplaySmg()                    //四位数码管,考虑小数点
{
    unsigned char     i;                 //等价于 "static unsigned char i = 0;"
    unsigned char     temp;
    switch(i)
    {
        case 0:
        {
            GPIO_SEG = 0x00;                //消影
            if(DotDig0==1)                    //小数点
            {
                temp = LedData[LedBuf[0]] | 0x80;  //点亮小数点
            }
            else
            {
                temp = LedData[LedBuf[0]];            
            }
            GPIO_SEG = temp;                //段码
            GPIO_SEL = LedAddr[0];            //位选
            i++;
            break;
        }
            
        case 1:
            GPIO_SEG = 0x00;    
            if(DotDig1==1)                    //小数点
            {
                temp = LedData[LedBuf[1]] | 0x80;
            }
            else
            {
                temp = LedData[LedBuf[1]];
            }
            GPIO_SEG = temp;
            GPIO_SEL = LedAddr[1];
            i++;
            break;
        case 2:
            GPIO_SEG = 0x00;
            if(DotDig2==1)                    //小数点
            {
                temp = LedData[LedBuf[2]] | 0x80;
            }
            else
            {
                temp = LedData[LedBuf[2]];
            }
            GPIO_SEG = temp;
            GPIO_SEL = LedAddr[2];
            i++;
            break;
        case 3:
            GPIO_SEG = 0x00;
            if(DotDig3==1)                    //小数点
            {
                temp = LedData[LedBuf[3]] | 0x80;
            }
            else
            {
                temp = LedData[LedBuf[3]];
            }
            GPIO_SEG = temp;
            GPIO_SEL = LedAddr[3];
            i=0;
            break;
        default:break;
    }
}

2.3 定时计数器模块

#ifndef __Timer0_H__
#define __Timer0_H__

#include <reg52.h>

void Timer0_Init(void);

#endif
#include "Timer0.h"

void Timer0_Init(void)        //1毫秒@11.0592MHz
{
    TMOD &= 0xF0;        //设置定时器模式
    TMOD |= 0x01;        //设置定时器模式
    TL0 = 0x66;            //设置定时初始值
    TH0 = 0xFC;            //设置定时初始值
    TF0 = 0;            //清除TF0标志
    TR0 = 1;            //定时器0开始计时
    
    ET0 = 1;            //定时器0中断开关
//    EA  = 1;             //中断总开关
}


//中断服务函数一定是一个没有返回值的函数
//中断服务函数一定是没有参数的函数
//中断服务函数函数名后跟着关键字interrupt
//interrupt n 0~4 5个中断源,8*n+0003H
// 0003H INT0, 00BH T0, 0013H INT1, 001BH T1, 0023H ES
//中断服务函数不能被主程序或者其他程序所调用
//n后面跟着using m(0~3)工作寄存器组

//void Timer0_ISR(void) interrupt 1 
//{
//    TL0 = 0x66;            //设置定时初始值
//    TH0 = 0xFC;            //设置定时初始值
//}

3 参考来源

(1)单片机应用——ADC0809查询方式实现一路模拟信号的A/D转换(数码管显示)_哔哩哔哩_bilibili

标签:动态显示,定时器,temp,void,unsigned,LedBuf,数码管,GPIO
From: https://www.cnblogs.com/zclv/p/17084229.html

相关文章