74HC595的Q0~Q7依次接上数码管的dp,a,b,c,d,e,f,g
数码管的GND和OE接地,Vcc接5V,共阳数码管公共脚接了个1K电阻。
以下代码实现从串口监视器中输入0至9的数字,让数码管显示出对应的数字。
unsigned char number[10] = { //共阳 B10000000, // 0 B11110010, // 1 B01001000, // 2 B01100000, // 3 B00110010, // 4 B00100100, // 5 B00000100, // 6 B11110000, // 7 B00000000, // 8 B00100000 // 9 }; const byte Pin_DS = 11; //data const byte Pin_ST_CP = 12; //latch const byte Pin_SH_CP = 13; //clock char Buff[1]; void setup() { // put your setup code here, to run once: Serial.begin(9600); pinMode(Pin_ST_CP, OUTPUT);//ST_CP pinMode(Pin_DS, OUTPUT);//DS pinMode(Pin_SH_CP, OUTPUT);//SH_CP digitalWrite(Pin_DS, LOW); } void loop() { // put your main code here, to run repeatedly: if (Serial.available()) { Serial.readBytes(Buff, sizeof(Buff)); int comInt = charToInt(); digitalWrite(Pin_ST_CP, LOW); //将ST_CP口上面加低电平让芯片准备好接收数据 shiftOut(Pin_DS, Pin_SH_CP, MSBFIRST, number[comInt]);//串行数据输出,高位在先 MSBFIRST LSBFIRST digitalWrite(Pin_ST_CP, HIGH); //将ST_CP这个针脚恢复到高电平 delay(1000); } } int charToInt() //charToInt()(定义) { int tmp = 0; for (int i = 0; i < 1; i++) { tmp = tmp * 10 + (Buff[i] - 48); } return tmp; }