#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#define DQ 3
OneWire oneWire(DQ);;//初始单总线上的单总线设备
DallasTemperature DS18B20(&oneWire);
Adafruit_SSD1306 oled(128,64,&Wire);
//设备索引号
int num = 0;
//设备总数
int count;
//存储设备地址
DeviceAddress myDS18B20;
void setup(){
//put your setup code here, to run once:
oled.begin(SSD1306_SWITCHCAPVCC,0x3c);
oled.setTextColor(WHITE);
DS18B20.begin(); //启动温度传感器
count = DS18B20.getDeviceCount(); //获取温度传感器数量
}
void loop(){
// put your main code here, to run repeatedly:
oled.clearDisplay();
oled.setTextSize(2);
oled.setCursor(0,5);
DS18B20.requestTemperatures();//逐个显示
for(int i=0; i < count; i++){
if (DS18B20.getAddress(myDS18B20,i)){
oled.print(i);
oled.print(":");
//显示温度
oled.println(DS18B20.getTempCByIndex(i));
}
}
oled.display();
}