|
本帖最后由 a2823411 于 2013-12-9 13:56 编辑
以下是我參考別人的可是為什麼卡在
pinMode(myClockPin, OUTPUT);
pinMode(myDataPin, OUTPUT);
刪掉的話又可以run
所以要怎麼辦 #include <OneWire.h>#include <DallasTemperature.h>
// pin setups
int latchPin = 8;
int clockPin = 12;
int dataPin = 11;
int tempPin = 7;
// librraries for connecting to sensor
OneWire oneWire(tempPin);
DallasTemperature tempSens(&oneWire);
// characters for displaying on 7-seg display 0-9
byte numberSet[10] = {
B01111011, B01000001, B00110111, B01100111, // 0,1,2,3
B01001101, B01101110, B01111110, B01000011, // 4,5,6,7
B01111111, B01101111 // 8,9};
void setup() {
// init serial
Serial.begin(9600);
// init temp sensor
tempSens.begin();
// set pin modes for shift registors
pinMode(latchPin, OUTPUT);
pinMode(myClockPin, OUTPUT);
pinMode(myDataPin, OUTPUT);
}
void loop() {
tempSens.requestTemperatures();
float t = tempSens.getTempCByIndex(0);
Serial.println(t);
// cast to float (only have 2 digits to use on display)
int rT = (int)t;
// get units of temp
int units = rT % 10;
// get tens value of temp
rT = rT/10;
int tens = rT % 10;
displayNumb(units, tens);
delay(100);
}
void displayNumb(int a, int b) {
// get the code for the numbers
byte bitsA = numberSet[a];
byte bitsB = numberSet;
// set ready to shift out
digitalWrite(latchPin, LOW);
// shift units
shiftOut(dataPin, clockPin, bitsA);
// shift tens
shiftOut(dataPin, clockPin, bitsB);
// shift out data
digitalWrite(latchPin, HIGH);
}
// shift the data out to the shift registors
void shiftOut(int myDataPin, int myClockPin, byte myDataOut) {
int i=0;
int pinState;
digitalWrite(myDataPin, 0);
digitalWrite(myClockPin, 0);
// iterate over each bit in the myDataOut byte
for (i=7; i>=0; i--) {
digitalWrite(myClockPin, 0);
if ( myDataOut & (1<<i) ) {
pinState= 1;
}
else {
pinState= 0;
}
digitalWrite(myDataPin, pinState);
digitalWrite(myClockPin, 1);
digitalWrite(myDataPin, 0);
}
digitalWrite(myClockPin, 0);
}
|
|