漫威电影宇宙最精彩的科普总结
2022-03-16 529

MCU是微控制器单元(Microcontroller Unit)的缩写,俗称单片机,其原理是在适当降低CPU的频率和规格的同时,将存储器、计数器、USB接口、A/D转换器、UART、PLC、DMA甚至LCD驱动电路等外围接口集成到单个芯片上,形成芯片级计算机。它可以对不同的应用进行不同的控制组合,例如手机、PC外设、遥控器、汽车电子、工业步进电机和机器人手臂控制等。MCU的结构如图所示。



一种
微控制器发展简史



微控制器的历史虽然不长,但发展速度却非常迅猛。它的生产和发展通常与微处理器(CPU)的生产和发展同步进行。自美国英特尔公司于1971年首次推出4位微处理器以来,其发展历程大致可以分为五个阶段。以下是对英特尔公司微控制器发展历程的介绍。


1971 ~ 1976


The initial stage of the development of microcontrollers. In November 1971, Intel first designed the Intel 4004, a 4-bit microprocessor with an integration of 2,000 transistors/chip, equipped with RAM, ROM and shift register, forming the first MCS-4 microprocessor. Then it launched the 8-bit microprocessor Intel 8008, and other 8-bit microprocessors launched by other companies.


1976 ~ 1980


低性能微控制器阶段。以英特尔于1976年推出的MCS-48系列为代表,它采用单片结构,将8位CPU、8位并行I/O接口、8位定时器/计数器、RAM和ROM集成在一颗半导体芯片上。虽然其寻址范围有限(不超过4KB),没有串行I/O,RAM和ROM容量较小,中断系统也相对简单,但其功能足以满足一般工业控制和智能仪表的需求。


1980 ~ 1983


High-performance microcontroller stage. The high-performance 8-bit microcontrollers launched at this stage generally have serial ports, multi-level interrupt processing systems, and multiple 16-bit timers/counters. The capacity of the on-chip RAM and ROM is increased, and the addressing range can reach 64 KB. Some chips also have an A/D conversion interface.


1983~late 1980s


16-bit microcontroller stage. In 1983, Intel launched the high-performance 16-bit microcontroller MCS-96 series. Due to its adoption of the latest manufacturing technology, the chip integration level is as high as 120,000 transistors/chip.


1990s


微控制器在集成度、功能性、速度、可靠性和应用领域等各个方面都在不断发展,迈向更高的水平。




单片机的分类和应用



根据存储器类型,MCU 可以分为两类:无片上 ROM 的和有片上 ROM 的。对于无片上 ROM 的芯片,必须连接外部 EPROM 才能使用(通常为 8031);有片上 ROM 的芯片又分为片上 EPROM 型(典型芯片为 87C51)、片上掩模 ROM 型(典型芯片为 8051)、片上 Flash 型(典型芯片为 89C51)和其他类型。


根据用途,可分为通用型和专用型;根据数据总线的宽度和一次可处理的数据字节长度,可分为 8 位、16 位和 32 位 MCU。


目前,国内MCU应用最广泛的领域是消费电子领域,其次是工业领域和汽车电子市场。消费电子领域包括家用电器、电视、游戏机、音视频系统等;工业领域包括智能家居、自动化、医疗应用以及新能源发电和输配电;汽车领域包括汽车动力总成和安全控制系统。




微控制器的基本功能



对于大多数微控制器(MCU)而言,以下功能是最常用和最基本的。不同MCU对这些功能的描述方式可能有所不同,但本质上是相同的:


1. TImer (timer): Although there are many types of TImer, they can be summarized into two categories: One type is TImer with fixed time intervals, that is, the timing time is set by the system and cannot be controlled by the user program. The system only provides several fixed time intervals for the user program to choose, such as 32Hz, 16Hz, 8Hz, etc. This type of TImer is more common in 4-bit MCUs, so it can be used to implement clock, timing and other related functions.


The other type is 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 setting of preset number. Some MCUs have all three at the same time, while others may have one or two of them. This type of Timer application is very flexible, and its actual use is also ever-changing. One of the most common applications is to use it to implement PWM output.


Since the clock source can be freely selected, this type of Timer is generally combined with the Event Counter.


2. I/O 端口:任何 MCU 都具有一定数量的 I/O 端口。如果没有 I/O 端口,MCU 就失去了与外界通信的通道。根据 I/O 端口的可配置性,可以将其分为以下几种类型:


纯输入/纯输出端口:这种类型的I/O端口由MCU硬件设计决定,只能用于输入或输出,不能通过软件进行实时设置。


直接读写I/O端口:例如,MCS-51的I/O端口就属于这种类型的I/O端口。当执行读取I/O端口指令时,它是一个输入端口;当执行写入I/O端口指令时,它自动成为一个输出端口。


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 actual needs. The application is relatively flexible and can realize some bus-level applications, such as I2C bus, various LCD, LED Driver control buses, etc.


