arduino的串口通信控制七段数码管

   因为比较简单,不做更多解析


直接上代码

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);
    }
  }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

失意哦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值