两位电子秒表
仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)
单片机源程序如下:
- #include<reg51.h>
- #define uchar unsigned char
- #define uint unsigned int
- sbit K1 = P3^7;
- uchar i,s=0,Key_Flag_Idx=0,shi=0,ge=0;
- bit Key_State;
- uchar DSY_CODE[]=
- {
- 0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f
- };
- void DelayMS(uint ms)
- {
- uchar t;
- while(ms--)
- {
- for(t=0;t<120;t++);
- }
- }
- void qidong()
- {
- for(i=1;i<=10;i++)
- {DelayMS(100);
- if(Key_State != K1)
- {
- DelayMS(10);
- Key_State = K1;
- if(Key_State == 0)
- {
- Key_Flag_Idx=(Key_Flag_Idx+1)%3;
- return;
- }
- }
- }
- ge=(ge+1)%10;
- if(ge==0)shi=shi+1;
- if(shi==6)shi=0;
-
- }
- void zhanting()
- {
- while(Key_Flag_Idx==2)
- {if(Key_State != K1)
- {
- DelayMS(10);
- Key_State = K1;
- if(Key_State == 0)
- {
- Key_Flag_Idx=(Key_Flag_Idx+1)%3;return;
- }
- }
- }
- }
- void main()
- {
- P0 = 0x3f;
- P2 = 0x3f;
- Key_State = 1; //P3.7引脚的初始状态为1
-
- while(1)
- {
- if(Key_State != K1)//若P3.7引脚值有变化,其中K1为P3.7引脚值
- {
- DelayMS(10);//消抖
- Key_State = K1; //将当前P3.7值赋给Key_State变量
- if(Key_State == 0) //若当前P3.7值为0,即若有按键按下
- {
- Key_Flag_Idx=(Key_Flag_Idx+1)%3; //按键按下次数为...
- }
- }
- switch(Key_Flag_Idx)
- {
- case 1:qidong();break;
- case 2:zhanting();break;
- case 0:shi=0;ge=0;
- }
-
- P0 = DSY_CODE[shi];
- P2 = DSY_CODE[ge];
- }
- }
复制代码
所有资料51hei提供下载:
秒表.zip
(57.24 KB, 下载次数: 23)
|