MSP430的软硬件C延时程序设计

Publisher:温馨家园Latest update time:2016-08-15 Source: eefocusKeywords:MSP430  软硬件  C延时程序 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
MSP430是超低功耗16位单片机,越来越受到电子工程师亲睐并得到广泛应用。C程序直观,可读性好,易于移植和维护,已被很多单片机编程人员所采用。MSP430集成开发环境(如IAR Embedded Workbench和AQ430)都集成了C编译器和C语言级调试器C—SPY。但是C语言难以实现精确延时,这一直困扰着很多MSP430单片机程序员。笔者在实际项目开发过程中,遇到很多需要严格时序控制的接口器件,如单总线数字温度传感器DSl8820、实时时钟芯片PCF8563(需要用普通]/o模拟12C总线时序)、三线制数字电位器AD8402、CF卡(Compact Flash Card)等都需要μs级甚至纳ns级精确延时;而一些慢速设备只需要ms到s级的延时。为此,笔者提出了适合于不同延时级别需要的软件或硬件精确延时方法,并已实际应用,效果良好,大大缩短了开发周期。

 

1  硬件延时

    MSP430单片机系统程序多采用事件驱动机制,即在没有外部事件触发的情况下CPU休眠于低功耗模式中。当外部事件到来时,产生中断激活CPU,进入相应的中断服务程序(ISR)中。中断响应程序只完成两个任务,一是置位相应事件的标志,二是使MCU退出低功耗模式。主程序负责使MCU在低功耗模式和事件处理程序之间切换,即在主程序中设一个无限循环,系统初始化以后直接进入低功耗模式。MCU被唤醒后,判断各标志是否置位。如果是单一标志置位,那么MCU执行相应的事件处理程序,完成后转入低功耗模式;若是有多个标志同时置位,主程序按照事先排好的消息队列对它们依次判别并进行处理,所有事件处理完毕以后MCU休眠,系统进入低功耗状态(该消息队列的顺序是按照任务的重要性设定的优先级)。在这种前后台系统中,由于主程序是无限循环,就必须关闭看门狗,与其闲置,不如用其定时器的功能作硬件延时。使用MSP430单片机看门狗定时器实现任意时长精确延时,既满足了系统实时低功耗的要求,也弥补了使用无限循环延时的时间难确定和占用CPU时间长的缺点。通过下例,讲解在同一WDT ISR中完成不同时长延时的技巧。

  #pragma vector="WD"_r_VECTOR

  interrupt void WDT_Delay(void){

//看门狗中断服务程序

if((DelayTime&Delay500ms)==Delay500ms){

//判断需要500 ms延时的标志是否置位

static unsigned int n250MS=O;

n250MS++;

if(n250MS==2){    //延时250ms×2=500ms

      n250MS=0;    //清零计数器

      DelayTime&=~Delay500ms;//复位标志位

WDTCTL=WDTHOLD+WDTPW;

1El&=~WDTlE;//关闭看门狗定时器并禁止其中断

      }

}

if((DelayTime&Delay30s)==Delay30s){

//判断需要的30 s延时标志是否置位

static unsigned int nS="0";

nS++;

if(nS==30){    //延时1 s×30=30 s

nS=0;    //清零计数器

DelayTime&=~Delay30s;//复位标志位

WDTCTL=WDTHOLD+WDTPW;

IEl&=~WDTlE;  //关闭看门狗定时器并禁止其中断

        }

      }

}

   

    如果任务1需要500 ms的延时,只需在需要延时处执行如下语句:

WDTCTL=WDT_ADLY_250;

IE┃ =WDTIE;    //①

DelayTime┃=Delay500ms    //②

