📄 timer.c
字号:
/*****************************************************************************************
Description:The CPU clock : 200MHz,and the timer counter clock is CPU Clock /4 = 50MHz.
Ver 3.0 Jun 28th,2005 By Zhang ,IPRAI
*****************************************************************************************/
#include "timer.h"
#include <stdio.h>
void timer_start(int timerNum);
//unsigned int timer_count2us(unsigned int count);
void delay_us(int timerNum , unsigned int usec);
int TIMER_INTset_ms(int timerNum,int msec);
#if 0
/*-------------------------------------*/
void timer0_cnt_start();
unsigned int timer0_read();
void timer0_delay_us(unsigned int usec);
void timer0_halt();
void timer0_set(unsigned int Ivalue);
void timer0_start();
void timer0_INTset_ms(int msec);
void timer1_cnt_start();
unsigned int timer1_read();
void timer1_delay_us(unsigned int usec);
void timer1_halt();
void timer1_set(unsigned int Ivalue);
void timer1_start();
void timer1_INTset_ms(int msec);
/*-------------------------------------*/
#endif
/*-------------------------------------------------------------------------*/
/* timer_start() - used to start TIMER */
/*-------------------------------------------------------------------------*/
void timer_start(int timerNum)
{
// *(unsigned volatile int *)(TIMER_CTRL_ADDR(timerNum)) &= 0xff3f; /* hold the timer */
*(unsigned volatile int *)(TIMER_CTRL_ADDR(timerNum)) = 0;
// SET_BIT(TIMER_CTRL_ADDR(timerNum), CLKSRC); //Internal clock source
// RESET_BIT(TIMER_CTRL_ADDR(timerNum), C_P); //TIMER_PULSE_MODE
// *(unsigned volatile int *)(TIMER_CTRL_ADDR(timerNum)) |= ((1<<CLKSRC)+(TIMER_PULSE_MODE<<C_P)); /* use CPU CLK/8 */
*(unsigned volatile int *)(TIMER_PERIOD_ADDR(timerNum)) |= 0xffffffff;/* set for 32 bit cnt*/
// *(unsigned volatile int *)(TIMER_CTRL_ADDR(timerNum)) |= (1<<GO) + (1<<HLD); /* start the timer */
*(unsigned volatile int *)(TIMER_CTRL_ADDR(timerNum)) = (1<<GO) + (1<<HLD) + (0<<C_P) + (1<<CLKSRC);
}
#if 0
/*-------------------------------------------------------------------------*/
/* timer_count2us() - Translate used timer count to used time in us */
/*-------------------------------------------------------------------------*/
unsigned int timer_count2us(unsigned int count)
{
return (((count*1000.0)/((unsigned int)Internal_CLK_F/1000.0)));
}
#endif
/*-------------------------------------------------------------------------*/
/* delay_us() - used to delay DSP by user specified time in msec */
/*-------------------------------------------------------------------------*/
void delay_us(int timerNum , unsigned int usec)
{
unsigned int time_start;
// unsigned int timer_limit = (Internal_CLK_F/1000000)* usec;
unsigned int timer_limit = 50 * usec; //1us = 50 TIMER_CLK:200M/4
timer_start(timerNum);
time_start = TIMER_READ(timerNum);
while ((TIMER_READ(timerNum)-time_start) < timer_limit);
}
/************************************************************************************/
int TIMER_INTset_ms(int timerNum, int msec)
{
volatile TIMER_REG *timer_reg ;
if(timerNum>2)
return(1);
else
timer_reg = (volatile TIMER_REG *)(TIMER_CTRL_ADDR(timerNum));
timer_reg->Control = 0; /* Hold it */
timer_reg->Period = (50000 * msec); // 1ms=50 000 Timer_clk, (Internal_CLK_F/1000 * msec)
/*-----------------------------------**
** 9 CLKSRC = 1 CPU clock / 4 **
** 8 C/P = 1 Clock mode **
** 7 /HLD = 1 Allowed to count **
** 6 GO = 1 Here we go **
**-----------------------------------*/
// timer_reg->Control = (1<<GO) +(1<<HLD) + (1<<C_P) + (1<<CLKSRC); //old code
/*************************************************************************/
timer_reg->Control = (1<<GO) + (1<<HLD) + (0<<C_P) + (1<<CLKSRC);
/*************************************************************************/
return (0);
}
#if 0
/*-------------------------------------------------------------------------*/
/* C6000_Set_Up_TIMER_Tick_In_Microseconds() - 设置时钟中断频率 */
/*-------------------------------------------------------------------------*/
int C6000_Set_Up_TIMER_Tick_In_Microseconds (int timerNum,int Microseconds)
{
unsigned int clock_cycles ;
volatile TIMER_REG *timer_reg ;
if(timerNum>2)
return(1);
else
timer_reg = (volatile TIMER_REG *)(TIMER_CTRL_ADDR(timerNum));
clock_cycles = (float)Microseconds * 1e-6 * Internal_CLK_F ;
timer_reg->Control = 0; /* Hold it */
timer_reg->Period = clock_cycles;
/*-----------------------------------**
** 9 CLKSRC = 1 CPU clock / 8 **
** 8 C/P = 1 Clock mode **
** 7 /HLD = 1 Allowed to count **
** 6 GO = 1 Here we go **
**-----------------------------------*/
// timer_reg->Control = (1<<GO) +(1<<HLD) + (1<<C_P) + (1<<CLKSRC); //old code
/*************************************************************************/
timer_reg->Control = (1<<GO) + (1<<HLD) + (0<<C_P) + (1<<CLKSRC); //modified by shiyan 2002-09-18
/*************************************************************************/
return (0);
}
/*-------------------------------------------------------------------------*/
/* timer0_cnt_start() - start TIMER0 to count */
/*-------------------------------------------------------------------------*/
void timer0_cnt_start()
{
*(unsigned volatile int *)TIMER0_CTL &= 0xff3f; /* hold the timer */
*(unsigned volatile int *)TIMER0_CTL |= 0x200; /* use CPU CLK/4 */
*(unsigned volatile int *)TIMER0_PRD |= 0xffffffff;/* set for 32 bit cnt*/
*(unsigned volatile int *)TIMER0_CTL |= 0x3C0; /* start the timer */
}
/*-------------------------------------------------------------------------*/
/* timer0_read() - read TIMER0 count */
/*-------------------------------------------------------------------------*/
unsigned int timer0_read()
{ unsigned int i;
i = *(unsigned volatile int *)TIMER0_COUNT;
return i;
}
/*-------------------------------------------------------------------------*/
/* timer0_delay_us() */
/*-------------------------------------------------------------------------*/
void timer0_delay_us(unsigned int usec) //timer clk = 50 MHz
{
unsigned int timer_limit = (usec*50); //1us = 50clk;
unsigned int time_start;
timer0_cnt_start();
time_start = timer0_read();
while ((timer0_read()-time_start) < timer_limit);
}
/************************************************************************************/
void timer0_halt() //Halt the timer1
{
*(unsigned volatile int *)TIMER0_CTL = 0xa10; //halt the timer
// Rsvd TSTAT INVINP CLKSRC C/P HLD GO Rsvd PWID DATIN DATOUT INVOUT FUNC
// 31 - 12 11 10 9 8 7 6 5 4 3 2 1 0
// 0 - 0 1 0 1 0 0 0 0 1 0 0 0 0
}
/************************************************************************************/
void timer0_set(unsigned int Ivalue) // Set the timer1's PRD register
{
*(unsigned volatile int *)TIMER0_PRD = Ivalue; //Set the initial value of PRD
}
/************************************************************************************/
void timer0_start() //Start the timer1
{
*(unsigned volatile int *)TIMER0_CTL |= 0x0c0; // Start the timer
// Rsvd TSTAT INVINP CLKSRC C/P HLD GO Rsvd PWID DATIN DATOUT INVOUT FUNC
// 31 - 12 11 10 9 8 7 6 5 4 3 2 1 0
// 0 - 0 1 0 1 0 1 1 0 1 0 0 0 0
}
/************************************************************************************/
void timer0_INTset_ms(int msec)
{
timer0_halt();
timer0_set(50000*msec); //1us=50 clk ,1ms=50000 clk
timer0_start();
}
/*-------------------------------------------------------------------------*/
/* timer1_cnt_start() - start TIMER1 to count */
/*-------------------------------------------------------------------------*/
void timer1_cnt_start()
{
*(unsigned volatile int *)TIMER1_CTL &= 0xff3f; /* hold the timer */
*(unsigned volatile int *)TIMER1_CTL |= 0x200; /* use CPU CLK/4 */
*(unsigned volatile int *)TIMER1_PRD |= 0xffffffff;/* set for 32 bit cnt*/
*(unsigned volatile int *)TIMER1_CTL |= 0x3C0; /* start the timer */
}
/*-------------------------------------------------------------------------*/
/* timer1_read() - read TIMER1 count */
/*-------------------------------------------------------------------------*/
unsigned int timer1_read()
{ unsigned int i;
i = *(unsigned volatile int *)TIMER1_COUNT;
return i;
}
/*-------------------------------------------------------------------------*/
/* timer1_delay_us() */
/*-------------------------------------------------------------------------*/
void timer1_delay_us(unsigned int usec) //timer clk = 50 MHz
{
unsigned int timer_limit = (usec*50); //1us = 50clk;
unsigned int time_start;
timer1_cnt_start();
time_start = timer1_read();
while ((timer1_read()-time_start) < timer_limit);
}
/************************************************************************************/
void timer1_halt() //Halt the timer1
{
*(unsigned volatile int *)TIMER1_CTL = 0xa10; //halt the timer
// Rsvd TSTAT INVINP CLKSRC C/P HLD GO Rsvd PWID DATIN DATOUT INVOUT FUNC
// 31 - 12 11 10 9 8 7 6 5 4 3 2 1 0
// 0 - 0 1 0 1 0 0 0 0 1 0 0 0 0
}
/************************************************************************************/
void timer1_set(int Ivalue) // Set the timer1's PRD register
{
*(unsigned volatile int *)TIMER1_PRD = Ivalue; //Set the initial value of PRD
}
/************************************************************************************/
void timer1_start() //Start the timer1
{
*(unsigned volatile int *)TIMER1_CTL |= 0x0c0; // Start the timer
// Rsvd TSTAT INVINP CLKSRC C/P HLD GO Rsvd PWID DATIN DATOUT INVOUT FUNC
// 31 - 12 11 10 9 8 7 6 5 4 3 2 1 0
// 0 - 0 1 0 1 0 1 1 0 1 0 0 0 0
}
/************************************************************************************/
void timer1_INTset_ms(int msec)
{
timer1_halt();
timer1_set(50000*msec); //1us=50 clk ,1ms=50000 clk
timer1_start();
}
/*------------------------------------------------------------------*/
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -