Software programming methods and techniques

This article mainly introduces the practical experience and application skills of TI MSP430 microcontroller software programming design.

The first part describes the standard program flow mode based on interrupts, which is suitable for most product application programming. The other part describes some key issues, important methods and precautions that MSP430 microcontroller programmers need to pay attention to when developing products, such as: program initialization. The process, precautions for crystal oscillator initialization, etc., can greatly reduce the product development cycle and improve product stability.

1. MSP430 microcontroller standard software design flow The standard process of MSP430 microcontroller software design can refer to Figure 1. This standard software flow minimizes overall system power consumption.

figure 1

The software flow code depicted in Figure 1 is based on an interrupt routine. The MCU is in a sleep state until the MCU is woken up when an interrupt occurs, minimizing system power consumption and optimizing power efficiency.

Understand the interrupt service subroutine (1SRs) flow shown in Figure 1, to better understand the MSP430 microcontroller processing low power mode. The low power mode of the MSP430 microcontroller is controlled by the control bits of the status register SR, and the low power mode control bits of the status register SR can be preferentially pushed before the interrupt service routine is executed. When the interrupt service routine returns, the main program calls the low power mode control bit before the interrupt, thus entering the low power mode state before the interrupt. Of course, if we modify the low power mode control bit stored in the SR before the ISR interrupt service routine returns, the main program flow can be transferred to another different low power mode after the ISR interrupt subroutine is executed. .

The interrupt wake-up mechanism acts as part of the MSP430 microcontroller's internal low-power mode, allowing the system to wake up quickly and respond to interrupt events. For example, when an MSP430 microcontroller is in low-power LPM0 mode before an interrupt event occurs, the MCU first saves the low-power mode control bit value in the SR to the stack and then clears the SR value before executing the interrupt service routine, clearing the SR value. The MCU enters from the LPMO mode to the execution interrupt active mode state. In the ISR interrupt service subroutine, the software designer can write a statement to clear the low power mode control bits in the SR. When the interrupt routine is completed, the MCU reloads the status values ​​of the respective registers from the stack. If the low power mode control bit of SR is not modified, the system still returns to enter LPM0 low power mode after exiting the interrupt. If the SR control bit has been modified at this time, when the system will work in active mode after exiting from the ISR interrupt program, And continue to execute the program at the address indicated by the PC pointer before entering the interrupt.

Since the low power mode can be changed in the fSR interrupt service routine, the designer can choose to perform all tasks in the ISR program, or he can choose to process the task in the main program after the ISR wakes up the MCU.

When processing in an ISR program, it ensures that the interrupt event can be immediately responded to, and the interrupt task can be processed immediately when the interrupt event occurs. However, when an interrupt event is processed, other interrupts cannot be loaded until the interrupt task is completed, and thus Interruption of time will reduce the responsiveness of the system, so the designer must choose the best treatment according to different system requirements.

In the flow chart shown in Figure 1, the main program needs to handle two interrupt events, and the tasks to be processed by the two interrupt events are processed in the main loop. ISRs perform two different event tasks. The first interrupt, ISRs changes the value of SR stored in the stack, can make the system exit the interrupt and enter the active working mode, the interrupt event is executed in the main program, the interrupt event can be an arbitrary application event, such as a timer, Key processing, AD conversion, etc. The second interrupt sets a flag in the ISR interrupt service routine, and the main program detects the flag to determine whether to perform the corresponding task. If the interrupt event to be processed can be completed quickly in a short period of time, it can be directly executed in the interrupt service subroutine without entering the main program processing. At this time, the ISR interrupt service subroutine does not need to set the flag bit or change the SR low. The power control bit exits sleep mode and the MCU returns to sleep mode after exiting the ISR interrupt service routine. The process can be determined according to the complexity of the system application. For example, when only one interrupt can wake up the main program, there is no need to set the system flag. At this time, the main program is waking up by interrupt, and then the main program performs the corresponding task operation. Finally, the MCU re-enters sleep mode.

