這個實驗是用四位共陰數碼管做累加器實驗,左邊的按鈕是減號鍵,連接9腳,右邊的按鈕是加號鍵,連接0腳。如果單按的話,按加鍵時,四位共陰數碼管上顯示的數字便加一;按減鍵時,四位共陰數碼管上顯示的數字便減一;若長按的話,按加鍵一秒鐘增加10,按減鍵一秒鐘減少10。
#include
#define SEG_A 2
#define SEG_B 3
#define SEG_C 4
#define SEG_D 5
#define SEG_E 6
#define SEG_F 7
#define SEG_G 8
#define COM1 10
#define COM2 11
#define COM3 12
#define COM4 13
unsigned long longPressed;
unsigned long changeTime;
#define KEY_U 0
#define KEY_D 9
int SUM =0;
int Flag_up = 1;
int Flag_down = 1;
int KeyPressd = 0;
int lighttime = 100;
unsigned char table[10][8] =
{
{0, 0, 1, 1, 1, 1, 1, 1}, //0
{0, 0, 0, 0, 0, 1, 1, 0}, //1
{0, 1, 0, 1, 1, 0, 1, 1}, //2
{0, 1, 0, 0, 1, 1, 1, 1}, //3
{0, 1, 1, 0, 0, 1, 1, 0}, //4
{0, 1, 1, 0, 1, 1, 0, 1}, //5
{0, 1, 1, 1, 1, 1, 0, 1}, //6
{0, 0, 0, 0, 0, 1, 1, 1}, //7
{0, 1, 1, 1, 1, 1, 1, 1}, //8
{0, 1, 1, 0, 1, 1, 1, 1} //9
};
void setup()
{
pinMode(SEG_A,OUTPUT); //設置為輸出引腳
pinMode(SEG_B,OUTPUT);
pinMode(SEG_C,OUTPUT);
pinMode(SEG_D,OUTPUT);
pinMode(SEG_E,OUTPUT);
pinMode(SEG_F,OUTPUT);
pinMode(SEG_G,OUTPUT);
pinMode(COM1,OUTPUT);
pinMode(COM2,OUTPUT);
pinMode(COM3,OUTPUT);
pinMode(COM4,OUTPUT);
pinMode(KEY_U,INPUT_PULLUP);
pinMode(KEY_D,INPUT_PULLUP); //按鍵IO設定為帶上拉輸入
}
void loop()
{
if( ScanKeyUp() == 1) //當有按鍵按下時
{
SUM++; //SUM加一
if(SUM>9999) SUM = 0;
}
if( ScanKeyDown() == 1)
{
SUM--;
if(SUM<0)
{
SUM = 9999;
}
}
if( ScanKeyUp() == 2 && (millis() - changeTime) > lighttime ) //每100毫秒增加1
{
SUM ++;
if(SUM>9999) SUM = 0;
changeTime = millis();
}
if( ScanKeyDown() == 2 && (millis() - changeTime) > lighttime ) //每100毫秒減少1
{
SUM --;
if(SUM<0) SUM = 9999;
changeTime = millis();
}
Display(1,SUM/1000); //顯示SUM千位值
delay(3);
Display(2,SUM00/100); //顯示SUM百位值
delay(3);
Display(3,SUM0/10); //顯示SUM十位值
delay(3);
Display(4,SUM); //顯示SUM個位值
delay(3);
}
unsigned char ScanKeyUp() //按鍵掃描程序,返回鍵值
{
if(Flag_up && digitalRead(KEY_U) == LOW) //當按鍵并未按下時按鍵按下
{
Flag_up = 0; //清空Flag_up
delay(20); //延時去抖動
if(digitalRead(KEY_U) == LOW) //按鍵按下
{
longPressed = millis();
return 1; //返回鍵值1
}
}
if(!Flag_up && digitalRead(KEY_U) == LOW && (millis() - longPressed) > 2000) return 2;
//&&意為AND,表示兩個條件必須同時成立。
if(digitalRead(KEY_U) == HIGH) Flag_up = 1; //按鍵松開時置位Flag_up
return 0;
}
unsigned char ScanKeyDown()
{
if(Flag_down && digitalRead(KEY_D) == LOW)
{
Flag_down = 0; //清空Flag_up
delay(20); //延時去抖動
if(digitalRead(KEY_D) == LOW) //按鍵按下
{
longPressed = millis();
return 1; //返回鍵值1
}
}
if(!Flag_down && digitalRead(KEY_D) == LOW && (millis() - longPressed) > 2000) return 2;
if(digitalRead(KEY_D) == HIGH) Flag_down = 1; //按鍵松開時置位Flag_up
return 0;
}
void Display(unsigned char com,unsigned char num)
{
digitalWrite(SEG_A,LOW);
digitalWrite(SEG_B,LOW);
digitalWrite(SEG_C,LOW);
digitalWrite(SEG_D,LOW);
digitalWrite(SEG_E,LOW);
digitalWrite(SEG_F,LOW);
digitalWrite(SEG_G,LOW);
switch(com) //選通位選
{
case 1:
// digitalWrite(COM1,LOW);
digitalWrite(COM2,HIGH);
digitalWrite(COM3,HIGH);
digitalWrite(COM4,HIGH);
if (SUM<1000) digitalWrite(COM1,HIGH);
else digitalWrite(COM1,LOW); //高位屏蔽
break;
case 2:
digitalWrite(COM1,HIGH);
// digitalWrite(COM2,LOW);
digitalWrite(COM3,HIGH);
digitalWrite(COM4,HIGH);
if (SUM<100) digitalWrite(COM2,HIGH);
else digitalWrite(COM2,LOW);
break;
case 3:
digitalWrite(COM1,HIGH);
digitalWrite(COM2,HIGH);
// digitalWrite(COM3,LOW);
digitalWrite(COM4,HIGH);
if (SUM<10) digitalWrite(COM3,HIGH);
else digitalWrite(COM3,LOW);
break;
case 4:
digitalWrite(COM1,HIGH);
digitalWrite(COM2,HIGH);
digitalWrite(COM3,HIGH);
// digitalWrite(COM4,LOW);
break;
default:break;
}
digitalWrite(SEG_A,table[num][7]); //a查詢碼值表
digitalWrite(SEG_B,table[num][6]);
digitalWrite(SEG_C,table[num][5]);
digitalWrite(SEG_D,table[num][4]);
digitalWrite(SEG_E,table[num][3]);
digitalWrite(SEG_F,table[num][2]);
digitalWrite(SEG_G,table[num][1]);
}