这里需要用到定时器
上一篇已经讲了动态数码管的显示原理
这里利用数码管做一个简单的计数器
仿真图:
代码实现
//数码管动态显示关键在于延时和消影
//采用低延时可以利用视觉暂留效应做到多位数码管显示
//同时低延时会导致数码管显示窜位
//所以需要在显示完数字之后将其归零,避免重影、窜位
#include <REGX52.H>
#include <REGX52.H>
#define uint unsigned int
#define uchar unsigned char
//利用数组Arraynum[]存放0~9
uchar Arraynum[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
//延时函数
void delay(uint t)
{
uint x,y;
for(x=0;x<t;x++)
{
for(y=110;y>0;y--);
}
}
//封装Nixie
//该函数主要用于控制位显示
//函数参数一:第一到四个数码管(1-4)
//函数参数二:显示数字0-9
//利用Arraynum[]显示数字0~9
void Nixie(uchar Location,Number)
{
switch(Location)//利用switch循环标号选择数码管位数
{
//P3口负责控制位选
case 1:P3_3=1;P3_2=1;P3_1=1;P3_0=0;break;
case 2:P3_3=1;P3_2=1;P3_1=0;P3_0=1;break;
case 3:P3_3=1;P3_2=0;P3_1=1;P3_0=1;break;
case 4:P3_3=0;P3_2=1;P3_1=1;P3_0=1;break;
default:break;
}
//P2口控制段码,显示0-9
P2=Arraynum[Number];
delay(1);//延时
P2=0x00;//消影,将数码管清零,避免串位
}
void Add()
{
//Num是计数函数,负责(0-9999)
//first是第一位数码管,千位
//second是第二位数码管,百位
//third是第三位数码管,十位
//forth是第四位数码管,个位
uint first,second,third,forth;
first=result/1000;
second=(result/100)%10;
third=(result/10)%10;
forth=result%10;
Nixie(1,first);
Nixie(2,second);
Nixie(3,third);
Nixie(4,forth);
}
void main()
{
while(1)
{
while(1)
{
Add();
}
}