What should I do when the microcontroller encounters data from multiple modules that need to be processed?

Here, using a practical example, a small mistake often made by junior engineers, or a detour that often has to be taken, is targeted correction. I hope that I can help you, and the writing is not good. There are many places in the article that are unclear.

I don't want to say the normative thing of programming in this article. If you want your program file to be at least intuitive and beautiful, it is recommended to look for Huawei's "C language programming specification."

I just want to say that when our microcontroller encounters multiple modules of data that need to be processed, what should we do when it is similar to "multitasking"?

The background is like this. In September, an engineer began to arrange electric vehicle AC charging piles. The mechanical design part was handled by the company's mechanical structure department. The electronic part of the charging pile is generally divided into X parts (resources used), resistive touch screen (RS232), M1 card reading and writing (RS232), energy meter (RS485), voice prompt (SPI), power switch (relay) IO), communication interface (RS485, CAN).

The process of the engineers was very diligent, and the period was difficult. Many versions were changed. In June of the following year, the charging pile was finally set up.

Let's check it out. We found that we can't handle the touch screen when reading the card. We can't process the card when playing the voice. The voice play can't be interrupted or jumped. Anyway, all the events must be step by step. Once the operation is wrong, it needs more. Execution, waiting, and even coming back.

How can an engineer who has worked for more than 3 years make the product like this? Look at the program!

It doesn't matter if you look at it! The whole process is illogical, and one line writes down...

While(1){//Power on to enter the main program or trigger the touch screen // play prompt voice Delay(); // wait for playback to complete // read M1 card information Delay(); // wait for card data return/play Prompt voice Delay (); / / wait for the completion of the playback / / M1 card data interaction, determine the next operation and prompt Delay (); / / wait for the data processing is completed ... ...}

It is said that this engineer basically does not have any overall concept for the product he designed, or it is unclear what kind of practical effect it will have on the design of the program developed by himself.

He made a few of the most taboo problems we have in the process of developing programs:

1, delay (death, etc.) Such functions are only used in the process of verifying a function in the laboratory. In actual product development, whether it is in the main loop while, in the function it calls, or in the interrupt service routine. Can never be used.

2. The logical relationship between each sub-module of the product design is too strong. For example, you must wait for the broadcast to complete before you can read the card and proceed to the next step.

We say that only the logical relationship between each event processing module is weakened in product design, so that it can be processed more flexibly. For example: two events A and B, if the program is developed to make A necessary condition of B event, the trigger of B event must wait for the occurrence of A event. Conversely, if the A event is treated as a special case of the B event, then the program becomes much more flexible to develop.

3. It does not take into account that the microcontroller itself is a single-core single-task architecture. Each event will monopolize the CPU core. When multiple task modules exist at the same time, we should distinguish each event. We should divide the situation and the event real-time requirements. Treat it differently.

What should I do when the microcontroller encounters data from multiple modules that need to be processed?

So what should we do with this kind of problem, or when we encounter similar projects?

A few suggestions:

1. Divide the hardware system into independent units and make the underlying driver function and application function separately, and the function should have parameters and return values ​​normally, and the return value is necessary. How to measure such functions? This type of function is highly portable, as long as a .h file and a .c file are free to be placed in any project. For example: voice playback, M1 card reading, 485 processing, and so on.

2. Time evaluation of all functions in 1 with two evaluation points. One is the execution time t of the function, and the second is the time T at which the function periodically occurs. One of the most basic conditions is t < T, and the ideal case should be t << T.

3. Create a centralized logic processing function in which each function in 1 is scheduled. This function works like system scheduling in an embedded system. This scheduling is the scheduling of all event processing in the entire hardware logic. Its purpose is to complete a process, but never rely on the necessary processing of any event. This weakens the logical relationship between the events mentioned in question 2, and becomes very flexible in handling, so that the relationships are not necessary for each other.

4. In order to ensure the normal implementation of the previous content, it is necessary to establish a necessary time management function for the cycle of various events. The basis of the time function is generally completed by an internal timer interrupt. The cycle of the interrupt is generally considered by us. -10ms. According to actual needs, N timer interrupts are defined as an event processing period TT, which should ensure that all ts that may occur in the worst case are processed, and TT < T is guaranteed.

5. There are exceptions to this. Some events with high real-time requirements should be completed with interrupts. The processing event of the interrupt handler should be as short as possible. See time 2 for time requirements.

Absolute Encoder

Absolute rotary Encoder measure actual position by generating unique digital codes or bits (instead of pulses) that represent the encoder`s actual position. Single turn absolute encoders output codes that are repeated every full revolution and do not output data to indicate how many revolutions have been made. Multi-turn absolute encoders output a unique code for each shaft position through every rotation, up to 4096 revolutions. Unlike incremental encoders, absolute encoders will retain correct position even if power fails without homing at startup.

Absolute Encoder,Through Hollow Encoder,Absolute Encoder 13 Bit,14 Bit Optical Rotary Encoder

Jilin Lander Intelligent Technology Co., Ltd , https://www.jilinlandermotor.com

Posted on