因为比较简单,不做更多解析
直接上代码
int A=9;//接口定义
int B=8;
int C=3;
int D=4;
int E=5;
int G=6;
int F=7;
int DP=2;
char read=-1;//输入变量初始化
void setup()
{
for(int i=2;i<=9;i++){
pinMode(i, OUTPUT);
}
Serial.begin(9600);
}
void loop()
{
for(int i=2;i<=9;i++){//数码管初始化
digitalWrite(i,LOW);
}
while(1){
if(Serial.available()>0){//读取串口
read=Serial.read();
delay(2);
}
Serial.println(read);
if(read=='1'){//分析串口数据
digitalWrite(B,HIGH);
digitalWrite(C,HIGH);
digitalWrite(A,LOW);
digitalWrite(D,LOW);
digitalWrite(E,LOW);
digitalWrite(F,LOW);
digitalWrite(G,LOW);
}
if(read=='0'){
digitalWrite(A,HIGH);
digitalWrite(B,HIGH);
digitalWrite(C,HIGH);
digitalWrite(D,HIGH);
digitalWrite(E,HIGH);
digitalWrite(F,HIGH);
digitalWrite(G,LOW);
}
if(read=='2'){
digitalWrite(E,HIGH);
digitalWrite(G,HIGH);
digitalWrite(D,HIGH);
digitalWrite(B,HIGH);
digitalWrite(A,HIGH);
digitalWrite(C,LOW);
digitalWrite(F,LOW);
}
if(read=='3'){
digitalWrite(A,HIGH);
digitalWrite(B,HIGH);
digitalWrite(C,HIGH);
digitalWrite(D,HIGH);
digitalWrite(E,LOW);
digitalWrite(F,LOW);
digitalWrite(G,HIGH);
}
if(read=='4'){
digitalWrite(F,HIGH);
digitalWrite(G,HIGH);
digitalWrite(B,HIGH);
digitalWrite(C,HIGH);
digitalWrite(A,LOW);
digitalWrite(D,LOW);
digitalWrite(E,LOW);
}
if(read=='5'){
digitalWrite(A,HIGH);
digitalWrite(B,LOW);
digitalWrite(E,LOW);
digitalWrite(F,HIGH);
digitalWrite(G,HIGH);
digitalWrite(C,HIGH);
digitalWrite(D,HIGH);
}
if(read=='6'){
digitalWrite(A,HIGH);
digitalWrite(B,LOW);
digitalWrite(G,LOW);
digitalWrite(F,HIGH);
digitalWrite(C,HIGH);
digitalWrite(G,HIGH);
digitalWrite(E,HIGH);
digitalWrite(D,HIGH);
}
if(read=='7'){
digitalWrite(A,HIGH);
digitalWrite(B,HIGH);
digitalWrite(C,HIGH);
digitalWrite(D,LOW);
digitalWrite(E,LOW);
digitalWrite(F,LOW);
digitalWrite(G,LOW);
}
if(read=='8'){
digitalWrite(A,HIGH);
digitalWrite(B,HIGH);
digitalWrite(C,HIGH);
digitalWrite(D,HIGH);
digitalWrite(E,HIGH);
digitalWrite(F,HIGH);
digitalWrite(G,HIGH);
}
if(read=='9'){
digitalWrite(A,HIGH);
digitalWrite(B,HIGH);
digitalWrite(C,HIGH);
digitalWrite(D,HIGH);
digitalWrite(G,HIGH);
digitalWrite(F,HIGH);
digitalWrite(E,LOW);
}
}
}