这个实验是用四位共阴数码管做累加器实验,左边的按钮是减号键,连接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]);
}