新聞
新聞
MCU 最強科普摘要[推薦合集] (2)
2022-03-17 230

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室

whatsapp

WhatsApp

Whatsapp:+8618073002950