Different from the general form of software programming, embedded system programming on a specific hardware platform, it is bound to require its programming language has a strong direct operating ability of the hardware. Undoubtedly, the assembly language has such a characteristic. However, due to the complexity of the assembly language development process, it is not a general choice for embedded system development. Compared with this, C language-a "high-level low-level" language, is the best choice for embedded system development. In the process of developing embedded system project, I feel the subtlety of C language again and again, indulge in the convenience of C language to the embedded development.
Figure 1 shows the hardware platform based on the discussion in this article, which is actually the hardware platform of most embedded systems. It consists of two parts:
(1) A universal processor-centric Protocol processing module for network Control Protocol processing;
(2) Digital signal Processor (DSP) as the center of the signal Processing module for modulation, demodulation and/or analog signal conversion.
The discussion in this article revolves around a general-purpose processor-centric protocol processing module because it involves more specific C language programming techniques. While DSP programming focuses on the specific digital signal processing algorithms, mainly related to the field of communication knowledge, not the focus of this article.
Focusing on the general embedded system C programming skills, the system's protocol processing module does not choose a special CPU, but chose the well-known CPU chip--80186, each of the readers of the "microcomputer principle" should have a basic understanding of the chip, and its instruction set more familiar. 80186 word length is 16 bits, can be addressed to the memory space of 1MB, only real address mode. C language compiler generated pointers to 32-bit (two-word), high 16-bit for the segment address, low 16-bit for the paragraph compiled, a maximum of 64KB.
Figure 1 System Hardware architecture
The flash and RAM in the Protocol processing module are almost the necessary equipment for each embedded system, the former is used for storing the program, and the latter is the location of the program runtime instruction and data. The system selects Flash and RAM with a bit width of 16 bits, which is consistent with the CPU.
The real-time clock chip can be the system timing, given the current year, month, Day and time (hours, minutes, seconds and milliseconds), you can set its after a period of time to the CPU to interrupt or set alarm time to the CPU to interrupt (similar to the alarm clock function).
NVRAM (Non-volatile RAM) has the characteristics of losing power and not losing data, and can be used to save setup information of the system, such as network protocol parameters. The previous setup information can still be read after the system has been dropped or restarted. Its bit width is 8 bits, smaller than the CPU word length. This paper deliberately chooses a memory chip which is inconsistent with the CPU word length, and creates the conditions for the discussion in the later section.
UART completes the CPU parallel data transmission and the RS-232 serial data transmission transformation, it may receive the [1~max_buffer] byte to interrupt to the CPU, Max_buffer receives the byte maximum buffer for the UART chip storage.
The keyboard controller and the display controller complete the System Man-machine interface control.
The above provides a more complete embedded system hardware architecture, the actual system may contain fewer peripherals. The reason why we choose a complete system is to discuss all aspects of the embedded system C programming skills in a more comprehensive way, all the devices will become the analysis target after the text.
Embedded system needs good software development environment support, because embedded system target machine resource is limited, it is impossible to build large and complex development environment on it, so its development environment and target running environment are separated from each other. Therefore, embedded application software development is generally, in the host (host) on the establishment of the development environment, the application code and cross compilation, and then host to the target machine (target) to establish a connection, the application download to the target machine for cross debugging, after debugging and optimization, Finally, the application is solidified into the target machine to actually run.
Cad-ul is an embedded application development environment for the x86 processor that runs on top of the Windows operating system, generates the target code for the x86 processor, and downloads it to the target computer via the PC's COM port (RS-232 serial port) or Ethernet port, as shown in Figure 2. The monitor program, which resides in the target flash memory, can monitor the user debugging instructions on the host Windows debugging platform, get the value of the CPU register and the contents of the target machine storage space and I/O space.
Figure 2 Cross-development environment
The following chapters will explain the programming techniques of C-language embedded system from the aspects of software architecture, memory operation, screen operation, keyboard operation and performance optimization. Software architecture is a macroscopic concept, there is little connection with specific hardware; Memory operations mainly involve Flash, RAM and NVRAM chips in the system, while screen operation involves display controller and real clock, keyboard operation mainly involves keyboard controller, and performance optimization gives some specific techniques to reduce program time and space consumption.
In our cultivation journey will pass 25 passes, these gateway main divides into two kinds, one kind is the skill type, has the very strong applicability, the class is the common sense type, theoretically has the significance.
So, let ' s go.