对于 I/O 端口的使用,必须牢记一点:对于输入端口,必须有清晰的电平信号以确保其不会浮空(这可以通过添加上拉或下拉电阻来实现);对于输出端口,输出状态电平必须考虑其外部连接条件,并且应确保在待机或静态状态下没有电流拉取或吸收。


3. 外部中断:外部中断也是大多数MCU的基本功能。它通常用于信号的实时触发、数据采样和状态检测。中断方式包括上升沿触发、下降沿触发和电平触发。外部中断通常通过输入端口实现。如果是I/O端口,其中断功能仅在设置为输入时启用;如果是输出端口,外部中断功能将自动关闭(ATMEL的ATiny系列是例外,即使作为输出端口也可以触发中断功能)。外部中断的应用如下:


Detection of external trigger signals: One is based on real-time requirements, such as control of thyristors, detection of sudden signals, etc., while the other is the need to save power.


Measurement of signal frequency: In order to ensure that the signal is not missed, external interrupt is the ideal choice.


数据解码:在遥控应用领域,为了降低设计成本,通常需要使用软件来解码各种编码数据,例如曼彻斯特编码和PWM编码的解码。


Key detection and system wake-up: For MCUs that enter the sleep state, they generally need to be awakened through external interrupts. The most basic form is a key press, which produces level changes through the action of the key.


4. 通信接口:MCU提供的通信接口一般包括SPI接口、UART、I2C接口等,具体描述如下:


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. In popular terms, it depends on who provides the clock signal. The party providing the clock is the master, and the opposite party is the Slaver.


UART (Universal Asynchronous Receive Transmit): It is the most basic asynchronous transmission interface. It has only two signal lines, 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 occupied by one bit of data is called Baud Rate.


For most MCUs, the length of data bits, data check mode (odd check, even check or no check), stop bit length and Baud Rate can be flexibly set through program programming. The most commonly used method of this type of interface is to communicate with the serial port of a PC.


I2C接口:I2C是由飞利浦公司开发的一种数据传输协议。它也使用两个信号来实现:SDAT(串行数据输入/输出)和SCLK(串行时钟)。​​其最大的优势在于可以将多个设备连接到该总线上,并通过地址进行识别和访问;I2C总线的另一个主要优势是可以通过I/O端口使用软件轻松实现。与对速率有严格要求的UART接口不同,I2C的数据传输速率完全由SCLK控制,可以设置得很快也可以很慢。


5. 看门狗(看门狗定时器):看门狗也是大多数MCU的基本配置(某些4位MCU可能不具备此功能)。大多数MCU的看门狗只允许程序复位,而不能将其关闭(有些MCU在程序烧录时就已设置,例如Microchip PIC系列MCU),而有些MCU则使用特定方法来决定是否开启看门狗,例如三星KS57系列。只要程序访问看门狗寄存器,看门狗就会自动开启,并且无法再次关闭。一般来说,看门狗的复位时间可以通过编程方式设置。看门狗最基本的应用是为因意外故障而崩溃的MCU提供自恢复能力。




全球主流微控制器制造商

(排名不分先后,按主流厂商分类。如有遗漏,请在评论区补充。)




欧洲和美国




1. Freescale+NXP (Freescale+NXP): Netherlands, mainly provides 16-bit and 32-bit MCUs. Applications: Automotive electronics, LED and general lighting, healthcare, multimedia convergence, home appliances and power tools, building automation technology motor control, power supplies and power converters, energy and smart grids, automation, computer and communication infrastructure.




2. Microchip+Atmel(Microchip Technology + Atmel):美国,主要提供 16 位和 32 位 MCU。应用领域:汽车电子、工业应用、电机控制、汽车、楼宇自动化、家用电器、家庭娱乐、工业自动化、照明、物联网、智能能源、移动电子设备、计算机外围设备。




3. Cypress+Spansion(Cypress + Spansion Semiconductor):美国,主要提供 8 位、16 位和 32 位 MCU。应用领域:汽车电子、家用电器、医疗、消费电子、通信和电信、工业、无线通信。




4. ADI(Analog Devices):美国,主要提供 8 位、16 位和 32 位 MCU。应用领域:航空航天与国防、汽车应用、楼宇技术、通信、消费电子、能源、医疗保健、仪器仪表与测量、电机、工业自动化、安防。




5. 英飞凌:德国,主要提供 16 位和 32 位 MCU。应用领域:汽车电子、消费电子、工程、商用和农用车辆、数据处理、电动交通、工业应用、医疗设备、移动设备、电机控制和驱动、电源、摩托车、电动自行车和小型电动汽车、智能电网、照明、太阳能系统解决方案、风能系统解决方案。




6. 意法半导体:意大利/法国,主要提供 32 位 MCU。应用领域:LED 和通用照明、交通运输、医疗保健、多媒体融合、家用电器和电动工具、楼宇自动化技术电机控制、电源和电源转换器、能源和智能电网、自动化、计算机和通信基础设施。




