Arduino串口通信和数码管问题

本人做的项目需要串口通信,根据读取不同的值在数码管上显示不同的数字

char buf;    //定义串口发送的内容
//设置阴极接口
int a = 1;
int b = 2;
int c = 3;
int d = 4;
int e = 5;
int f = 6;
int g = 7;
int p = 8;
//设置阳极接口
int d4 = 9;
int d3 = 10;
int d2 = 11;
int d1 = 12;
unsigned char num[17][8] =
{
  //a  b  c  d  e  f  g  h
  {0, 0, 0, 0, 0, 0, 1, 1},     //0
  {1, 0, 0, 1, 1, 1, 1, 1},     //1
  {0, 0, 1, 0, 0, 1, 0, 1},     //2
  {0, 0, 0, 0, 1, 1, 0, 1},     //3
  {1, 0, 0, 1, 1, 0, 0, 1},     //4
  {0, 1, 0, 0, 1, 0, 0, 1},     //5
  {0, 1, 0, 0, 0, 0, 0, 1},     //6
  {0, 0, 0, 1, 1, 1, 1, 1},     //7
  {0, 0, 0, 0, 0, 0, 0, 1},     //8
  {0, 0, 0, 0, 1, 0, 0, 1},     //9
  {1, 1, 1, 0, 1, 1, 1, 0},     //A
  {1, 1, 1, 1, 1, 1, 1, 0},     //B
  {1, 0, 0, 1, 1, 1, 0, 0},     //C
  {1, 1, 1, 1, 1, 1, 0, 0},     //D
  {1, 0, 0, 1, 1, 1, 1, 0},     //E
  {1, 0, 0, 0, 1, 1, 1, 0},     //F
  {0, 0, 0, 0, 0, 0, 0, 0},     //.
};
void setup()
{
Serial.begin(9600);   //设置比特率,此行若去掉则数码管正常显示
  pinMode(d1, OUTPUT);
  pinMode(d2, OUTPUT);
  pinMode(d3, OUTPUT);
  pinMode(d4, OUTPUT);
  pinMode(a, OUTPUT);
  pinMode(b, OUTPUT);
  pinMode(c, OUTPUT);
  pinMode(d, OUTPUT);
  pinMode(e, OUTPUT);
  pinMode(f, OUTPUT);
  pinMode(g, OUTPUT);
  pinMode(p, OUTPUT);
}
void loop()
{
  buf = Serial.read();
  if (buf == 'a')
  {
    for (int q = 0; q < 125; q++)
    {
      Display(1, 0);
      Display(2, 0);
      Display(3, 0);
      Display(4, 0);
    }
  }
}
void Display(unsigned char com, unsigned char n)    //显示函数,com可选数值范围1-4,num可选数值范围0-9
{
  digitalWrite(a, HIGH);     //去除余晖
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, HIGH);
  digitalWrite(e, HIGH);
  digitalWrite(f, HIGH);
  digitalWrite(g, HIGH);
  digitalWrite(p, HIGH);
  switch (com)          //选通位选
  {
    case 1:
      digitalWrite(d1, HIGH);  //选择位1
      digitalWrite(d2, LOW);
      digitalWrite(d3, LOW);
      digitalWrite(d4, LOW);
      break;
    case 2:
      digitalWrite(d1, LOW);
      digitalWrite(d2, HIGH);  //选择位2
      digitalWrite(d3, LOW);
      digitalWrite(d4, LOW);
      break;
    case 3:
      digitalWrite(d1, LOW);
      digitalWrite(d2, LOW);
      digitalWrite(d3, HIGH);  //选择位3
      digitalWrite(d4, LOW);
      break;
    case 4:
      digitalWrite(d1, LOW);
      digitalWrite(d2, LOW);
      digitalWrite(d3, LOW);
      digitalWrite(d4, HIGH);  //选择位4
      break;
    default: break;
  }
  digitalWrite(a, num[n][0]);     //a查询码值表
  digitalWrite(b, num[n][1]);
  digitalWrite(c, num[n][2]);
  digitalWrite(d, num[n][3]);
  digitalWrite(e, num[n][4]);
  digitalWrite(f, num[n][5]);
  digitalWrite(g, num[n][6]);
  digitalWrite(p, num[n][7]);
}

其中,加上Serial.begin(9600); 之后,数码管最上边一段不会显示
图片说明

去掉的话就可正常显示
图片说明

请问各位大佬,这是怎么回事?该怎么改?

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问