| MAXREFDES82# Code Documentation V01.00 Maxim Smart Force Sensor

- Main Page
- Modules
- Files

- File List
- Globals

All Files Functions Variables Macros Groups Pages
maintask.c
Go to the documentation of this file.
1
43 #include "main.h"
44 #include "GUI.h"
45 #include "string.h"
46 #include "stm32f429i_discovery_lcd.h"
47 #include "stm32f4xx_hal.h"
48 #include "stm32f429i_discovery.h"
49 #include "math.h"
50
51 #define POWERUP_CAL_NUM 200
52 #define FACOTRY_CAL_NUM 200
53
54 #define MAJORVERSION 3
55 #define MINORVERSION 5
56 uint8_t usbTxBuf[32]={0};
57
58 /*Variable used for Erase procedure*/
59 static FLASH_EraseInitTypeDef EraseInitStruct;
60 uint32_t SectorError = 0;
61 static void ErrorHandler(void);
62
63 extern USBD_HandleTypeDef USBD_Device;
64 uint8_t request = 0;
65
66 GUI_RECT Rect = {0, 0, 320, 240};
67
68 uint8_t spiWritePacket[4] = {0};
69
70 // data registers from MAX11254.
71 uint8_t data0[4], data1[4], data2[4], data3[4];
72
73 // data in binary offset format.
74 uint32_t data0BO, data1BO, data2BO, data3BO;
75
76 // load cell result in decimal format.
77 int cell0Load, cell1Load, cell2Load, cell3Load;
78
79 // zero gram load reading
80 double cell0Load0=80606.899, cell1Load0=2316.475, cell2Load0=-4303.219, cell3Load0=48106.286;
81 uint32_t cell0Load0Int, cell1Load0Int, cell2Load0Int, cell3Load0Int;
82 uint32_t cell0Load0Sign, cell1Load0Sign, cell2Load0Sign, cell3Load0Sign;
83
84 // 700 gram load reading
85 double cell0Load700=1386219.167, cell1Load700=1306319.674, cell2Load700=1238527.327, cell3Load700=1278776.285;
86 uint32_t cell0Load700Int, cell1Load700Int, cell2Load700Int, cell3Load700Int;
87 uint32_t cell0Load700Sign, cell1Load700Sign, cell2Load700Sign, cell3Load700Sign;
88
89 // power up calibration results in decimal format.
90 double cell0Powerup, cell1Powerup, cell2Powerup, cell3Powerup;
91
92 // load in grams.
93 double cell0Grams, cell1Grams, cell2Grams, cell3Grams;
94
95 void ConfigureSPIandGPIO(void);
96 void ConfigureMAX11254(void);
97 void ReadMAX11254Results(void);
98 void Calibration(int8_t type, int32_t conversions);
99
100 double x=0, y=0, r=0;
101
102 /*********************************************************************
103 *
104 * Public code
105 *
106 **********************************************************************
107 */
108 /*********************************************************************
109 *
110 * MainTask
111 */
112 void MainTask(void)
113 {
114 //GUI_MEMDEV_Handle hMem = GUI_MEMDEV_Create(0,0,100,100);
115 //GUI_MEMDEV_Select(hMem);
116 //GUI_MEMDEV_CopyToLCD(hMem);
117 GUI_Clear();
118 //GUI_SetFont(&GUI_Font16B_1);
119 GUI_SetFont(&GUI_Font8x16);
120
121 ConfigureSPIandGPIO();
122 HAL_Delay(5);
123
124 ConfigureMAX11254();
125 HAL_Delay(5);
126
127 cell0Load0Sign = *(__IO uint32_t*)0x08100000;
128 cell1Load0Sign = *(__IO uint32_t*)0x08100008;
129 cell2Load0Sign = *(__IO uint32_t*)0x08100010;
130 cell3Load0Sign = *(__IO uint32_t*)0x08100018;
131 cell0Load700Sign = *(__IO uint32_t*)0x08100020;
132 cell1Load700Sign = *(__IO uint32_t*)0x08100028;
133 cell2Load700Sign = *(__IO uint32_t*)0x08100030;
134 cell3Load700Sign = *(__IO uint32_t*)0x08100038;
135 cell0Load0Int = *(__IO uint32_t*)0x08100004;
136 cell1Load0Int = *(__IO uint32_t*)0x0810000C;
137 cell2Load0Int = *(__IO uint32_t*)0x08100014;
138 cell3Load0Int = *(__IO uint32_t*)0x0810001C;
139 cell0Load700Int = *(__IO uint32_t*)0x08100024;
140 cell1Load700Int = *(__IO uint32_t*)0x0810002C;
141 cell2Load700Int = *(__IO uint32_t*)0x08100034;
142 cell3Load700Int = *(__IO uint32_t*)0x0810003C;
143 cell0Load0Sign==1? (cell0Load0 = 0.0-(double)cell0Load0Int):(cell0Load0 = cell0Load0Int);
144 cell1Load0Sign==1? (cell1Load0 = 0.0-(double)cell1Load0Int):(cell1Load0 = cell1Load0Int);
145 cell2Load0Sign==1? (cell2Load0 = 0.0-(double)cell2Load0Int):(cell2Load0 = cell2Load0Int);
146 cell3Load0Sign==1? (cell3Load0 = 0.0-(double)cell3Load0Int):(cell3Load0 = cell3Load0Int);
147 cell0Load700Sign==1? (cell0Load700 = 0.0-(double)cell0Load700Int):(cell0Load700 = cell0Load700Int);
148 cell1Load700Sign==1? (cell1Load700 = 0.0-(double)cell1Load700Int):(cell1Load700 = cell1Load700Int);
149 cell2Load700Sign==1? (cell2Load700 = 0.0-(double)cell2Load700Int):(cell2Load700 = cell2Load700Int);
150 cell3Load700Sign==1? (cell3Load700 = 0.0-(double)cell3Load700Int):(cell3Load700 = cell3Load700Int);
151
152 Calibration(0, POWERUP_CAL_NUM);
153
154 // loop forever.
155 while(1)
156 {
157 // start one conversion.
158 HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, GPIO_PIN_RESET);
159 spiWritePacket[0] = 0xB4; // 200 SPS
160 SPIx_Write_2(spiWritePacket, 1);
161 HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, GPIO_PIN_SET);
162
163 // wait for the conversion to complete.
164 while(1)
165 {
166 if(HAL_GPIO_ReadPin(GPIOG, GPIO_PIN_13) == GPIO_PIN_RESET)
167 break;
168 }
169 BSP_LED_Toggle(LED3);
170
171 ReadMAX11254Results();
172
173 // calculate results.
174 cell0Grams = (cell0Load - cell0Powerup) * 700.0 / (cell0Load700-cell0Load0);
175 cell1Grams = (cell1Load - cell1Powerup) * 700.0 / (cell1Load700-cell1Load0);
176 cell2Grams = (cell2Load - cell2Powerup) * 700.0 / (cell2Load700-cell2Load0);
177 cell3Grams = (cell3Load - cell3Powerup) * 700.0 / (cell3Load700-cell3Load0);
178
179 x = 240-(cell1Grams+cell3Grams)/(cell0Grams+cell1Grams+cell2Grams+cell3Grams) * 240;
180 y = 240-(cell0Grams+cell1Grams)/(cell0Grams+cell1Grams+cell2Grams+cell3Grams) * 240;
181 r = (cell0Grams+cell1Grams+cell2Grams+cell3Grams)/500.0 * 120.0;
182
183 GUI_ClearRect(0, 0, 240, 240);
184 GUI_FillCircle(x,y,r);
185 if (((cell0Grams+cell1Grams+cell2Grams+cell3Grams)<1) && ((cell0Grams+cell1Grams+cell2Grams+cell3Grams)>-1))
186 GUI_ClearRect(0, 0, 240, 240);
187
188 char string5[200];
189
190 sprintf(string5, "Smart Force Sensor\nLoad1=%6.1f g Load4=%6.1f g\nLoad2=%6.1f g Load3=%6.1f g\nTotal Load=%6.1f g",
191 cell0Grams, cell2Grams, cell1Grams, cell3Grams, cell0Grams+cell1Grams+cell2Grams+cell3Grams);
192 //GUI_RECT Rect = {241, 0, 320, 240};
193 Rect.x0 = 241;
194 Rect.y0 = 0;
195 Rect.x1 = 320;
196 Rect.y1 = 240;
197 //char acText[] = &string1[0];
198 GUI_SetTextMode(GUI_TM_XOR);
199 GUI_FillRectEx(&Rect);
200 //GUI_DispStringInRectEx(acText, &Rect, GUI_TA_HCENTER | GUI_TA_VCENTER, strlen(acText), GUI_ROTATE_CW);
201 GUI_DispStringInRectEx(string5, &Rect, GUI_TA_HCENTER | GUI_TA_VCENTER, strlen(string5), GUI_ROTATE_CW);
202
203
204 // send data to PC
205 if(request == 0xFF) // host request handshake
206 {
207
208 request = 0; // clear
209 usbTxBuf[0] = 0xFE;
210 usbTxBuf[1] = MAJORVERSION;
211 usbTxBuf[2] = MINORVERSION;
212
213 USBD_CDC_SetTxBuffer(&USBD_Device, &usbTxBuf[0], 32);
214 if(USBD_Device.dev_state == USBD_STATE_CONFIGURED)
215 USBD_CDC_TransmitPacket(&USBD_Device);
216
217 }
218 else if (request == 0xFD) // host request data
219 {
220 usbTxBuf[0] = 0xFC;
221
222 usbTxBuf[1] = (cell0Grams > 0)? 1:0;
223 usbTxBuf[2] = abs(cell0Grams)/100;
224 usbTxBuf[3] = abs(cell0Grams)%100;
225 usbTxBuf[4] = abs(cell0Grams*10.0)%10;
226
227 usbTxBuf[5] = (cell1Grams > 0)? 1:0;
228 usbTxBuf[6] = abs(cell1Grams)/100;
229 usbTxBuf[7] = abs(cell1Grams)%100;
230 usbTxBuf[8] = abs(cell1Grams*10.0)%10;
231
232 usbTxBuf[9] = (cell3Grams > 0)? 1:0;
233 usbTxBuf[10] = abs(cell3Grams)/100;
234 usbTxBuf[11] = abs(cell3Grams)%100;
235 usbTxBuf[12] = abs(cell3Grams*10.0)%10;
236
237 usbTxBuf[13] = (cell2Grams > 0)? 1:0;
238 usbTxBuf[14] = abs(cell2Grams)/100;
239 usbTxBuf[15] = abs(cell2Grams)%100;
240 usbTxBuf[16] = abs(cell2Grams*10.0)%10;
241
242 usbTxBuf[17] = (x > 0)? 1:0;
243 usbTxBuf[18] = abs(x)/100;
244 usbTxBuf[19] = abs(x)%100;
245
246 usbTxBuf[20] = (y > 0)? 1:0;
247 usbTxBuf[21] = abs(y)/100;
248 usbTxBuf[22] = abs(y)%100;
249
250 usbTxBuf[23] = (r > 0)? 1:0;
251 usbTxBuf[24] = abs(r)/100;
252 usbTxBuf[25] = abs(r)%100;
253
254 USBD_CDC_SetTxBuffer(&USBD_Device, &usbTxBuf[0], 32);
255
256 if(USBD_Device.dev_state == USBD_STATE_CONFIGURED)
257 USBD_CDC_TransmitPacket(&USBD_Device);
258
259 }
260 else if (request == 0xFB) // host request stop streaming
261 request = 0;
262
263 // check if SW2 is pressed.
264 if(HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_8) == GPIO_PIN_RESET)
265 // factory calibration.
266 {
267 char string[] = "Factory Calibration!\n\n\
268 If You Are Not\n\n\
269 Factory Calibrator\n\n\
270 Release\n\n\
271 ***SW2***\n\n\
272 Immediately!\n\n\n\
273 Otherwise, Hold On\n\n\
274 For 5 Seconds\n\n\
275 Then Release SW2";
276 GUI_Clear();
277 Rect.x0 = 0;
278 Rect.y0 = 0;
279 Rect.x1 = 320;
280 Rect.y1 = 240;
281 GUI_SetTextMode(GUI_TM_XOR);
282 GUI_FillRectEx(&Rect);
283 GUI_DispStringInRectEx(string, &Rect, GUI_TA_HCENTER | GUI_TA_VCENTER, strlen(string), GUI_ROTATE_CW);
284 HAL_Delay(5000);
285
286 if(HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_8) != GPIO_PIN_SET)
287 {
288 // start after SW2 is released, otherwise, type 1 "PRESS SW2 WHEN READY" SCREEN will be missed.
289 while(1)
290 {
291 if(HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_8) == GPIO_PIN_SET)
292 break;
293 }
294
295 Calibration(1, FACOTRY_CAL_NUM);
296 Calibration(2, FACOTRY_CAL_NUM);
297 Calibration(3, FACOTRY_CAL_NUM);
298 Calibration(4, FACOTRY_CAL_NUM);
299 Calibration(5, FACOTRY_CAL_NUM);
300
301 cell0Load0<0? (cell0Load0Sign=1):(cell0Load0Sign=0);
302 cell0Load0Int = abs(cell0Load0);
303 cell1Load0<0? (cell1Load0Sign=1):(cell1Load0Sign=0);
304 cell1Load0Int = abs(cell1Load0);
305 cell2Load0<0? (cell2Load0Sign=1):(cell2Load0Sign=0);
306 cell2Load0Int = abs(cell2Load0);
307 cell3Load0<0? (cell3Load0Sign=1):(cell3Load0Sign=0);
308 cell3Load0Int = abs(cell3Load0);
309
310 cell0Load700<0? (cell0Load700Sign=1):(cell0Load700Sign=0);
311 cell0Load700Int = abs(cell0Load700);
312 cell1Load700<0? (cell1Load700Sign=1):(cell1Load700Sign=0);
313 cell1Load700Int = abs(cell1Load700);
314 cell2Load700<0? (cell2Load700Sign=1):(cell2Load700Sign=0);
315 cell2Load700Int = abs(cell2Load700);
316 cell3Load700<0? (cell3Load700Sign=1):(cell3Load700Sign=0);
317 cell3Load700Int = abs(cell3Load700);
318
319 /* Unlock the Flash to enable the flash control register access *************/
320 HAL_FLASH_Unlock();
321 /* Fill EraseInit structure*/
322 EraseInitStruct.TypeErase = FLASH_TYPEERASE_SECTORS;
323 EraseInitStruct.VoltageRange = FLASH_VOLTAGE_RANGE_3;
324 EraseInitStruct.Sector = FLASH_SECTOR_12;
325 EraseInitStruct.NbSectors = 1;
326 if(HAL_FLASHEx_Erase(&EraseInitStruct, &SectorError) != HAL_OK)
327 ErrorHandler();
328
329 if(HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, 0x08100000, cell0Load0Sign) != HAL_OK)
330 ErrorHandler();
331 if(HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, 0x08100008, cell1Load0Sign) != HAL_OK)
332 ErrorHandler();
333 if(HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, 0x08100010, cell2Load0Sign) != HAL_OK)
334 ErrorHandler();
335 if(HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, 0x08100018, cell3Load0Sign) != HAL_OK)
336 ErrorHandler();
337 if(HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, 0x08100020, cell0Load700Sign) != HAL_OK)
338 ErrorHandler();
339 if(HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, 0x08100028, cell1Load700Sign) != HAL_OK)
340 ErrorHandler();
341 if(HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, 0x08100030, cell2Load700Sign) != HAL_OK)
342 ErrorHandler();
343 if(HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, 0x08100038, cell3Load700Sign) != HAL_OK)
344 ErrorHandler();
345 if(HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, 0x08100004, cell0Load0Int) != HAL_OK)
346 ErrorHandler();
347 if(HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, 0x0810000C, cell1Load0Int) != HAL_OK)
348 ErrorHandler();
349 if(HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, 0x08100014, cell2Load0Int) != HAL_OK)
350 ErrorHandler();
351 if(HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, 0x0810001C, cell3Load0Int) != HAL_OK)
352 ErrorHandler();
353 if(HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, 0x08100024, cell0Load700Int) != HAL_OK)
354 ErrorHandler();
355 if(HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, 0x0810002C, cell1Load700Int) != HAL_OK)
356 ErrorHandler();
357 if(HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, 0x08100034, cell2Load700Int) != HAL_OK)
358 ErrorHandler();
359 if(HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, 0x0810003C, cell3Load700Int) != HAL_OK)
360 ErrorHandler();
361
362 HAL_FLASH_Lock();
363
364 } // factory calibartion
365 } // factory calibration
366 } // while(1)
367 } // MainTask()
368
369
370 void ConfigureSPIandGPIO(void)
371 {
372 // Configure U1_RSTB pin: PG14
373 GPIO_InitTypeDef GPIO_InitStructure;
374 GPIO_InitStructure.Pin = GPIO_PIN_14;
375 GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
376 GPIO_InitStructure.Pull = GPIO_NOPULL;
377 GPIO_InitStructure.Speed = GPIO_SPEED_FAST;
378 HAL_GPIO_Init(GPIOG, &GPIO_InitStructure);
379
380 HAL_GPIO_WritePin(GPIOG, GPIO_PIN_14, GPIO_PIN_RESET);
381 HAL_Delay(1);
382 HAL_GPIO_WritePin(GPIOG, GPIO_PIN_14, GPIO_PIN_SET);
383
384 // Configure U1_RDYB pin: PG13
385 GPIO_InitStructure.Pin = GPIO_PIN_13;
386 GPIO_InitStructure.Mode = GPIO_MODE_INPUT;
387 GPIO_InitStructure.Pull = GPIO_PULLUP;
388 GPIO_InitStructure.Speed = GPIO_SPEED_FAST;
389 HAL_GPIO_Init(GPIOG, &GPIO_InitStructure);
390
391 // Configure U1_CSB pin: PA15
392 GPIO_InitStructure.Pin = GPIO_PIN_15;
393 GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
394 GPIO_InitStructure.Pull = GPIO_NOPULL;
395 GPIO_InitStructure.Speed = GPIO_SPEED_FAST;
396 HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);
397
398 // Configure SW2, PA8
399 GPIO_InitStructure.Pin = GPIO_PIN_8;
400 GPIO_InitStructure.Mode = GPIO_MODE_INPUT;
401 GPIO_InitStructure.Pull = GPIO_PULLUP;
402 GPIO_InitStructure.Speed = GPIO_SPEED_FAST;
403 HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);
404
405 SPIx_Init();
406 }
407
408 void ConfigureMAX11254(void)
409 {
410 //SEQ
411 HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, GPIO_PIN_RESET);
412 spiWritePacket[0] = 0xD0;
413 spiWritePacket[1] = 0x0B;
414 SPIx_Write_2(spiWritePacket, 2);
415 HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, GPIO_PIN_SET);
416
417 //DELAY
418 HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, GPIO_PIN_RESET);
419 spiWritePacket[0] = 0xCA;
420 spiWritePacket[1] = 0xFF;
421 spiWritePacket[2] = 0x00;
422 SPIx_Write_2(spiWritePacket, 3);
423 HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, GPIO_PIN_SET);
424
425 //CTRL3
426 HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, GPIO_PIN_RESET);
427 spiWritePacket[0] = 0xC6;
428 spiWritePacket[1] = 0x1C;
429 SPIx_Write_2(spiWritePacket, 2);
430 HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, GPIO_PIN_SET);
431
432 //CHMAP0
433 HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, GPIO_PIN_RESET);
434 spiWritePacket[0] = 0xCE;
435 spiWritePacket[1] = 0x0B;
436 spiWritePacket[2] = 0x27;
437 spiWritePacket[3] = 0x4F;
438 SPIx_Write_2(spiWritePacket, 4);
439 HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, GPIO_PIN_SET);
440
441 //CHMAP1
442 HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, GPIO_PIN_RESET);
443 spiWritePacket[0] = 0xCC;
444 spiWritePacket[1] = 0x00;
445 spiWritePacket[2] = 0x00;
446 spiWritePacket[3] = 0x73;
447 SPIx_Write_2(spiWritePacket, 4);
448 HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, GPIO_PIN_SET);
449
450 //CTRL2
451 HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, GPIO_PIN_RESET);
452 spiWritePacket[0] = 0xC4;
453 // gain = 128 or 0?
454 //spiWritePacket[1] = 0x20;
455 spiWritePacket[1] = 0x2F;
456 SPIx_Write_2(spiWritePacket, 2);
457 HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, GPIO_PIN_SET);
458
459
460 //CTRL1
461 HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, GPIO_PIN_RESET);
462 spiWritePacket[0] = 0xC2;
463 // input=bipolar, fromat=offset binary, SCYCLE=1, CONTSC=0.
464 spiWritePacket[1] = 0x06;
465 SPIx_Write_2(spiWritePacket, 2);
466 HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, GPIO_PIN_SET);
467 }
468
469 void ReadMAX11254Results(void)
470 {
471 // ch0
472 spiWritePacket[0]= 0xDD;
473 HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, GPIO_PIN_RESET);
474 SPIx_WriteRead_2(spiWritePacket, data0, 4); //ch0
475 HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, GPIO_PIN_SET);
476
477 data0BO = ((data0[1] << 16) + (data0[2] << 8) + data0[3]);
478 cell0Load = data0BO - 0x800000;
479
480 // ch1
481 spiWritePacket[0]= 0xDF;
482 HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, GPIO_PIN_RESET);
483 SPIx_WriteRead_2(spiWritePacket, data1, 4);
484 HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, GPIO_PIN_SET);
485
486 data1BO = ((data1[1] << 16) + (data1[2] << 8) + data1[3]);
487 cell1Load = data1BO - 0x800000;
488
489 // ch2
490 spiWritePacket[0]= 0xE1;
491 HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, GPIO_PIN_RESET);
492 SPIx_WriteRead_2(spiWritePacket, data2, 4);
493 HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, GPIO_PIN_SET);
494
495 data2BO = ((data2[1] << 16) + (data2[2] << 8) + data2[3]);
496 cell2Load = data2BO - 0x800000;
497
498 // ch3
499 spiWritePacket[0]= 0xE3;
500 HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, GPIO_PIN_RESET);
501 SPIx_WriteRead_2(spiWritePacket, data3, 4);
502 HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, GPIO_PIN_SET);
503
504 data3BO = ((data3[1] << 16) + (data3[2] << 8) + data3[3]);
505 cell3Load = data3BO - 0x800000;
506 }
507
508 // type: 0-powerup, 1-factory 0 load, 2-cell0 700 g,
509 // 3-cell1 700g, 4-cell2 700g, 5-cell3 700g.
510 void Calibration(int8_t type, int32_t conversions)
511 {
512
513 char string00[] = "Powerup Calibration:\n\n\n\n\n\
514 Remove Everything\n\n\
515 On The Top\n\n\n\
516 Place The Board\n\n\
517 On A Flat Surface\n\n\n\
518 Press SW2 When Ready";
519
520 char string01[] = "Powerup Calibration:\n\n\n\n\n\
521 Remove Everything\n\n\
522 On The Top\n\n\n\
523 Place The Board\n\n\
524 On A Flat Surface\n\n\n\
525 Do Not Touch The Board";
526
527 char string10[] = "Factory 0 Load Calibration:\n\n\n\n\n\
528 Remove Everything\n\n\
529 On The Sensors\n\n\n\
530 Place The Board\n\n\
531 On A Flat Surface\n\n\n\
532 Press SW2 When Ready";
533
534 char string11[] = "Factory 0 Load Calibration:\n\n\n\n\n\
535 Remove Everything\n\n\
536 On The Sensors\n\n\n\
537 Place The Board\n\n\
538 On A Flat Surface\n\n\n\
539 Do Not Touch The Board";
540
541 char string20[] = "Factory 700g Load Calibration:\n\n\n\n\n\
542 Place 700 gram Weight\n\n\
543 On The Load Cell --1--\n\n\n\
544 Place The Board\n\n\
545 On A Flat Surface\n\n\n\
546 Press SW2 When Ready";
547
548 char string21[] = "Factory 700g Load Calibration:\n\n\n\n\n\
549 Place 700 gram Weight\n\n\
550 On The Load Cell --1--\n\n\n\
551 Place The Board\n\n\
552 On A Flat Surface\n\n\n\
553 Do Not Touch The Board";
554
555 char string30[] = "Factory 700g Load Calibration:\n\n\n\n\n\
556 Place 700 gram Weight\n\n\
557 On The Load Cell --2--\n\n\n\
558 Place The Board\n\n\
559 On A Flat Surface\n\n\n\
560 Press SW2 When Ready";
561
562 char string31[] = "Factory 700g Load Calibration:\n\n\n\n\n\
563 Place 700 gram Weight\n\n\
564 On The Load Cell --2--\n\n\n\
565 Place The Board\n\n\
566 On A Flat Surface\n\n\n\
567 Do Not Touch The Board";
568
569 char string40[] = "Factory 700g Load Calibration:\n\n\n\n\n\
570 Place 700 gram Weight\n\n\
571 On The Load Cell --3--\n\n\n\
572 Place The Board\n\n\
573 On A Flat Surface\n\n\n\
574 Press SW2 When Ready";
575
576 char string41[] = "Factory 700g Load Calibration:\n\n\n\n\n\
577 Place 700 gram Weight\n\n\
578 On The Load Cell --3--\n\n\n\
579 Place The Board\n\n\
580 On A Flat Surface\n\n\n\
581 Do Not Touch The Board";
582
583 char string50[] = "Factory 700g Load Calibration:\n\n\n\n\n\
584 Place 700 gram Weight\n\n\
585 On The Load Cell --4--\n\n\n\
586 Place The Board\n\n\
587 On A Flat Surface\n\n\n\
588 Press SW2 When Ready";
589
590 char string51[] = "Factory 700g Load Calibration:\n\n\n\n\n\
591 Place 700 gram Weight\n\n\
592 On The Load Cell --4--\n\n\n\
593 Place The Board\n\n\
594 On A Flat Surface\n\n\n\
595 Do Not Touch The Board";
596
597 char *string0, *string1;
598 Rect.x0 = 0;
599 Rect.y0 = 0;
600 Rect.x1 = 320;
601 Rect.y1 = 240;
602
603 switch (type)
604 {
605 case 0:
606 string0 = string00;
607 string1 = string01;
608 break;
609 case 1:
610 string0 = string10;
611 string1 = string11;
612 break;
613 case 2:
614 string0 = string20;
615 string1 = string21;
616 break;
617 case 3:
618 string0 = string30;
619 string1 = string31;
620 break;
621 case 4:
622 string0 = string40;
623 string1 = string41;
624 break;
625 case 5:
626 string0 = string50;
627 string1 = string51;
628 break;
629 default:
630 ;
631 }
632 GUI_Clear();
633 GUI_SetTextMode(GUI_TM_XOR);
634 GUI_FillRectEx(&Rect);
635 GUI_DispStringInRectEx(string0, &Rect, GUI_TA_HCENTER | GUI_TA_VCENTER, strlen(string0), GUI_ROTATE_CW);
636
637 // wait for SW2 being pressed
638 while(1)
639 {
640 if(HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_8) == GPIO_PIN_RESET)
641 break;
642 }
643 GUI_Clear();
644 GUI_SetTextMode(GUI_TM_XOR);
645 GUI_FillRectEx(&Rect);
646 GUI_DispStringInRectEx(string1, &Rect, GUI_TA_HCENTER | GUI_TA_VCENTER, strlen(string1), GUI_ROTATE_CW);
647
648 GUI_Delay(5000); // wait for the board to be stable.
649
650 int32_t loop = 0;
651 double cell0Total = 0, cell1Total = 0, cell2Total = 0, cell3Total = 0;
652 for(loop = 0; loop < conversions; loop++)
653 {
654 // start one conversion.
655 HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, GPIO_PIN_RESET);
656 spiWritePacket[0] = 0xB4; // 200 SPS
657 SPIx_Write_2(spiWritePacket, 1);
658 HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, GPIO_PIN_SET);
659
660 // wait for conversion to complete.
661 while(1)
662 {
663 if(HAL_GPIO_ReadPin(GPIOG, GPIO_PIN_13) == GPIO_PIN_RESET)
664 break;
665 }
666 BSP_LED_Toggle(LED3);
667
668 ReadMAX11254Results();
669
670 cell0Total += cell0Load;
671 cell1Total += cell1Load;
672 cell2Total += cell2Load;
673 cell3Total += cell3Load;
674 }
675
676 switch (type)
677 {
678 case 0:
679 cell0Powerup = cell0Total/conversions;
680 cell1Powerup = cell1Total/conversions;
681 cell2Powerup = cell2Total/conversions;
682 cell3Powerup = cell3Total/conversions;
683 break;
684 case 1:
685 cell0Load0 = cell0Total/conversions;
686 cell1Load0 = cell1Total/conversions;
687 cell2Load0 = cell2Total/conversions;
688 cell3Load0 = cell3Total/conversions;
689 break;
690 case 2:
691 cell0Load700 = cell0Total/conversions; // LOADCELL1=cell0
692 break;
693 case 3:
694 cell1Load700 = cell1Total/conversions; // LOADCELL2=cell1
695 break;
696 case 4:
697 cell3Load700 = cell3Total/conversions; // LOADCELL3=cell3
698 break;
699 case 5:
700 cell2Load700 = cell2Total/conversions; // LOADCELL4=cell2
701 break;
702 default:
703 ;
704 }
705 }
706
707 static void ErrorHandler(void)
708 {
709 while(1)
710 {
711 HAL_Delay(200);
712 BSP_LED_Toggle(LED2);
713 }
714 }
715 /*************************** End of file ****************************/