|
用51單片機(jī)實(shí)現(xiàn)籃球計(jì)分器,有加1/2分、減1/2分、清零功能,分?jǐn)?shù)范圍為00-99。實(shí)物已經(jīng)連接出來(lái)并實(shí)現(xiàn)功能,但是仿真不知道為何兩個(gè)一位數(shù)碼管一直顯示88,按鍵按下可以正常加減數(shù),但是松開(kāi)按鍵又是88
4.png (71.36 KB, 下載次數(shù): 0)
下載附件
2025-7-5 15:11 上傳
#include <REGX52.H>
// 共陰數(shù)碼管段碼表(0-9)
unsigned char code table[] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
unsigned int count = 0; // 用unsigned int避免負(fù)數(shù)問(wèn)題
// 延時(shí)消抖函數(shù)
void delay_ms(unsigned int ms) {
unsigned int i, j;
for(i=0; i<ms; i++)
for(j=0; j<112; j++); // 12MHz晶振,約1ms
}
// 動(dòng)態(tài)顯示函數(shù)
void display() {
unsigned char shi, ge;
shi = count / 10; // 十位
ge = count % 10; // 個(gè)位
// 十位顯示(P2口)
P2 = table[shi];
delay_ms(1);
// 個(gè)位顯示(P0口)
P0 = table[ge];
delay_ms(1);
}
void main() {
P0 = 0x00;
P2 = 0x00;
while(1) {
display();
// 加1(P1.0)
if(P1_0 == 0) {
delay_ms(10); // 消抖
if(P1_0 == 0) {
if(count < 99) count++;
while(P1_0 == 0) display(); // 等待釋放時(shí)刷新顯示
}
}
// 加2(P1.1)
if(P1_1 == 0) {
delay_ms(10);
if(P1_1 == 0) {
if(count <= 97) count += 2;
else if(count == 98) count = 99; // 兼容count=98時(shí)加2到99
while(P1_1 == 0) display();
}
}
// 減1(P1.2)
if(P1_2 == 0) {
delay_ms(10);
if(P1_2 == 0) {
if(count > 0) count--;
while(P1_2 == 0) display();
}
}
// 減2(P1.3)
if(P1_3 == 0) {
delay_ms(10);
if(P1_3 == 0) {
if(count >= 2) count -= 2;
else if(count == 1) count = 0; // 兼容count=1時(shí)減2到0
while(P1_3 == 0) display();
}
}
// 清零(P1.4)
if(P1_4 == 0) {
delay_ms(10);
if(P1_4 == 0) {
count = 0;
while(P1_4 == 0) display();
}
}
}
}
|
|