1. Whenever we need to consider the preparation of the program from the perspective of practical application.
2. Don't waste time waiting for the CPU at any time, especially for delays (over 1MS).
3. When designing the corresponding driver circuit, the data sheet of the chip should be carefully read to understand the driving ability of each pin and the driving ability of the entire chip.
4. The most important thing is how to release the CPU (if it is led flashing once every 500ms, then you can set 1ms as the benchmark. After the timer is set to 1ms, enter the corresponding operation to increase the count by 1. When the judgment reaches 500, enter led(). The execution time of these functions is quite short. If there are other functions in the main program, the CPU will execute them sequentially. For other functions (if any), the same measures must be taken to ensure that they do not block the CPU. If all are implemented based on this method, our small system can still guarantee multiple tasks (multiple functions) to be performed at the same time, and the real-time nature of the system is guaranteed.) This is the basis for writing qualified programs.
5. The digital display shows that the dynamic display brightness is slightly worse than the static display, so the current limiting resistor should be slightly smaller than the static display circuit. The dynamic scan display refresh frequency is preferably greater than 50HZ, ie the time for each display round is not more than 20ms, each digital display time can not be too long or too short, too long time will affect the refresh rate, resulting in the overall display flash phenomenon, too short time LED light conduction time is short, will Affect the overall display brightness, generally control the best around 1ms.
6. Modular programming, beginner MCU time (beginning with C language), is to contact some chips to achieve independent functions, such as DS1302, DS18B20, etc., the program will not be great, so all the programs are placed in a file inside. With the gradual deepening of learning, the program is getting larger and larger. This brings certain difficulties to debugging. Later, we learned about the concept of modular programming, and greatly improved the readability and portability of the program. The following brief introduction
C language source file *.c Slightly larger design will involve a large number of modules, we can be packaged into different *.c source files by separate modules, the module function is defined in the file, the declaration part is generally not put
C language header file *.h put each module's declaration file (said module interface is more appropriate), in the corresponding *.h header file, the corresponding module corresponds to the corresponding .h header file.
#ifndef __DS1302_H__
#define __DS1302_H__
/* Declaration of functions, etc. in the module ds1302.c file */
#endif
//The above few conditional compilations and macro definitions are to prevent duplicate inclusion
In this way, we bring the interface functions of each module to their respective header files, and then call the *.h header files of these modules in the main program. This is similar to the C++ class.
When defining data types, sometimes using typedefs will achieve good results.
Speaking of modular programming, we have seen many examples of prawn before. Their source files and output download files are placed in different folders under the project folder. For example, the source files are placed in the src folder and output. Hex is placed in the output folder so that the entire project becomes clearer and clearer.
7. Multi-tasking program, this concept has also been mentioned in the front, is to fully use the CPU to achieve multi-tasking before and after the operation, equivalent to their own program to construct a multi-tasking operating system based on the front and back, in other words, through the timing The interrupter properly allocates CPU resources to respond to different tasks. The frequency with which multiple tasks need to be taken care of by the CPU is not the same. We choose the fastest frequency as the timer beat (usually the LCM, which facilitates timer assignment), and then divide the frequency by the timer, ie, to satisfy each task The response to the beat. For example, task A frequency is 50HZ, task B is 40HZ, so we can go to the timer interrupt cycle is 200HZ, each task set a beat control counter C, when C counts 4 times, the task switches to A, count to 5 times to switch B...
At the same time, we also clearly know that some tasks take a long time, such as the DS18B20 temperature sensor to complete a temperature conversion takes a very long time (compared to the us level of the microcontroller), so that we can divide the device-driven tasks in the For: initial segment, startup segment, acquisition result segment, and other subtask segments, the flag bit (switch + case statement) can be set separately, where it takes a long time to wait, allowing the CPU to perform the remaining tasks, in addition to the main Breaks are added to the program task rotation to prioritize tasks.
DC Permanent Magnet Gear Motor
Dc Motor,24V Dc Gear Motor,Electric Gear Motor,Dc Permanent Magnet Gear Motor
NingBo BeiLun HengFeng Electromotor Manufacture Co.,Ltd. , https://www.hengfengmotor.com