51单片机定时器累加计数(0000-9999)+4位数码管(循环显示)+Proteus仿真
51单片机定时器累加计数(0000-9999)+4位数码管(循环显示)+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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
| #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; uint ge=0,shi=0,bai=0,qian=0; uchar CNT=0; void delayms(uint xms) //延时程序 { uint i,j; for(i=xms;i>0;i--) for(j=110;j>0;j--); }
void display() { a=1; //共阳极数码管高电平有效 P0=table[ge]; //循环显示0-9 delayms(5); //刷新 P0=0XFF; a=0;
b=1; //共阳极数码管高电平有效 P0=table[shi]; //循环显示0-9 delayms(5); //刷新 P0=0XFF; b=0; c=1; //共阳极数码管高电平有效 P0=table[bai]; //百位,循环显示0-9 delayms(5); //刷新 P0=0XFF; c=0; d=1; //共阳极数码管高电平有效 P0=table[qian]; //千位,循环显示0-9 delayms(5); //刷新 P0=0XFF; d=0; }
void jishu() interrupt 1 { if(CNT==20) { //10\*50MS定时时间为0.5秒 CNT=0; Num++; if(Num<10) ge = Num; if(Num==10) { Num=0; shi ++; } if(shi==10) { shi = 0; bai ++; } if(bai==10) { bai =0; qian ++; } if(qian ==10){//超过1000归零 ge=0,shi=0,bai=0,qian=0; } } TH0=(65536-50000)/256; //重新赋值 TL0=(65536-50000)%256; CNT++; //50毫秒加1,加20次为一秒 }
void main() {
TMOD=0X01; /\*中断初始化\*/ TH0=(65536-50000)/256; TL0=(65536-50000)%256; EA=1; //中断总开关 ET0=1; //请求中断 TR0=1; //允许中断
while(1) { display();//调用函数 } }
|
源码
1 2 3
| 链接:https://pan.baidu.com/s/1R8yBeKi5htEJ97OfJ9RXjw 提取码:00zk
|
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!