S3C6410裸機(jī)定時器
#include "system.h"
#include "timer.h"
//定時器0中斷處理程序
void (*Timer0_Isr)(void) = NULL;
//定時器0中斷處理程序
void (*Timer1_Isr)(void) = NULL;
//定時器2中斷處理程序
void (*Timer2_Isr)(void) = NULL;
static void __irq Isr_Timer0(void)
{
rTINT_CSTAT |= BIT5; //清除中斷標(biāo)志
if(Timer0_Isr != NULL) //注冊過中斷服務(wù)程序
{
Timer0_Isr(); //調(diào)用中斷處理程序
}
VICInterruptEnd(); //中斷結(jié)束
}
void Timer0_Init(u32 RTime,FunctionalState EnInt,void (*TimerIsr)(void))
{
rTCFG0 &= (~0xff); //清除設(shè)置
rTCFG0 |= 65; //定時器0預(yù)分頻65+1,由PCLK=66提供時鐘,66分頻產(chǎn)生1MHz的定時器時鐘
rTCON &= (~0xff); //清除設(shè)置
rTCON |= BIT3; //定時器0自動更新使能
rTCNTB0 = RTime; //重裝值
rTINT_CSTAT |= BIT5; //清除中斷標(biāo)志
rTINT_CSTAT |= (EnInt == ENABLE) ? BIT0 : 0; //使能定時器0中斷
Timer0_Isr = TimerIsr; //注冊中斷服務(wù)程序
Set_IsrAddr(INT_TIMER0,(u32)Isr_Timer0); //設(shè)置中斷矢量入口
Set_IntEnable(INT_TIMER0,EnInt); //使能定時器0全局中斷
//以下操作啟動定時器0
rTCON |= BIT1; //手動更新
rTCON &= ~BIT1; //結(jié)束手動更新
rTCON |= BIT0; //啟動定時器0
}
static void __irq Isr_Timer1(void)
{
rTINT_CSTAT |= BIT6; //清除中斷標(biāo)志
if(Timer1_Isr != NULL) //注冊過中斷服務(wù)程序
{
Timer1_Isr(); //調(diào)用中斷處理程序
}
VICInterruptEnd(); //中斷結(jié)束
}
void Timer1_Init(u32 RTime,FunctionalState EnInt,void (*TimerIsr)(void))
{
rTCFG0 &= (~0xff); //清除設(shè)置
rTCFG0 |= 65; //定時器0預(yù)分頻65+1,由PCLK=66提供時鐘,66分頻產(chǎn)生1MHz的定時器時鐘,
rTCON &= (~0xff00); //清除設(shè)置
rTCON |= BIT11; //定時器1自動更新使能
rTCNTB1 = RTime; //重裝值
rTINT_CSTAT |= BIT6; //清除中斷標(biāo)志
rTINT_CSTAT |= (EnInt == ENABLE) ? BIT1 : 0; //使能定時器0中斷
Timer1_Isr = TimerIsr; //注冊中斷服務(wù)程序
Set_IsrAddr(INT_TIMER1,(u32)Isr_Timer1); //設(shè)置中斷矢量入口
Set_IntEnable(INT_TIMER1,EnInt); //使能定時器1全局中斷
//以下操作啟動定時器0
rTCON |= BIT9; //手動更新
rTCON &= ~BIT9; //結(jié)束手動更新
rTCON |= BIT8; //啟動定時器0
}
static void __irq Isr_Timer2(void)
{
rTINT_CSTAT |= BIT7; //清除中斷標(biāo)志
if(Timer2_Isr != NULL) //注冊過中斷服務(wù)程序
{
Timer2_Isr(); //調(diào)用中斷處理程序
}
VICInterruptEnd(); //中斷結(jié)束
}
void Timer2_Init(u32 RTime,FunctionalState EnInt,void (*TimerIsr)(void))
{
rTCFG0 &= ~(0xff << 8); //清除設(shè)置
rTCFG0 |= 65 << 8; //定時器2預(yù)分頻65+1,由PCLK=66提供時鐘,66分頻產(chǎn)生1MHz的定時器時鐘,
rTCON &= ~(0xf << 12); //清除設(shè)置
rTCON |= 1 << 15; //定時器2自動更新使能
rTCNTB2 = RTime; //重裝值
rTINT_CSTAT |= BIT7; //清除中斷標(biāo)志
rTINT_CSTAT |= (EnInt == ENABLE) ? BIT2 : 0; //使能定時器2中斷
Timer2_Isr = TimerIsr; //注冊中斷服務(wù)程序
Set_IsrAddr(INT_TIMER2,(u32)Isr_Timer2); //設(shè)置中斷矢量入口
Set_IntEnable(INT_TIMER2,EnInt); //使能定時器1全局中斷
//以下操作啟動定時器2
rTCON |= BIT13; //手動更新
rTCON &= ~BIT13; //結(jié)束手動更新
rTCON |= BIT12; //啟動定時器2
}
void RunTimeInit(void)
{
Timer2_Init(0xffffffff,DISABLE,NULL);
}
void RunTimeReset(void)
{
rTCON &= ~BIT12; //停止定時器2
nop;nop;
rTCON |= BIT13; //手動更新
rTCON &= ~BIT13; //結(jié)束手動更新
rTCON |= BIT12; //啟動定時器2
}
u32 GetRunTime(void)
{
return (0xffffffff - rTCNTO2);
}
#ifndef TIMER_H_
#define TIMER_H_
void Timer0_Init(u32 RTime,FunctionalState EnInt,void (*TimerIsr)(void));//定時器0初始化函數(shù)
void Timer1_Init(u32 RTime,FunctionalState EnInt,void (*TimerIsr)(void));//定時器1初始化函數(shù)
void Timer2_Init(u32 RTime,FunctionalState EnInt,void (*TimerIsr)(void));//定時器2初始化函數(shù)
void RunTimeInit(void);//程序運(yùn)行時間計算初始化
void RunTimeReset(void);//程序運(yùn)行時間計算計數(shù)器復(fù)位并開始
u32 GetRunTime(void);//獲取程序運(yùn)行時間
#endif
評論