利用外部中断,按下按键s5启动,按下s4暂停模拟时钟
程序代码:
#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 counter=1; 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}; 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) { if(counter==0) { time(); display(); } else 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() { s1++; if(s1>=10) { s1=0; num[7]=s%10; num[6]=s/10; ++s; if(s==60) { s=0; ++m; num[4]=m%10; num[3]=m/10; if(m==60) { ++h; m=0; num[1]=h%10; num[0]=h/10; if(h==24) { h=0; } } } } } void interrupt_int0(void) interrupt 0 //P3.2 Pin { counter=0; } void interrupt1_int1(void) interrupt 2 //P3.3 Pin { counter=~counter; }