系統(tǒng)時鐘和定時器——看門狗定時器
WATCHDOG定時器所涉及的寄存器如下:
本文引用地址:http://butianyuan.cn/article/201611/316625.htm1、WTCON寄存器
用于設置預分頻系數(shù)、選擇工作頻率、決定是否使能中斷、是否啟用WATDOG功能(即是否輸出復位信號)。
WATDOG定時器工作頻率=PCLK/(prescaler value+1)/(divider value)
prescaler value=0~255;
divider value=16,32,64,128;
芯片手冊中的WTCON如下圖:
#include "2440addr.h"
#define U32 unsigned int
void Delay(int a)
{
int k;
for(k=0;k
;
}
void __irq watchdog(void)
{
rGPBDAT |= 1; /*蜂鳴器響*/
/*清中斷標志位*/
/*注意接下來這兩行代碼。一般來說中斷標志清除順序為:SUBSRCPND-->SRCPND-->INTPND。但是在用CodeWarrior調(diào)用AXD調(diào)試過程中,并沒有因為其先后順序不同而出現(xiàn)中斷,但是當我點擊STOP鍵,之后再點擊GO鍵時,每一次都會聽到蜂鳴,這個現(xiàn)象是后來發(fā)現(xiàn)的,是不是每次STOP之后,相當于直接產(chǎn)生中斷信號,致使不能正常喂狗?這里不是很理解,希望哪位達人給予講解*/
rSRCPND = 0x1<<9;
rSUBSRCPND = 0x1<<13;
rINTPND = 0x1<<9;
}
void Main(void)
{
int light;
int temp;
int i;
rGPBCON = 0x155555; /*B0輸出,給蜂鳴器;B5~B8輸出,給LED*/
rGPBUP = 0x7ff;
rWTCON = 0xf9<<8;/*設置預分頻系數(shù)[15:8],以及時鐘選擇[4:3]。PCLK=50MHz Prescaler = 15*16+9=249, Division = WTCON[4:3]=0b00=16,時鐘頻率為12.5kHz*/
rWTDAT = 50000; /*設置看門狗定時器超時時間為4秒(50÷12.5)*/
rWTCNT = 50000;
rWTCON |= (1<<5)|(1<<2); /*開啟定時器[5],中斷使能[2],不發(fā)出復位信號[0]*/
/*中斷設置,看門狗有子中斷源*/
rSRCPND = 0x1<<9;
rSUBSRCPND = 0x1<<13;
rINTPND = 0x1<<9;
rINTSUBMSK = ~(0x1<<13);
rINTMSK = ~(0x1<<9);
pISR_WDT_AC97 = (U32)watchdog;
light = 0x000;
temp = light | 1;
rGPBDAT = ~temp;
Delay(500000); /*大約延時0.4S*/
while(1)
{
for(i=0;i<4;i++)
{
light = 1<<(5+i);
temp = light | 1;
rGPBDAT = ~temp;
Delay(500000);
if(i>=2)
{
light = 1<<(4+2*i);
temp = light | 1;
rGPBDAT = ~temp;
Delay(500000);
}
}
rWTCNT = 50000; /*喂狗,重新賦值,防止中斷*/
for(i=0;i<4;i++)
{
light = 1>>(5+i);
temp = light | 1;
rGPBDAT = ~temp;
Delay(500000);
if(i>=2)
{
light = 1>>(4+2*i);
temp = light | 1;
rGPBDAT = ~temp;
Delay(500000);
}
}
}
}
評論