51单片机:在动态显示数字8051基础上,按键按一下数码管数字+1
题目要求:
数码管一直在动态刷新,数码管的初值为8051,利用外部中断实现按键按一下数码管示数加一
代码如下:
(写的不好仅供参考)
//利用外部中断实现开关按一下,数码管数字在8051的基础上+1
#include<reg52.h>
unsigned char code DSY_CODE[] ={0xC0,0xF9,0xA4,
0xB0,0x99,0x92,
0x82,0xF8,0x80,0x90}; //共阳极数码管
typedef unsigned char uchar;
typedef unsigned int uint;
uint cnt = 8051,i = 0,a = 0x10;
void DelayMS(unsigned int x); //延时函数
uchar LED_Play[4]; //个十百千位显示
void main()
{
IT0 = 1;
IE = 0X81;//使用外部中断0
while(1)
{
LED_Play[0] = cnt/1000; //千位
LED_Play[1] = cnt/100%10; //百位
LED_Play[2] = cnt/10%10; //十位
LED_Play[3] = cnt%10; //个位
if(i > 3)
{
i = 0;
a = 0x10;
}
P2 = 0x00;
P0 = DSY_CODE[LED_Play[i++]];
P2 = a;
DelayMS(2);
a = a<<1;//左移位
}
}
void EX0_INT(void) interrupt 0//外部中断0,实现计数+1
{
cnt++;
}
void DelayMS(unsigned int x) //延时函数
{
unsigned int i;
while(x--)
{
for(i=0;i<120;i++);
}
}
仿真结果: