How MCU Drives LCD Screen and Design Considerations
Classification and application of single chip microcomputer
According to its memory type, MCU can be divided into two types without on-chip ROM and with on-chip ROM. For chips without on-chip ROM, they must be connected with an external EPROM (typically 8031); chips with on-chip ROM are further divided into on-chip EPROM (typically 87C51), MASK on-chip mask ROM (typically 87C51) Chip is 8051), on-chip Flash type (typical chip is 89C51) and other types.
According to the purpose, it can be divided into general-purpose and special-purpose; according to the width of the data bus and the length of data bytes that can be processed at one time, it can be divided into 8, 16, and 32-bit MCUs.
At present, the domestic MCU application market is the most widely used in the field of consumer electronics, followed by the industrial field and the automotive electronics market. Consumer electronics includes home appliances, televisions, game consoles, and audio and video systems, among others. Industrial fields include smart home, automation, medical applications, and new energy generation and distribution. The automotive field includes automotive powertrain and safety control systems, etc.
Shenzhen Hongjia Technology Co., Ltd. specializes in R&D, production and sales of 1.14-inch-10.1-inch LCD screens and touch screens, which can be customized, and provide supporting MCU displays, including SPI interface, MCU interface, RGB interface, MIPI interface, etc. There are many sizes and models, Matching resistive touch screen and capacitive touch screen can also be provided.
The basic function of single chip microcomputer
For most MCUs, the following functions are the most common and basic. For different MCUs, the description may be different, but they are basically the same in essence:
1. TImer (timer): Although there are many types of TImer, they can be classified into two categories: one is TImer with fixed time interval, that is, the timing is set by the system, and the user program cannot be controlled. Only several fixed time intervals are provided for user programs to choose, such as 32Hz, 16Hz, 8Hz, etc. This kind of TImer is more common in 4-bit MCUs, so it can be used to implement related functions such as clock and timing.
The other type is Programmable Timer (programmable timer). As the name suggests, the timing time of this type of Timer can be controlled by the user's program. The control methods include: selection of clock source, selection of frequency division (Prescale) and Prefabricated number setting, etc. Some MCUs have all three at the same time, while others may have one or two of them. This kind of Timer application is very flexible, and the actual use is also ever-changing. One of the most common applications is to use it to realize PWM output.
Since the clock source can be freely selected, such Timers are generally combined with Event Counters.
2. IO ports: Any MCU has a certain number of IO ports. Without IO ports, the MCU will lose the communication channel with the outside world. According to the configuration of the IO port, it can be divided into the following types:
Pure input or pure output port: This type of IO port is determined by the MCU hardware design. It can only be input or output, and cannot be set by software in real time.
Directly read and write IO ports: For example, the IO ports of MCS-51 belong to this type of IO ports. When executing the read IO port instruction, it is an input port; when executing a write IO port instruction, it is automatically an output port.
Program programming to set the input and output direction: the input or output of this type of IO port is set by the program according to the actual needs, the application is relatively flexible, and some bus-level applications can be realized, such as I2C bus, various LCDs, LED Drivers control bus, etc.
For the use of the IO port, the important point must be kept in mind: for the input port, there must be a clear level signal to ensure that it cannot be floating (it can be achieved by adding a pull-up or pull-down resistor); for the output port, its output The state level must consider its external connection, and it should be ensured that there is no current source or sink in the Standby or static state.
3. External interrupt: External interrupt is also a basic function of most MCUs. It is generally used for real-time triggering of signals, data sampling and status detection. There are several types of interrupts: rising edge, falling edge trigger and level trigger. External interrupts are generally implemented through input ports. If it is an IO port, the interrupt function will only be enabled when it is set to input; if it is an output port, the external interrupt function will be automatically turned off (there are some exceptions in the ATiny series of ATMEL, the output port can also trigger the interrupt function). The application of external interrupt is as follows:
Detection of external trigger signals: one is based on real-time requirements, such as the control of silicon controlled rectifiers, detection of burst signals, etc., and the other is the need for power saving.
Measurement of signal frequency: In order to ensure that the signal is not missed, an external interrupt is the ideal choice.
Data decoding: In the field of remote control applications, in order to reduce the design cost, it is often necessary to use software to decode various encoded data, such as the decoding of Manchester and PWM encoding.
Key detection and system wake-up: For an MCU that enters the Sleep state, it generally needs to be woken up through an external interrupt. The most basic form is a key, and the level change is generated by the action of the key.
4. Communication interface: The communication interface provided by MCU generally includes SPI interface, UART, I2C interface, etc., which are described as follows:
SPI interface: This type of interface is the most basic communication method provided by most MCUs. Its data transmission is controlled by a synchronous clock. The signals include: SDI (serial data input), SDO (serial data output), SCLK (serial clock) and Ready signal; in some cases, there may be no Ready signal; this type of interface can work in Master mode or Slave mode, the popular saying is to see who provides the clock signal, the party that provides the clock is the Master, and the opposite party Then it is Slaver.
UART (Universal Asynchronous Receive Transmit): It is the most basic asynchronous transmission interface. Its signal lines are only Rx and Tx. The basic data format is: Start Bit + Data Bit(7-bits/8-bits) + Parity Bit(Even, Odd or None) + Stop Bit(1~2Bit). The time taken by one bit of data is called Baud Rate (baud rate).
For most MCUs, the length of data bits, data check method (odd check, even check or no check), the length of stop bit (Stop Bit) and Baud Rate can be flexibly set through programming. Certainly. The most commonly used way of this type of interface is to communicate with the serial port of the PC.
I2C interface: I2C is a data transmission protocol developed by Philips, which is also implemented by two signals: SDAT (serial data input and output) and SCLK (serial clock). Its biggest advantage is that multiple devices can be connected to this bus, which can be identified and accessed through addresses; one of the biggest benefits of the I2C bus is that it is very convenient to use software to realize it through the IO port, and its transmission data rate is completely controlled by SCLK To control, it can be fast or slow, unlike the UART interface, which has strict speed requirements.
5. Watchdog (watchdog timer): Watchdog is also a basic configuration of most MCUs (some 4-bit MCUs may not have this function), and most MCU Watchdogs can only allow programs to reset them and cannot reset them. It is closed (some are set when the program is burned in, such as Microchip PIC series MCU), and some MCUs determine whether to open it in a specific way, such as Samsung's KS57 series, as long as the program accesses the Watchdog register, is automatically turned on and cannot be turned off again. Generally speaking, the reset time of watchdog can be set by program. The most basic application of Watchdog is to provide a self-recovery capability for the MCU to crash due to unexpected failures.
Microcontroller programming
There is a big difference between the programming of MCU programs and the programming of PC programs. Although C-based MCU development tools are becoming more and more popular, for an efficient program code and a designer who likes to use assembly, assembly language Still the most concise and efficient programming language.
For MCU programming, its basic framework can be said to be roughly the same, generally divided into three parts: initialization part (this is the biggest difference between MCU programming and PC programming), main program loop body and interrupt processing program, which are respectively explained as follows:
1. Initialization: For the design of all MCU programs, initialization is the most basic and important step, generally including the following:
Mask all interrupts and initialize the stack pointer: The initialization part generally does not want any interrupts to occur.
Clear the RAM area of the system and display Memory: Although sometimes it may not be completely necessary, from the perspective of reliability and consistency, especially to prevent accidental errors, it is recommended to develop good programming habits.
Initialization of IO port: According to the application requirements of the project, set the input and output mode of the relevant IO port. For the input port, you need to set its pull-up or pull-down resistance; for the output port, you must set its initial level output , to prevent unnecessary errors.
Interrupt settings: For all interrupt sources that need to be used in the project, they should be enabled and the trigger conditions for interrupts should be set, while for redundant interrupts that are not used, they must be turned off.
Initialization of other functional modules: For all the peripheral functional modules of the MCU that need to be used, corresponding settings must be made according to the application requirements of the project, such as UART communication, Baud Rate, data length, verification method and Stop Bit need to be set The length, etc., and for the Programmer Timer, you must set its clock source, frequency division and Reload Data, etc.
Parameter initialization: After completing the initialization of MCU hardware and resources, the next step is to initialize some variables and data used in the program. The initialization of this part needs to be designed according to the specific project and the overall arrangement of the program. For some applications that use EEPROM to save project prefabricated data, it is recommended to copy the relevant data to the RAM of the MCU during initialization to improve the access speed of the program to the data and reduce the power consumption of the system (in principle, access to the external EEPROM will increase the power consumption of the power supply).
2. The loop body of the main program: Most MCUs run continuously for a long time, so the main program body is basically designed in a cyclic manner. For applications with multiple working modes, there may be multiple A loop body is converted between each other through the state flag. For the main program body, the following modules are generally arranged:
Calculation program: The calculation program is generally time-consuming, so it is firmly opposed to any interrupt processing, especially multiplication and division operations.
Processing programs with low real-time requirements or no real-time requirements;
Display transmission program: mainly for applications with external LED and LCD Driver.
3. Interrupt processing program: The interrupt program is mainly used to process tasks and events with high real-time requirements, such as detection of external sudden signals, detection and processing of keys, timing counting, LED display scanning, etc.
In general, the interrupt program should keep the code as concise and short as possible. For functions that do not need to be processed in real time, you can set the trigger flag in the interrupt, and then the main program will execute the specific transaction—this is very important. Especially for low-power, low-speed MCUs, it is necessary to ensure timely response to all interrupts.
4. For the arrangement of different task bodies, different MCUs have different processing methods:
For example, for low-speed, low-power MCU (Fosc=32768Hz) applications, considering that such projects are all handheld devices and use ordinary LCD displays, the response to buttons and displays requires high real-time performance, so generally Timed interrupts are used to process button actions and data display; and for high-speed MCUs, such as Fosc>1MHz applications, since the MCU has enough time to execute the main program loop body at this time, it can only be interrupted in the corresponding Set various trigger flags in , and put all the tasks in the main program body to execute.
5. In the programming design of MCU, one more point that needs special attention is:
To prevent simultaneous access or setting of the same variable or data in the interrupt and the main program body. An effective preventive method is to arrange the processing of such data in a module, and determine whether to execute the relevant operation of the data by judging the trigger flag; while in other program bodies (mainly interrupts), the data that needs to be processed The processing place only sets the triggered flag. - This ensures that the execution of the data is predictable and unique.
Microcontroller Development Skills
1. How to reduce bugs in the program
For how to reduce program bugs, you should first consider the following over-range management parameters that should be considered during system operation.
Physical parameters: These parameters are mainly the input parameters of the system, including excitation parameters, operating parameters during acquisition and processing, and result parameters at the end of processing.
Resource parameters: These parameters are mainly the resources of circuits, devices, and functional units in the system, such as memory capacity, storage unit length, and stacking depth.
Application parameters: These application parameters often represent the application conditions of some single-chip microcomputers and functional units. Process parameters: Refers to the parameters that change in an orderly manner during the operation of the system.
2. How to improve the efficiency of C language programming code
It is an inevitable trend in the development and application of single-chip microcomputer to use C language to design the single-chip microcomputer program. If you want to achieve the highest efficiency when programming in C, it is best to be familiar with the C compiler you are using. First test the number of statement lines in assembly language corresponding to each C language compiled, so that you can clearly know the efficiency. When programming in the future, use the statement with the highest compilation efficiency. Each C compiler will have certain differences, so the compilation efficiency will also be different. The code length and execution time of an excellent embedded system C compiler are only 5-20% longer than the same function level written in assembly language.
For complex projects with tight development time, C language can be used, but the premise is that you are very familiar with the C language and C compiler of the MCU system, and pay special attention to the data types and algorithms that the C compiler system can support. Although C language is the most common high-level language, the C language compilation systems of different MCU manufacturers are different, especially in the operation of some special function modules. So if you don't understand these features, there will be a lot of problems in debugging, which will lead to lower execution efficiency than assembly language.
3. How to solve the anti-interference problem of the single-chip microcomputer The most effective way to prevent interference is to remove the interference source and cut off the interference path, but it is often difficult to do so, so it only depends on whether the anti-interference ability of the single-chip microcomputer is strong enough. While improving the anti-jamming ability of the hardware system, the software anti-jamming is characterized by its flexible design,