共阳极数码管和arduino连接方法,公共端接在5v电压上。2到8引脚连接数码管A--F上
![9f1cd0f6a3dfa4bf6a2b526043b26614.png](https://i-blog.csdnimg.cn/blog_migrate/f3963e41c09dc86a40f1446d81c29be3.jpeg)
1位7段数码管引脚
![db03ba6facf7d78633e31033a2e579c4.png](https://i-blog.csdnimg.cn/blog_migrate/4bc99b67f53bf2b0380825a210b3635e.jpeg)
#define type 1 //O代表共阴极数码管,1代表共阳极数码管const int displayPins[7] = {2,3,4,5,6,7,8}; //定义引脚const int dicimalPoint = 9;// 小数点// 数组的值显示0-9,共阳极数码管中,引脚高电平不亮,低电平亮byte displayLEDs[10][7] = { { 0,0,0,0,0,0,1 }, // = 0 显示0,数码管上G不亮 { 1,0,0,1,1,1,1 }, // = 1 显示1,只有B,C亮 { 0,0,1,0,0,1,0 }, // = 2 显示2,A,B,G,E,D亮 { 0,0,0,0,1,1,0 }, // = 3 { 1,0,0,1,1,0,0 }, // = 4 { 0,1,0,0,1,0,0 }, // = 5 { 0,1,0,0,0,0,0 }, // = 6 { 0,0,0,1,1,1,1 }, // = 7 { 0,0,0,0,0,0,0 }, // = 8 { 0,0,0,0,1,0,0 } // = 9 }; void writeDigit(int digit) { Serial.print(digit); Serial.print(" "); int digitValue; for (int i=0; i < 7; i++) { digitalWrite(displayPins[i], convertHighLow(displayLEDs[digit][i])); Serial.print(convertHighLow(displayLEDs[digit][i])); }}int convertHighLow(int v){ int convertedValue; if(type ==0) { convertedValue = 1 - v;// 把高电平转成低电平,把低电平转成高电平 }else{ convertedValue = v; } return convertedValue;}void setup() { for(int d=0; d<8; d++) { pinMode(displayPins[d], OUTPUT);//设置引脚的输出模式 } pinMode(dicimalPoint,OUTPUT); digitalWrite(dicimalPoint,HIGH); Serial.begin(9600);// 初始化串口通信}void loop() { for (int i=0; i<=9 ; i++) { writeDigit(i); Serial.println("====="); delay(1000); } digitalWrite(dicimalPoint,LOW); delay(3000); writeDigit(4); delay(3000);}