Design of Software and Hardware C Delay Program for MSP430 Single Chip Microcomputer

Publisher:PositiveEnergyLatest update time:2016-01-20 Source: eefocusKeywords:MSP430 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
MSP430 is an ultra-low power 16-bit microcontroller that is increasingly favored by electronic engineers and widely used. C programs are intuitive,
Good readability, easy to transplant and maintain, has been adopted by many single-chip microcomputer programmers. MSP430 integrated development environment (such as
IAR Embedded Workbench and AQ430) integrates C compiler and C language debugger C-SPY. However,
it is difficult to achieve accurate delay in C language, which has always troubled many MSP430
single-chip microcomputer programmers. In the actual project development process, the author encountered many interface devices that require strict timing control,
such as single bus digital temperature sensor DS18820, real-time clock chip PCF8563 (need to use ordinary 1/2C
bus timing), three-wire digital potentiometer AD8402, CF card (Compact Flash Card), etc., which all require μs or even nanons
level accurate delay; while some slow devices only need ms to s level delay. For this reason,
the author proposed a software or hardware accurate delay method suitable for different delay levels, and it has been actually applied with good results
, greatly shortening the development cycle.  
Hardware Delay
   The MSP430 single-chip microcomputer system program mostly adopts an event-driven mechanism, that is, the CPU
sleeps in low-power mode when there is no external event trigger. When an external event arrives, an interrupt is generated to activate the CPU and enter the corresponding interrupt service program (
ISR). The interrupt response program only completes two tasks, one is to set the flag of the corresponding event, and the other is to make the MCU
exit the low-power mode. The main program is responsible for switching the MCU between the low-power mode and the event handler,
that is, an infinite loop is set in the main program, and the system enters the low-power mode directly after initialization. After the MCU is awakened, it
determines whether each flag is set. If a single flag is set, the MCU executes the corresponding event handler and
enters the low-power mode after completion; if multiple flags are set at the same time,
the main program judges and processes them in turn according to the pre-arranged message queue. After all events are processed, the MCU
sleeps and the system enters a low-power state (the order of the message queue is set according to the priority of the task importance)
. In this front-end and back-end system, since the main program is an infinite loop, the watchdog must be turned off. Instead of idling,
it is better to use its timer function for hardware delay. Using the MSP430
microcontroller watchdog timer to achieve an arbitrary length of accurate delay not only meets the system's real-time low power requirements,
but also makes up for the shortcomings of using an infinite loop delay that is difficult to determine and takes up a long CPU time. The following example explains
the techniques for completing delays of different lengths in the same WDT ISR.
#pragma vector=WD_r_VECTOR
 interrupt void WDT_Delay(void)
 {
   //Watchdog interrupt service routineif
  ((DelayTime&Delay500ms)==Delay500ms)
 {
     //Judge whether the flag requiring 500 ms delay is setstatic
     unsigned int n250MS=O;
     n250MS++;
     if(n250MS==2)
    
   //Delay 250ms×2=500ms
        n250MS=0;          //Clear counterDelayTime
        &=~Delay500ms;   //Reset flag
      WDTCTL=WDTHOLD+WDTPW;
      1El&=~WDTlE;      //Turn off the watchdog timer and disable its interrupt
       }
   }
   if((DelayTime&Delay30s)==Delay30s)
 {
     //Judge whether the required 30 s delay flag is setstatic
     unsigned int nS=0;
     nS++;
     if(nS==30){           //Delay 1 s×30=30 s
     nS=0;             //Clear counter
     DelayTime&=~Delay30s;      //Reset flag
     WDTCTL=WDTHOLD+WDTPW;
     IEl&=~WDTlE;        //Turn off watchdog timer and disable its interrupt
   }
}
If task 1 requires a 500 ms delay, just execute the following statement where the delay is required:
   WDTCTL=WDT_ADLY_250;
   IE|=WDTIE;             //①
   DelayTime|=Delay500ms          //②
   while((DelayTime&Delay500ms)==Delay500ms);   //③
   ① is to configure the watchdog to work in timer mode, and WDT
generates an interrupt request every 250 ms. The clock beat can be changed as needed. When using a 32768 Hz crystal oscillator as the clock source,
a delay base of 1.9ms, 16 ms, 250 ms and 1000 ms can be generated. In the header file msp430xl4x.h,
the WDT configuration macros for these four flip times are defined as: WDT_ADLY_1_9, WDT_ADLY_16, WDT_ADLY_250 and
WDT_ADLY_1000. If DCOCLK is used as the clock source of SMCLK, WDT selects SMCLK = 1 MHz as the clock source,
so that 0.064 ms, 0.5 ms, 8 ms and 32 ms delay bases can be used.
   ② Set a flag bit to facilitate WDT ISR to identify and enter the corresponding delay branch.
