#define ONEWIRE_DQ 27 //OneWire 引脚
int OneWire_Reset(int); int OneWire_ReadByte(int, int); int OneWire_ReadBit(int, int); void OneWire_WriteByte(int, int); void OneWire_WriteBit(int, int);
int OneWire_DS18B20_ConvertTemperature(int); float OneWire_DS18B20_ReadTemperature(int); int OneWire_DS18B20_ReadPower(int);
void setup(){ Serial1.begin(9600); pinMode(ONEWIRE_DQ,OUTPUT_OPEN_DRAIN); }
void loop(){
float Celsius;//摄氏度 float Fahrenheit;//华氏度
if (OneWire_DS18B20_ConvertTemperature(ONEWIRE_DQ) == 1) { SerialUSB.println("Sensor Error");//报错 }
delay(1000); // Wait for temperature conversion to complete Celsius = OneWire_DS18B20_ReadTemperature(ONEWIRE_DQ); Fahrenheit = ((Celsius * 9) / 5) + 32; SerialUSB.print("Celsius:"); SerialUSB.println(Celsius); SerialUSB.print("Fahrenheit:"); SerialUSB.println(Fahrenheit); }
//////////////////驱动库
int OneWire_Reset(int pin_DQ) /********************************************************************************/ /* Issue a OneWire Reset Pulse */ /********************************************************************************/ { int result = 1;
pinMode(pin_DQ, OUTPUT_OPEN_DRAIN); // Set Data Direction to output digitalWrite(pin_DQ, LOW); // Pull DQ data line low (+0v). delayMicroseconds(480); // Hold low for a minimum of 480uSec
pinMode(pin_DQ, INPUT); // Set Data Direction to input, this releases the DQ data line delayMicroseconds(60); // 4.7k pullup resistor on data line will take the bus high // Wait for 60uSec, within 15 to 60uSec the DS1820 will // pull DQ data line low
result = digitalRead(pin_DQ); // Read Data line, 0=DS18B20 found, 1=DS18B20 not found delayMicroseconds(240); // Wait for 240usec to ensure 'presence' pulse is complete
return (result); // Return result }
void OneWire_WriteByte(int pin_DQ, int data) /********************************************************************************/ /* Write 1-wire data byte */ /********************************************************************************/ { int loop;
for (loop = 0; loop < 8; loop++) // Loop to write each bit in the byte, LS-bit first { OneWire_WriteBit(pin_DQ, data & 0x01); data >>= 1; // shift the data byte for the next bit } }
void OneWire_WriteBit(int pin_DQ, int bit) /********************************************************************************/ /* Send a 1-wire write bit */ /********************************************************************************/ { if (bit) // Write '1' bit { pinMode(pin_DQ, OUTPUT_OPEN_DRAIN); // drive DQ low to generate a time slot digitalWrite(pin_DQ, LOW); delayMicroseconds(5); // 5uSec.
pinMode(pin_DQ, INPUT); // release DQ. allow DQ to float high, generating a 'write 1' slot delayMicroseconds(60); // 60usec. Wait for DS18B20 to finish sampling the 1 bit } else // Write '0' bit { pinMode(pin_DQ, OUTPUT_OPEN_DRAIN); // drive DQ low to generate a time slot digitalWrite(pin_DQ, LOW); delayMicroseconds(60); // 60uSec. hold bus low, generating a 'write 0' slot
pinMode(pin_DQ, INPUT); // release DQ }
delayMicroseconds(10); // 10uSec recovery time }
int OneWire_ReadByte(int pin_DQ) /********************************************************************************/ /* Read 1-Wire data byte and return it */ /********************************************************************************/ { int loop, result=0;
for (loop = 0; loop < 8; loop++) { result >>= 1; // shift the result to get it ready for the next bit
if (OneWire_ReadBit(pin_DQ)) // if result is one, then set MS bit result |= 0x80; } return result; }
int OneWire_ReadBit(int pin_DQ) /********************************************************************************/ /* Read a bit from the 1-Wire bus and return it */ /********************************************************************************/ { int result;
pinMode(pin_DQ, OUTPUT_OPEN_DRAIN); // drive DQ low digitalWrite(pin_DQ, LOW); delayMicroseconds(5); // 5uSec delay
pinMode(pin_DQ, INPUT); // release DQ to generate a 'Read' time slot delayMicroseconds(5); // 5uSec. wait for read slot data to be valid
result = digitalRead(pin_DQ); // Sample the bus delayMicroseconds(60); // 60uSec. complete the time slot
return (result); }
int OneWire_TouchByte(int pin_DQ, int data) /********************************************************************************/ /* Write a 1-Wire data byte and return the sampled result. */ /********************************************************************************/ { int loop; int result=0;
for (loop = 0; loop < 8; loop++) { result >>= 1; // shift the result to get it ready for the next bit
if (data & 0x01) // If sending a '1' then read a bit else write a '0' { if (OneWire_ReadBit(pin_DQ)) result |= 0x80; } else OneWire_WriteBit(pin_DQ, 0);
data >>= 1; // shift the data byte for the next bit } return result; }
void OneWire_Block(int pin_DQ, unsigned char *data, int data_len) /********************************************************************************/ /* Write a block of 1-Wire data bytes and return the sampled result in the same */ /* buffer. */ /********************************************************************************/ { int loop;
for (loop = 0; loop < data_len; loop++) { data[loop] = OneWire_TouchByte(pin_DQ, data[loop]); } }
int OneWire_DS18B20_ConvertTemperature(int pin_DQ) /********************************************************************************/ /* Instruct device to convert temperature. */ /********************************************************************************/ { int error;
error = OneWire_Reset(pin_DQ); // Perform Master Reset of OneWire Bus OneWire_WriteByte(pin_DQ, 0xCC); // skip ROM OneWire_WriteByte(pin_DQ, 0x44); // convert temperature OneWire_Reset(pin_DQ); // Perform Master Reset of OneWire Bus return(error); }
float OneWire_DS18B20_ReadTemperature(int pin_DQ) /********************************************************************************/ /* Obtain a celsius temperature value from a DS18B20 device. */ /********************************************************************************/ { unsigned char get[10]; int x; long temp; float ftemp;
OneWire_WriteByte(pin_DQ, 0xCC); // skip ROM OneWire_WriteByte(pin_DQ, 0xBE); // read scratch pad for (x=0; x<9; x++) // read 9 bytes from scratchpad { get[x]=OneWire_ReadByte(pin_DQ); } temp = get[1]; // Get MSB of Scratchpad temp = temp << 8; // Shift the MSB left 8 bits temp = (temp | get[0]); // Get LSB of Scratchpad if (get[1] >= 0x80) // Check if MSB indicates negative temperature { temp = 0xffff0000 | temp; // Set first 2 bytes of long data type to reflect 2s compliment negative value } ftemp = temp;
return (ftemp/16); }
int OneWire_DS18B20_ReadPower(int pin_DQ) /********************************************************************************/ /* Determine Power Mode */ /********************************************************************************/ { int x;
OneWire_WriteByte(pin_DQ, 0xCC); // skip ROM OneWire_WriteByte(pin_DQ, 0xB4); // read scratch pad x = OneWire_ReadBit(pin_DQ);
return (x); }
|