The sleep mode LPMn mentioned in Figure 1 is the sleep low power mode that the system uses. The mode involved in each application may be different. The actual sleep mode is used by the entire system (such as a timer). , AD, serial port, etc.) depends on whether the system module can be awakened by interrupt in the corresponding sleep mode. For example, when the MCU's timer is responsible for waking up the CPU, and the timer clock is ACLK, then ACLK must remain in active mode, then the MCU can work in LPM3 mode; but if the timer clock is DCO, then the MCU must work in LPMO.

2 scheme design skills 2.1 watchdog and crystal oscillator configuration In any MSP430 microcontroller, the watchdog defaults to 32ms reset, so the first step of program initialization should first configure the watchdog WDT. If the WDT is not processed quickly during initialization, the watchdog may overflow quickly causing the chip program to reset. To prevent the CPU from entering the reset infinite loop, the watchdog needs to be configured at the very beginning of the program by setting the time, setting the control bits, or turning off the watchdog.

In the MSP430F4xx or MSP430F2xx microcontrollers, when using the LFXT1 low-frequency crystal oscillator, the corresponding load capacitance of the crystal oscillator should be configured through the FLL_CTL0 register before the code is run. Otherwise, the crystal oscillator may not operate normally. This technique is used in many TI MSP430 microcontroller application example codes. Visible in both.

2.2 Using TI's standard header files

TJ provides a standard header file for each MSP430 microcontroller. The file contains all the register and bit constant definition information in the corresponding chip model. Using the information defined in the file can greatly improve the readability of the program and increase System portability allows other designers to quickly master the design of the program. These header files can be applied to every application and code instance of TI.

2.3 Using internal functions to handle low power modes and other functions

When the MSP430 microcontroller uses the C language to develop software, it can directly call several internal functions included in the compiler. Sometimes the only way to handle some key events is to use internal functions. Using internal functions can greatly improve the efficiency of task processing.

The most common example of using internal functions is to enter and exit the low-power mode. Usually, some bits in the CPU register must be operated in C language. For example, if you need to enter LPM3 mode in the IAR compiler environment, you can use it directly. Internal function:

BISSR (LPM3_bits+GIE).

Other internal functions sometimes have optimization functions. For example, the MSP430 microcontroller converts the internal instruction function of the BCD code. If there is no combination of these instructions, a long string of C language code is required, and the editor cannot directly convert the code into MSP430 micro control. The BCD math instruction recognized by the device. The use of internal functions allows people who use C programming to maximize program code and reduce system power consumption.

The development environment compiler for the MSP430 microcontroller contains a series of function functions, and you should pay attention to the latest version when developing the design.

2.4 crystal oscillator failure processing function

The MSP430 microcontroller has the ability to check if the clock has failed. All microcontroller families offer fail-safe detection of internal DCO and high frequency crystal oscillators. The MSP430F4xx and MSP430F2xx series feature full clock failure detection, such as external 32768Hz low frequency clock and high frequency clock failure detection.

Specific details can be found in the user manual.

This article mainly considers two cases of crystal failure and considers whether it is necessary to deal with it.

(1) The process of crystal stabilization when each MCU is powered up. In this process, the failure of the crystal oscillator is caused. Especially for the initialization of the low-frequency crystal oscillator, the clock stabilization takes a certain time, often several thousand milliseconds.

(2) Crystal oscillator failure occurs during the running of the program. For example, a certain conductor shorts the crystal pin, so some special methods are needed. When the crystal fails, the DC0 clock module will continue to support the CPU.

If the clock source ACLK, SMCLK, or crystal oscillator is not stable, any peripherals that use these clocks will be affected. The only way is to detect and process through software. When some time-sensitive peripherals (such as timers) are clocked by low-frequency crystal oscillators, if the crystal oscillator is not oscillating, the initialization will fail. If the program code does not wait until the crystal oscillator is stable, the result of the peripheral output is unpredictable.

If the MCLK clock source of the system is provided by LFXT1, LT×T2, and the crystal oscillator fails, the MCLK clock source will automatically switch to the internal DCO clock module, and the internal DCO will be used as the MCLK clock source.

A simple method is to initialize the crystal can be kept clear repeated, waits for detection of the default flag oscillator fail, until the oscillator fail flag is not set, can refer to user manual. For example, MSP430F: 2xx series, LF×T1 low-frequency crystal oscillator can not detect whether the crystal oscillator fails. At this time, a delay program can be called to ensure sufficient initialization stabilization time. This method does not capture whether the crystal oscillator has failed during normal operation of the normal program, but can be processed in the NMI interrupt service subroutine by setting the OFIE bit.

