-
Notifications
You must be signed in to change notification settings - Fork 0
/
HX711.c
148 lines (121 loc) · 3.37 KB
/
HX711.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
#include <HX711.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/cpufunc.h>
#include <util/delay.h>
// static uint8_t PD_SCK; // Power Down and Serial Clock Input Pin
// static uint8_t DOUT;// Serial Data Output Pin
static uint8_t GAIN;// amplification factor
static long OFFSET = 0; // used for tare weight
static float SCALE = 1; // used to return weight in grams, kg, ounces, whatever
#define clock_high() (PORT_CLK |= (1 << PIN_CLK))
#define clock_low() (PORT_CLK &= ~(1 << PIN_CLK))
#define get_DOUT() ((READ_PORT_DOUT & (1 << PIN_DOUT)) >> PIN_DOUT)
void HX711_init(uint8_t gain) {
// PD_SCK = pd_sck;
// DOUT = dout;
// pinMode(PD_SCK, OUTPUT);
DDR_CLK |= (1 << PIN_CLK);
// pinMode(DOUT, INPUT);
DDR_DOUT &= ~(1 << PIN_DOUT);
HX711_set_gain(gain);
}
uint8_t HX711_is_ready() {
//return digitalRead(DOUT) == LOW;
return (get_DOUT() == 0);
}
void HX711_set_gain(uint8_t gain) {
switch (gain) {
case 128: // channel A, gain factor 128
GAIN = 1;
break;
case 64: // channel A, gain factor 64
GAIN = 3;
break;
case 32: // channel B, gain factor 32
GAIN = 2;
break;
}
//digitalWrite(PD_SCK, LOW);
clock_low();
HX711_read();
}
long HX711_read() {
// wait for the chip to become ready
while (!HX711_is_ready()) {
_NOP();
}
unsigned long value = 0;
uint8_t data[3] = { 0 };
uint8_t filler = 0x00;
// pulse the clock pin 24 times to read the data
for(int8_t n = 2; n>=0; n--) {
for(int8_t i=7; i>=0; i--) {
clock_high();
_delay_us(1); // let some time to hx711 to update output value
data[n] |= get_DOUT() << i;
clock_low();
}
}
// set the channel and the gain factor for the next reading using the clock pin
for (unsigned int i = 0; i < GAIN; i++) {
clock_high();
_delay_us(1); // let some time to hx711 to understand the command
clock_low();
}
// Replicate the most significant bit to pad out a 32-bit signed integer
if (data[2] & 0x80) {
filler = 0xFF;
} else {
filler = 0x00;
}
// Construct a 32-bit signed integer
value = ( (unsigned long)(filler) << 24
| (unsigned long)(data[2]) << 16
| (unsigned long)(data[1]) << 8
| (unsigned long)(data[0]) );
return (long)(value);
}
long HX711_read_average(uint8_t times) {
long sum = 0;
for (uint8_t i = 0; i < times; i++) {
sum += HX711_read();
// yield();
}
return sum / times;
}
double HX711_get_value() {
return HX711_read_average(1) - OFFSET;
}
double HX711_get_mean_value(uint8_t times) {
return HX711_read_average(times) - OFFSET;
}
float HX711_get_units() {
return HX711_get_value() / SCALE;
}
float HX711_get_mean_units(uint8_t times) {
return HX711_get_mean_value(times) / SCALE;
}
void HX711_tare(uint8_t times) {
double sum = HX711_read_average(times);
HX711_set_offset(sum);
}
void HX711_set_scale(float scale) {
SCALE = scale;
}
float HX711_get_scale() {
return SCALE;
}
void HX711_set_offset(long offset) {
OFFSET = offset;
}
long HX711_get_offset() {
return OFFSET;
}
void HX711_power_down() {
clock_low();
clock_high();
}
void HX711_power_up() {
clock_low();
}