7. 高通公司:美国,主要提供 16 位和 32 位微控制器。应用领域:智能手机、平板电脑、无线调制解调器。




8. Texas Instruments: United States, mainly provides 16-bit and 32-bit MCUs. Application scope: automotive electronics, consumer electronics, medical equipment, mobile devices, communications.




9. Maxim (Maxim): United States, mainly provides 32-bit MCU. Application scope: automotive electronics, consumer electronics, industrial applications, security.




日本和韩国  




1. 瑞萨电子:日本,主要提供16位和32位MCU。应用范围:计算机及外围设备、消费电子、医疗电子、汽车电子、工业和通信。




2. Toshiba: Japan, mainly provides 16-bit and 32-bit MCUs. Application scope: automotive electronics, industrial use, motor control, wireless communications, mobile phones, computers and peripheral equipment, imaging and audio and video, consumer (home appliances), LED lighting, security, power management, entertainment equipment.




3. 富士通:日本,主要提供32位MCU。应用范围:汽车、医疗、机械、家用电器。




4. Samsung Electronics: South Korea, mainly provides 16-bit and 32-bit MCUs. Applications: automotive electronics, industrial use, motor control, automobiles, building automation, home appliances, home entertainment, industrial automation, lighting, Internet of Things, smart energy, mobile electronic equipment, computer peripherals.




中国地区  




中国大陆  




1. Sigma Microelectronics:主要提供 32 位 MCU,应用范围:电信、制造、能源、交通、电力等。




2. Zhuhai Orbit: Mainly provides 32-bit MCU, application scope: aerospace: satellite station ships, aircraft; high-end industrial control: embedded computers; ship control, industrial control, power equipment, environmental monitoring.




3. GigaDevice Innovation:主要提供 32 位 MCU,应用范围:工业自动化、人机界面、电机控制、安全监控、智能家居、物联网。




4. 盛思微电子:主要提供8位和32位MCU。应用范围:小型家用电器、消费电子、遥控器、鼠标、锂电池、数码产品、汽车电子、医疗器械及测量、玩具、工业控制、智能家居及安防等领域。




5. 芯海科技:主要提供16位和32位MCU。应用范围:仪器仪表、物联网、消费电子、家用电器和汽车电子。




6. 联华集成电路:主要提供8位和16位MCU。应用范围:消费电子、白色家电、工业控制、通信设备、汽车电子和计算机。




7. Zhuhai Jianrong: Mainly provides 8-bit MCU, application scope: household appliances, mobile power supply.




8. Actions Technology:主要提供 8 位至 32 位 MCU,应用范围:平板电脑、智能家居、多媒体、蓝牙、Wi-Fi 音频。




9. 艾斯柯微电子:主要提供8位和16位MCU。应用范围:消费芯片、通信芯片、信息芯片和家用电器。




10. Huaxin Microelectronics: Mainly provides 8-bit and 4-bit MCUs. Applications: satellite receivers, mobile phone chargers, perpetual calendars, and all-in-one remote controls.




11. Shanghai Belling (Huada Semiconductor Holdings): Mainly provides 8-bit, 16-bit, and 32-bit MCUs. Applications: computer peripherals, HDTV, power management, small appliances, and digital appliances.




12. 海尔集成电路:主要提供14位、15位和16位MCU。应用范围:消费电子、汽车电子、工业和智能仪器。




13. Beijing Ingenic: Mainly provides 32-bit MCU, application scope: wearable devices, Internet of Things, smart home appliances, automobiles, consumer electronics, tablet computers.




14. 中国微电子:主要提供8位MCU。应用领域:智能家电、汽车电子、安防监控、LED照明及景观、智能玩具、智能家居、消费电子产品。




15. Shenzhou Loongson Integrated Circuit: Mainly provides 32-bit MCU, application scope: power monitoring, smart grid, industrial digital control, Internet of Things, smart home, data monitoring.




16. Ziguang Microelectronics: Mainly provides 8-bit and 16-bit MCUs. Application scope: smart home appliances.




17.时代民信:主要提供32位MCU,应用范围:汽车导航、交通监控、渔船监控、电力通信网络。




18. China Resources Silicon Microelectronics (a subsidiary of China Resources Microelectronics): mainly provides 8-bit and 16-bit MCUs. Application scope: consumer electronics, industrial control, and home appliances.




19. 国家核心技术:主要提供 32 位 MCU,应用范围:信息安全领域、办公自动化领域、通信网络领域、信息安全领域。




20. Zhongtian Micro: Mainly provides 32-bit MCUs. Applications: smartphones, digital TVs, set-top boxes, automotive electronics, GPS, e-readers, and printers.




21. 华润微电子:主要提供8位和16位MCU。应用范围:家用电器、消费电子产品和工业自动化控制的通用控制电路。




22. Zhongying Electronics: Mainly provides 4-bit, 8-bit, 16-bit, and 32-bit MCUs. Application scope: home appliances and motors.