2.5 gradually increase the MCLK frequency

The MCLK of most MSP430 microcontrollers can be configured to 8MHz, and the MSP430F2xx can reach 16MHz. The system requires that the VCC voltage value increase with increasing frequency. When the program runs, if set the desired ratio of the MCLK frequency voltage VCC is supplied to the real unpredictable problems occurring in the high voltage value during the microcontroller is run.

Even if the actual working VCC value of the MCU is much higher than the actual working voltage required by the operating frequency, it takes a certain time for the VCC to rise to a steady state, so we must ensure that the chip voltage can support the normal operation of the crystal before the operating frequency rises.

If the MCU includes a SVS (voltage detection module) module, SVS may alert the system voltage VCC reaches a desired voltage value; when the MCU module SVS does not have, but contains ADC module available, the ADC module may sample the voltage VCC, so that Analyzing chip voltage value before lifting frequency is a high frequency to meet job requirements.

2.6 initialization function

Usually when the C compiler writes code, the program automatically initializes all defined memory and inserts the code values ​​into the main() main program. If you define a lot of memory (large number of variables), it will lead to overflow of the watchdog. When many variables are initialized, the time taken will be so long that the watchdog has overflowed before executing the first statement of the main() main program, which will result in an infinite loop.

. This phenomenon generally occurs when the microcontroller's RAM exceeds 2K.

To avoid this, the easiest and straightforward way is to not initialize the variables that do not need to be initialized at the very beginning. For example, when using IAR, replace the long string of initialization code with a long code matrix statement _no_init_intx_arrav[2500] to avoid watchdog overflow.

In addition to the above methods, the designer can also define an initialization function to handle the overflow of the watchdog. The RAM memory is still initialized, but the watchdog is first configured. In an IAR environment, we can usually add a _low_level_init() code to the watchdog configuration code, for example:

Void_low_level_init(){WDTCTL=WDTPW+WDTHOLD]

If the above initialization function is not available in the development environment, you can modify the startup file before each C program. When initializing a large number of initialization codes of the MSP430F4xx series, you can configure the LF×T1 crystal oscillator capacitor in the initialization or startup file, so that more start-up time required for the crystal oscillator can be supplied before the main program starts to make the crystal oscillator stable. Section 2.1.

2.7 Support for online programming (ISP)

If you use the ISP function of the MSP430 microcontroller to erase the FALSH memory, to ensure proper read and write, you should pay attention to the following points:

(1) Set the FFF value of the correct FLASH erasing frequency, otherwise the result of programming is unpredictable.

(2) Set the FALSH lock bit when the ISP operation is completed to prevent accidental program interference.

(3) Pay attention to the cumulative programming time of the FALSH module.

(4) Provide sufficient VCC voltage. The FLASH programming voltage VCC value must be larger than the minimum programming/erasing voltage in the data sheet and slightly higher than the CPU operating voltage requirement.

When performing FLASH programming, it may be interfered by voltage during power-on, or it may be interfered when the power supply battery voltage is insufficient (but can also maintain CPU operation), even when the initial voltage is high enough, but when it starts The FLASH erase current draws a large amount, pulling the voltage down to be less than the minimum threshold voltage.

The VCC voltage can be verified by the SVS or ADC module. The advantage of the SVS module is that it can provide continuous detection during operation. How to configure the SVS module to monitor the VCC voltage before FLASH erasing can refer to the mclk described in 2.5. c program.

2.8 Verifying Flash Integrity with Checksum Function

In some critical applications, periodically performing checksum functions ensures the integrity of the FLASH memory. According to the need of redundant data, the check value can be stored in one or more memories, and the check chart provided by the check value can be used as a basis for processing the error occurrence.

3 Conclusion

The programming method and programming considerations of the MSP430 microcontroller described in this article are some of the experiences of the author in the process of developing products in the MSP430 for many years. Following these principles can greatly improve the reliability of the product.

Small household appliances

tcl , https://www.tclgroupss.com