An introduction to electronic weighing
Software Development
The use of IDEs can be controversial and very much a matter of taste, and it's certainly possible to do this type of microcontroller development without one. The ARM compilers and standard libraries can be downloaded from your distro's repository, and you're off, using any editor that suits you and Make or Cmake – again, your choice. Once you have a compiled binary, utilities for ST-Link allow you to program your device, and you can use GDB (the GNU debugger) to debug you program.
That said, ST's STM32CubeIDE, based on Eclipse, does streamline the process by integrating ST's CubeMX tool, a utility that allows you to configure your microcontroller and generate a software framework that does all the initialization and leaves you with a blank main()
function to add to your code. You can label the pins of the microcontroller (bonus points if you use the same names as on the schematic!).
The pin configuration for this design is shown in Figure 7. Once saved, the IDE generates a set of #define
directives for the I/O pins that you can use in your code and a complete set of initialization routines. At this point, you can continue to use the IDE or ignore it and use Make with the generated makefile. However, if you stay with the IDE and have your hardware connected by an ST-Link programmer, a single mouse click compiles, downloads, and runs your code. This level of preconfiguration – including, if you want, the inclusion of a real-time operating system (RTOS) such as FreeRTOS – can leave you free to concentrate on your application code. In a commercial environment, time to market is everything, and time savings like this can be invaluable.
Serial I/O and printf
My hardware design and microcontroller configuration includes a serial port for debugging. Being able to output data and wait for keystrokes during debugging can be invaluable as an alternative to, or as an adjunct to, a debugger. The STM32 libraries make it easy to override the low-level routines that printf
and scanf
eventually call and redirect them to a serial port (Listing 1). Once this is done, these functions are available from whatever terminal (e.g., the Linux minicom
tool) you choose to connect to the serial port. This serial port could also be used in an application for batch weighing, in which an external computer or programmable logic controller (PLC) monitors the weight signal on a continuous basis.
Listing 1
Overriding Low-Level I/O Primitives
01 /** read from the serial port */ 02 int _read(int file, char *ptr, int len) 03 { 04 HAL_UART_Receive(&huart1, (uint8_t *)ptr, 1, -1); 05 return 1; 06 } 07 08 /** write to the serial port */ 09 int _write(int file, char *ptr, int len) 10 { 11 int DataIdx; 12 13 for (DataIdx = 0; DataIdx < len; DataIdx++) 14 { 15 if (*ptr == '\n') 16 { 17 HAL_UART_Transmit(&huart1, (uint8_t *)"\r\n", 2, -1); 18 } 19 else 20 { 21 HAL_UART_Transmit(&huart1, (uint8_t *)ptr, 1, -1); 22 } 23 ptr++; 24 } 25 return len; 26 } 27 28 /** wait for a keystroke */ 29 uint8_t kbhit(void) 30 { 31 return __HAL_UART_GET_FLAG(&huart1, UART_FLAG_RXNE); 32 }
ADC Driver
The ADS1232 has a very simple two-wire serial interface with no configuration registers, so all setup (clock speed, gain, etc.) happens from pins on the chip. To read the data from the chip, the software must first wait for a valid sample, which it does by waiting for the DOUT (data out) line to go low (Listing 2). At this point, data is clocked out in a serial manner by pulsing the clock pin from low to high and reading the resulting data bit from DOUT. This process is repeated 24 times to clock out a complete 24-bit sample. A further clock pulse resets DOUT to its "data ready" function.
Listing 2
Reading the ADC
01 /** wait for ADC sample ready and read it out */ 02 uint32_t ADS1232_Read(void) 03 { 04 int32_t value = 0; 05 06 // wait for DOUT to go low : data ready 07 while (HAL_GPIO_ReadPin(ADC_NDRDY_DOUT_GPIO_Port, ADC_NDRDY_DOUT_Pin)) 08 ; 09 10 for (uint8_t i = 0; i < 24; i++) 11 { 12 value = value << 1; 13 14 // send clock pulse 15 HAL_GPIO_WritePin(ADC_SCLK_GPIO_Port, ADC_SCLK_Pin, 1); 16 HAL_GPIO_WritePin(ADC_SCLK_GPIO_Port, ADC_SCLK_Pin, 0); 17 18 // read the data in 19 if (HAL_GPIO_ReadPin(ADC_NDRDY_DOUT_GPIO_Port, ADC_NDRDY_DOUT_Pin)) 20 { 21 value |= 1; 22 } 23 } 24 25 // reset DOUT 26 HAL_GPIO_WritePin(ADC_SCLK_GPIO_Port, ADC_SCLK_Pin, 1); 27 HAL_GPIO_WritePin(ADC_SCLK_GPIO_Port, ADC_SCLK_Pin, 0); 28 29 // 24-32 bit 2s complement conversion 30 value <<= 8; 31 value /= 256; 32 33 return (value); 34 }
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.