23. Lingdong Microelectronics: Mainly provides 32-bit, application scope: motor control, Bluetooth control, high-definition display, wireless charging, drones, micro printers, smart labels, electronic cigarettes, LED dot matrix screens, etc.




24. Nuvoton Technology: Mainly provides 8-bit MCU, application scope: lighting, Internet of Things, etc.




25. Neusoft Carrier: Mainly provides 8-bit and 32-bit MCUs. Applications: home appliances, smart homes, instrumentation, LCD panel controllers, industrial control, etc.




26. Betley:主要提供 32 位 MCU,应用范围:智能家居、工业控制和消费产品。




27. Shengquan Technology: Mainly provides 8-bit MCU, application scope: small and medium-sized display panels for automotive, education, industrial control, medical, etc.




28. Hangshun Chip: Mainly provides 8-bit and 32-bit MCUs. Application scope: automobiles, Internet of Things, etc.




29. 复旦微电子:主要提供16位和32位MCU。应用领域:智能电表、智能门锁等。




30. 华大半导体:主要提供8位、16位和32位MCU。应用领域:工业控制、智能制造、智慧生活和物联网。




台湾,中国  




1.鸿景科技:主要提供32位MCU。应用范围:通信、工业控制、信息家电、语音。




2. Holtek Semiconductor: mainly provides 8-bit and 32-bit MCUs. Application scope: consumer electronics, LED lighting, etc.




3.凌阳科技:主要提供8位和16位MCU。应用范围:家庭音视频。




4. 中盈电子:主要提供4位和8位MCU。应用范围:充电器、移动电源、家用电器、工业控制。




5. 松汉科技:主要提供8位和32位MCU。应用范围:遥控器、智能充电器、大小型系统、电子秤、耳温计、血压计、胎压计、各种测量和健康设备。




6. 华邦电子:主要提供8位和16位MCU。应用范围:汽车电子、工业电子、网络、计算机、消费电子、物联网。




7. 十速科技:主要提供4位、8位和51位MCU。应用范围:遥控器、小型家用电器。




8. 友华微电子:主要提供4位和8位MCU。应用范围:记录集成电路产品、消费电子产品、家用产品。




9. 盈光科技微控制器:主要提供4位和8位MCU。应用范围:机械、自动化、家用电器、机器人。




10. Elan Electronics: mainly provides 8-bit and 16-bit MCUs. Application scope: consumer electronics, computers, smartphones.
 





Tips for learning microcontrollers
   




所有单片机的基本原理和功能都大同小异,唯一的区别在于外围功能模块、指令系统等的配置和数量。




For instruction systems, although they appear to be very different in form, they are actually just different symbols. The meanings they represent, the functions to be completed, and the addressing methods are basically similar.




要了解一个MCU,首先需要了解它的ROM空间、RAM空间、I/O端口数量、定时器数量和定时方法、提供的外围功能模块(外围电路)、中断源、工作电压和功耗等。




在了解了这些 MCU 特性之后,下一步是将所选 MCU 的功能与实际项目开发所需的功能进行比较,并明确当前需要哪些资源以及本项目未使用哪些资源。




For functions that need to be used in the project but are not provided by the selected MCU, you need to carefully understand the relevant information of the MCU in order to implement them using indirect methods. For example, if the project you are developing needs to communicate with the PC COM port, and the selected MCU does not provide a UART port, you can consider using external interrupts to implement it.




For the resources needed for project development, you need to carefully understand and read the Manua*, while you can ignore or browse the unnecessary functional modules. For MCU learning, application is the key and the main purpose.




After clarifying the relevant functions of the MCU, you can start programming.




对于初次使用这款MCU的初学者或设计人员来说,他们可能会遇到许多关于MCU功能描述不够清晰的问题。针对这类问题,有两种解决方法。一种是编写专门的验证程序来理解数据中描述的功能;另一种是暂时忽略这些描述,根据自己目前的理解编写MCU程序,并在调试过程中进行修改和完善。前一种方法适用于时间较为充裕的项目和初学者,而后一种方法则适用于具有一定微控制器开发经验或项目时间紧迫的情况。




Don't take any special time to understand the command system. The instruction system is just a symbol of logical description. You can only view the relevant instructions according to your own logic and the logical requirements of the program during programming. As programming progresses, you will become more and more proficient in the instruction system, and you can even memorize it unconsciously.





Microcontroller programming




There is a big difference between MCU program writing and PC program writing. Although C-based MCU development tools are becoming more and more popular, for designers who have efficient program code and like to use assembly, assembly language is still the most concise and effective programming language.




For MCU programming, the basic framework can be said to be roughly the same. It 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. Their respective descriptions are as follows:




1. 初始化:对于所有MCU程序的设计而言,初始化是最基本也是最重要的步骤,通常包括以下步骤:




