-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDS18x20.cpp
More file actions
74 lines (60 loc) · 1.41 KB
/
DS18x20.cpp
File metadata and controls
74 lines (60 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
* DS18x20.cpp
*
* Created on: 10.04.2020
* Author: Daniel
*/
#include "DS18x20.h"
namespace SD_Utils {
OneWire_ErrorState DS18x20::ReadTempAndWait() {
this->ConvertTemp();
if (this->IsError) return this->ErrorState;
this->WaitForNotBusy();
if (this->IsError) return this->ErrorState;
this->ReadTemp();
return this->ErrorState;
}
void DS18x20::ReadTemp(){
uint8_t scratch[9];
this->ReadScratchpad(scratch);
uint16_t tr = scratch[1];
tr = tr << 8;
tr |= scratch[0];
convert(this->TempAsString, &tr);
}
OneWire_ErrorState DS18x20::ConvertTemp(void) {
this->Init();
if (this->ErrorState != NoError)
return this->ErrorState;
if(this->RomID == 0x00) {
this->WriteByte(ONEWIRE_CMD_SKIPROM);
}
this->WriteByte(ONEWIRE_CMD_CONVERTT);
return NoError;
}
OneWire_ErrorState DS18x20::WaitForNotBusy() {
uint32_t ts = HAL_GetTick();
uint8_t busy = 1;
busy ^= this->ReadBit();
while(busy) {
busy ^= this->ReadBit();
if ((HAL_GetTick() - ts) >= this->ConvertTempTimeout){
this->SetErrorPin(true);
return TimeOut;
}
}
return NoError;
}
DS18x20::DS18x20(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin) {
this->DataPin = GPIO_Pin;
this->DataPort = GPIOx;
}
DS18x20::DS18x20(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, uint64_t romID) {
this->DataPin = GPIO_Pin;
this->DataPort = GPIOx;
this->RomID = romID;
}
DS18x20::~DS18x20() {
// TODO Auto-generated destructor stub
}
} /* namespace SD_Utils */