两个数码管,K1,K2两个按键,完成K1启动计数,K2暂停计数,每一秒钟数码管增加1,60秒钟后,蜂鸣器响一声,数码管回归0,重新计数。
仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)
单片机源程序如下:
- #include <reg51.h>
- sbit P2_7=P2^7;
- sbit P3_2=P3^2;
- sbit P3_3=P3^3;
- int count=0;
- int sign=0;
- int count1=0;
- int count1_h=0;
- int count1_l=0;
- char count2[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
- void delay(long time)
- {
- int i=0;
- while(time--)
- for(i=0;i<125;i++);
- }
- void delay1(long time)
- {
- int i=0;
- while(time--)
- for(i=0;i<125;i++);
- }
- void buz_on()
- {
- P2_7=1;
- delay(100);
- P2_7=0;
- }
- void clear()
- {
- count=0;
- buz_on();
- }
- void k1on() interrupt 0
- {
- if(P3_2!=1)
- {
- delay1(50);
- if(P3_2!=1)
- {
- TMOD=0x01;
- TH0=(65536-50000)/256;
- TL0=(65536-50000)%256;
- ET0=1;
- TR0=1;
- }
- }
- sign=1;
- }
- void k2on() interrupt 2
- {
- TR0=0;
- sign=0;
- }
- void timer0() interrupt 1
- {
- TH0=(65536-50000)/256;
- TL0=(65536-50000)%256;
- count++;
- }
- void main()
- {
- P2_7=0;
- P3_2=1;
- P3_3=1;
- P1=0x3f;
- P2=0x3f;
- EA=1;
- EX0=1;
- EX1=1;
- TCON=0x05;
- while(1)
- {
-
- count1=count/20;
- count1_h=count1/10;
- count1_l=count1%10;
- P2=count2[count1_h];
- P1=count2[count1_l];
- if(count1>=60)
- {
- clear();
- }
- }
- }
复制代码
所有资料51hei提供下载:
实验四.zip
(54.68 KB, 下载次数: 22)
|