屏蔽所有中断并初始化堆栈指针:初始化部分通常不希望发生任何中断。




清除系统 RAM 区域和显示内存:虽然有时可能并非完全必要,但从可靠性和一致性的角度来看,尤其是为了防止意外错误,建议养成良好的编程习惯。




Initialization of the 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.




中断设置:项目中所有需要使用的中断源都应启用,并设置中断触发条件。未使用的冗余中断必须禁用。




其他功能模块的初始化:对于所有需要使用的MCU外围功能模块,必须根据项目应用需求进行相应的设置。例如,对于UART通信,需要设置波特率、数据长度、验证方式和停止位长度。对于编程定时器,需要设置时钟源、分频比和重载数据。




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:




计算程序:计算程序通常很耗时,因此我们坚决反对在任何中断中处理它们,特别是乘法和除法运算。




Processing programs with low or no real-time requirements;
 




显示传输程序:主要用于带有外部 LED 和 LCD 驱动器的应用。




3. 中断处理程序:中断程序主要用于处理实时性要求高的任务和事件,例如检测外部突发信号、检测和处理按钮、计时计数、LED 显示扫描等。




一般来说,中断程序的代码应尽可能简洁明了。对于无需实时处理的功能,可以在中断中设置触发标志,然后由主程序执行特定的事务。这一点非常重要,尤其对于低功耗、低速的微控制器而言,它们必须确保对所有中断的及时响应。




4. 对于不同任务体的排列,不同的MCU有不同的处理方法:




例如,对于低速、低功耗的MCU(Fosc=32768Hz)应用,考虑到此类项目通常是手持设备并使用普通LCD显示屏,按键响应和显示响应需要较高的实时性,因此通常使用定时中断来处理按键动作和数据显示;而对于高速MCU(例如Fosc>1MHz的应用),由于此时MCU有足够的时间执行主程序循环,因此只需在相应的中断中设置各种触发标志,并将所有任务放在主程序循环中执行即可。




5. In MCU programming, one thing that needs special attention is:

必须防止在中断和主程序体中同时访问或设置相同的变量或数据。一种有效的防止方法是将此类数据的处理安排在一个模块中,并通过判断触发标志来确定是否对数据执行相关操作;在其他程序体(主要是中断)中,仅在需要处理数据的地方设置触发标志。——这样可以确保数据执行的可预测性和唯一性。




XNUMX所   Engineer's summary of microcontroller programming  




1. 养成总结的好习惯。总结不仅是对自身学习内容的总结,也是对学习过程的回顾和深化。它还可以避免再次犯错。


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. 在C语言中,模块化编程必须先将每个模块拆分成多个部分,然后逐个模块进行编程,确定编程顺序,并严格按照顺序执行,直到一个模块成功完成后才能编写下一个模块。对于头文件,也需要在模块编写完成后再编写该模块的头文件。


4. 不要忽视出现的警告。这意味着程序中肯定存在一些不合理的问题。你需要找出问题的根源并找到解决方案。查找问题根源时要具体明确。你可以上网搜索相关信息,或者向他人寻求建议。例如,另一个项目中的主函数被添加到了这个项目中。实际上存在重复的函数名称。此外,还需要分析实验现象并逐步推进。也可能是在定义端口时选择了错误的接口。有时,如果实在无法解决,不妨停下来思考一下。无论问题多么简单,都应该重视,因为其中可能存在错误。   
In the development of microcontroller applications, problems such as code usage efficiency, anti-interference performance and reliability of microcontrollers are still troubled. Now we summarize several basic skills that should be mastered in the development of microcontroller.        
 
 
  微控制器开发技能  
1 如何减少程序中的错误
关于如何减少程序错误,您首先应该考虑以下在系统运行期间应考虑的超出范围的管理参数。  
 
  • 物理参数:这些参数主要是系统的输入参数,包括激励参数、采集处理过程中的操作参数和处理结束时的结果参数。
  • 资源参数:这些参数主要是系统中电路、器件和功能单元的资源,例如内存容量、存储单元长度和堆叠深度。
  • 应用参数:这些应用参数通常作为某些微控制器和功能单元的应用条件出现。过程参数:指在系统运行过程中按顺序变化的参数。

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 如何解决微控制器的抗干扰问题
The most effective way to prevent interference is to remove the interference source and block the interference path, but it is often difficult to do so, so we can only see whether the anti-interference ability of the microcontroller is strong enough. While improving the anti-interference ability of hardware systems, software anti-interference is receiving more and more attention because of its flexible design, saving of hardware resources, and good reliability.  
微控制器干扰最常见的现象是复位。至于程序失控,实际上可以使用软件陷阱和看门狗机制将程序拉回到复位状态。因此,微控制器软件抵抗干扰的关键在于妥善处理复位状态。  
Generally, microcontrollers will have some flag registers that can be used to determine the cause of reset; in addition, you can also bury some flags in RAM yourself. Each time the program is reset, different reset causes can be determined by judging these flags; you can also jump directly to the corresponding program based on different flags. This allows the program to run continuously, and the user will not notice that the program has been reset when using it.  
4 How to test the reliability of the microcontroller system
When a microcontroller system design is completed, there will be different test items and methods for different microcontroller system products, but some must be tested:
 
  • 测试微控制器软件功能的完整性
  • Power on and power off test
  • 老化测试
  • Tests such as ESD and EFT

