應網(wǎng)友要求,下面寫一個用C語言編寫的程序。
電路中,共使用了三個按鍵,兩位數(shù)碼顯示。使用12MHz或11.0592MHz的晶振皆可。
用Proteus仿真運行后截圖如下:
程序如下:
//===================================================
#include
#define uint unsigned int
#define uchar unsigned char
sbit ADD_1 = P3^2;
sbit SUB_1 = P3^3;
sbit RST_0 = P3^4;
uchar num;
uchar code DIS_SEG7[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
//---------------------------------------------------
void delay(uint ms)
{
uint i, j;
for(i = ms; i > 0; i--) for(j = 110; j > 0; j--);
}
//---------------------------------------------------
void display(uchar num)
{
P2 = 0; //消隱
P0 = DIS_SEG7[num / 10]; //輸出段碼
P2 = 2; //顯示位置
delay(3); //顯約3ms
P2 = 0;
P0 = DIS_SEG7[num % 10];
P2 = 1;
delay(3);
}
//---------------------------------------------------
void main()
{
num = 0;
while(1) { //主循環(huán)
display(num); //顯示
if (ADD_1 == 0) { //如果有鍵按下
display(num); //用顯示代替延時
if (ADD_1 == 0) { //如果鍵還在按下
num++; //按鍵功能處理
if (num >= 100) num = 0;
while(ADD_1 == 0) display(num); //等待按鍵釋放
}
}
if (SUB_1 == 0) {
display(num);
if (SUB_1 == 0) {
num--;
if (num == -1) num = 99;
while(SUB_1 == 0) display(num);
}
}
if (RST_0 == 0) {
display(num);
if (RST_0 == 0) num = 0;
while(RST_0 == 0) display(num);
}
} }
//===================================================