代码
int SHCP = PD2;
int STCP = PD3;
int DS = PD4;
int sw1 = 8;
int sw2 = 9;
int sw3 = 10;
int sw4 = 11;
// 共阳极数码管数字段码
int table[] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x9c,0xc6};
void setup()
{
pinMode(STCP,OUTPUT);
pinMode(SHCP, OUTPUT);
pinMode(DS, OUTPUT);
pinMode(sw1, OUTPUT);
pinMode(sw2, OUTPUT);
pinMode(sw3, OUTPUT);
pinMode(sw4, OUTPUT);
Serial.begin(9600);
}
void loop()
{
int val = analogRead(A2);
int dat = val * (5 / 10.24);
Serial.print(dat);
Serial.println("C");
digitalWrite(STCP,LOW);
shiftOut(DS,SHCP,LSBFIRST,table[dat / 10]);
digitalWrite(STCP, HIGH);
digitalWrite(sw1, HIGH);
delay(5);
digitalWrite(sw1, LOW);
digitalWrite(STCP, LOW);
shiftOut(DS, SHCP, LSBFIRST, table[dat % 10]);
digitalWrite(STCP, HIGH);
digitalWrite(sw2, HIGH);
delay(5);
digitalWrite(sw2, LOW);
digitalWrite(STCP, LOW);
shiftOut(DS, SHCP, LSBFIRST, table[10]);
digitalWrite(STCP, HIGH);
digitalWrite(sw3, HIGH);
delay(2);
digitalWrite(sw3, LOW);
digitalWrite(STCP, LOW);
shiftOut(DS, SHCP, LSBFIRST, table[11]);
digitalWrite(STCP, HIGH);
digitalWrite(sw4, HIGH);
delay(2);
digitalWrite(sw4, LOW);
}
重点说明
int table[] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x9c,0xc6};
分别对应0、1、2、3、4、5、6、7、8、9、0段码表
0x9c;对应的二进制为1001 1100在数码管上显示上方显示圆圈
0xc6;对应二进制1100 0110在数码管上显示C
演示
演示.jpg
网友评论