本帖最后由 xiaoqi976633690 于 2023-2-14 20:41 编辑
arduino 2个74hc595级联驱动8位数码管
/*
Arduino code for Individual control over each pin
Support for 40+ 74HC595 8 bit shift registers
http://bildr.org/2011/02/74hc595/
*/
#define DATA_PIN 8 // Pin connected to DS of 74HC595串行数据输入
#define LATCH_PIN 9 // Pin connected to STCP of 74HC595上升沿数据从寄存器输出到引脚
#define CLOCK_PIN 10 // Pin connected to SHCP of 74HC595上升沿串行数据移位存入寄存器
void disp(uint16_t V1,uint16_t V2);
void write_74hc595(uint8_t *data);
//数码管数量
#define NUM_SHIFT_REGS 8
//共阳数码管
unsigned char leddata[]={
0xC0, //"0"
0xF9, //"1"
0xA4, //"2"
0xB0, //"3"
0x99, //"4"
0x92, //"5"
0x82, //"6"
0xF8, //"7"
0x80, //"8"
0x90, //"9"
0x88, //"A"
0x83, //"B"
0xC6, //"C"
0xA1, //"D"
0x86, //"E"
0x8E, //"F"
0x89, //"H"
0xC7, //"L"
0xC8, //"n"
0xC1, //"u"
0x8C, //"P"
0xA3, //"o"
0xBF, //"-"
0xFF, //熄灭
0xFF //自定义
};
uint8_t x595[NUM_SHIFT_REGS]={0x00,0xff};
void setup() {
pinMode(DATA_PIN, OUTPUT);
pinMode(CLOCK_PIN, OUTPUT);
pinMode(LATCH_PIN, OUTPUT);
Serial.begin(9600);
pinMode(A0,INPUT);
pinMode(A1,INPUT);
}
bool toggle = false;
uint16_t x1=1990,x2=3520;
uint16_t time=0;
uint16_t ADC_vlue1=0,ADC_vlue2=0;
float volt1,volt2;
void loop() {
ADC_vlue1=analogRead(A0);
ADC_vlue2=analogRead(A1);
volt1=ADC_vlue1*0.48828125;
volt2=ADC_vlue2*0.48828125;
time++;
disp(volt1,volt2);
if(time==100)
{
time=0;
x1++;
x2++;
Serial.println(volt1);
}
}
void write_74hc595(uint8_t *data)
{
uint8_t m[NUM_SHIFT_REGS];
digitalWrite(LATCH_PIN, LOW);
for(int x=0;x<2;x++)
{
m[x]=data[x];
for(int i=0;i<8;i++)
{
digitalWrite(CLOCK_PIN, LOW);
digitalWrite(DATA_PIN,(m[x]&0x80)>1?HIGH:LOW);
digitalWrite(CLOCK_PIN, HIGH);
m[x]<<=1;
}
}
digitalWrite(LATCH_PIN, HIGH);//上升沿输出
}
void disp(uint16_t V1,uint16_t V2)
{
for(int8_t i=0;i<NUM_SHIFT_REGS;i++)
{
switch(i)
{
case 0:
if(V1>=1000) x595[0]=leddata[V1/1000%10];
else x595[0]=0xff;//小于1000的千位不显示
x595[1]=1<<i;write_74hc595(x595);break;
case 1: x595[0]=leddata[V1/100%10];x595[1]=1<<i;write_74hc595(x595);break;
case 2: x595[0]=leddata[V1/10%10];x595[1]=1<<i;write_74hc595(x595);break;
case 3: x595[0]=leddata[V1%10];x595[1]=1<<i;write_74hc595(x595);break;
case 4:
if(V2>=1000) x595[0]=leddata[V2/1000%10];
else x595[0]=0xff;//小于1000的千位不显示
x595[1]=1<<i;write_74hc595(x595);break;
case 5: x595[0]=leddata[V2/100%10];x595[1]=1<<i;write_74hc595(x595);break;
case 6: x595[0]=leddata[V2/10%10];x595[1]=1<<i;write_74hc595(x595);break;
case 7: x595[0]=leddata[V2%10];x595[1]=1<<i;write_74hc595(x595);break;
}
}
x595[0]=0x7F;x595[1]=1<<1;
write_74hc595(x595);
x595[0]=0x7F;x595[1]=1<<5;
write_74hc595(x595);
}
电路图json:
{
"version": 1,
"author": "Anderson Costa",
"editor": "wokwi",
"parts": [
{ "type": "wokwi-arduino-uno", "id": "uno", "top": 0, "left": 0, "attrs": {} },
{ "type": "wokwi-74hc595", "id": "sr1", "top": 255, "left": 200, "attrs": {} },
{ "type": "wokwi-74hc595", "id": "sr2", "top": 255, "left": 360, "attrs": {} },
{
"type": "wokwi-resistor",
"id": "r1",
"top": 330,
"left": 140,
"rotate": 90,
"attrs": { "value": "180" }
},
{
"type": "wokwi-resistor",
"id": "r2",
"top": 330,
"left": 160,
"rotate": 90,
"attrs": { "value": "180" }
},
{
"type": "wokwi-resistor",
"id": "r3",
"top": 330,
"left": 180,
"rotate": 90,
"attrs": { "value": "180" }
},
{
"type": "wokwi-resistor",
"id": "r4",
"top": 330,
"left": 200,
"rotate": 90,
"attrs": { "value": "180" }
},
{
"type": "wokwi-resistor",
"id": "r5",
"top": 330,
"left": 220,
"rotate": 90,
"attrs": { "value": "180" }
},
{
"type": "wokwi-resistor",
"id": "r6",
"top": 330,
"left": 240,
"rotate": 90,
"attrs": { "value": "180" }
},
{
"type": "wokwi-resistor",
"id": "r7",
"top": 330,
"left": 260,
"rotate": 90,
"attrs": { "value": "180" }
},
{
"type": "wokwi-resistor",
"id": "r8",
"top": 330,
"left": 280,
"rotate": 90,
"attrs": { "value": "180" }
},
{
"type": "wokwi-resistor",
"id": "r9",
"top": 330,
"left": 300,
"rotate": 90,
"attrs": { "value": "180" }
},
{
"type": "wokwi-resistor",
"id": "r10",
"top": 330,
"left": 320,
"rotate": 90,
"attrs": { "value": "180" }
},
{
"type": "wokwi-resistor",
"id": "r11",
"top": 330,
"left": 340,
"rotate": 90,
"attrs": { "value": "180" }
},
{
"type": "wokwi-resistor",
"id": "r12",
"top": 330,
"left": 360,
"rotate": 90,
"attrs": { "value": "180" }
},
{
"type": "wokwi-resistor",
"id": "r13",
"top": 330,
"left": 380,
"rotate": 90,
"attrs": { "value": "180" }
},
{
"type": "wokwi-resistor",
"id": "r14",
"top": 330,
"left": 400,
"rotate": 90,
"attrs": { "value": "180" }
},
{
"type": "wokwi-resistor",
"id": "r15",
"top": 330,
"left": 420,
"rotate": 90,
"attrs": { "value": "180" }
},
{
"type": "wokwi-resistor",
"id": "r16",
"top": 330,
"left": 440,
"rotate": 90,
"attrs": { "value": "180" }
},
{
"type": "wokwi-led",
"id": "led1",
"top": 380,
"left": 154,
"rotate": 180,
"attrs": { "color": "red" }
},
{
"type": "wokwi-led",
"id": "led2",
"top": 380,
"left": 174,
"rotate": 180,
"attrs": { "color": "red" }
},
{
"type": "wokwi-led",
"id": "led3",
"top": 380,
"left": 194,
"rotate": 180,
"attrs": { "color": "red" }
},
{
"type": "wokwi-led",
"id": "led4",
"top": 380,
"left": 214,
"rotate": 180,
"attrs": { "color": "red" }
},
{
"type": "wokwi-led",
"id": "led5",
"top": 380,
"left": 234,
"rotate": 180,
"attrs": { "color": "red" }
},
{
"type": "wokwi-led",
"id": "led6",
"top": 380,
"left": 254,
"rotate": 180,
"attrs": { "color": "red" }
},
{
"type": "wokwi-led",
"id": "led7",
"top": 380,
"left": 274,
"rotate": 180,
"attrs": { "color": "red" }
},
{
"type": "wokwi-led",
"id": "led8",
"top": 380,
"left": 294,
"rotate": 180,
"attrs": { "color": "red" }
},
{
"type": "wokwi-led",
"id": "led9",
"top": 380,
"left": 314,
"rotate": 180,
"attrs": { "color": "red" }
},
{
"type": "wokwi-led",
"id": "led10",
"top": 380,
"left": 334,
"rotate": 180,
"attrs": { "color": "red" }
},
{
"type": "wokwi-led",
"id": "led11",
"top": 380,
"left": 354,
"rotate": 180,
"attrs": { "color": "red" }
},
{
"type": "wokwi-led",
"id": "led12",
"top": 380,
"left": 374,
"rotate": 180,
"attrs": { "color": "red" }
},
{
"type": "wokwi-led",
"id": "led13",
"top": 380,
"left": 394,
"rotate": 180,
"attrs": { "color": "red" }
},
{
"type": "wokwi-led",
"id": "led14",
"top": 380,
"left": 414,
"rotate": 180,
"attrs": { "color": "red" }
},
{
"type": "wokwi-led",
"id": "led15",
"top": 380,
"left": 434,
"rotate": 180,
"attrs": { "color": "red" }
},
{
"type": "wokwi-led",
"id": "led16",
"top": 380,
"left": 454,
"rotate": 180,
"attrs": { "color": "red" }
},
{
"type": "wokwi-7segment",
"id": "sevseg3",
"top": 529.21,
"left": 225.9,
"attrs": { "digits": "4" }
},
{
"type": "wokwi-7segment",
"id": "sevseg1",
"top": 528.62,
"left": 436.56,
"attrs": { "digits": "4" }
},
{ "type": "wokwi-potentiometer", "id": "pot1", "top": 71.57, "left": 402.18, "attrs": {} },
{ "type": "wokwi-potentiometer", "id": "pot2", "top": 73.12, "left": 519.64, "attrs": {} }
],
"connections": [
[ "uno:GND.2", "sr1:OE", "black", [ "v50", "h102" ] ],
[ "uno:GND.2", "sr2:OE", "black", [ "v50", "h102" ] ],
[ "uno:GND.2", "sr1:GND", "black", [ "v50", "h150", "v50", "h-45" ] ],
[ "uno:GND.2", "sr2:GND", "black", [ "v50", "h300", "v50", "h-9" ] ],
[ "uno:GND.2", "led1:C", "black", [ "v50", "h150", "v137", "h-143" ] ],
[ "uno:GND.2", "led2:C", "black", [ "v50", "h150", "v137", "h-143" ] ],
[ "uno:GND.2", "led3:C", "black", [ "v50", "h150", "v137", "h-143" ] ],
[ "uno:GND.2", "led4:C", "black", [ "v50", "h150", "v137", "h-143" ] ],
[ "uno:GND.2", "led5:C", "black", [ "v50", "h150", "v137", "h-143" ] ],
[ "uno:GND.2", "led6:C", "black", [ "v50", "h150", "v137", "h-143" ] ],
[ "uno:GND.2", "led7:C", "black", [ "v50", "h150", "v137", "h-143" ] ],
[ "uno:GND.2", "led8:C", "black", [ "v50", "h150", "v137", "h-143" ] ],
[ "uno:GND.2", "led9:C", "black", [ "v50", "h150", "v137", "h-143" ] ],
[ "uno:GND.2", "led10:C", "black", [ "v50", "h150", "v137", "h-143" ] ],
[ "uno:GND.2", "led11:C", "black", [ "v50", "h150", "v137", "h-143" ] ],
[ "uno:GND.2", "led12:C", "black", [ "v50", "h150", "v137", "h-143" ] ],
[ "uno:GND.2", "led13:C", "black", [ "v50", "h150", "v137", "h-143" ] ],
[ "uno:GND.2", "led14:C", "black", [ "v50", "h150", "v137", "h-143" ] ],
[ "uno:GND.2", "led15:C", "black", [ "v50", "h150", "v137", "h-143" ] ],
[ "uno:GND.2", "led16:C", "black", [ "v50", "h150", "v137", "h-143" ] ],
[ "uno:5V", "sr1:VCC", "red", [ "v44", "h61" ] ],
[ "uno:5V", "sr1:MR", "red", [ "v44", "h61" ] ],
[ "uno:5V", "sr2:VCC", "red", [ "v44", "h61" ] ],
[ "uno:5V", "sr2:MR", "red", [ "v44", "h61" ] ],
[ "uno:8", "sr1:DS", "orange", [ "v-20", "h111", "v222", "h-42" ] ],
[ "uno:9", "sr1:STCP", "purple", [ "v-27", "h129", "v237", "h-34" ] ],
[ "uno:10", "sr1:SHCP", "brown", [ "v-34", "h147", "v252", "h-29" ] ],
[ "uno:9", "sr2:STCP", "purple", [ "v-27", "h129", "v237", "h-34" ] ],
[ "uno:10", "sr2:SHCP", "brown", [ "v-34", "h147", "v252", "h-29" ] ],
[ "sr1:Q7S", "sr2:DS", "orange", [ "h0", "v-9", "h112" ] ],
[ "sr1:Q0", "r1:1", "green", [ "v-7", "h-50" ] ],
[ "sr1:Q1", "r2:1", "green", [ "v8", "h-15" ] ],
[ "sr1:Q2", "r3:1", "green", [ "v13", "h-5" ] ],
[ "sr1:Q3", "r4:1", "green", [ "v24", "h6" ] ],
[ "sr1:Q4", "r5:1", "green", [ "v21", "h17" ] ],
[ "sr1:Q5", "r6:1", "green", [ "v17", "h29" ] ],
[ "sr1:Q6", "r7:1", "green", [ "v13", "h38" ] ],
[ "sr1:Q7", "r8:1", "green", [ "v9", "h48" ] ],
[ "sr2:Q0", "r9:1", "green", [ "v-4", "h-41" ] ],
[ "sr2:Q1", "r10:1", "green", [ "v8", "h-15" ] ],
[ "sr2:Q2", "r11:1", "green", [ "v13", "h-5" ] ],
[ "sr2:Q3", "r12:1", "green", [ "v24", "h6" ] ],
[ "sr2:Q4", "r13:1", "green", [ "v21", "h17" ] ],
[ "sr2:Q5", "r14:1", "green", [ "v17", "h29" ] ],
[ "sr2:Q6", "r15:1", "green", [ "v13", "h38" ] ],
[ "sr2:Q7", "r16:1", "green", [ "v9", "h48" ] ],
[ "led1:A", "r1:2", "green", [ "v0" ] ],
[ "led2:A", "r2:2", "green", [ "v0" ] ],
[ "led3:A", "r3:2", "green", [ "v0" ] ],
[ "led4:A", "r4:2", "green", [ "v0" ] ],
[ "led5:A", "r5:2", "green", [ "v0" ] ],
[ "led6:A", "r6:2", "green", [ "v0" ] ],
[ "led7:A", "r7:2", "green", [ "v0" ] ],
[ "led8:A", "r8:2", "green", [ "v0" ] ],
[ "led9:A", "r9:2", "green", [ "v0" ] ],
[ "led10:A", "r10:2", "green", [ "v0" ] ],
[ "led11:A", "r11:2", "green", [ "v0" ] ],
[ "led12:A", "r12:2", "green", [ "v0" ] ],
[ "led13:A", "r13:2", "green", [ "v0" ] ],
[ "led14:A", "r14:2", "green", [ "v0" ] ],
[ "led15:A", "r15:2", "green", [ "v0" ] ],
[ "led16:A", "r16:2", "green", [ "v0" ] ],
[ "sevseg3:DIG1", "r1:1", "green", [ "v-32.82", "h-179.17", "v-217.55" ] ],
[ "sevseg3:DIG2", "r2:1", "green", [ "v-38.39", "h-200.56", "v-176.05", "h37.06" ] ],
[ "sevseg3:DIG3", "r3:1", "green", [ "v-44.88", "h-203.68", "v-162.15", "h47.26" ] ],
[ "sevseg3:DIG4", "r4:1", "green", [ "v27", "h-205.87", "v-294.65", "h44.48" ] ],
[ "sevseg1:DIG1", "r5:1", "green", [ "v-53.55", "h-362.03", "v-138.99", "h38.92" ] ],
[ "sevseg1:DIG2", "r6:1", "green", [ "v-59.11", "h-385.28", "v-128.79", "h26.87" ] ],
[ "sevseg1:DIG3", "r7:1", "green", [ "v-62.82", "h-387.47", "v-119.53", "h27.8" ] ],
[ "sevseg1:DIG4", "r8:1", "green", [ "v40.56", "h-392.44", "v-285.39", "h44.48" ] ],
[ "sevseg3:A", "r9:1", "green", [ "v-106.96", "h205.95", "v-80.61", "h-53.74" ] ],
[ "sevseg3:A", "sevseg1:A", "green", [ "v-106.96", "h142.94" ] ],
[ "sevseg3:B", "r10:1", "green", [ "v-99.55", "h185.15", "v-93.58", "h-34.28" ] ],
[ "sevseg1:B", "sevseg3:B", "green", [ "v-98.68", "h-209.9", "v99.27" ] ],
[ "sevseg3:C", "sevseg1:C", "green", [ "v53.27", "h96.46" ] ],
[ "sevseg1:C", "r11:1", "green", [ "v53.26", "h122.12", "v-319.91", "h-142.52" ] ],
[ "sevseg3:D", "sevseg1:D", "green", [ "v64.1", "h61.54" ] ],
[ "sevseg1:D", "r12:1", "green", [ "v64.09", "h148.54", "v-336.14", "h-197.24" ] ],
[ "sevseg3:E", "sevseg1:E", "green", [ "v76.13", "h36.26" ] ],
[ "sevseg1:E", "r13:1", "green", [ "v75.51", "h167.16", "v-353.58", "h-173.78" ] ],
[ "sevseg3:F", "sevseg1:F", "green", [ "v-88.27", "h210.66" ] ],
[ "sevseg1:F", "r14:1", "green", [ "v-87.17", "h156.37", "v-128.48", "h-248.86" ] ],
[ "sevseg3:G", "sevseg1:G", "green", [ "v32.83", "h80.84" ] ],
[ "sevseg1:G", "r15:1", "green", [ "v31.62", "h144.39", "v-319.47", "h-236.08" ] ],
[ "sevseg3:DP", "sevseg1:DP", "green", [ "v89.25", "h100.12" ] ],
[ "sevseg1:DP", "r16:1", "green", [ "v89.84", "h168.66", "v-210.58" ] ],
[ "pot1:SIG", "uno:A0", "green", [ "v64.43", "h-232.5" ] ],
[ "pot1:GND", "sr2:GND", "black", [ "v100.96", "h38.4", "v49.86", "h-32.47" ] ],
[ "pot1:VCC", "sr2:MR", "red", [ "v95.74", "h-26.24" ] ],
[ "pot2:SIG", "uno:A1", "green", [ "v57.36", "h-335.08" ] ],
[ "pot1:GND", "pot2:GND", "black", [ "v30.78", "h63.24" ] ],
[ "pot1:VCC", "pot2:VCC", "red", [ "v41.26", "h86.82" ] ]
],
"dependencies": {}
}
实验现象:
在线模拟链接:https://wokwi.com/
|