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