An introduction to electronic weighing
Storing Calibration Parameters
The design provides no non-volatile storage for persistent data such as calibration parameters. In a larger system, it is customary to have an external SPI flash memory or EEPROM chip to provide this function. The current design uses a small portion (one page, 1KB) of the microcontroller's program flash memory as non-volatile storage (Listing 5), and you can tell the linker to exclude a portion of this flash memory from program memory (usually a page at the highest address available). That block can then be labeled in the linker file so that it is accessible from program code, and primitives in the STM32 libraries can erase pages and read/write bytes or words from that area. This technique saves the expense of an external memory chip and the I/O pins that would be required to drive it. Listing 6 from the linker file shows how to reserve a portion of flash memory for parameter storage.
Listing 5
Nonvolatile Storage Code
01 /* 02 * nvdata.c 03 * 04 * Created on: 19 Mar 2021 05 * Author: andrew 06 */ 07 08 #include "main.h" 09 10 extern uint32_t _nvdata[1024]; // start of non-volatile data (last block of main flash) defined in linker file 11 12 /** */ 13 void NVDATA_SaveCalibrationParams(uint32_t span, uint32_t zero) 14 { 15 HAL_FLASH_Unlock(); 16 17 FLASH_EraseInitTypeDef eraseInit; 18 19 eraseInit.TypeErase = FLASH_TYPEERASE_PAGES; 20 eraseInit.NbPages = 1; 21 eraseInit.PageAddress = (uint32_t)&_nvdata[0]; 22 23 uint32_t sectorError; 24 printf("erasing flash page at %p\n", &_nvdata[0]); 25 HAL_StatusTypeDef res = HAL_FLASHEx_Erase(&eraseInit, §orError); 26 27 if (res != HAL_OK) 28 { 29 printf("FLASH erase error %d\n", res); 30 } 31 32 res = HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, (uint32_t)&_nvdata[0], span); 33 34 if (res != HAL_OK) 35 { 36 printf("FLASH program error %d\n", res); 37 } 38 39 res = HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, (uint32_t)&_nvdata[1], zero); 40 41 if (res != HAL_OK) 42 { 43 printf("FLASH program error %d\n", res); 44 } 45 46 HAL_FLASH_Lock(); 47 } 48 49 /** */ 50 void NVDATA_LoadCalibrationParams(uint32_t *span, uint32_t *zero) 51 { 52 *span = _nvdata[0]; 53 *zero = _nvdata[1]; 54 }
Listing 6
Linker File Extract
01 /* Entry Point */ 02 ENTRY(Reset_Handler) 03 04 /* Highest address of the user mode stack */ 05 _estack = ORIGIN(RAM) + LENGTH(RAM); /* end of "RAM" Ram type memory */ 06 _nvdata = ORIGIN(NVDATA); 07 08 _Min_Heap_Size = 0x200 ; /* required amount of heap */ 09 _Min_Stack_Size = 0x400 ; /* required amount of stack */ 10 11 /* Memories definition */ 12 MEMORY 13 { 14 RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 4K 15 FLASH (rx) : ORIGIN = 0x8000000, LENGTH = 15K //reduce flash by 1K 16 NVDATA (rx) : ORIGIN = 0x8003c00, LENGTH = 1K //define the nv region 17 } 18 19 /* Sections */ 20 SECTIONS 21 { 22 ### ommited ### 23 24 .nvdata : // give the nv region a name so it can be referenced from code 25 { 26 KEEP(*(.nvdata)) 27 } >NVDATA 28 29 ### ommited ### 30 }
Calibration
The design has two push buttons for calibration: one for tare and one for span. In the main acquisition loop (Listing 7), the code looks for push-button events. If the tare button is pushed, the current reading is stored and subtracted from subsequent readings to provide a zero output. Similarly, if the span button is pushed, the required span factor is calculated and stored (in this case, the code assumes a 100g calibration weight has been placed on the scale).
Listing 7
Calibration
01 int32_t zero = 0; 02 int32_t span = 1; 03 04 // main acquisition loop 05 while (true) 06 { 07 // get averaged data here 08 09 if (sw1PushedEvent()) 10 { 11 zero = average; 12 NVDATA_SaveCalibrationParams(span, zero); 13 } 14 15 if (sw2PushedEvent()) 16 { 17 raw = (average - zero) * 100; 18 19 span = raw / 1000; 20 21 NVDATA_SaveCalibrationParams(span, zero); 22 } 23 24 display = ((average * 100 - zero * 100) / span); 25 26 LCD_PrintNumber(display, 0); 27 }
Completed Unit
I used FreeCAD to design an enclosure for the completed instrument and had it printed by a local 3D printing house. FreeCAD is another open source package, and every time I return to FreeCAD, I find the developers have made another step increase in functionality. It really is an exemplary open source project. The STEP files created were sent directly to a local 3D print house and were printed in just a few days.
I purchased a suitable stainless steel pan with a tare weight of just 50g, and the completed assembly can be see in Figure 8. I've left off the front panel to show the mounting arrangement of the PCB and the display. The load cell is mounted behind, with a vertical riser coming up through an aperture in the enclosure. A small circular plastic plate is attached to that, on which the pan sits.
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
![Learn More](https://www.linux-magazine.com/var/linux_magazin/storage/images/media/linux-magazine-eng-us/images/misc/learn-more/834592-1-eng-US/Learn-More_medium.png)
News
-
NVIDIA Released Driver for Upcoming NVIDIA 560 GPU for Linux
Not only has NVIDIA released the driver for its upcoming CPU series, it's the first release that defaults to using open-source GPU kernel modules.
-
OpenMandriva Lx 24.07 Released
If you’re into rolling release Linux distributions, OpenMandriva ROME has a new snapshot with a new kernel.
-
Kernel 6.10 Available for General Usage
Linus Torvalds has released the 6.10 kernel and it includes significant performance increases for Intel Core hybrid systems and more.
-
TUXEDO Computers Releases InfinityBook Pro 14 Gen9 Laptop
Sporting either AMD or Intel CPUs, the TUXEDO InfinityBook Pro 14 is an extremely compact, lightweight, sturdy powerhouse.
-
Google Extends Support for Linux Kernels Used for Android
Because the LTS Linux kernel releases are so important to Android, Google has decided to extend the support period beyond that offered by the kernel development team.
-
Linux Mint 22 Stable Delayed
If you're anxious about getting your hands on the stable release of Linux Mint 22, it looks as if you're going to have to wait a bit longer.
-
Nitrux 3.5.1 Available for Install
The latest version of the immutable, systemd-free distribution includes an updated kernel and NVIDIA driver.
-
Debian 12.6 Released with Plenty of Bug Fixes and Updates
The sixth update to Debian "Bookworm" is all about security mitigations and making adjustments for some "serious problems."
-
Canonical Offers 12-Year LTS for Open Source Docker Images
Canonical is expanding its LTS offering to reach beyond the DEB packages with a new distro-less Docker image.
-
Plasma Desktop 6.1 Released with Several Enhancements
If you're a fan of Plasma Desktop, you should be excited about this new point release.