It can also be written in task0 (finally delete task0)
void
{
init_system();
os_create_task
os_create_task
os_delete_task
}
In the past month, I have been using my spare time to study the STC12A5C60S2 microcontroller and its applications to relax my nervous mood and mind after working on SharePoint every day. (It turns out that it would be strange if I could relax!)
After tinkering with LCD 1602, LCD 12864 (parallel and serial), PCF 8563 (clock chip), nRF24L01 (wireless communication), matrix keyboard, LN 298 (H-bridge motor driver), optocoupler counter, HC-SR 04 (ultrasonic ranging), I decided to give up the "idiot" "multitasking" program based on interrupts and timer loops and install an operating system. Otherwise, it would be almost impossible for me to write a slightly complex application later.
The following is the minimum system of the STC12A5C60S2 microcontroller I use. It comes with a USB-to-serial chip, all IO ports are brought out, and it is very convenient to burn and debug the chip:
This small board has its disadvantages, that is, there are no mounting holes, the whole board can only "float", and is fixed by the tension of the Dupont line (uh, that is, it is not fixed:), so when it is used on the car later, it can only be installed on the perforated board by welding the "bracket" at the bottom like this:
Choice of operating system
The first choice I considered was uc/OS-II, because it is well-known (I often see it), has powerful functions (it sounds good), and the book I bought also introduced this operating system.
The task scheduling of us/OS-II is a preemptive scheduling method based entirely on task priority. A low-priority task being executed can be interrupted by a high-priority task in the "READY" state. This can also be achieved in STC12A5C60S2 by setting the hardware interrupt priority, but it is impossible for me to interrupt every task. So I still have to use the operating system.
I looked up some information online and was stunned. us/OS-II is too complicated. No webpage can explain how to use it in a few paragraphs. Moreover, us/OC-II needs to be ported to STC12A5C60S2 (that is, you have to modify its source code to adapt to the special conditions of the hardware). Uh…
Then, I started to consider RTX-51. Because the C51 development tool Keil comes with an RTX-51 operating system and directly supports compilation and debugging in Keil, it is quite attractive:)
RTX-51 has 2 versions: Full and Tiny.
Full requires more resources, but supports preemptive task scheduling and interrupt tasks, as well as messaging between tasks; Tiny does not support preemptive scheduling, and tasks cannot send messages to each other, but consumes fewer resources.
Although the Full version is powerful, it takes up 8K ROM, and requires at least 450 bytes of xData! The STC12A5C60S2 only has 1024 bytes of on-chip xData RAM, and I also need xData as LCD display memory. After thinking about it, I decided to choose RTX-51 Tiny.
The Tiny version does not occupy xData and occupies very little RAM (7+3×number of tasks), which is quite suitable. As for the disadvantages, you can only know them after using them. The Tiny version is open source, and the source code is assembly. Someone in China wrote an operating system based on it, called
Chen Mingji, the author of Small RTOS 51, was unable to find a suitable MCU operating system and wrote this operating system in a rage. This operating system has the advantages of the Tiny version with less resource usage and supports preemptive task scheduling. (If you are unhappy, you create one. This seems to be the fundamental motivation for the emergence of many talented people and new technologies! So, when there is no good wheel to use, you have to invent a new wheel yourself.)
RTX-51 Tiny is very easy to use. I just browsed online and immediately found a usable guide ( MCU software design based on RTX51 ). The author wrote it very well. I understood it in 10 minutes and started working on it.
Build the environment
The project name is Wave.
First, create a good project directory structure. Each subdirectory should contain corresponding content. Otherwise, all files will be piled up under the project folder, and you will soon feel dizzy:
In the picture above,
- code put your own code
- lib stores the code of external libraries
- listing puts the intermediate files generated by Keil (memory address mapping of methods and variables, etc.)
- Output: HEX file generated by Keil
- document put document
This directory structure is different from the "directory" structure in Keil IDE (and does not need to be the same).
Then, open Keil IDE and create a new project, and put it in the directory just now:
Keil IDE has a trial version. The trial version has all the functions, the only limitation is that it can only compile target code within 2K.
[page]
Select the chip library, I use STC here:
There is no STC chip in Keil's default chip library. You can add the STC chip library to Keil by following the steps below:
- Download UV3.CDB from the Hongjing official website
- Rename UV3.CDB to STC.CDB and copy it to the UV4 subdirectory of the Keil installation directory
- Modify the TOOLS.ini file in the Keil installation directory and add the following above [C51]: CDB0=UV4STC.CDB("STC Chip")
- Create a new project, and then you can select the corresponding chip database
(Personal note: This part can be done with the latest version of STC-ISP, tab "keil simulation settings", click "add STC simulation driver to Keil, add model and header file to Keil", it's simple and easy)
Then, select the chip:
When asked if you want to automatically include the startup code, select "No" (unless you want to change the program start address or something):
Change the name of the Target to make it look better, and then set the project properties:
Enter the crystal frequency and select "RTX-51 Tiny" as the operating system, so that Keil will automatically compile the lib file into the output (it is better to use Small as the Memory Model to avoid writing data modifiers every time you declare variables):
Then select the output and listing directories to the directory structure created earlier:
Then, ignore the call warning message (this warning will remind you which functions are defined but not called, so annoying!):
Set the debugging options (STC12A5C60S2 supports ISD-51 online debugging, but it is not very useful in practice. The peripheral hardware module will not wait for you at the breakpoint. It is better to use software simulation debugging first):
Next, drag the commonly used library files into the lib directory:
- ISD51.h, ISD51.A51 are used for online debugging.
They conflict with serial port 0, so use with caution. - stc12c51a.h MCU built-in header file
- Conf_tny.A51
RTX-51 Tiny configuration program
Conf_tny.A51 still needs some modifications:
- INT_CLOCK, defines the number of clock cycles corresponding to each timer interrupt, the default is 10000, I changed it to 1000;)
This value will affect the length of the second parameter of os_wait. For example, ow_wait(K_TMO, 5, 0) waits for 5 timer cycles, that is, waits for 5000 clock cycles. - TIMESHARING, the number of clock interrupts assigned to each task. The default is 5.
In this way, each task will be assigned a task time of 5000 clock cycles. After the expiration, the task will be suspended and other tasks in the READY state will run.
If this value is 0, then the Round Robin task polling algorithm will stop, and you must manually os_send_signal or os_switch_task to switch tasks. Sometimes, this will improve real-time performance. - RAMTOP. Specifies the top address of the available RAM, the default is 0FFH, which is 256 bytes of RAM.
For the STC chip, this default setting is fine when the task code is very short, but if the task code is long, it is recommended to change it to a smaller value, such as 0CFH. The reason is unknown, but it may be because the direct access method of the upper 128 bytes of RAM is a special register for reading and writing. I found that it conflicts with RTX-51 Tiny and generates runtime errors such aserror 65 access violation at 0x3480 .
Type a piece of code and run it first (software debugging, Ctrl+F5):
You can see in the monitoring window that c0 and c1 are jumping randomly:
Here is another piece of code to see Keil's advanced functions (logic analysis):
Now you can see the waveform:
The environment is ready, remember to submit it to the configuration library. Now you can start porting the code to the new RTX-51 Tiny-based project.
Previous article:Summary of methods for setting header file paths in keil4
Next article:How to use IIC bus
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Huawei's Strategic Department Director Gai Gang: The cumulative installed base of open source Euler operating system exceeds 10 million sets
- Download from the Internet--ARM Getting Started Notes
- Learn ARM development(22)
- Learn ARM development(21)
- Learn ARM development(20)
- Learn ARM development(19)
- Learn ARM development(14)
- Learn ARM development(15)
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- Realization of voltage to current conversion in Multisim12
- 【Smart Network Desk Lamp】7. Get real-time weather information and analyze it
- [RISC-V MCU CH32V103 Review] +01 Try the use of GPIO to light up the first LED
- PCB layout optimization of multi-output buck converter based on phase-shift control to improve EMI performance
- [Raspberry Pi Pico Review] Small size, big use~PICO here I come~
- 12V 60Ah large capacity lithium battery
- Review summary: National Technology's new M4F core N32G430 is here~
- Can you tell me what the Cf2 capacitor does in the circuit? Thanks!
- Top 10 hot technologies in 2019: 5G ranks first, and "brain-computer interaction" achieves breakthrough
- DSP Demo Programming Example Sharing