MSP430的看门狗常见用法以及中断函数的书写方法
[复制链接]
本帖最后由 Aguilera 于 2017-10-21 10:53 编辑
看门狗是为了防止程序跑飞而设定的,但是由于看门狗是一个类似于定时器,因此可以把他当作定时器来使用 示例代码:用看门狗定时器使一个led闪烁 #include void main( void ){ // Stop watchdog timer to prevent time out reset WDTCTL = WDT_MDLY_32;//宏定义 IE1 |=WDTIE; //允许看门狗定时器中断 P2DIR |= BIT7; _BIS_SR(LPM0_bits+GIE);//在这里的话将cpu进入低功耗模式}#pragma vector = WDT_VECTOR__interrupt void watchdag_timer(void){ P2OUT ^=BIT7;//异或操作基本上是取反操作}
注意这里有一个比较好的宏定义:WDTCTL = WDT_MDLY_32;进入该宏定义会发现类似的宏定义有很多 /* WDT-interval times [1ms] coded with Bits 0-2 *//* WDT is clocked by fSMCLK (assumed 1MHz) */#define WDT_MDLY_32 (WDTPW+WDTTMSEL+WDTCNTCL) /* 32ms interval (default) */#define WDT_MDLY_8 (WDTPW+WDTTMSEL+WDTCNTCL+WDTIS0) /* 8ms " */#define WDT_MDLY_0_5 (WDTPW+WDTTMSEL+WDTCNTCL+WDTIS1) /* 0.5ms " */#define WDT_MDLY_0_064 (WDTPW+WDTTMSEL+WDTCNTCL+WDTIS1+WDTIS0) /* 0.064ms " *//* WDT is clocked by fACLK (assumed 32KHz) */#define WDT_ADLY_1000 (WDTPW+WDTTMSEL+WDTCNTCL+WDTSSEL) /* 1000ms " */#define WDT_ADLY_250 (WDTPW+WDTTMSEL+WDTCNTCL+WDTSSEL+WDTIS0) /* 250ms " */#define WDT_ADLY_16 (WDTPW+WDTTMSEL+WDTCNTCL+WDTSSEL+WDTIS1) /* 16ms " */#define WDT_ADLY_1_9 (WDTPW+WDTTMSEL+WDTCNTCL+WDTSSEL+WDTIS1+WDTIS0) /* 1.9ms " *//* Watchdog mode -> reset after expired time *//* WDT is clocked by fSMCLK (assumed 1MHz) */#define WDT_MRST_32 (WDTPW+WDTCNTCL) /* 32ms interval (default) */#define WDT_MRST_8 (WDTPW+WDTCNTCL+WDTIS0) /* 8ms " */#define WDT_MRST_0_5 (WDTPW+WDTCNTCL+WDTIS1) /* 0.5ms " */#define WDT_MRST_0_064 (WDTPW+WDTCNTCL+WDTIS1+WDTIS0) /* 0.064ms " *//* WDT is clocked by fACLK (assumed 32KHz) */#define WDT_ARST_1000 (WDTPW+WDTCNTCL+WDTSSEL) /* 1000ms " */#define WDT_ARST_250 (WDTPW+WDTCNTCL+WDTSSEL+WDTIS0) /* 250ms " */#define WDT_ARST_16 (WDTPW+WDTCNTCL+WDTSSEL+WDTIS1) /* 16ms " */#define WDT_ARST_1_9 (WDTPW+WDTCNTCL+WDTSSEL+WDTIS1+WDTIS0) /* 1.9ms " */
这里我们可以根据通常的需要,选择合适的定时时间
说到定时时间,这里又不得不讲一下中断函数,在IAR这个编译环境中,一般中断函数的写法是 #pragma vector = 中断向量
__interrupt void 中断函数名(void)
{
用户程序;
} 注意,中断向量可以在430的头文件中查找,中断函数的声明必须是void 中断函数名(void),这是因为中断函数没有返回值,也没有中断的形式参数
|