Sometimes, we can also simulate the damage that may occur during human use. For example, deliberately rub the contact port of the microcontroller system with the human body or clothing fabric to test the anti-static ability. Use a high-power electric drill to work close to the microcontroller system to test the ability to resist electromagnetic interference.


综上所述,单片机已成为计算机发展和应用的重要组成部分。单片机应用的重要意义在于,它从根本上改变了传统的控制系统设计思路和设计方法。


过去必须由模拟电路或数字电路实现的大部分功能,现在都可以通过使用微控制器的软件方法来实现。这种软件替代硬件的控制技术也称为微控制技术,它是传统控制技术的一次革命。  
此外,在开发和应用过程中,我们必须掌握技能并提高效率,以便它能够用于更广泛的用途。        
       
Chip operation summary
       
芯片上的操作主要是对芯片内部寄存器的操作。芯片中的寄存器在内存中都有各自唯一的地址,操作就是对相应地址进行的操作。在查看芯片时,首先要查看时序图,然后了解相应的寄存器及其工作原理,定义所需的端口(这些端口可以通过程序识别),并编写写入操作流程和读取操作流程。


如何将数据写入芯片,如何读取数据,以及通过哪个端口输入或读取数据(这是最重要的)。


When connecting chips through a bus, you must first understand the protocol of the bus. The chip connected to the I2C bus mainly controls the chip through this bus.


1. One 74hc595 in the dot matrix is ​​used for column selection, and the other two are used for color selection. The dot matrix is ​​equivalent to a collection of diodes.


Only when one end is given a high level and the other end is given a low level can the diode light up. It's just that when one end is selected differently, different colors will light up.


Selection of timer working mode: The high four bits set the timer T1, and the low four bits set T0. Then the last two digits of each mode set the working mode. When setting two timers, be careful to use or (|). When using interrupts, pay attention to clearing the ones that should be cleared after entering the interrupt.


2. 串口收发器:模式 2(自动重载初始值)通常用于设置波特率。由于不同设备的数据处理能力不同,设置波特率主要是为了兼顾低速设备之间的通信。中断标志位必须由软件清除。设置串口中断时,无论是发送还是接收产生的中断都可能进入中断功能,因此需要注意中断功能的设置。(一般根据自身情况设置,作为主机或从机)。


如果使用中断发送数据,则必须先弄清楚如何首次进入中断状态,因此必须先发送一次数据,然后才能进入中断状态。每次只能发送一个字节,并且只有在 TI 被置位后才能发送下一个字节。


3. PCF8591AD转换器有四个输入通道。读取PCF8591时,选择其中一个通道,读取该通道的输入电压。转换后的数据存储在芯片中,然后读取出来。读取时,首先写入芯片地址,然后写入器件子地址(0x40|通道号),最后写入读取的数据。


4. DA转换首先将设备地址写入芯片,然后写入子地址(0x40),最后写入待转换的数字量。设备地址芯片信息已输入。


5. 对于液晶显示屏,数据写入显示后,将一直显示,不会持续刷新。如果需要更改,只能重新输入。


6. 对于DS1302时钟芯片,读取数据时,写入数据时,第一个数据在第八个时钟周期的下降沿读取,然后准备下一个输出。注意程序的写入方法和返回值的位置。


7. 首先在DS1302中指定寄存器,然后向其写入数据。芯片上的寄存器数据指示地址。(我仍然不太理解写保护程序。写入操作不是一直都在进行吗?为什么写保护仍然开启?)


