-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
328 lines (288 loc) · 9.16 KB
/
main.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
#include "lcd1602a.h"
#include "hx711.h"
#include "stm8s_clk.h"
#include "stm8s_tim1.h"
#include "stm8s_tim2.h"
#include "stm8s_gpio.h"
#include "stm8s_wwdg.h"
#include <stdbool.h>
#define NULL ((void *) 0)
#define MEASUREMENTS_COUNT 50
// PD1 - is swim by default.
static struct {
GPIO_TypeDef *portBase;
GPIO_Pin_TypeDef pin;
} LcdPinMap[] = {
[LcdPinDB4] = {.portBase = GPIOA, .pin = GPIO_PIN_1},
[LcdPinDB5] = {.portBase = GPIOD, .pin = GPIO_PIN_6},
[LcdPinDB6] = {.portBase = GPIOD, .pin = GPIO_PIN_5},
[LcdPinDB7] = {.portBase = GPIOD, .pin = GPIO_PIN_4},
[LcdPinReadWrite] = {.portBase = GPIOD, .pin = GPIO_PIN_1},
[LcdPinRegisterSelect] = {.portBase = GPIOA, .pin = GPIO_PIN_3},
[LcdPinEnable] = {.portBase = GPIOA, .pin = GPIO_PIN_2},
};
static struct {
GPIO_TypeDef *portBase;
GPIO_Pin_TypeDef pin;
} hx711PinMap[] = {
[Hx711PinData] = {.portBase = GPIOD, .pin = GPIO_PIN_2},
[Hx711PinClock] = {.portBase = GPIOD, .pin = GPIO_PIN_3},
};
static struct {
GPIO_TypeDef *portBase;
GPIO_Pin_TypeDef pin;
} tareButton = {.portBase = GPIOC, .pin = GPIO_PIN_7};
static volatile uint16_t timeTick = 0;
static volatile uint8_t usDelayPassed = 0;
static LcdErr lcdPinWriteCallback(LcdPin pin, LcdPinState state)
{
if (pin >= LcdPinCount || state >= LcdPinStateCount)
return LcdErrParam;
if (state == LcdPinStateHigh) {
GPIO_WriteHigh(LcdPinMap[pin].portBase, LcdPinMap[pin].pin);
} else {
GPIO_WriteLow(LcdPinMap[pin].portBase, LcdPinMap[pin].pin);
}
return LcdErrOk;
}
static LcdErr lcdPinReadCallback(LcdPin pin, LcdPinState *state)
{
if (pin >= LcdPinCount || state == NULL)
return LcdErrParam;
if (GPIO_ReadInputPin(LcdPinMap[pin].portBase, LcdPinMap[pin].pin)) {
*state = LcdPinStateHigh;
} else {
*state = LcdPinStateLow;
}
return LcdErrOk;
}
static LcdErr lcdPinConfigCallback(LcdPin pin, LcdPinDirection dir)
{
if (pin >= LcdPinCount || dir >= LcdPinDirectionCount)
return LcdErrParam;
if (dir == LcdPinDirectionInput) {
GPIO_Init(LcdPinMap[pin].portBase, LcdPinMap[pin].pin, GPIO_MODE_IN_FL_NO_IT);
} else {
GPIO_Init(LcdPinMap[pin].portBase, LcdPinMap[pin].pin, GPIO_MODE_OUT_PP_LOW_SLOW);
}
return LcdErrOk;
}
static Hx711Err hx711PinWriteCallback(Hx711Pin pin, Hx711PinState state)
{
if (pin >= Hx711PinCount || state >= Hx711PinStateCount)
return Hx711ErrParam;
if (state == Hx711PinStateHigh) {
GPIO_WriteHigh(hx711PinMap[pin].portBase, hx711PinMap[pin].pin);
} else {
GPIO_WriteLow(hx711PinMap[pin].portBase, hx711PinMap[pin].pin);
}
return Hx711ErrOk;
}
static Hx711Err hx711PinReadCallback(Hx711Pin pin, Hx711PinState *state)
{
if (pin >= Hx711PinCount || state == NULL)
return Hx711ErrParam;
if (GPIO_ReadInputPin(hx711PinMap[pin].portBase, hx711PinMap[pin].pin)) {
*state = Hx711PinStateHigh;
} else {
*state = Hx711PinStateLow;
}
return Hx711ErrOk;
}
static uint16_t getTimeTick(void)
{
disableInterrupts();
uint16_t tick = timeTick;
enableInterrupts();
return tick;
}
static void delayUs(uint16_t us)
{
usDelayPassed = 0;
TIM2_SetCounter(0);
TIM2_SetAutoreload(us);
TIM2_Cmd(ENABLE);
while(usDelayPassed == 0) { }
}
static void delayMs(uint16_t ms)
{
uint16_t timestamp = getTimeTick();
while (getTimeTick() - timestamp < ms) {
}
}
static void printValue(int32_t value)
{
unsigned char len = 0;
unsigned char buff[16] = {0};
int32_t temp = value < 0 ? -value : value;
do {
buff[sizeof(buff) - 1 - len] = temp % 10 + '0';
temp /= 10;
len++;
} while (temp);
buff[sizeof(buff) - 1 - len] = value < 0 ? '-' : ' ';
len++;
lcdPrint(&buff[sizeof(buff) - len], len);
}
static bool getSensorData(signed long *data)
{
*data = 0;
return hx711ReadChannel(Hx711ChannelA128, data) == Hx711ErrOk ? true : false;
}
int main( void )
{
/* Configure system clock */
CLK_HSICmd(ENABLE);
while (!CLK->ICKR & (1 << 1)); // wait untill clock became stable
CLK_SYSCLKConfig(CLK_PRESCALER_HSIDIV1); // 16MHz SYS
CLK_SYSCLKConfig(CLK_PRESCALER_CPUDIV1); // 16MHz CPU
/* Enable clocks for peripherals */
CLK_PeripheralClockConfig(CLK_PERIPHERAL_TIMER1, ENABLE);
CLK_PeripheralClockConfig(CLK_PERIPHERAL_TIMER2, ENABLE);
/* Timer 1 Init, ms timer*/
TIM1_DeInit();
TIM1_PrescalerConfig(16 - 1, TIM1_PSCRELOADMODE_IMMEDIATE); // fCK_CNT = fCK_PSC/(PSCR[15:0]+1)
TIM1_SetAutoreload(1000); // frequency = 16 000 000 / 16 / 1000 = 1000 Hz
TIM1_ClearITPendingBit(TIM1_IT_UPDATE);
TIM1_ITConfig(TIM1_IT_UPDATE, ENABLE);
TIM1_CounterModeConfig(TIM1_COUNTERMODE_UP);
/* Timer 2 Init, us timer*/
TIM2_DeInit();
TIM2_UpdateRequestConfig(TIM2_UPDATESOURCE_REGULAR);
TIM2_PrescalerConfig(TIM2_PRESCALER_16, TIM2_PSCRELOADMODE_IMMEDIATE); // frequency = 16M / 16 / = 1Mhz
TIM2_SetAutoreload(0);
TIM2_SelectOnePulseMode(TIM2_OPMODE_SINGLE);
TIM2_ClearITPendingBit(TIM2_IT_UPDATE);
TIM2_ITConfig(TIM2_IT_UPDATE, ENABLE);
enableInterrupts();
/* Start application timer */
TIM1_Cmd(ENABLE);
/* Onboard led init */
GPIO_Init(GPIOB, GPIO_PIN_5, GPIO_MODE_OUT_PP_LOW_SLOW);
/* Tare button init */
GPIO_Init(tareButton.portBase, tareButton.pin, GPIO_MODE_IN_PU_IT);
EXTI_SetExtIntSensitivity(EXTI_PORT_GPIOC, EXTI_SENSITIVITY_FALL_ONLY);
/* Hx711 pins init */
GPIO_Init(hx711PinMap[Hx711PinData].portBase, hx711PinMap[Hx711PinData].pin, GPIO_MODE_IN_FL_NO_IT);
GPIO_Init(hx711PinMap[Hx711PinClock].portBase, hx711PinMap[Hx711PinClock].pin, GPIO_MODE_OUT_PP_LOW_FAST);
Hx711Handle hx711Handle = {
.pinWriteCb = hx711PinWriteCallback,
.pinReadCb = hx711PinReadCallback,
.delayUsCb = delayUs,
.initChannel = Hx711ChannelA128
};
hx711Init(&hx711Handle);
LcdHandle lcdHandle = {
.pinWriteCb = lcdPinWriteCallback,
.pinReadCb = lcdPinReadCallback,
.pinConfigCb = lcdPinConfigCallback,
.delayUsCb = delayUs
};
/* Wait for more than 40 ms after VCC rises to 2.7 V */
delayMs(50);
lcdInit(&lcdHandle, LcdInterface4Bit, LcdFontType5x8, LcdTwoLineMode);
lcdTurnOn();
delayUs(100);
while(lcdCheckBusyFlag() == LcdErrBusy) { }
// lcdCursorOn();
delayUs(100);
while(lcdCheckBusyFlag() == LcdErrBusy) { }
lcdClearScreen();
while(lcdCheckBusyFlag() == LcdErrBusy) { }
lcdPrint("Calibration...", 14);
while(lcdCheckBusyFlag() == LcdErrBusy) { }
signed long scaleOffset = 0;
signed long average = 0;
int count = 0;
// sync data measurements
while (hx711GetStatus() != Hx711ErrBusy) { }
while (hx711GetStatus() != Hx711ErrOk) { }
while (count < 100) {
signed long data = 0;
if (getSensorData(&data)) {
scaleOffset += data;
count++;
}
}
scaleOffset /= count;
count = 0;
uint16_t lastTick = 0;
while (1) {
if (count < MEASUREMENTS_COUNT) {
signed long data = 0;
if (getSensorData(&data)) {
average += data;
count++;
}
} else {
average /= MEASUREMENTS_COUNT;
average -= scaleOffset;
lcdClearScreen();
while(lcdCheckBusyFlag() == LcdErrBusy) { }
printValue(average);
lcdCursorPositionSet(1, 0);
printValue(average / 11750);
lcdPringChar(',');
signed char decimal = (average % 11750) / 1175;
if (decimal < 0) {
decimal = -decimal;
}
lcdPringChar(decimal + '0');
lcdPringChar('g');
average = 0;
count = 0;
}
if (getTimeTick() - lastTick > 500) {
GPIO_WriteReverse(GPIOB, GPIO_PIN_5);
lastTick = getTimeTick();
}
}
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t* file, uint32_t line)
{
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* Infinite loop */
while (1)
{
}
}
#endif // USE_FULL_ASSERT
/* Application Timer Interrupt Handler */
/**
* @brief Timer1 Update/Overflow/Trigger/Break Interrupt routine.
* @param None
* @retval None
*/
INTERRUPT_HANDLER(TIM1_UPD_OVF_TRG_BRK_IRQHandler, 11)
{
TIM1_ClearITPendingBit(TIM1_IT_UPDATE);
timeTick++;
}
/**
* @brief Timer2 Update/Overflow/Break Interrupt routine
* @param None
* @retval None
*/
INTERRUPT_HANDLER(TIM2_UPD_OVF_BRK_IRQHandler, 13)
{
TIM2_ClearITPendingBit(TIM2_IT_UPDATE);
usDelayPassed = 1;
}
/**
* @brief External Interrupt PORTC Interrupt routine
* @param None
* @retval None
*/
INTERRUPT_HANDLER(EXTI_PORTC_IRQHandler, 5)
{
WWDG_SWReset();
}