In this article, I'll focus on a common mistake made by junior engineers or a frequent detour that they often take. I hope this can be helpful to you, though I'm aware that my writing isn't perfect and there are many unclear parts. I won't talk about programming standards in this piece. If you want your code to be at least intuitive and well-structured, I recommend checking out Huawei's "C Language Programming Specification." Instead, I want to share what to do when a microcontroller has to handle multiple data modules at once—something that feels like multitasking.
The background of this example is from an engineer who started working on electric vehicle AC charging piles in September. The mechanical design was handled by the company’s mechanical team, while the electronics were divided into several parts: a resistive touch screen (RS232), M1 card reader/writer (RS232), energy meter (RS485), voice prompt module (SPI), power switch (relay IO), and communication interfaces (RS485, CAN). The development process was tough, with many iterations. By June of the following year, the system was finally set up, but we found some serious issues.
For instance, the touch screen couldn’t be used while reading the card, and the voice prompt couldn’t be interrupted or skipped. Everything had to happen step by step, and if one operation went wrong, it caused delays, waiting, or even required restarting the process.
How could an engineer with over three years of experience end up with such a flawed product? Looking at the code, it was clear why. The entire program was written in a linear, unstructured way, with long delay calls blocking the CPU. For example:
while(1) { // Power on and enter main loop // Play voice prompt Delay(); // Wait for playback to finish // Read M1 card information Delay(); // Wait for card data return; // Play voice again Delay(); // Wait for playback // Process card data and decide next action Delay(); // ... and so on }
This kind of code shows a lack of overall system design. The engineer didn’t understand how to structure the software properly, leading to a rigid, non-responsive system. The program suffered from three major issues:
1. Using delay() functions in the main loop or interrupt service routines is a big no-no in real products. These should only be used during lab testing.
2. The logical dependencies between modules were too tight. For example, the system waited for the voice to finish before reading the card, which made it inflexible.
3. The microcontroller is a single-core, single-task device. When multiple tasks are running, each should be treated independently, with different priorities based on their real-time requirements.
So, how can we fix this? Here are a few practical suggestions:
1. Divide the hardware into independent modules. Keep the driver layer separate from the application layer. Each function should have parameters and return values, making them highly portable. For example, functions for voice playback, M1 card reading, or RS485 communication should be self-contained and reusable across projects.
2. Evaluate all functions based on two key metrics: execution time (t) and the period (T) at which the function runs. Ideally, t should be much smaller than T, ensuring smooth operation without bottlenecks.
3. Create a centralized scheduling function that manages all event processing. This function works like a mini operating system, handling events without relying on any one task being completed first. This reduces the tight coupling between events, making the system more flexible.
4. Implement a time management system using a timer interrupt, typically at 10ms intervals. Define time slots for event processing, ensuring that all critical operations can be handled within the allocated time.
5. Some high-priority tasks must be handled via interrupts. However, the interrupt handler should be as short as possible, adhering to the timing constraints defined earlier.
Off-Grid Solar Inverter,Home Use Solar Inverter,Single Phase Off Grid Inverter,Off Grid Inverter 5Kw
JIANGSU BEST ENERGY CO.,LTD , https://www.bestenergy-group.com