利用外部中断实现数码管的数字加减,其中0-1=65535,有不足之处请大家指正!
程序代码:
#include<reg51.h> void display(); void time(); void delay10ms(unsigned char i) //延迟函数(for循环需要时间) 当第二层for函数为p=100时(经过测量),i为多少,延迟时间就是i*10ms { //目的是在两次灯亮之间空出时间 int n,p; for(n=0;n<i;n++) for(p=0;p<200;p++); } unsigned int i; unsigned int duanma[11]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x98,0xbf}; unsigned int weima[8]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80}; unsigned int s1=0,s=0,m=0,h=0,number=0; unsigned int num[8]={0,0,10,0,0,10,0,0}; unsigned int counter; void main() { IT0=1; // IT1=1; //下降沿触发方式 EX0=1; EX1=1; EA=1; //以下两行为片选语句,不可以删除。否则无法操作LED P2 = 0xa0;P0 = 0x00;P2 = 0; //选中蜂鸣器,关闭蜂鸣器 P2 = 0xC0;P0 = 0xFF;P2 = 0; //位选全部选中 P2 = 0xE0; //选中数码管 打开Y7 while(1) { time(); display(); } } void display() { for(i=0;i<8;i++) { number=num[i]; P2=0xC0;P0=weima[i];P2=0; P2=0xE0;P0=duanma[number];P2=0; delay10ms(1); P0=0xFF; } } void time() { num[7]=counter%10; num[6]=(counter/10)%10; num[5]=(counter/100)%10; num[4]=(counter/1000)%10; num[3]=(counter/10000)%10; num[2]=(counter/100000)%10; num[1]=(counter/1000000)%10; num[0]=(counter/10000000)%10; } void interrupt_int0(void) interrupt 0 //P3.2 Pin { counter++; } void interrupt1_int1(void) interrupt 2 //P3.3 Pin { counter--; }