Skip to content

Commit ccfcfa8

Browse files
jmhobbsdeadprogram
authored andcommitted
Add MAX6675 device
1 parent 87c205f commit ccfcfa8

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

max6675/max6675.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Datasheet: https://www.analog.com/media/en/technical-documentation/data-sheets/max6675.pdf
2+
package max6675
3+
4+
import (
5+
"errors"
6+
"machine"
7+
)
8+
9+
// ErrThermocoupleOpen is returned when the thermocouple input is open.
10+
// i.e. not attached or faulty
11+
var ErrThermocoupleOpen = errors.New("thermocouple input open")
12+
13+
type Device struct {
14+
bus machine.SPI
15+
cs machine.Pin
16+
}
17+
18+
// Create a new Device to read from a MAX6675 thermocouple.
19+
// Pins must be configured before use. Frequency for SPI
20+
// should be 4.3MHz maximum.
21+
func NewDevice(bus machine.SPI, cs machine.Pin) *Device {
22+
return &Device{
23+
bus: bus,
24+
cs: cs,
25+
}
26+
}
27+
28+
// Read and return the temperature in celsius
29+
func (d *Device) Read() (float32, error) {
30+
var (
31+
read []byte = []byte{0, 0}
32+
value uint16
33+
)
34+
35+
d.cs.Low()
36+
if err := d.bus.Tx([]byte{0, 0}, read); err != nil {
37+
return 0, err
38+
}
39+
d.cs.High()
40+
41+
// datasheet: Bit D2 is normally low and goes high if the thermocouple input is open.
42+
if read[1]&0x04 == 0x04 {
43+
return 0, ErrThermocoupleOpen
44+
}
45+
46+
// data is 12 bits, split across the two bytes
47+
// -XXXXXXX XXXXX---
48+
value = (uint16(read[0]) << 5) | (uint16(read[1]) >> 3)
49+
50+
return float32(value) * 0.25, nil
51+
}

0 commit comments

Comments
 (0)