③ Always identify the Delay500ms bit in the DelayTime flag group. If it is in the set state,
it means that the required delay has not arrived. Perform no operation until the delay time is reached, reset Delay500ms in WDTISR, jump out of
the while() loop, and execute the next instruction.
Similarly, if task 2 requires a 30 s delay, WDT interrupt is activated by WDTCTL=WDT_ADLY_1000, and the interrupt
is performed every 1 s. In the WDT ISR, the flag is judged to be set to Delay30s instead of Delay500ms, and the 30 s
delay program branch is executed. Each time the interrupt is performed, the counter nS is increased by 1 until it reaches 30, indicating that the 30 s delay is completed, the counter is cleared, and
the watchdog is stopped (WETCTL=WETHOLD+WDTPW;) to stop the interrupt and reset the delay flag
to notify the task that the delay time is up, and the following instructions can be executed.
In the WDT ISR
, any length of time delay can be realized according to the combination of the delay base and the counter. When designing the system program,
first determine the different delay times required, and then
add the corresponding delay branch in the WDT ISR. The embedded real-time operating system μC/OS-II transplanted to the MSP430
microcontroller uses the watchdog timer to generate clock beats.
For a relatively simple system, only a single delay is required. If the system power consumption is considered,
another method of using the watchdog timer interrupt to complete the delay is introduced. If the delay is 1 s, set the WDT
to interrupt once every 250 ms. When the delay is required, start the watchdog timer and allow its interrupt, and the system enters the low power mode 3 (there are 5
modes in total) to sleep. In the interrupt service program, the delay time is accumulated, and when it reaches 1 s, the CPU is woken up
and the watchdog timer interrupt is stopped. The example code is as follows:
void main(void)
 {
  WDTCTL=WDT_ADT_ADLY_250)    //Start WDT, interrupt once every 250 ms    
   IEII=WDTIE)       //Enable watchdog timer interrupt
   BIS_SR(LPM3_bitS+GIE);     //System sleeps in low power mode 3, turn on general interrupt    
}
#pragrna vector=WDT_VECTOR
__interrupt void WDT_Delay(void)    //Watchdog interrupt service routine

   statlc unsigned charn=4;
   if(一一n==0){          //Delay 4×250 ms=1 s
   _BlC_SR_IRQ(LPM3_blts);     //Wake up the CPU from low power mode 3              
   WDTCTL=WDTHOLD+WDTPW:
IEII&=~WDTIE;)      //Turn off the watchdog timer and disable its interrupts
}
This method makes full use of the ultra-low power consumption characteristics of the MSP430 series. During the waiting delay, the CPU
does not need to keep checking the flag to know that the delay has ended, but enters the power saving mode. During the waiting process,
only a very short time will accumulate time and make judgments in the interrupt service program. The CPU can be set to
enter different low-power modes LPMx as needed. If the system uses multiple peripheral interrupts
and there are statements to wake up the CPU in other interrupt service programs, this method is no longer applicable.
It is not suitable to use hardware delay for μs-level delays, because frequent in and out interrupts will cause the CPU
to spend a lot of time responding to interrupts and executing interrupt return operations. The hardware delay method is suitable for
long delays above the ms level.
Software delay
In the operation of the digital temperature sensor DS18820, the delays used are: 15 μs, 90 μs, 270 μs, 540 μs
, etc. These delays are short and do not take up too much CPU time,
so they are more suitable for software delay methods. It is easy to control the time of a program written in assembly language.
We know the execution time of each statement,
the execution time of each macro, and the time consumed by each subroutine and the statement called. Therefore, to
compile a more accurate delay program in C language, it is necessary to study the assembly code generated by the C program.
Loop structure delay: The delay time is equal to the product of the instruction execution time and the number of instruction loops. For example,
the following delay program is experimentally analyzed.
   void delay(unsigned int time){
while(time一一){};
In main(), call the delay function delayr(n); What is the delay time obtained? It is necessary to
compile and test in the integrated compilation environment IAR Embedded Workbench IDE 3.10A of the MSP430 microcontroller.
Use C430 to write a section of executable code, add a delay function to it, and call it in the main function. Take delay(1OO)
as an example. Set the project options Options, select Driver as Simulator in the Debugger column, and
perform software simulation. In the simulation environment C-SPY Debugger, call up the Disassembly and Register
windows from the View menu. The former displays the assembly program generated by the programming software based on the C language program. In the latter window, open the CPU
Register subwindow and observe the instruction cycle counter CYCLE-COUNTER. It can be seen that delay()
is compiled to obtain the following code segment:
   delav:
   001112  OF4C mov. w R12,R15
   OOlll4  0C4F mov. w  R15. R12
   001116    3C53  add. w  #0xFFFF. R12
   001118  0F93  tst.  w R15
00111A  FB23  jne   deIay
single-step execution, observe the CYCLECOUNTER, and find that the value of CYCLECOUNTER increases by 1 for each instruction executed, indicating that these 5
instructions each take up 1 instruction cycle, and the loop body while() requires 5 instruction cycles each execution,
plus the function call and function return each take up 3 instruction cycles, delay(100) delays 5×100+6-506
instruction cycles. As long as the instruction cycle is known,
the delay time can be easily calculated. The execution time of the delay function varies due to different loop statements and compilers.
According to the above method, the purpose of flexible programming can be achieved.
The instruction execution speed of MSP430 is the number of cycles used by the instruction. The clock cycle here refers to
the cycle of the main system clock MCLK. After the microcontroller is powered on, if the clock system is not set, the default 800 kHz DCOCLK is
the clock source of MCLK and SMCLK, and LFXTl is connected to a 32768 Hz crystal and works in low-frequency mode (XTS=O) as the clock source of ACLK. The instruction cycle of the CPU
is determined by MCLK, so the default instruction cycle is 1/800 kHz = 1.25 μs. To get an
instruction cycle of lμs, you need to adjust the DCO frequency, that is, MCLK = 1 MHz, just set it as follows: BCSCTLl = XT20FF + RSEL2;
   // Turn off the XT2 oscillator and set the DCO frequency to 1 MHz
   DCOCTL = DCO2
// Make the single instruction cycle lμs
It does not mean that the delay benchmark with the smallest delay of the MSP430 microcontroller software is lμs. When the XT2 = 8 MHz high-frequency oscillator is turned on,
the instruction cycle can reach 125 ns. The MSP430F4XX series of microcontrollers uses the enhanced frequency-locked loop technology FLL+, which
can double the DCO frequency to 40MHz, thereby obtaining the fastest instruction cycle of 25 ns.
The method of calling the delay function is suitable for delays between 100 μs and 1 ms.
Short delays below 100 μs are best achieved through the no-operation statement _NoP()
or any combination thereof. Macro definitions can be used to achieve the required delay. For example, if a delay of 3 μs is required, then: #define
DELAY5US{_NOP();_NOP();_NOP();} 
Conclusion The hardware delay solution and software delay method based on the MSP430 on-chip watchdog timer
   proposed in this paper meet the delay requirements of different time width levels, especially software delay. The assembly program analysis method is used to obtain the accurate delay time of the delay function, which greatly improves the software delay accuracy and program debugging efficiency, and is applied in a variety of chip interface programs with good operation results.



Keywords:MSP430 Reference address:Design of Software and Hardware C Delay Program for MSP430 Single Chip Microcomputer

Previous article:Example of calculating the initial value of a microcontroller timer
Next article:Communication between MCU and 24C02

Recommended ReadingLatest update time:2024-11-23 10:59

MSP430 non-simulated IIC bus control program
The learning experience of MSP430 is a transition from pain to deep affection for 430. Of course, it was quite annoying when I started learning. There is not much relevant information on the Internet. Even if there is information, it is not complete. It is not very convenient to refer to and learn. After many efforts a
[Microcontroller]
MSP430 non-simulated IIC bus control program
MSP430F5529 Extra (I) Commonly used built-in functions and some explanations
1) MSP430F5529 supports a maximum operating frequency of 25MHZ, which means that              there is a limit to how fast you can increase the system speed by multiplying the frequency of the phase-locked loop, and              the maximum can only be 25MHZ (no point in going any higher).   2) Several important in
[Microcontroller]
Compare ARM, AVR, MSP430, Coldfire, DSP, FPGA
1 I think this is meaningless. The biggest feature of embedded systems is the word "embedded", which means that your control system is embedded in your controlled object. Therefore, it must first obey the needs and characteristics of the object. What basis is there to discuss who is better and who is worse without con
[Microcontroller]
Design of frequency conversion servo system based on MSP430
  In recent years, the development of servo systems has always been based on stability, responsiveness and precision, which are also the factors that users value most during use. In machine tool servo systems, robot control systems, radar antenna control systems and other occasions, DC servo motors and DC servo control
