mirror of
https://github.com/Xinyuan-LilyGO/TTGO_TWatch_Library.git
synced 2026-03-15 04:26:56 +01:00
Added ds18b20
This commit is contained in:
12
examples/Sensor/ds18b20/config.h
Normal file
12
examples/Sensor/ds18b20/config.h
Normal 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>
|
||||
120
examples/Sensor/ds18b20/ds18b20.ino
Normal file
120
examples/Sensor/ds18b20/ds18b20.ino
Normal 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);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user