forked from Ho-Ro/ComponentTester
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMAX6675.c
375 lines (284 loc) · 8.9 KB
/
MAX6675.c
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/* ************************************************************************
*
* functions for MAX6675 (K thermocouple ADC)
*
* (c) 2021 by Markus Reschke
*
* ************************************************************************ */
/*
* hints:
* - pin assignment
* SCK SPI: SCK
* SO SPI: MISO
* /CS MAX6675_CS
* - max. SPI clock: 4.3 MHz
* - Vcc 5V or 3.3V (requires level shifter)
*/
/* local includes */
#include "config.h" /* global configuration */
#ifdef HW_MAX6675
/*
* local constants
*/
/* source management */
#define MAX6675_C
/* operation modes */
#define MODE_MANUAL 0 /* manual mode */
#define MODE_AUTO 1 /* automatic mode */
/*
* include header files
*/
/* local includes */
#include "common.h" /* common header file */
#include "variables.h" /* global variables */
#include "functions.h" /* external functions */
/*
* local variables
*/
#ifdef SPI_HARDWARE
/* SPI */
uint8_t ClockRate; /* SPI clock rate bits */
uint8_t OldClockRate; /* SPI clock rate bits */
#endif
/* ************************************************************************
* low level functions for SPI interface
* ************************************************************************ */
/*
* protocol (read only):
* - /CS low (also starts new conversion)
* - read 16 bits on falling edge of SCK
* - data is MSB
* - D15 dummy sign bit (always 0)
* - D14-3 temperature, 12 bits (in 0.25°C / 0 - 1023.75°C)
* - D2 thermocouple input (0: closed / 1: open)
* - D1 device ID (always 0)
* - D0 state (three-state)
* - conversion time (in background): 0.17 - 0.22 s
*/
/*
* set up interface bus
* - should be called at firmware startup
*/
void MAX6675_BusSetup(void)
{
/*
* set control signals
*/
/* set directions for required pins: */
MAX6675_DDR |= (1 << MAX6675_CS); /* /CS */
/* set default levels */
MAX6675_PORT |= (1 << MAX6675_CS); /* set /CS high */
/*
* init SPI bus
* - SPI bus is set up already in main()
*/
#ifdef SPI_HARDWARE
/*
* set SPI clock rate (max. 2MHz)
*/
/* 1MHz -> f_osc/2 (SPR1 = 0, SPR0 = 0, SPI2X = 1) */
#if CPU_FREQ / 1000000 == 1
ClockRate = SPI_CLOCK_2X;
#endif
/* 8MHz -> f_osc/4 (SPR1 = 0, SPR0 = 0, SPI2X = 0) */
#if CPU_FREQ / 1000000 == 8
ClockRate = 0;
#endif
/* 16MHz -> f_osc/8 (SPR1 = 0, SPR0 = 1, SPI2X = 1) */
#if CPU_FREQ / 1000000 == 16
ClockRate = SPI_CLOCK_R0 | SPI_CLOCK_2X;
#endif
/* 20MHz -> f_osc/16 (SPR1 = 0, SPR0 = 1, SPI2X = 0) */
#if CPU_FREQ / 1000000 == 20
ClockRate = SPI_CLOCK_R0;
#endif
#endif
}
/*
* select MAX6675
* - also update clock for hardware SPI
*/
void MAX6675_SelectChip(void)
{
/* select chip */
MAX6675_PORT &= ~(1 << MAX6675_CS); /* set /CS low */
wait1us(); /* wait >100ns */
#ifdef SPI_HARDWARE
/* change SPI clock for touch controller */
OldClockRate = SPI.ClockRate; /* save old clock settings */
SPI.ClockRate = ClockRate; /* set new clock rate */
SPI_Clock(); /* update SPI clock */
#endif
}
/*
* deselect MAX6675
* - also update clock for hardware SPI
*/
void MAX6675_DeselectChip(void)
{
/* disable chip */
MAX6675_PORT |= (1 << MAX6675_CS); /* set /CS high */
wait1us(); /* wait >100ns */
#ifdef SPI_HARDWARE
/* change SPI clock for display controller */
SPI.ClockRate = OldClockRate; /* set old clock rate */
SPI_Clock(); /* update SPI clock */
#endif
}
/* ************************************************************************
* high level functions
* ************************************************************************ */
/*
* MAX6675: read temperature
*
* requires:
* - Value: pointer to temperature in °C
* - Scale: pointer to scale factor / decimal places
*
* returns:
* - 1 on success
* - 0 on any problem
*/
uint8_t MAX6675_ReadTemperature(int32_t *Value, int8_t *Scale)
{
uint8_t Flag = 0; /* return value */
uint8_t HighByte; /* MSB */
uint8_t LowByte; /* LSB */
uint16_t Temp; /* temperature */
/*
* read data from MAX6675
* - MSB to LSB
*/
MAX6675_SelectChip(); /* select chip */
/* read two bytes */
HighByte = SPI_WriteRead_Byte(0); /* read MSB */
LowByte = SPI_WriteRead_Byte(0); /* read LSB */
MAX6675_DeselectChip(); /* deselect chip */
/*
* process data
*/
/* check thermocouple input (D2) */
if ((LowByte & 0b00000100) == 0) /* 0: closed */
{
/* check dummy sign (D15) */
if ((HighByte & 0b10000000) == 0) /* expected to be 0 */
{
/* get temperature (D14-3, in 0.25°C) */
Temp = HighByte; /* copy MSB */
Temp <<= 8; /* shift to MSB */
Temp |= LowByte; /* copy LSB */
Temp >>= 3; /* get rid of D2-0 */
*Value = Temp; /* copy value */
*Value *= 25; /* scale to 0.01, * 0.25°C */
*Scale = 2; /* two decimal places (10^-2) */
Flag = 1; /* signal success */
}
}
return Flag;
}
/* ************************************************************************
* tool
* ************************************************************************ */
/*
* MAX6675 tool
* - reads and displays temperature of K thermocouple
*/
void MAX6675_Tool(void)
{
uint8_t Flag = 1; /* loop control */
uint8_t Test; /* key / feedback */
uint8_t Mode = MODE_MANUAL; /* operation mode */
uint16_t Timeout = 0; /* timeout for user feedback */
int8_t Scale; /* temperature scale / decimal places */
int32_t Value; /* temperature value */
/*
* display info
*/
LCD_Clear();
#ifdef UI_COLORED_TITLES
Display_ColoredEEString(MAX6675_str, COLOR_TITLE); /* display: MAX6675 */
#else
Display_EEString(MAX6675_str); /* display: MAX6675 */
#endif
LCD_CharPos(1, 2); /* move to line #2 */
Display_EEString(Start_str); /* display: Start */
/*
* processing loop
*/
while (Flag)
{
/*
* user input
*/
/* wait for user input */
Test = TestKey(Timeout, CURSOR_BLINK | CHECK_KEY_TWICE | CHECK_BAT);
if (Test == KEY_LONG) /* long key press */
{
/* display mode in line #1 */
LCD_ClearLine(1); /* clear line #1 */
LCD_CharPos(1, 1); /* move to line #1 */
#ifdef UI_COLORED_TITLES
Display_ColoredEEString_Space(MAX6675_str, COLOR_TITLE);
#else
Display_EEString_Space(MAX6675_str);
#endif
/* change mode */
if (Mode == MODE_MANUAL) /* manual mode */
{
Mode = MODE_AUTO; /* set automatic mode */
Timeout = 1000; /* wait for max. 1s */
/* indicate auto mode */
Display_Char('*'); /* display: * */
}
else /* automatic mode */
{
Mode = MODE_MANUAL; /* set manual mode again */
Timeout = 0; /* wait for user */
}
MilliSleep(500); /* smooth UI */
}
else if (Test == KEY_TWICE) /* two short key presses */
{
Flag = 0; /* end loop */
}
LCD_ClearLine2(); /* clear line #2 */
/*
* read and show temperature
*/
if (Flag) /* ok to proceed */
{
/* get temperature from MAX6675 (in °C) */
Test = MAX6675_ReadTemperature(&Value, &Scale);
if (Test) /* got temperature */
{
#ifdef UI_FAHRENHEIT
/* convert Celsius into Fahrenheit */
Value = Celsius2Fahrenheit(Value, Scale);
#endif
/* todo: add degree symbol to bitmap fonts */
Display_FullValue(Value, Scale, '°');
#ifdef UI_FAHRENHEIT
Display_Char('F'); /* display: F (Fahrenheit) */
#else
Display_Char('C'); /* display: C (Celsius) */
#endif
}
else /* some error */
{
Display_Minus(); /* display: - */
}
}
}
}
/* ************************************************************************
* clean-up of local constants
* ************************************************************************ */
/* local constants */
#undef MODE_MANUAL
#undef MODE_AUTO
/* source management */
#undef MAX6675_C
#endif
/* ************************************************************************
* EOF
* ************************************************************************ */