数码管动态显示(软件延时)+多参数分时显示+Protues仿真
1 实验现象(四个参数:窗口(通道)+分隔符+参数数据)
2 参考程序
2.1 主函数
#include "DisplaySmg.h" unsigned char wnd; //窗口或参数的个数,状态机设计 unsigned char param1=55; //参数1 unsigned char param2=66; //参数2 unsigned char param3=77; //参数3 unsigned char param4=88; //参数4 void disp_service(); void main() { unsigned int i; //0~65535 for(i=0;i<5000;i++) //数码管开机显示 { DisplaySmg(); //函数执行一次,for循环i控制显示时间 } while(1) { disp_service(); } } void disp_service() { unsigned int i; //如果为char,i始终到不了5000 switch(wnd) { case 0: { LedBuf[0]=0; LedBuf[1]=22; LedBuf[2]=param1/10; LedBuf[3]=param1%10; for(i=0;i<5000;i++) { DisplaySmg(); //调用数码管显示函数,该函数仅执行一次 } wnd++; break; } case 1: { LedBuf[0]=1; LedBuf[1]=22; LedBuf[2]=param2/10; LedBuf[3]=param2%10; for(i=0;i<5000;i++) { DisplaySmg(); //调用数码管显示函数,该函数仅执行一次 } wnd++; break; } case 2: { LedBuf[0]=2; LedBuf[1]=22; LedBuf[2]=param3/10; LedBuf[3]=param3%10; for(i=0;i<5000;i++) { DisplaySmg(); //调用数码管显示函数,该函数仅执行一次 } wnd++; break; } case 3: { LedBuf[0]=3; LedBuf[1]=22; LedBuf[2]=param4/10; LedBuf[3]=param4%10; for(i=0;i<5000;i++) { DisplaySmg(); //调用数码管显示函数,该函数仅执行一次 } wnd=0; break; } default:break; } }
2.2 数码管显示函数模块(采用switch case语言实现)
#ifndef __DisplaySmg_H__ #define __DisplaySmg_H__ #include <REG52.H> #include "DelayXms.h" #define GPIO_SEG P0 //段选端 #define GPIO_SEL P2 //位选端 #define SMG_NUM 4 //数码管的个数 extern unsigned char LedBuf[]; //外部变量声明 void DisplaySmg(); #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 code LedAddr[]={0xfe,0xfd,0xfb,0xf7}; //数码管位选 unsigned char LedBuf[]={22,22,22,22}; //显示缓存区 void DisplaySmg() { unsigned char i; //等价于 "static unsigned char i = 0;" //第一步 送段码 //第二步 送位选 //第三步 延时1ms,<10ms //第四步 消影(共阴形) switch(i) { case 0: GPIO_SEG = LedData[LedBuf[0]]; GPIO_SEL = LedAddr[0]; DelayXms(1); GPIO_SEG = 0x00; i++; break; case 1: GPIO_SEG = LedData[LedBuf[1]]; GPIO_SEL = LedAddr[1]; DelayXms(1); GPIO_SEG = 0x00; i++; break; case 2: GPIO_SEG = LedData[LedBuf[2]]; GPIO_SEL = LedAddr[2]; DelayXms(1); GPIO_SEG = 0x00; i++; break; case 3: GPIO_SEG = LedData[LedBuf[3]]; GPIO_SEL = LedAddr[3]; DelayXms(1); GPIO_SEG = 0x00; i=0; break; default:break; } }
2.3 延时函数模块
#ifndef __DelayXms_H__ #define __DelayXms_H__ #include <intrins.h> void DelayXms(unsigned char xms); #endif
#include "DelayXms.h" void DelayXms(unsigned char xms) //@11.0592MHz { unsigned char i, j; while(xms--) { _nop_(); i = 2; j = 199; do { while (--j); } while (--i); } }
3 参考来源
(1)单片机应用 数码管动态显示之switch case语句实现动态刷新及多参数的分时显示_哔哩哔哩_bilibili;