-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPIC16F690.go
128 lines (104 loc) · 2.36 KB
/
PIC16F690.go
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
package main
import (
"fmt"
"log"
"strconv"
"time"
"github.com/MichaelS11/go-hx711"
"periph.io/x/conn/v3/physic"
"periph.io/x/conn/v3/spi"
"periph.io/x/conn/v3/spi/spireg"
"periph.io/x/host/v3"
)
var characterMap = map[rune]byte{
'0': 0x30,
'1': 0x31,
'2': 0x32,
'3': 0x33,
'4': 0x34,
'5': 0x35,
'6': 0x36,
'7': 0x37,
'8': 0x38,
'9': 0x39,
'a': 0x16,
'b': 0x26,
'c': 0x36,
'd': 0x46,
}
func calibrate() {
err := hx711.HostInit()
if err != nil {
fmt.Println("HostInit error:", err)
return
}
hx711, err := hx711.NewHx711("GPIO6", "GPIO5")
if err != nil {
fmt.Println("NewHx711 error:", err)
return
}
// SetGain default is 128
// Gain of 128 or 64 is input channel A, gain of 32 is input channel B
// hx711.SetGain(128)
var weight1 float64
var weight2 float64
weight1 = 0
weight2 = 202
hx711.GetAdjustValues(weight1, weight2)
}
func floattostr(input_num float64) string {
// to convert a float number to a string
return strconv.FormatFloat(input_num, 'g', 1, 64)
}
func lcdDisplay(data rune, characterMap map[rune]byte) {
if _, err := host.Init(); err != nil {
fmt.Println("host.Init error:", err)
return
}
// Use spireg SPI port registry to find the first available SPI bus.
p, err := spireg.Open("")
if err != nil {
log.Fatal("Open: ", err)
}
defer p.Close()
// the spi.Port into a spi.Conn so it can be used for communication.
c, err := p.Connect(physic.KiloHertz, spi.Mode3, 8)
if err != nil {
log.Fatal("Connect: ", err)
}
lcdDisplay := []byte{characterMap[data]}
read := make([]byte, len(lcdDisplay))
if err := c.Tx(lcdDisplay, read); err != nil {
fmt.Println("LCD Turned On!", err)
log.Fatal(err)
}
}
func getWeight(data [5]float64) {
err := hx711.HostInit()
if err != nil {
fmt.Println("HostInit error:", err)
return
}
hx711, err := hx711.NewHx711("GPIO6", "GPIO5")
if err != nil {
fmt.Println("NewHx711 error:", err)
return
}
// SetGain default is 128
// Gain of 128 or 64 is input channel A, gain of 32 is input channel B
// hx711.SetGain(128)
// make sure to use your values from calibration above
hx711.AdjustZero = 5507
hx711.AdjustScale = 30904
//var data[5] float64
for i := 0; i < 5; i++ {
time.Sleep(200 * time.Microsecond)
data[i], err = hx711.ReadDataMedian(11)
if err != nil {
fmt.Println("ReadDataMedian error:", err)
continue
}
fmt.Println(data[i])
}
return
}