Added ds18b20

This commit is contained in:
lewisxhe
2020-12-15 15:07:35 +08:00
parent 4be1acbfcd
commit 30a4089ff7
2 changed files with 132 additions and 0 deletions

View File

@@ -0,0 +1,12 @@
// => Hardware select
#define LILYGO_WATCH_2019_WITH_TOUCH // To use T-Watch2019 with touchscreen, please uncomment this line
// #define LILYGO_WATCH_2019_NO_TOUCH // To use T-Watch2019 Not touchscreen , please uncomment this line
// #define LILYGO_WATCH_BLOCK // To use T-Watch Block , please uncomment this line
//NOT SUPPORT ...
// //#define LILYGO_WATCH_2020_V1
//NOT SUPPORT ...
#include <LilyGoWatch.h>

View File

@@ -0,0 +1,120 @@
#include "config.h"
TTGOClass *ttgo;
TFT_eSPI *tft ;
// Simple ds18b20 class
class DS18B20
{
public:
DS18B20(int gpio)
{
pin = gpio;
}
float temp()
{
uint8_t arr[2] = {0};
if (reset()) {
wByte(0xCC);
wByte(0x44);
delay(750);
reset();
wByte(0xCC);
wByte(0xBE);
arr[0] = rByte();
arr[1] = rByte();
reset();
return (float)(arr[0] + (arr[1] * 256)) / 16;
}
return 0;
}
private:
int pin;
void write(uint8_t bit)
{
pinMode(pin, OUTPUT);
digitalWrite(pin, LOW);
delayMicroseconds(5);
if (bit)digitalWrite(pin, HIGH);
delayMicroseconds(80);
digitalWrite(pin, HIGH);
}
uint8_t read()
{
pinMode(pin, OUTPUT);
digitalWrite(pin, LOW);
delayMicroseconds(2);
digitalWrite(pin, HIGH);
delayMicroseconds(15);
pinMode(pin, INPUT);
return digitalRead(pin);
}
void wByte(uint8_t bytes)
{
for (int i = 0; i < 8; ++i) {
write((bytes >> i) & 1);
}
delayMicroseconds(100);
}
uint8_t rByte()
{
uint8_t r = 0;
for (int i = 0; i < 8; ++i) {
if (read()) r |= 1 << i;
delayMicroseconds(15);
}
return r;
}
bool reset()
{
pinMode(pin, OUTPUT);
digitalWrite(pin, LOW);
delayMicroseconds(500);
digitalWrite(pin, HIGH);
pinMode(pin, INPUT);
delayMicroseconds(500);
return digitalRead(pin);
}
};
#define DS18B20_PIN 25
DS18B20 temp18B20(DS18B20_PIN);
void setup(void)
{
Serial.begin(115200);
ttgo = TTGOClass::getWatch();
ttgo->begin();
tft = ttgo->tft;
ttgo->openBL();
tft->setTextColor(TFT_WHITE, TFT_BLACK);
tft->setTextFont(4);
}
void loop(void)
{
float temp = temp18B20.temp();
if (temp > 0) {
tft->drawCentreString(String(temp), 120, 120, 7);
Serial.printf("Temp:%.2f\n", temp);
}
delay(1000);
}