51单片机定时器累加计数(000-999)+4位数码管(循环显示)+Proteus仿真
51单片机定时器累加计数(000-999)+4位数码管(循环显示)+Proteus仿真
说明:为什么采用4位的数码管,因为Proteus里面没有3位的数码管器件可选,也可以采用2+1,或者1+1+1,的数码管方式来拼接。
- Proteus仿真

- Proteus器件:共阳数码管、

实例代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
| #include <reg52.h> #define uint unsigned int #define uchar unsigned char sbit a = P2^0; sbit b = P2^1; sbit c = P2^2; sbit d =P2^3; uchar code table[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90}; //共阳极0-916进制编码
uint num=0; uchar shi=0; void delayms(uint xms) //延时程序 { uint i,j; for(i=xms;i>0;i--) for(j=110;j>0;j--); }
void display(uint num) { a=1; //共阳极数码管高电平有效 P0=table[num%10]; //循环显示0-9 delayms(5); //刷新 P0=0XFF; a=0;
b=1; //共阳极数码管高电平有效 P0=table[(num/10)%10]; //循环显示0-9 delayms(5); //刷新 P0=0XFF; b=0; c=1; //共阳极数码管高电平有效 P0=table[num/100]; //循环显示0-9 delayms(5); //刷新 P0=0XFF; c=0; }
void jishu() interrupt 1 { if(shi==20) { //10\*50MS定时时间为0.5秒 shi=0; num++; if(num ==1000){//超过1000归零 num=0; } } TH0=(65536-50000)/256; //重新赋值 TL0=(65536-50000)%256; shi++; //50毫秒加1,加20次为一秒 }
void main() {
TMOD=0X01; /\*中断初始化\*/ TH0=(65536-50000)/256; TL0=(65536-50000)%256; EA=1; //中断总开关 ET0=1; //请求中断 TR0=1; //允许中断 d = 0; while(1) { display(num);//调用函数 } }
|
源码
1 2 3
| 链接:https://pan.baidu.com/s/1zeZkTqO3gvjhjBm-OEOnLw 提取码:n0uy
|
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!