目的:通過按下按鍵作為外部的中斷輸入信號,通過數(shù)碼管顯示中斷次數(shù)。
問題:目前在PROTEUS仿真上能正常實現(xiàn)功能,但在實物上出現(xiàn)類似不能消影的問題,當按下按鍵后,數(shù)碼管全亮(會一直顯示8888),計數(shù)功能也出現(xiàn)問題,按下按鍵后數(shù)碼管4位同時改變(在顯示8888時,可以勉強觀察到數(shù)字有在變化)。
- #include <reg52.h>
- #define uchar unsigned char
- #define uint unsigned int
-
- //P0為SA-SH P1為C0-C3
- sbit K1=P3^2;
- uchar dis_code[11]={0xc0,0xf9,0xa4,0xb0, // 0, 1, 2, 3
- 0x99,0x92,0x82,0xf8,0x80,0x90, 0xff}; // 4, 5, 6, 7, 8, 9, off
- uchar buf[4];
- uint cnt=0;
-
- void delay(uchar ms)
- {
- while(ms--)
- ;
- }
- void initial()
- {
- IT0=1;
- EX0=1;
- EA=1;
- }
- int main()
- {
- initial();
- P0=0xff;
- P1=0;
- buf[0]=dis_code[0];
- buf[1]=dis_code[0];
- buf[2]=dis_code[0];
- buf[3]=dis_code[0];
-
- while(1)
- {
- P0=buf[3];//顯示千位
- P1=0x01;
- delay(5);
- P0=0xff;
-
- P0=buf[2];//顯示百位
- P1=0x02;
- delay(5);
- P0=0xff;
-
- P0=buf[1];//顯示十位
- P1=0x04;
- delay(5);
- P0=0xff;
-
- P0=buf[0];//顯示個位
- P1=0x08;
- delay(5);
- P0=0xff;
-
- }
-
- }
- void init0() interrupt 0
- {
- EX0=0;
- if(K1==0)
- {
- cnt++;
- if(cnt>9999)
- {
- cnt=0;
- }
- }
-
- buf[3]=dis_code[cnt/1000]; //千位
- buf[2]=dis_code[cnt%1000/100];//百位
- buf[1]=dis_code[cnt%100/10];//十位
- buf[0]=dis_code[cnt%10];//個位
- EX0=1;
-
- }
復制代碼
|