(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 it 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. 对于红外收发器,接收信号时,它根据两个下降沿之间的时间间隔来判断信号是高电平还是低电平。编写程序时,首先使用定时器来确定时间间隔并保存,然后将其转换为二进制(阅读更多关于如何编写此程序的内容,非常好)。


10. 步进电机:主要用于开关控制。步进电机的扭矩随转速的增加而减小。主要用于机床加工零件的自动送料,也可用于精度要求较高的控制场合。


步进电机是一种开环控制元件,它将电脉冲信号转换为角位移或线位移。在非过载条件下,电机的转速和停止位置仅取决于脉冲信号的频率和脉冲数,不受负载变化的影响。当步进驱动器接收到脉冲信号时,它会驱动步进电机以固定的角度(称为“步进角”)沿设定的方向旋转。电机的旋转以固定的角度逐步进行。通过控制脉冲数可以控制角位移,从而实现精确定位;同时,通过控制脉冲频率可以控制电机的转速和加速度,从而实现调速。


11. 伺服电机:伺服电机是指伺服系统中控制机械部件运行的动力装置。它是一种辅助电机的间接传动装置。伺服电机能够非常精确地控制速度和位置,并将电压信号转换为扭矩和转速来驱动控制对象。伺服电机的转子速度由输入信号控制,响应速度快。在自动控制系统中,它被用作执行器,具有机电时间常数小、线性度高、启动电压低等特点。它可以将接收到的电信号转换为电机轴上的角位移或角速度输出。伺服电机分为直流伺服电机和交流伺服电机两类。它们的主要特点是:当信号电压为零时,电机不旋转;随着扭矩的增加,转速以恒定速度降低。直流伺服电机:应用范围广,主要用于小型汽车。


12. 汉字概述:
为了在显示器或打印机上输出汉字,需要根据图形符号将汉字设计成点阵,从而得到相应的点阵码(字形码)。


The unified encoding method used to represent Chinese characters in the computer forms a Chinese character encoding called an internal code (such as a national standard code), and the internal code is unique (equivalent to the ID number of the character). The Chinese character encoding formed to facilitate the input of Chinese characters is an input code, which is an external code of Chinese characters. The input code is different due to different encoding methods and is diverse. The Chinese character code formed for displaying and printing out Chinese characters is a glyph code. The computer finds the glyph code of the Chinese character in the font model library through the Chinese character internal code and realizes its conversion.


相机内代码


According to the provisions of the national standard code, each Chinese character has a certain binary code, but this code will conflict with the ASCII code when processed internally by the computer. To solve this problem, add 1 to the first digit of each byte of the national standard code. Since the ASCII code only uses 7 bits, the "1" in the first place can be used as a mark to identify Chinese character codes. When the computer processes the code with "1" in the first place, it understands it as Chinese character information, and when it processes the code with "0" in the first place, it understands it as an ASCII code. The national standard code (internal code) processed in this way is the internal code.


如果我们把这个“口”字形中的“.”替换成“0”,就能非常清晰地得到“口”字的字形代码:0000H 0004H 3FFAH 2004H 2004H 2004H 2004H 2004H 2004H 2004H 2004H 2004H 3FFAH 2004H 0000H 0000H。当计算机想要输出“口”字时,它首先找到显示字体库的第一个地址,根据“口”字的内部代码进行计算,然后找到“口”字的字形代码,再通过字符生成器的控制,按照字形代码(二进制)顺序扫描屏幕。当二进制代码为“0”时,扫描“0”所在的位置,扫描“1”所在的位置并高亮显示,从而得到“口”字的字符模式。


汉字字体按照国家标准代码的顺序排列,并以二进制文件的形式存储在内存中,形成汉字字体库,也称为汉字字形库或汉字库。


两种编码方法,参见头文件


GB1616.h//------------------ Chinese character font data structure definition ------------------------//structtypFNT_GB16 //Chinese character font data structure {unsignedcharIndex[3]; //Chinese character internal code index unsignedchar Msk[32];//dot matrix code data };
//////////////////////////////////////////////////////////////////////////////// Chinese character font table//// Chinese character library: Song Dynasty 16.dot, horizontally modulo left high bit, data arrangement: from left to right, from top to bottom///////////////////////////////////////////////////////////////////////////////////conststructtypFNT_GB16 codeGB_16[]= //data table{/*---------------------------------------------------------------------------------;source file/text :Xu;width×height (pixels):16×16--------------------------------------------------------------------------------*/"Xu",0x10,0x80,0x10,0x80,0x21 ,0x40,0x42,0x20,0x94,0x10,0x1B,0xEC,0x20,0x80,0x60,0x80,0xAF,0xF8,0x 20,0x80,0x22,0xA0,0x24,0x90,0x2A,0x88,0x21,0x00,0x00,0x00,0x00,0x00,
 
这种结构非常简单:一部分是内部代码,一部分是点阵序列。之前的点阵库是按照内部代码的顺序放置的,不需要内部代码索引。如果只放置一些汉字,则需要内部代码索引。(前面的汉字“Xu”是为了在输出“Xu”时查找对应的点阵序列。这个点阵序列是我自己编写的。当使用1602显示时,由于芯片内存中已经存储了英文点阵序列,所以不需要再编写。)通常,内部代码只需要两个字节就足够了。如果需要多用一个字节,只需在末尾添加一个0即可。这样,就可以直接将汉字字符串放入汉字内部代码中;


codeGB_16[k].Index[0] codeGB_16[k] indicates that there is an array of structure typFNT_GB16 called codeGB_16 codeGB_16[k] is the k+1 member of the array index is a member of structure typFNT_GB16, so it can be referenced with codeGB_16[k].Index. At the same time, index is an array, so it can be indexed[0] If((codeGB_16[k].Index[0]==c[0])&&(codeGB_16[k].Index[1]==c[1])) && is a logical AND operator, which means that the values on both sides of the && symbol are true. Only the value of && is true, that is, true && true =true. This sentence means: codeGB_16[k].Index[0]==c[0] and codeGB_16[k].Index[1]==c[1] are established at the same time. If the following statement is executed, codeGB_16[] is a structure array, codeGB_16[k].Index[0] means the 0th element value of the index member of the Kth structure of the structure array.  
      13. 12864 LCD:

每个显示点对应一个二进制数,1 表示开,0 表示关。存储这些点阵信息的 RAM 称为显示数据存储器。要显示某个图形或汉字,就需要将相应的点阵信息写入相应的存储单元。  
The address counter (AC) of the graphics RAM will only automatically increment the horizontal address (X axis) by one. When the horizontal address = 0FH, it will be reset to 00H. However, it will not automatically increment the vertical address with a carry. Therefore, when multiple data are written continuously, the program needs to determine whether the vertical address needs to be reset.


14. 绘图 RAM(GDRAM)

The drawing display RAM provides 128×8 bytes of memory space. When changing the drawing RAM, first write the horizontal and vertical coordinate values ​​continuously, and then write two bytes of data to the drawing RAM. The address counter (AC) will automatically increase the horizontal address (X address) by one. When the horizontal address is 0XFH, it will be reset to 00H; the vertical address will not be automatically incremented by 1. The drawing display must be turned off during writing to drawing RAM,

 
[cpp] view plain copy//Display Chinese characters voiddispString (uchar X, Y,uchar *msg)//Which row is X and which column is Y. msg is Chinese characters {if(X==0) write_data(*msg++); //Display Chinese characters }}////////////////////////////////// //////////////// ////////////////// Display image voiddisppicture(uchar code *adder){ uint i,j;//*******Display the upper half screen content settings for(i=0;i<32;i++)//32 column addresses in the upper half of the screen { write_com(0x80 + i);//SET vertical address VERTICALADD write_com(0x80);//SET horizontal address HORIZONTAL ADDfor(j=0;j<16;j++) { write_data(*adder); adder++; }}//************Display content settings in the lower half of the screen for(i=0;i<32;i++) //{ write_com(0x80 + i); //SET vertical address VERTICALADD write_com(0x88); //SET horizontal address HORIZONTAL ADDfor(j=0;j<16;j++){write_data(*adder);adder++;} }}
 
在 C 语言中,系统会自动为定义的变量分配空间,变量的地址就是变量名。通过这个名称,可以从内存中检索数据,也可以通过计算获得新的数据。然而,在汇编语言中,程序员需要定义存储空间,并将数据发送到累加器进行计算。每一步都需要程序员的操作。而在 C 语言中,这些过程都由编译器自动完成。




15. Some useful questions and answers

① 在微控制器C语言中,变量的内存分配是如何进行的?编译器是否会在编译过程中智能地添加分配和回收代码?关键在于,我编写的程序如何确保不会出现内存溢出错误?如果我进行递归操作,那么内存需求很难自行计算。




②. Will the microcontroller C language be restricted in variable definition? For example, the multiplication and division operations of floating-point data are written through assembly, and the code is quite complicated. If written directly in C language, wouldn't it be too simple?




③. In the hex file generated by the C language of the microcontroller, is the address distribution of the instruction and data ROM automatically assigned by the compiler? Can users assign it?




答案 1:首先,一个用 C 语言编写的微控制器程序被一个程序(似乎是 c51.exe)编译。编译完成后,变量的存储空间大小已分配完毕,但具体地址尚未分配(地址是浮空的)。接下来,另一个程序(似乎是 a51.exe)被连接。连接完成后,具体地址被确定。  
If there are too many variables, the compiler will prompt that the data segment is too large. To ensure that there is no memory overflow error, the main consideration is whether the stack overflows, and it depends on experience.




微控制器的C语言通常禁止递归,递归操作也一般避免使用。毕竟,微控制器不是PC,递归会影响速度。如果需要递归,最好使用DSP芯片。总之,必须能够选择合适的芯片。




答案 2:变量的大小(位数)通常与芯片累加器的位数相同。例如,数字 51 通常使用 8 位,因为它是 8 位微控制器。

The microcontroller can define bit variables, but it cannot define bit arrays. Writing in C language seems simple, but actually generates the largest amount of code. The microcontroller used for control hardly uses floating point operations, which is not only slow but troublesome and takes up space. If it is a DSP chip, it will be much better if it has a suitable hardware structure.




答案 3:通常情况下,它是自动分配的。它可以用 C 语言和汇编语言混合编程,也可以使用 Keil C 在线汇编。芯片与外部之间的数据交换通过端口进行。



公司电话号码:+86-0755-83044319
传真:+86-0755-83975897
电子邮件:1615456225@qq.com
QQ:3518641314 李经理

QQ:332496225 邱经理

地址:深圳市龙华新区民治大道1079号展涛科技大厦C座809室

whatsapp

WhatsApp

Whatsapp:+8618073002950