S3C6410裸机定时器
2016-04-05 来源:eefocus
#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; //清除中断标志
if(Timer0_Isr != NULL) //注册过中断服务程序
{
Timer0_Isr(); //调用中断处理程序
}
VICInterruptEnd(); //中断结束
}
void Timer0_Init(u32 RTime,FunctionalState EnInt,void (*TimerIsr)(void))
{
rTCFG0 &= (~0xff); //清除设置
rTCFG0 |= 65; //定时器0预分频65+1,由PCLK=66提供时钟,66分频产生1MHz的定时器时钟
rTCON &= (~0xff); //清除设置
rTCON |= BIT3; //定时器0自动更新使能
rTCNTB0 = RTime; //重装值
rTINT_CSTAT |= BIT5; //清除中断标志
rTINT_CSTAT |= (EnInt == ENABLE) ? BIT0 : 0; //使能定时器0中断
Timer0_Isr = TimerIsr; //注册中断服务程序
Set_IsrAddr(INT_TIMER0,(u32)Isr_Timer0); //设置中断矢量入口
Set_IntEnable(INT_TIMER0,EnInt); //使能定时器0全局中断
//以下操作启动定时器0
rTCON |= BIT1; //手动更新
rTCON &= ~BIT1; //结束手动更新
rTCON |= BIT0; //启动定时器0
}
static void __irq Isr_Timer1(void)
{
rTINT_CSTAT |= BIT6; //清除中断标志
if(Timer1_Isr != NULL) //注册过中断服务程序
{
Timer1_Isr(); //调用中断处理程序
}
VICInterruptEnd(); //中断结束
}
void Timer1_Init(u32 RTime,FunctionalState EnInt,void (*TimerIsr)(void))
{
rTCFG0 &= (~0xff); //清除设置
rTCFG0 |= 65; //定时器0预分频65+1,由PCLK=66提供时钟,66分频产生1MHz的定时器时钟,
rTCON &= (~0xff00); //清除设置
rTCON |= BIT11; //定时器1自动更新使能
rTCNTB1 = RTime; //重装值
rTINT_CSTAT |= BIT6; //清除中断标志
rTINT_CSTAT |= (EnInt == ENABLE) ? BIT1 : 0; //使能定时器0中断
Timer1_Isr = TimerIsr; //注册中断服务程序
Set_IsrAddr(INT_TIMER1,(u32)Isr_Timer1); //设置中断矢量入口
Set_IntEnable(INT_TIMER1,EnInt); //使能定时器1全局中断
//以下操作启动定时器0
rTCON |= BIT9; //手动更新
rTCON &= ~BIT9; //结束手动更新
rTCON |= BIT8; //启动定时器0
}
static void __irq Isr_Timer2(void)
{
rTINT_CSTAT |= BIT7; //清除中断标志
if(Timer2_Isr != NULL) //注册过中断服务程序
{
Timer2_Isr(); //调用中断处理程序
}
VICInterruptEnd(); //中断结束
}
void Timer2_Init(u32 RTime,FunctionalState EnInt,void (*TimerIsr)(void))
{
rTCFG0 &= ~(0xff << 8); //清除设置
rTCFG0 |= 65 << 8; //定时器2预分频65+1,由PCLK=66提供时钟,66分频产生1MHz的定时器时钟,
rTCON &= ~(0xf << 12); //清除设置
rTCON |= 1 << 15; //定时器2自动更新使能
rTCNTB2 = RTime; //重装值
rTINT_CSTAT |= BIT7; //清除中断标志
rTINT_CSTAT |= (EnInt == ENABLE) ? BIT2 : 0; //使能定时器2中断
Timer2_Isr = TimerIsr; //注册中断服务程序
Set_IsrAddr(INT_TIMER2,(u32)Isr_Timer2); //设置中断矢量入口
Set_IntEnable(INT_TIMER2,EnInt); //使能定时器1全局中断
//以下操作启动定时器2
rTCON |= BIT13; //手动更新
rTCON &= ~BIT13; //结束手动更新
rTCON |= BIT12; //启动定时器2
}
void RunTimeInit(void)
{
Timer2_Init(0xffffffff,DISABLE,NULL);
}
void RunTimeReset(void)
{
rTCON &= ~BIT12; //停止定时器2
nop;nop;
rTCON |= BIT13; //手动更新
rTCON &= ~BIT13; //结束手动更新
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初始化函数
void Timer1_Init(u32 RTime,FunctionalState EnInt,void (*TimerIsr)(void));//定时器1初始化函数
void Timer2_Init(u32 RTime,FunctionalState EnInt,void (*TimerIsr)(void));//定时器2初始化函数
void RunTimeInit(void);//程序运行时间计算初始化
void RunTimeReset(void);//程序运行时间计算计数器复位并开始
u32 GetRunTime(void);//获取程序运行时间
#endif