用18b20温度传感器和四位数码管做了一个温度计,可是数码管显示一直在闪,改变延时会变暗了继续闪就像接触不良一样。求教啊
- #include
- #include
- #define ONE_WIRE_BUS 12
- OneWire oneWire(ONE_WIRE_BUS);
- DallasTemperature sensors(&oneWire);
- int R=9;
- int G=10;
- int B=11;
- int a = 1;
- int b = 2;
- int c = 3;
- int d = 4;
- int e = 5;
- int f = 6;
- int g = 7;
- int p = 8;
- int d4 = A5;
- int d3 = A4;
- int d2 = A3;
- int d1 = A2;
- int del = 10000;
- int time=100;
- float w;
- int y;
- // unsigned char DIS_BUFFER[4]={1,2,3,4};
- // unsigned int Dis_time=0;
- // unsigned char Dis_bit=1;
- void setup()
- {
- //Serial.begin(9600);
- pinMode ( ONE_WIRE_BUS ,INPUT_PULLUP);
- //sensors.begin();
-
- pinMode(d1, OUTPUT);
- pinMode(d2, OUTPUT);
- pinMode(d3, OUTPUT);
- pinMode(d4, OUTPUT);
- pinMode(a, OUTPUT);
- pinMode(b, OUTPUT);
- pinMode(c, OUTPUT);
- pinMode(d, OUTPUT);
- pinMode(e, OUTPUT);
- pinMode(f, OUTPUT);
- pinMode(g, OUTPUT);
- pinMode(p, OUTPUT);
- pinMode(13, OUTPUT);
- }
- void loop()
- {
-
-
-
- sensors.requestTemperatures();
- w=sensors.getTemPCByIndex(0);
- y=w*100;
-
- clearLEDs();
- pickDigit(1);
- dispDec(1);
- pickNumber(y/1000);
- delayMicroseconds(del);
-
-
- clearLEDs();
- pickDigit(2);
- dispDec(2);
- pickNumber(y/100%10);
- delayMicroseconds(del);
-
- clearLEDs();
- pickDigit(3);
- pickNumber(y/10%10);
- delayMicroseconds(del);
-
- clearLEDs();
- pickDigit(4);
- pickNumber(y%10);
- delayMicroseconds(del);
- clearLEDs();
- }
-
-
-
-
- void pickDigit(int x) //定义pickDigit(x),其作用是开启dx端口
- {
- digitalWrite(d1, LOW);
- digitalWrite(d2, LOW);
- digitalWrite(d3, LOW);
- digitalWrite(d4, LOW);
-
-
- switch( x)
- {
- case 1:
- digitalWrite(d1, HIGH);
- break;
- case 2:
- digitalWrite(d2, HIGH);
- break;
- case 3:
- digitalWrite(d3, HIGH);
- break;
- default:
- digitalWrite(d4, HIGH);
- break;
- }
- }
-
- void pickNumber(int y)
- {
- switch(y)
- {
- default:
- zero();
- break;
- case 1:
- one();
- break;
- case 2:
- two();
- break;
- case 3:
- three();
- break;
- case 4:
- four();
- break;
- case 5:
- five();
- break;
- case 6:
- six();
- break;
- case 7:
- seven();
- break;
- case 8:
- eight();
- break;
- case 9:
- nine();
- break;
- }
- }
-
- void dispDec(int v)
- {
- digitalWrite(p, LOW);
- }
-
- void clearLEDs()
- {
- digitalWrite(a, HIGH);
- digitalWrite(b, HIGH);
- digitalWrite(c, HIGH);
- digitalWrite(d, HIGH);
- digitalWrite(e, HIGH);
- digitalWrite(f, HIGH);
- digitalWrite(g, HIGH);
- digitalWrite(p, HIGH);
- }
-
- void zero()
- {
- digitalWrite(a, LOW);
- digitalWrite(b, LOW);
- digitalWrite(c, LOW);
- digitalWrite(d, LOW);
- digitalWrite(e, LOW);
- digitalWrite(f, LOW);
- digitalWrite(g, HIGH);
- }
-
- void one()
- {
- digitalWrite(a, HIGH);
- digitalWrite(b, LOW);
- digitalWrite(c, LOW);
- digitalWrite(d, HIGH);
- digitalWrite(e, HIGH);
- digitalWrite(f, HIGH);
- digitalWrite(g, HIGH);
- }
-
- void two()
- {
- digitalWrite(a, LOW);
- digitalWrite(b, LOW);
- digitalWrite(c, HIGH);
- digitalWrite(d, LOW);
- digitalWrite(e, LOW);
- digitalWrite(f, HIGH);
- digitalWrite(g, LOW);
- }
-
- void three()
- {
- digitalWrite(a, LOW);
- digitalWrite(b, LOW);
- digitalWrite(c, LOW);
- digitalWrite(d, LOW);
- digitalWrite(e, HIGH);
- digitalWrite(f, HIGH);
- digitalWrite(g, LOW);
- }
-
- void four()
- {
- digitalWrite(a, HIGH);
- digitalWrite(b, LOW);
- digitalWrite(c, LOW);
- digitalWrite(d, HIGH);
- digitalWrite(e, HIGH);
- digitalWrite(f, LOW);
- digitalWrite(g, LOW);
- }
-
- void five()
- {
- digitalWrite(a, LOW);
- digitalWrite(b, HIGH);
- digitalWrite(c, LOW);
- digitalWrite(d, LOW);
- digitalWrite(e, HIGH);
- digitalWrite(f, LOW);
- digitalWrite(g, LOW);
- }
-
- void six()
- {
- digitalWrite(a, LOW);
- digitalWrite(b, HIGH);
- digitalWrite(c, LOW);
- digitalWrite(d, LOW);
- digitalWrite(e, LOW);
- digitalWrite(f, LOW);
- digitalWrite(g, LOW);
- }
-
- void seven()
- {
- digitalWrite(a, LOW);
- digitalWrite(b, LOW);
- digitalWrite(c, LOW);
- digitalWrite(d, HIGH);
- digitalWrite(e, HIGH);
- digitalWrite(f, HIGH);
- digitalWrite(g, HIGH);
- }
-
- void eight()
- {
- digitalWrite(a, LOW);
- digitalWrite(b, LOW);
- digitalWrite(c, LOW);
- digitalWrite(d, LOW);
- digitalWrite(e, LOW);
- digitalWrite(f, LOW);
- digitalWrite(g, LOW);
- }
-
- void nine()
- {
- digitalWrite(a, LOW);
- digitalWrite(b, LOW);
- digitalWrite(c, LOW);
- digitalWrite(d, LOW);
- digitalWrite(e, HIGH);
- digitalWrite(f, LOW);
- digitalWrite(g, LOW);
- }
复制代码
0
|
3个回答
|
|
|