Timer

来源:互联网 发布:mac 远程桌面共享文件 编辑:程序博客网 时间:2024/05/16 01:17


//=========================================================================
//  工程名称: Demo_2440_timer  
//  组成文件: timer.c
//  功能描述: 实现0.5S的定时输出信息,练习定时器的使用
//  硬件连接:
//=========================================================================


#include "2440addr.h"
#include "uart.h"
#include "2440lib.h"
     
void Timer_init(void);
void __irq timer0Handler(void);


int Main(void) 
{    
int i = 0,j=0;
memcpy(0x0,(unsigned char *)0x30000000,0x1000);    //拷贝程序到0地址


ChangeMPllValue(0x5c,1,1); //FCLK = 400M
ChangeClockDivider(14,12); //HCLK = 100M , PCLK = 50M


Uart_Select(0);
myUart_Init(50000000,115200);


Uart_Printf("Timer Test\n");
Timer_init();


  rTCON &= ~(0x0f<<0);   //寄存器清0
rTCON |= (0x09<<0);    //启动定时器
while(1);


}


void Timer_init(void)
{


rTCFG0 &= ~(0xff<<0);   //寄存器清0
rTCFG0 |= (0x63<<0);    //prescaler0 100     timer0的时钟频率为50M/100 = 0.5MHz
  
  rTCFG1 &= ~(0x0f<<0);   //寄存器清0
  rTCFG1 |= (0x01<<0);    //divide value 1/4   timer0的时钟频率为50M/100/4 = 0.5MHz/4 = 125KHz
  
rTCNTB0 = 62500;  //设置Timer初值 n=f*t=125K × 0.5s = 62500;


  rTCON &= ~(0x0f<<0);    //寄存器清0 ,以一个定时器的控制位为单位进行设置
  rTCON |= (0x02<<0);  //自动重载, 反向关闭,手运装载初值 ,暂不启动(第一次启动前先得手运装载一次初值)


pISR_TIMER0=(unsigned)timer0Handler; //建立中断向量表
EnableIrq(BIT_TIMER0);
}


void __irq timer0Handler(void)
{
static int cont_num = 0;
cont_num++;
Uart_Printf("timer0 test cont: %d\n",cont_num);

    ClearPending(BIT_TIMER0);
}