while((DelayTime&Delay500ms)==Delay500ms);  //③

    ①处是配置看门狗工作在定时器模式,WDT每隔250 ms产生一次中断请求。可以根据需要改变时钟节拍,在使用32768 Hz晶振作为时钟源时,可以产生1.9ms、16 ms、250 ms和1000 ms的延时基数。在头文件msp430xl4x.h中,将这4种翻转时间的WDT配置宏定义为:WDT_ADLY_1_9、WDT_ADLY_16、WDT_ADLY_250和WDT_ADLY_1000。如果用DCOCLK作为SMCLK的时钟源,WDT选择SMCLK=1 MHz为时钟源,这样可以有O.064 ms、0.5 ms、8 ms和32 ms延时基数可供使用。

    ②处设置一个标志位,方便WDT ISR判别并进入相应的延时分支。

    ③处一直判别DelayTime标志组中的Delay500ms位,如果处于置位状态,说明所需的延时未到,执行空操作,直到延时时间到,在WDTISR中将Delay500ms复位,跳出while()循环,执行下一条指令。

   

     同理,如果任务2需要30 s延时,通过WDTCTL=WDT_ADLY_1000激活WDT中断,每隔1 s进中断一次,在WDT ISR中判别标志发现是Delay30s置位而不是Delay500ms执行30 s延时程序分支。每中断一次,计数器nS加l,直到计到30,说明30 s延时完成,清零计数器,停止看门狗(WETCTL=WE)THOLD+WDTPW;)可停止产生中断,并复位该延时标志,以通知任务延时时间到,可以执行下面的指令了。

   

     在WDT ISR中可以根据延时基数和计数器的搭配实现任意长度的时间延时。在系统程序设计时,先确定所需的不同延时时间,然后在WDT。ISR中添加相应的延时分支即可。嵌入式实时操作系统μC/OS—II移植于MSP430单片机就是使用看门狗定时器产生时钟节拍的。

   

     对于系统比较简单,只需要单一时长的延时.而又要考虑系统功耗时,介绍另一种使用看门狗定时器中断完成延时的方法。若要延时1 s,则设定WDT每250 ms中断一次。在需要延时处,启动看门狗定时器并允许其中断,系统进入低功耗模式3(共有5种.模式)休眠。在中断服务程序中对延时时间累加,当达到1 s时唤醒CPU,并停止看门狗定时器中断。实例代码如下:

vold main(vold){

WDTCTL=WDT_ADT_ADLY_250)

//启动WDT,每250 ms中断一次

IEII=WDTIE)//使能看门狗定时器中断

_BIS_SR(LPM3_bitS+GIE);

//系统休眠于低功耗模式3,开总中断

}

#pragrna vector="WDT"_VECTOR

—interrupt void WDT_Delay(void){  //看门狗中断服务程序

statlc unsigned charn="4";

if(一一n==O){    //延时4×250 ms="1" s

    —BlC_SR_IRQ(LPM3_blts);

    //将CPU从低功耗模式3唤醒

    WDTCTL=WDTHOLD+WDTPW:

IEl&=~WDTIE;)

// Turn off the watchdog timer and disable its interrupt

}

   

    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 the end of the delay, but enters the power saving mode. During the waiting process, only a very short time will be accumulated and judged 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 has other interrupt service programs

This method is no longer applicable for statements that wake up the CPU.

   

    It is not suitable to use hardware delay for μs-level delay, because frequent 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-term delays above ms level.

 

2 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 time through programs 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 call statement. 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 one one){};

   

    Call the delay function delayr(n) in main(); to get the delay time, you need to compile a test in the integrated compilation environment IAR Embedded Workbench IDE 3.10A for 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, taking 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 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:

mouth:

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 you know the instruction cycle, you can easily calculate the delay time. The execution time of the delay function varies due to different loop statements and compilers. According to the above method, specific analysis can achieve the purpose of flexible programming.

   

    The instruction execution speed of MSP430 is the number of cycles used for 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 used as the clock source of MCLK and SMCLK, and LFXT1 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 a lμs instruction cycle, you need to adjust the DCO frequency, that is, MCLK=1 MHz, and just set it as follows: BCSCTLl=XT20FF+RSEL2;

    // Turn off XT2 oscillator and set DCO frequency to 1 MHz

    DCOCTL=DCO2

    // Make the single instruction cycle lμs

   

    It does not mean that the minimum delay benchmark of MSP430 microcontroller software delay 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, thus 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. You can use macro definitions to achieve the required delay. For example, if you want a delay of 3 μs, 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 the 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. It is also applied in a variety of chip interface programs with good running effect.

Keywords:MSP430  软硬件  C延时程序 Reference address:MSP430的软硬件C延时程序设计

Previous article:Some commonly used C language control programs for msp430 --- ADC12 (1)
Next article:Defining your own flags with bit segments in MSP430

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号