```arduino
const int dataPin = 9;    // 数据引脚 (DS)
const int clockPin = 10;   // 时钟引脚 (SH_CP)
const int latchPin = 11;    // 锁存引脚 (ST_CP)
const byte disNum[11] = {
  // 数码管各段状态定义(共阴极:1=亮,0=灭)
  //  abcdefgdp  
  0b11111100, //0
  0b01100000, //1
  0b11011010, //2
  0b11110010, //3
  0b01100110, //4
  0b10110110, //5
  0b10111110, //6
  0b11100000, //7
  0b11111110, //8
  0b11110110, //9
  0b00000001  //.
};

void setup() {
  // put your setup code here, to run once:
  pinMode(dataPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(latchPin, OUTPUT);
  byte duan = 0b10110110;//5  //0表示选中
  byte wei = 0b00000010;//位  1表示选中
  digitalWrite(clockPin, LOW);
  digitalWrite(latchPin, LOW);
}
void disPlayNumber(int n) { //1234
  byte wei = 0b00000001;
  int arr[5]={};
  int index = 5;
  while (n != 0) {
    index -= 1;
    arr[index] = n%10;
    n/=10;
  }
  for(int i=1;i<=4;i++){
    shiftOut(dataPin, clockPin, MSBFIRST, wei);
    shiftOut(dataPin, clockPin, LSBFIRST, ~disNum[arr[i]]);
    digitalWrite(latchPin, LOW);         // 开始传输
    digitalWrite(latchPin, HIGH);        // 锁存数据
    wei = (wei<<1);
  }
}
void loop() {
  // put your main code here, to run repeatedly:
  disPlayNumber(8);
}

2 条评论

  • @ 2025-7-21 15:52:14

    • @ 2025-7-21 15:51:39
      const int dataPin = 9;    // 数据引脚 (DS)
      const int clockPin = 10;   // 时钟引脚 (SH_CP)
      const int latchPin = 11;    // 锁存引脚 (ST_CP)
      const byte disNum[11] = {
        // 数码管各段状态定义(共阴极:1=亮,0=灭)
        //  abcdefgdp  
        0b11111100, //0
        0b01100000, //1
        0b11011010, //2
        0b11110010, //3
        0b01100110, //4
        0b10110110, //5
        0b10111110, //6
        0b11100000, //7
        0b11111110, //8
        0b11110110, //9
        0b00000001  //.
      };
      
      ![](/file/2/LrLJDo8gHn3RfFFtOpa3B.png)
      
      
      void setup() {
        // put your setup code here, to run once:
        pinMode(dataPin, OUTPUT);
        pinMode(clockPin, OUTPUT);
        pinMode(latchPin, OUTPUT);
        byte duan = 0b10110110;//5  //0表示选中
        byte wei = 0b00000010;//位  1表示选中
        digitalWrite(clockPin, LOW);
        digitalWrite(latchPin, LOW);
      }
      void disPlayNumber(int digit,int number){
        byte v = 0b0;//
        //~bitClear(v,digit-1)
        shiftOut(dataPin, clockPin, MSBFIRST, bitSet(v,digit-1));
        shiftOut(dataPin, clockPin, LSBFIRST, ~disNum[number]);
        digitalWrite(latchPin, LOW);         // 开始传输
        digitalWrite(latchPin, HIGH);        // 锁存数据
      
      }
      void disPlayNumbers(int n){//1234
        int arr[5]={};
        arr[1] = n%10;//获取个位
        arr[2] = n/10%10;//获取十位
        arr[3] = n/100%10;
        arr[4] = n/1000%10;
        for(int i=1;i<=4;i++){
          disPlayNumber(4-i+1,arr[i]);
        }
      }
      
      void loop() {
        // put your main code here, to run repeatedly:
        //disPlayNumber(4,8);
        disPlayNumbers(721);
      }
      
      

      • 1