-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmax6675.cpp
102 lines (83 loc) · 3.08 KB
/
max6675.cpp
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
//***************************************************************************
// by iPa 04.10.2010
//***************************************************************************
#include "max6675.h"
#define TRUE 1
#define FALSE 0
/* SPI Receive one Byte */
static uint8_t SPI_RxByte(SPI_HandleTypeDef *hspi)
{
uint8_t dummy, data;
dummy = 0xFF;
data = 0;
while ((HAL_SPI_GetState(hspi) != HAL_SPI_STATE_READY));
HAL_SPI_TransmitReceive(hspi, &dummy, &data, 1, SPI_TIMEOUT);
return data;
}
//**************************************************************************
// Constructor
// Input: ^SPI GPIO PIN
//**************************************************************************
MAX6675::MAX6675(SPI_HandleTypeDef *hspi, GPIO_TypeDef* CS_Port, uint16_t CS_Pin) {
_hspi = hspi;
_CS_Port = CS_Port;
_CS_Pin = CS_Pin;
_filterLvl = FILTER_LVL;
HAL_GPIO_WritePin(_CS_Port, _CS_Pin, GPIO_PIN_SET); // CS High
// HAL_Delay(250); // 4Hz max
// _cFiltered = readCelsius(FALSE); // Read once to init filtered value
}
//**************************************************************************
// Init first reading
// Give the time for the first reading
// Initialise cFiltered
//**************************************************************************
void MAX6675::initMeasure(void) {
HAL_Delay(250); // 4Hz max wait for the first reading
_cFiltered = readCelsius(FALSE);
}
//**************************************************************************
// Read Celsius temperature
// input bool true if request filtered value
// @returns float last Celcius measure (member)
//**************************************************************************
float MAX6675::readCelsius(bool filter) {
unsigned short data;
//SELECT();
HAL_GPIO_WritePin(_CS_Port, _CS_Pin, GPIO_PIN_RESET); // CS Low
data = SPI_RxByte(_hspi);
data <<= 8;
data |= SPI_RxByte(_hspi);
//DESELECT();
HAL_GPIO_WritePin(_CS_Port, _CS_Pin, GPIO_PIN_SET); // CS High
if (data & 4) _connected = FALSE;
else _connected = TRUE;
data >>= 3;
_celcius = data * 0.25;
_cFiltered = (_filterLvl*_cFiltered)+((1-_filterLvl)*_celcius);
if (filter) return (_cFiltered);
return(_celcius);
}
//**************************************************************************
// input bool true if request filtered value
// @returns float last Celcius measure (member)
/**************************************************************************/
float MAX6675::get_celcius(bool filter) const {
if (filter) return (_cFiltered);
return (_celcius);
}
//**************************************************************************
// input -
// @returns TRUE if connected
/**************************************************************************/
bool MAX6675::get_status(void) const {
return (_connected);
}
//**************************************************************************
// input level value between 1-99
// @returns -
/**************************************************************************/
void MAX6675::set_filterLevel(uint8_t level) {
if (level>=100)level = 99;
_filterLvl=level/100;
}