[Microcontroller]
Design of frequency conversion servo system based on MSP430
How to generate HEX file in MSP430 IAR
When MSP430 is downloaded using JTAG, Hex files are not used. However, if Proteus is used for simulation (only Proteus 7.6 and above support MSP430 simulation), only Hex file simulation is supported, so it is necessary to output Hex files. Open the IAR project options and select Linker in the left column, as shown i
[Microcontroller]
How to generate HEX file in MSP430 IAR
Detailed explanation of SPI mode of MSP430G2xx1 series USI
Brief description There are three serial communication modules in the microcontroller of MSP430 series, namely USART, USI and USCI. USART supports two serial modes of the same hardware module, namely UART and SPI. USART implements independent transmit and receive conversion registers and separate transmit and receive
[Microcontroller]
Application of MSP430 MCU in medical equipment industry
MSP430 series microcontrollers are widely used in the medical equipment industry. They are favored by designers for their low power consumption and simple peripheral design. However, complex medical equipment such as electrocardiograms or CT are widely used. Electrocardiographs are one of the important instruments
[Microcontroller]
Application of MSP430 MCU in medical equipment industry
Design of portable medical automatic infusion device using MSP430 series single chip microcomputer
This paper introduces a design scheme of a medical automatic infusion device, which uses TI's MSO430 series single-chip microcomputer as the control chip, and is equipped with a display module, an infusion pump driver module, and a key control module, etc. The design scheme has a novel structure, small size, low pow
[Industrial Control]
Design of portable medical automatic infusion device using MSP430 series single chip microcomputer
Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号