服务热线
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提供自恢复能力。
四
全球主流微控制器制造商
Tips for learning microcontrollers
Microcontroller programming
必须防止在中断和主程序体中同时访问或设置相同的变量或数据。一种有效的防止方法是将此类数据的处理安排在一个模块中,并通过判断触发标志来确定是否对数据执行相关操作;在其他程序体(主要是中断)中,仅在需要处理数据的地方设置触发标志。——这样可以确保数据执行的可预测性和唯一性。
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语言编译系统,尤其是在某些特殊功能模块的操作方面。因此,如果您不了解这些特性,调试过程中会遇到很多问题,导致执行效率低于汇编语言。
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
芯片上的操作主要是对芯片内部寄存器的操作。芯片中的寄存器在内存中都有各自唯一的地址,操作就是对相应地址进行的操作。在查看芯片时,首先要查看时序图,然后了解相应的寄存器及其工作原理,定义所需的端口(这些端口可以通过程序识别),并编写写入操作流程和读取操作流程。
为了在显示器或打印机上输出汉字,需要根据图形符号将汉字设计成点阵,从而得到相应的点阵码(字形码)。
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即可。这样,就可以直接将汉字字符串放入汉字内部代码中;
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.
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 语言中,这些过程都由编译器自动完成。
① 在微控制器C语言中,变量的内存分配是如何进行的?编译器是否会在编译过程中智能地添加分配和回收代码?关键在于,我编写的程序如何确保不会出现内存溢出错误?如果我进行递归操作,那么内存需求很难自行计算。
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.
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.
公司电话号码:+86-0755-83044319
传真:+86-0755-83975897
电子邮件:1615456225@qq.com
QQ:3518641314 李经理
QQ:332496225 邱经理
地址:深圳市龙华新区民治大道1079号展涛科技大厦C座809室




粤公网安备44030002007346号