您的位置:首页 > 其它

S3C2440 定时器

2015-08-24 11:21 381 查看
一、2440定时器

timer0~timer3 有pwm功能,timer4 没有Tout引脚,只能作为内部定时使用。



二、使用timer0 的pwm功能播放电子音乐(原理:Tout 引脚分别会在定时器到达比较值,中断产生(计数值为0)时发生电平翻转,所以,为初值寄存器和比较值寄存器设置一定的值,就能产生特定频率的声音)

/***********************************************
Function  name  :   timer0_handle
Description     :   定时器0中断处理函数
Input parameter :   none
Return          :   none
Others		    :
*************************************************/
void __irq timer0_handle(void)
{

//清中断
rSRCPND |= 0x1<<10;
rINTPND |= 0x1<<10;
}

/***********************************************
Function  name  :   timer0_init
Description     :   定时器0初始化
Input parameter :   none
Return          :   none
Others		    :
*************************************************/
void timer0_init()
{
//Frequency = PCLK / {prescaler value+1} / {divider value}
//算出时钟频率为250kHz ,每个时钟周期是 4us
//设置配置寄存器0:timer0/1 预分频的值99
rTCFG0 &= ~(0x7f<<0);
rTCFG0 |= (0x63<<0);

//设置配置寄存器1:timer0 分频值为2
rTCFG1 &= ~(0xf<<0);

//配置定时器控制寄存器:timer0
//不使能死区操作、自动装载、输出电平不翻转
rTCON |= (1<<1);
rTCON &=  ~(1<<2);
rTCON |= (1<<3);    /* auto-reload */

//开启定时器中断
rINTMSK &= ~(1<<10);
rINTPND |= 0x1<<10;

//设置定时器中断处理函数
pISR_TIMER0 = (unsigned)timer0_handle;
}

/***********************************************
Function  name  :   timer0_setinitval
Description     :   定时器0设置初值
Input parameter :   val:初值
Return          :   none
Others		    :
*************************************************/
void timer0_setinitval(unsigned val)
{
//设置定时器初值寄存器
rTCNTB0 = val;
}

/***********************************************
Function  name  :   timer0_setcmptval
Description     :   定时器0设置比较值
Input parameter :   val:比较值
Return          :   none
Others		    :
*************************************************/
void timer0_setcmptval(unsigned val)
{
//设置定时器比较寄存器
rTCMPB0 = val;
}

/***********************************************
Function  name  :   timer0_start
Description     :   定时器0启动
Input parameter :   none
Return          :   none
Others		    :
*************************************************/
void timer0_start()
{
//设置GPB0引脚为TOUT0
rGPBCON &= ~(0x3<<0);
rGPBCON |= (0x2<<0);

rTCON |= (1<<1);   /* set manual update */
rTCON |= (1<<0);   /* start timer 0 */
rTCON &= ~(1<<1); /* clean manual update */
}

/***********************************************
Function  name  :   timer0_stop
Description     :   定时器0关闭
Input parameter :   none
Return          :   none
Others		    :
*************************************************/
void timer0_stop()
{
//停止定时器
rTCON &= ~(0x1 << 0);
//设置引脚为输出,并输出低电平
rGPBCON &= ~(0x3 << 0);
rGPBCON |= (0x1 << 0);
rGPBDAT &= ~(0x1 << 0);
}

/***********************************************
Function  name  :   buzzer_stop
Description     :   蜂鸣器关闭
Input parameter :   none
Return          :   none
Others		    :
*************************************************/
void buzzer_stop()
{
//设置引脚为输出,并输出低电平
rGPBCON &= ~(0x3 << 0);
rGPBCON |= (0x1 << 0);
rGPBDAT &= ~(0x1 << 0);
}
//音调频率表
unsigned  Syllable_freq[30]={
0,262,294,330,349,392,440,494,0,0, 
0,523,587,659,698,784,880,988,0,0,
0,1046,1175,1318,1397,1568,1760,1976,0,0
};
/***********************************************
Function  name  :   set_fre
Description     :   pwm将音调频率转换成初值和比较值并设置
Input parameter :   fre:频率
Return          :   none
Others		    :
*************************************************/
void set_fre(unsigned fre)
{
timer0_setinitval(T0FREQ/fre);
//占空比为1/4
timer0_setcmptval(T0FREQ/fre/2);
}

/***********************************************
Function  name  :   music_play
Description     :   播放电子音乐
Input parameter :   music[]:音调数组
Return          :   none
Others		:   音乐存储格式为以999结束,每个元素为三位数整数的数组。其中每个元素的个位表示节拍,十位和百位表示音调
*************************************************/
void music_play(short music[])
{
short tone,beat;

//初始化定时器0
timer0_init();

while(*music != 999)
{
tone = (*music)/10;
beat = (*music)%10;
if(0 == tone)
{
buzzer_stop();
}
else
{
set_fre(Syllable_freq[tone]);
timer0_start();
}
Delay_1Ms(Aquater*beat);
timer0_stop();
music++;
}
timer0_stop();
}

三、使用timer4作为内部定时器,用来准确延时、模拟多线程

/***********************************************
Function  name  :   timer4_handle
Description     :   定时器4中断处理函数
Input parameter :   none
Return          :   none
Others		:   该定时器用来准确计时,模拟多线程
*************************************************/
void __irq timer4_handle(void)
{
//清中断
rSRCPND |= 0x1<<14;
rINTPND |= 0x1<<14;

count++;
switch(count%4>
{ case 1:
.......
}

}

/***********************************************
Function  name  :   timer4_start
Description     :   定时器4启动
Input parameter :   none
Return          :   none
Others		    :
*************************************************/
void timer4_start()
{

rTCON |= (1<<21);   /* set manual update */
rTCON |= (1<<20);   /* start timer 4 */
rTCON &= ~(1<<21); /* clean manual update */
}

/***********************************************
Function  name  :   timer4_init
Description     :   定时器4初始化
Input parameter :   none
Return          :   none
Others		    :
*************************************************/
void timer4_init()
{
//Frequency = PCLK / {prescaler value+1} / {divider value}
//算出时钟频率为250kHz ,每个时钟周期是 4us
//设置配置寄存器0:timer2~4 预分频的值99
rTCFG0 &= ~(0x7f<<8);
rTCFG0 |= (0x63<<8);

//设置配置寄存器1:timer4 分频值为2
rTCFG1 &= ~(0xf<<16);

//配置定时器控制寄存器:timer4
rTCON |= (1<<21);
rTCON |= (1<<22);    /* auto-reload */

//开启定时器中断
rINTMSK &= ~(1<<14);
rSRCPND |= 0x1<<14;
rINTPND |= 0x1<<14;

//设置初值
rTCNTB4 = INITVAL;

//设置定时器中断处理函数
pISR_TIMER4 = (unsigned)timer4_handle;

timer4_start();

}

/***********************************************
Function  name  :   Delay_1Ms
Description     :   定时器延时函数
Input parameter :   time:延时的毫秒数
Return          :   none
Others		    :
*************************************************/
void Delay_1Ms(unsigned time)
{
count = 0;
while(count < time);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: