|
|
控制1个 DS18B20 温度传感器,使用1602 4线连接进行显示输出。
代码如下:
/*
LiquidCrystal Library - Hello World
Demonstrates the use a 16x2 LCD display. The LiquidCrystal
library works with all LCD displays that are compatible with the
Hitachi HD44780 driver. There are many of them out there, and you
can usually tell them by the 16-pin interface.
This sketch prints "Hello World!" to the LCD
and shows the time.
The circuit:
* LCD RS pin to digital pin 12
* LCD Enable pin to digital pin 11
* LCD D4 pin to digital pin 5
* LCD D5 pin to digital pin 4
* LCD D6 pin to digital pin 3
* LCD D7 pin to digital pin 2
* LCD R/W pin to ground
* 10K resistor:
* ends to +5V and ground
* wiper to LCD VO pin (pin 3)
Library originally added 18 Apr 2008
by David A. Mellis
library modified 5 Jul 2009
by Limor Fried (http://www.ladyada.net)
example added 9 Jul 2009
by Tom Igoe
modified 22 Nov 2010
by Tom Igoe
This example code is in the public domain.
http://www.arduino.cc/en/Tutorial/LiquidCrystal
*/
// include the library code:
#include <LiquidCrystal.h>
#include <DallasTemperature.h> //11-26 bao add!
#include <OneWire.h> // 11-26 bao add!
#define ONE_WIRE_BUS 8 // 11-26 bao add!
OneWire oneWire(ONE_WIRE_BUS); // 11-26 bao add!
DallasTemperature sensors(&oneWire); // 11-26 bao add!
float temperature = 0; //bao add 11-26
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup() {
Serial.begin(9600); //bao add 11-26
sensors.begin(); //bao add 11-26
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("I am Shuangbao");
delay(2000);
}
void loop() {
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
lcd.setCursor(0, 1);
/* // print the number of seconds since reset:
lcd.print(millis()/1000); */
numberOfDevices = sensors.getDeviceCount();
sensors.requestTemperatures(); // Send the command to get temperatures
temperature=sensors.getTempCByIndex(0); // 11-26 bao add
Serial.println(temperature); // 11-26 bao add
lcd.clear(); //清屏
lcd.setCursor(0,0);
lcd.print("Local Temperature");
lcd.setCursor(0, 1) ; //设置光标位置为第二行第一个位置
lcd.print(" is ");
lcd.setCursor(5, 1) ;
lcd.print( sensors.getTempCByIndex(0)); //获取温度
// delay(1000);
lcd.print((char)223); //显示o符号
lcd.print("C"); //显示字母C
delay(1000);
}
控制2个 DS18B20 温度传感器,使用1602 4线连接进行显示输出。
使用
[attachimg]27996
#include <OneWire.h>
#include <DallasTemperature.h>
#include <LiquidCrystal.h> // 11-26 BAO ADD
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // 11-26 BAO ADD
// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 8
#define TEMPERATURE_PRECISION 12
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
int numberOfDevices; // Number of temperature devices found
DeviceAddress tempDeviceAddress; // We'll use this variable to store a found device address
void setup(void)
{
// start serial port
Serial.begin(9600);
Serial.println("Dallas Temperature IC Control Library Demo");
// Start up the library
sensors.begin();
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("I am Shuangbao");
delay(2000);
// Grab a count of devices on the wire
numberOfDevices = sensors.getDeviceCount();
// locate devices on the bus
Serial.print("Locating devices...");
Serial.print("Found ");
Serial.print(numberOfDevices, DEC);
Serial.println(" devices.");
// report parasite power requirements
Serial.print("Parasite power is: ");
if (sensors.isParasitePowerMode()) Serial.println("ON");
else Serial.println("OFF");
// Loop through each device, print out address
for(int i=0;i<numberOfDevices; i++)
{
// Search the wire for address
if(sensors.getAddress(tempDeviceAddress, i))
{
Serial.print("Found device ");
Serial.print(i, DEC);
Serial.print(" with address: ");
printAddress(tempDeviceAddress);
Serial.println();
Serial.print("Setting resolution to ");
Serial.println(TEMPERATURE_PRECISION,DEC);
// set the resolution to 9 bit (Each Dallas/Maxim device is capable of several different resolutions)
sensors.setResolution(tempDeviceAddress, TEMPERATURE_PRECISION);
Serial.print("Resolution actually set to: ");
Serial.print(sensors.getResolution(tempDeviceAddress), DEC);
Serial.println();
}else{
Serial.print("Found ghost device at ");
Serial.print(i, DEC);
Serial.print(" but could not detect address. Check power and cabling");
}
}
}
// function to print the temperature for a device
void printTemperature(DeviceAddress deviceAddress)
{
// method 1 - slower
//Serial.print("Temp C: ");
//Serial.print(sensors.getTempC(deviceAddress));
//Serial.print(" Temp F: ");
//Serial.print(sensors.getTempF(deviceAddress)); // Makes a second call to getTempC and then converts to Fahrenheit
// method 2 - faster
float tempC = sensors.getTempC(deviceAddress);
Serial.print("Temp C: ");
Serial.print(tempC);
Serial.print(" Temp F: ");
Serial.println(DallasTemperature::toFahrenheit(tempC)); // Converts tempC to Fahrenheit
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Temp C: ");
lcd.setCursor(7,0);
lcd.print(tempC);
delay(2000);
}
void loop(void)
{
// call sensors.requestTemperatures() to issue a global temperature
// request to all devices on the bus
Serial.print("Requesting temperatures...");
sensors.requestTemperatures(); // Send the command to get temperatures
Serial.println("DONE");
// Loop through each device, print out temperature data
for(int i=0;i<numberOfDevices; i++)
{
// Search the wire for address
if(sensors.getAddress(tempDeviceAddress, i))
{
// Output the device ID
Serial.print("Temperature for device: ");
Serial.println(i,DEC);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Device: ");
lcd.setCursor(7, 0) ; //设置光标位置为第二行第个位置
lcd.print(i+1,DEC);
delay(1000);
lcd.clear();
// It responds almost immediately. Let's print out the data
printTemperature(tempDeviceAddress); // Use a simple function to print out the data
}
//else ghost device! Check your power requirements and cabling
}
delay(3000);
Serial.println(":");
Serial.println(":");
Serial.println(":");
}
// function to print a device address
void printAddress(DeviceAddress deviceAddress)
{
for (uint8_t i = 0; i < 8; i++)
{
if (deviceAddress < 16) Serial.print("0");
Serial.print(deviceAddress, HEX);
}
}
图7:外部供电方式的多点测温电路图
外部电源供电方式是DS18B20最佳的工作方式,工作稳定可靠,抗干扰能力强,而且电路也比较简单,可以开发出稳定可靠的多点温度 监控系统。站长推荐大家在开发中使用外部电源供电方式,毕竟比寄生电源方式只多接一根VCC引线。在外接电源方式下, 可以充分发挥DS18B20宽电源电压范围的优点,即使电源电压VCC降到3V时,依然能够保证温度量精度。
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?注册
x
|