LibDriver MAX6675
Loading...
Searching...
No Matches
driver_max6675.c
Go to the documentation of this file.
1
36
37#include "driver_max6675.h"
38
42#define CHIP_NAME "Maxim Integrated MAX6675"
43#define MANUFACTURER_NAME "Maxim Integrated"
44#define SUPPLY_VOLTAGE_MIN 3.0f
45#define SUPPLY_VOLTAGE_MAX 5.5f
46#define MAX_CURRENT 1.5f
47#define TEMPERATURE_MIN -20.0f
48#define TEMPERATURE_MAX 85.0f
49#define DRIVER_VERSION 1000
50
60static uint8_t a_max6675_spi_read(max6675_handle_t *handle, uint16_t *data)
61{
62 uint8_t buf[2];
63
64 if (handle->spi_read_cmd(buf, 2) != 0) /* spi read */
65 {
66 return 1; /* return error */
67 }
68 else
69 {
70 *data = (((uint16_t)buf[0]) << 8) | buf[1]; /* get the data */
71
72 return 0; /* success return 0 */
73 }
74}
75
87{
88 uint16_t data;
89
90 if (handle == NULL) /* check handle */
91 {
92 return 2; /* return error */
93 }
94 if (handle->debug_print == NULL) /* check debug_print */
95 {
96 return 3; /* return error */
97 }
98 if (handle->spi_init == NULL) /* check spi_init */
99 {
100 handle->debug_print("max6675: spi_init is null.\n"); /* spi_init is null */
101
102 return 3; /* return error */
103 }
104 if (handle->spi_deinit == NULL) /* check spi_deinit */
105 {
106 handle->debug_print("max6675: spi_deinit is null.\n"); /* spi_deinit is null */
107
108 return 3; /* return error */
109 }
110 if (handle->spi_read_cmd == NULL) /* check spi_read_cmd */
111 {
112 handle->debug_print("max6675: spi_read_cmd is null.\n"); /* spi_read_cmd is null */
113
114 return 3; /* return error */
115 }
116 if (handle->delay_ms == NULL) /* check delay_ms */
117 {
118 handle->debug_print("max6675: delay_ms is null.\n"); /* delay_ms is null */
119
120 return 3; /* return error */
121 }
122
123 if (handle->spi_init() != 0) /* spi init */
124 {
125 handle->debug_print("max6675: spi init failed.\n"); /* spi init failed */
126
127 return 1; /* return error */
128 }
129 if (a_max6675_spi_read(handle, &data) != 0) /* read data */
130 {
131 handle->debug_print("max6675: read data failed.\n"); /* read data failed */
132
133 return 1; /* return error */
134 }
135
136 handle->inited = 1; /* flag finish initialization */
137
138 return 0; /* success return 0 */
139}
140
152{
153 uint8_t res;
154
155 if (handle == NULL) /* check handle */
156 {
157 return 2; /* return error */
158 }
159 if (handle->inited != 1) /* check handle initialization */
160 {
161 return 3; /* return error */
162 }
163
164 res = handle->spi_deinit(); /* spi deinit */
165 if (res != 0) /* check result */
166 {
167 handle->debug_print("max6675: spi deinit failed.\n"); /* spi deinit failed */
168
169 return 1; /* return error */
170 }
171 handle->inited = 0; /* flag close */
172
173 return 0; /* success return 0 */
174}
175
189uint8_t max6675_read(max6675_handle_t *handle,uint16_t *raw, float *temp)
190{
191 uint8_t res;
192 uint16_t data;
193
194 if (handle == NULL) /* check handle */
195 {
196 return 2; /* return error */
197 }
198 if (handle->inited != 1) /* check handle initialization */
199 {
200 return 3; /* return error */
201 }
202
203 res = a_max6675_spi_read(handle, &data); /* read data */
204 if (res != 0) /* check result */
205 {
206 handle->debug_print("max6675: read data failed.\n"); /* read data failed */
207
208 return 1; /* return error */
209 }
210 if ((data & (1 << 2)) != 0) /* check the error */
211 {
212 handle->debug_print("max6675: thermocouple input is open.\n"); /* thermocouple input is open */
213
214 return 4; /* return error */
215 }
216 *raw = data >> 3; /* get the raw data */
217 *temp = (float)(*raw) * 0.25f; /* convert data */
218
219 return 0; /* success return 0 */
220}
221
233uint8_t max6675_get_reg(max6675_handle_t *handle, uint16_t *data)
234{
235 if (handle == NULL) /* check handle */
236 {
237 return 2; /* return error */
238 }
239 if (handle->inited != 1) /* check handle initialization */
240 {
241 return 3; /* return error */
242 }
243
244 return a_max6675_spi_read(handle, data); /* read data */
245}
246
256{
257 if (info == NULL) /* check handle */
258 {
259 return 2; /* return error */
260 }
261
262 memset(info, 0, sizeof(max6675_info_t)); /* initialize max6675 info structure */
263 strncpy(info->chip_name, CHIP_NAME, 32); /* copy chip name */
264 strncpy(info->manufacturer_name, MANUFACTURER_NAME, 32); /* copy manufacturer name */
265 strncpy(info->interface, "SPI", 8); /* copy interface name */
266 info->supply_voltage_min_v = SUPPLY_VOLTAGE_MIN; /* set minimal supply voltage */
267 info->supply_voltage_max_v = SUPPLY_VOLTAGE_MAX; /* set maximum supply voltage */
268 info->max_current_ma = MAX_CURRENT; /* set maximum current */
269 info->temperature_max = TEMPERATURE_MAX; /* set minimal temperature */
270 info->temperature_min = TEMPERATURE_MIN; /* set maximum temperature */
271 info->driver_version = DRIVER_VERSION; /* set driver version */
272
273 return 0; /* success return 0 */
274}
#define MAX_CURRENT
#define SUPPLY_VOLTAGE_MAX
#define TEMPERATURE_MAX
#define MANUFACTURER_NAME
#define TEMPERATURE_MIN
#define SUPPLY_VOLTAGE_MIN
#define CHIP_NAME
chip information definition
#define DRIVER_VERSION
driver max6675 header file
uint8_t max6675_read(max6675_handle_t *handle, uint16_t *raw, float *temp)
read the temperature
uint8_t max6675_info(max6675_info_t *info)
get chip's information
struct max6675_info_s max6675_info_t
max6675 information structure definition
uint8_t max6675_init(max6675_handle_t *handle)
initialize the chip
struct max6675_handle_s max6675_handle_t
max6675 handle structure definition
uint8_t max6675_deinit(max6675_handle_t *handle)
close the chip
uint8_t max6675_get_reg(max6675_handle_t *handle, uint16_t *data)
get the chip register
uint8_t(* spi_init)(void)
void(* delay_ms)(uint32_t ms)
uint8_t(* spi_read_cmd)(uint8_t *buf, uint16_t len)
void(* debug_print)(const char *const fmt,...)
uint8_t(* spi_deinit)(void)
float supply_voltage_max_v
uint32_t driver_version
char manufacturer_name[32]
float supply_voltage_min_v