服务热线
6
微控制器编程
MCU程序编写与PC程序编写之间存在着很大的差异。虽然基于C语言的MCU开发工具越来越受欢迎,但对于那些追求高效程序代码且喜欢使用汇编语言的设计人员来说,汇编语言仍然是最简洁高效的编程语言。
For MCU programming, the basic framework can be said to be roughly the same, which is generally divided into three parts: the initialization part (this is the biggest difference between MCU programming and PC), the main program loop body and the interrupt handler, which are explained as follows:
1、初始化:For the design of all MCU programs, initialization is the most basic and important step, which generally includes the following:
Mask all interrupts and initialize the stack pointer:The initialization part generally does not want any interrupts to occur.
Clear the system's RAM area and display Memory:Although sometimes it may not be completely necessary, from the perspective of reliability and consistency, especially to prevent unexpected 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 resistor; 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 turned on and the trigger conditions for the interrupt should be set, while redundant interrupts that are not used must be turned off.
Initialization of other functional modules:For all peripheral function modules of the MCU that need to be used, the corresponding settings must be made according to the requirements of the project application. For example, for UART communication, the Baud Rate, data length, verification method, and Stop Bit length need to be set. For the Programmer Timer, its clock source, frequency division number, and Reload Data must be set.
Initialization of parameters:After completing the initialization of the 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 program's access speed to data and reduce the power consumption of the system (in principle, accessing external EEPROM will increase the power consumption of the power supply).
2. Main program loop body:Most MCUs run continuously for a long time, so their main program bodies are basically designed in a loop. For applications with multiple working modes, there may be multiple loop bodies, which are converted through status flags. For the main program body, the following modules are generally arranged:
Calculation procedure:Calculation programs are generally time-consuming, so we are firmly opposed to processing them in any interrupt, especially multiplication and division operations.
对实时性要求低或无实时性要求的程序进行处理;
Show transfer procedure:Mainly aimed at applications with external LED and LCD Drivers.
3. Interrupt handler:Interrupt programs are mainly used to handle tasks and events with high real-time requirements, such as detection of external sudden signals, detection and processing of buttons, timing counting, LED display scanning, etc.
Under normal circumstances, 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, the trigger flag can be set in the interrupt, and then the main program will perform specific transactions. This is very important, especially for low-power, low-speed MCUs, which must ensure timely response to all interrupts.
4. 对于不同任务体的排列,不同的MCU有不同的处理方法:
例如,对于低速、低功耗的MCU(Fosc=32768Hz)应用,考虑到此类项目通常是手持设备并使用普通LCD显示屏,按键响应和显示响应需要较高的实时性,因此通常使用定时中断来处理按键动作和数据显示;而对于高速MCU(例如Fosc>1MHz的应用),由于此时MCU有足够的时间执行主程序循环,因此只需在相应的中断中设置各种触发标志,并将所有任务放在主程序循环中执行即可。
5. 在单片机编程中,有一点需要特别注意:
必须防止在中断和主程序体中同时访问或设置相同的变量或数据。一种有效的防止方法是将此类数据的处理安排在一个模块中,并通过判断触发标志来确定是否对数据执行相关操作;在其他程序体(主要是中断)中,仅在需要处理数据的地方设置触发标志。——这样可以确保数据执行的可预测性和唯一性。
7
工程师对微控制器编程的总结
1、Develop a good habit of summarizing. Summarizing is not only a summary of your own learning, but also a review and deepening of the learning process. It can also avoid making mistakes the second time.
2、Before writing a program, you must first have a familiar understanding of the project, be aware of it, and outline a general framework. It is very important to carefully consider how to lay out and what is the most reasonable layout. It is necessary to analyze which module to do first, the specific steps of this module, how to name each function, the connection with other modules, etc. It’s a good idea to get a piece of paper and jot down important processes.
3、For modular programming in C language, you must first divide each module, program module by module, determine a sequence, follow the sequence, and then write the next module after the module is successful. For header files, write the module's header file after the module is written.
4、Do not ignore warnings when they appear. It means that there must be something unreasonable about the program. It is necessary to understand its source and find a solution. Be specific when looking for sources. You can search the Internet for information in this area, or ask others for advice. For example, the main function in another project was actually added to this project. There are actually duplicate function names. There are also reasons to analyze the experimental phenomena and progress step by step. There was also the wrong interface selected when defining the port. Sometimes, it’s good to take a break and think about it if you really can’t solve it. No matter how simple it is, you should pay attention to it, as there may be mistakes.
在微控制器应用开发中,代码使用效率、抗干扰性能和微控制器可靠性等问题仍然困扰着我们。本文总结了微控制器开发中需要掌握的几项基本技能。
8
微控制器开发技能
1.How to reduce bugs in programs
关于如何减少程序错误,您首先应该考虑以下在系统运行期间应考虑的超出范围的管理参数。
-
物理参数:这些参数主要是系统的输入参数,包括激励参数、采集处理过程中的操作参数和处理结束时的结果参数。
-
资源参数:这些参数主要是系统中电路、器件和功能单元的资源,例如内存容量、存储单元长度和堆叠深度。
-
应用参数:这些应用参数通常作为某些微控制器和功能单元的应用条件出现。过程参数:指在系统运行过程中按顺序变化的参数。
2.How to improve the efficiency of C language programming code
使用C语言进行微控制器编程是微控制器开发和应用领域不可避免的趋势。如果想要在C语言编程中达到最高效率,最好熟悉所使用的C编译器。首先测试每种C语言编译方式对应的汇编语言语句行数,以便清楚地了解其效率。在以后的编程中,优先选择编译效率最高的语句。由于每个C编译器都会存在一定的差异,因此编译效率也会有所不同。对于嵌入式系统而言,优秀的C编译器生成的代码长度和执行时间仅比用汇编语言编写的相同函数长5%到20%。
对于开发时间紧迫的复杂项目,可以使用C语言,但前提是您必须非常熟悉C语言以及MCU系统的C编译器。尤其要注意C编译系统支持的数据类型和算法。虽然C语言是最常用的高级语言,但不同的MCU厂商拥有不同的C语言编译系统,尤其是在某些特殊功能模块的操作方面。因此,如果您不了解这些特性,调试过程中会遇到很多问题,导致执行效率低于汇编语言。
3.How to solve the anti-interference problem of microcontroller防止干扰最有效的方法是消除干扰源并阻断干扰路径,但这往往难以做到,因此我们只能检验微控制器的抗干扰能力是否足够强。在提升硬件系统抗干扰能力的同时,软件抗干扰因其设计灵活、节省硬件资源和可靠性高等优点,正受到越来越多的关注。
微控制器干扰最常见的现象是复位。至于程序失控,实际上可以使用软件陷阱和看门狗机制将程序拉回到复位状态。因此,微控制器软件抵抗干扰的关键在于妥善处理复位状态。
通常,微控制器会配备一些标志寄存器,用于判断复位原因;此外,你也可以自行将一些标志位存储在 RAM 中。每次程序复位时,都可以通过判断这些标志位来确定不同的复位原因;你还可以根据不同的标志位直接跳转到相应的程序。这样,程序就可以持续运行,用户在使用过程中不会察觉到程序已被复位。
4.How to test the reliability of microcontroller system当微控制器系统设计完成后,不同的微控制器系统产品会有不同的测试项目和方法,但有些项目是必须测试的:
- 测试微控制器软件功能的完整性
- 开机和关机测试
- 老化测试
- 诸如静电放电测试(ESD)和情绪释放测试(EFT)等。
有时,我们还可以模拟人为使用过程中可能造成的损坏。例如,故意用人体或衣物摩擦微控制器系统的触点,以测试其抗静电能力;或者使用大功率电钻靠近微控制器系统进行操作,以测试其抗电磁干扰能力。
In summary, single-chip microcomputer has become an important aspect of computer development and application. The important significance of single-chip microcomputer application is that it fundamentally changes the traditional control system design ideas and design methods.
Most functions that previously had to be implemented by analog circuits or digital circuits can now be implemented through software methods using microcontrollers. This kind of control technology in which software replaces hardware is also called micro-control technology, which is a revolution in traditional control technology.
此外,在开发和应用过程中,我们必须掌握技能并提高效率,以便它能够用于更广泛的用途。
9
芯片运行概述
芯片上的操作主要是对芯片内部寄存器的操作。芯片中的寄存器在内存中都有各自唯一的地址,操作就是对相应地址进行的操作。在查看芯片时,首先要查看时序图,然后了解相应的寄存器及其工作原理,定义所需的端口(这些端口可以通过程序识别),并编写写入操作流程和读取操作流程。
如何将数据写入芯片,如何读取数据,以及通过哪个端口输入或读取数据(这是最重要的)。
通过总线连接芯片时,首先必须了解总线的协议。连接到 I2C 总线的芯片主要通过该总线进行控制。
1、One 74hc595 in the lattice is used for column selection, and the other two are used for color selection. The lattice is equivalent to a collection of diodes,
只有当一端接高电平而另一端接低电平时,二极管才能亮起。只是当一端接低电平不同时,就会亮起不同颜色的光。
定时器工作模式的选择:高四位设置定时器T1,低四位设置定时器T0。然后,每个模式的最后两位数字设置工作模式。设置两个定时器时,注意使用“或”(|)。使用中断时,注意在进入中断后清除那些需要清除的定时器。
2、Serial port transceiver: The baud rate is generally set in mode 2 (automatic reloading to initial value). Because different devices have different data processing capabilities, setting the baud rate is mainly to take care of low-speed devices and to communicate with each other. The interrupt flag bit must be cleared by software. When setting the serial port interrupt, no matter which one is generated by sending or receiving, it can enter the interrupt function, so pay attention to setting the interrupt function. (Self-feeling generally sets a function, as a host computer or a slave computer).
如果使用中断发送数据,则必须先弄清楚如何首次进入中断状态,因此必须先发送一次数据,然后才能进入中断状态。每次只能发送一个字节,并且只有在 TI 被置位后才能发送下一个字节。
3、Pcf8591ad conversion has four channels of input. When reading pcf8591, which channel is selected, the voltage input by that channel is read. The converted data is stored in the chip and then read out. When reading, first write the address of the chip, then write the sub-address of the device (0x40|channel number), and then the read data.
4、Da conversion is to first write the device address into the chip, then write the sub-address (0x40), and then write the digital quantity to be converted. Device address chip information is introduced.
5、For the LCD display, after the data is written and displayed, it will always be displayed without continuous refresh. If you want to change it, you can only re-enter it.
6、For the ds1302 clock chip, when reading data, the first data is read at the falling edge of the eighth clock when writing data, and then prepares for the next output. Pay attention to the writing method of the program and the location of the return value.
7、In Ds1302, first specify the register and then write data to it. The register on the chip data indicates the address. (I still don’t quite understand the write protection program. Isn’t there always writing? Why is the write protection still turned on?)
(According to the previous hero, you can set a flag after the initialization time. If there is this flag, there is no need to initialize the time. However, if the power is turned off, the MCU's RAM cannot save this flag, so you can use the DS1302's RAM to save the flag, and read the flag after powering on. I am also a beginner, and I plan to use DS1302 recently. I don't know if this is correct, and I haven't implemented it yet. Please share more)
8、It is best to write down the initialization in case you forget it later. Sometimes pay attention to whether the lowest bit or the highest bit is operated first when reading or writing, which can be judged according to the timing diagram.
9、For infrared transceiver, when receiving, it determines whether it is a high level or a low level based on the time between two falling edges. When writing a program, first use a timer to determine the time, save it, and then convert it into binary (read more about how to write this program, it is very good).
10、Stepper motor: Mainly used for switching. The torque of the stepper motor decreases as the rotational speed increases. It is mainly used for automatic feeding of parts processed on machine tools. It can also be used in control places with higher precision.
Stepper motor is an open-loop control element stepper motor device that converts electrical pulse signals into angular displacement or linear displacement. Under non-overload conditions, the motor's speed and stop position only depend on the frequency and number of pulses of the pulse signal, and are not affected by load changes. When the stepper driver receives a pulse signal, it drives the stepper motor to rotate in the set direction at a fixed angle, called the "step angle". Its rotation runs step by step at a fixed angle. The angular displacement can be controlled by controlling the number of pulses to achieve accurate positioning; at the same time, the speed and acceleration of the motor rotation can be controlled by controlling the pulse frequency to achieve speed regulation.
11、Servo motor: (servo motor) refers to the engine that controls the operation of mechanical components in the servo system. It is an indirect transmission device that assists the motor. Servo motors can control speed and position accuracy very accurately, and can convert voltage signals into torque and rotational speed to drive control objects. The rotor speed of the servo motor is controlled by the input signal and can respond quickly. In the automatic control system, it is used as an actuator and has the characteristics of small electromechanical time constant, high linearity, starting voltage, etc. It can convert the received electrical signal into the angular displacement or angular velocity output on the motor shaft. They are divided into two categories: DC and AC servo motors. Their main feature is that there is no rotation when the signal voltage is zero, and the rotational speed decreases at a constant speed as the torque increases. DC motor: Large range, all on small cars.
Disclaimer: This article is reproduced from "International Electronic Business Information". This article only represents the author's personal views and does not represent the views of Sacco Micro and the industry. It is only for reprinting and sharing and supports the protection of intellectual property rights. Please indicate the original source and author for reprinting. If there is any infringement, please contact us to delete it.
公司电话号码:+86-0755-83044319
传真:+86-0755-83975897
电子邮件:1615456225@qq.com
QQ:3518641314 李经理
QQ:332496225 邱经理
地址:深圳市龙华新区民治大道1079号展涛科技大厦C座809室




粤公网安备44030002007346号