📄 timer.c
字号:
/*************************************************************************/
/* FUNCTIONS */
/* Timer basic library functions: */
/* */
/* tm_init Initialize the timer with 1 sec unit */
/* tmReset Clear timer counter and interrupt pending, */
/* Restart timer */
/* tmCntr Change the timer mode,periode,interrupt */
/* TimeStopCheck Timer start & stop check in polling method */
/* PrtSysTime System time will be displayed */
/* GetSysTime get system time display string */
/* tm0isr timer0 interrupt service routine */
/* tm1isr timer1 interrupt service routine */
/*************************************************************************/
//#include <stdio.h>
//#include "snds.h"
//#include "uart.h"
//#include "pollio.h"
#include "isr.h"
#include "timer.h"
#include "sysconf.h"
#define MAX_LINE_BUF 250
volatile unsigned int clk_tick0 = 0; /* time tick for timer 0 */
volatile unsigned int clk_tick1 = 0; /* time tick for timer 0 */
volatile unsigned int WatchDogStatus = 0;
/* Timer0,1 can be used for system real time clock */
TIME tm0 = {0,0,0,0,0,0};
TIME tm1 = {0,0,0,0,0,0};
/*************************************************************************/
/* */
/* NAME : tm_init(timer device, time periode) */
/* */
/* FUNCTIONS : Initialize the TIMER0,1. */
/* */
/* EXAMPLES : */
/* tm_init(TIMER_DEV0,ONE_SECOND/TICKS_PER_SECOND); */
/* */
/* Where, the TIMER_DEV0 means timer0. */
/* ONE_SECOND = 1000, */
/* TICKS_PER_SECOND = 100, */
/* then timer0 timer periode become to 10ms. */
/* */
/*************************************************************************/
void tm_init(int TIMER_DEV, int t)
{
switch( TIMER_DEV)
{
case 1 :
{
Disable_Int(nTIMER1_INT);
SysSetInterrupt(nTIMER1_INT, tm1isr);
//IOPDATA &= ~(1<<17); /* Output 0 to P17(TOUT1) */
//IOPMOD |= (1<<17); /* Enable P17 to output port */
/* TOUT1 will be cleared */
TDATA1 = t_data_ms(t); /* unit is [ms] */
TCNT1 = 0x0;
TMOD = TM1_TOGGLE; /* Toggle pulse will be out to port */
Enable_Int(nTIMER1_INT); /* Timer interrupt enable */
//IOPCON = (1<<30); /* Timer1 output(TOUT1)enable */
}
case 0:
{
Disable_Int(nTIMER0_INT);
SysSetInterrupt(nTIMER0_INT, tm0isr);
//IOPDATA &= ~(1<<16); /* Output 0 to P16(TOUT0) */
//IOPMOD |= (1<<16); /* Enable P16 to output port */
/* TOUT0 will be cleared */
TDATA0 = t_data_ms(t);
TCNT0 = 0x0;
TMOD = TM0_TOGGLE;
Enable_Int(nTIMER0_INT);
//IOPCON = (1<<29); /* Timer0 output(TOUT0)enable */
}
}
}
/*************************************************************************/
/* */
/* NAME : tmReset(timer device ) */
/* */
/* FUNCTIONS : Clear and initialize the TIMER0 or TIMER1 */
/* */
/* EXAMPLES : */
/* tmReset(TIMER_DEV0); */
/* */
/* Where, the TIMER_DEV0 means timer0. */
/* */
/* VARIABLES USED */
/* */
/* */
/* HISTORY */
/* */
/* NAME DATE REMARKS */
/* */
/* in4maker 06-07-1999 Created initial version 1.0 */
/* */
/*************************************************************************/
void tmReset(int TIMER_DEV)
{
if(TIMER_DEV) {
TMOD &= ~TM1_RUN; /* Clear Timer mode register */
TDATA1 = 0; /* Clear Timer data register */
TCNT1 = 0xffffffff; /* Clear timer counter register */
}
else {
TMOD &= ~TM0_RUN; /* Clear Timer mode register */
TDATA0 = 0; /* Clear Timer data register */
TCNT0 = 0xffffffff; /* Clear timer counter register */
}
ClrTimeVar(TIMER_DEV); /* Initialize timer variable */
}
void ClrTimeVar(int TIMER_DEV)
{
if(TIMER_DEV) /* for timer1 */
{
tm1.tm_sec = 0;
tm1.tm_min = 0;
tm1.tm_hour = 0;
tm1.tm_mday = 0;
tm1.tm_mon = 0;
tm1.tm_year = 0;
clk_tick1 = 0;
}
else /* for timer0 */
{
tm0.tm_sec = 0;
tm0.tm_min = 0;
tm0.tm_hour = 0;
tm0.tm_mday = 0;
tm0.tm_mon = 0;
tm0.tm_year = 0;
clk_tick0 = 0;
}
}
/*************************************************************************/
/* */
/* NAME : tmCntr(TM_PARAM *t) */
/* */
/* FUNCTIONS : Re-initialize the timer. */
/* */
/* EXAMPLES : */
/* t->TIMER_Lisr = Timer1Lisr; */
/* t->TM_CHANNEL = TIMER_DEV1; */
/* t->TM_MODE = TM1_TOGGLE; */
/* t->TM_DATA = ONE_SECOND/TIME_TICKS_PER_SECOND; */
/* t->TM_OUT_PORT = 1; //timer output enabled */
/* */
/* tmCntr(t); */
/* */
/* */
/* VARIABLES USED */
/* TM_PARAM ; TIMER parameter data structures */
/* */
/* HISTORY */
/* */
/* NAME DATE REMARKS */
/* */
/* in4maker 06-07-1999 Created initial version 1.0 */
/* */
/*************************************************************************/
void tmCntr(TM_PARAM *t)
{
if(t->TM_CHANNEL)
{
Disable_Int(nTIMER1_INT);
SysSetInterrupt(nTIMER1_INT, t->TIMER_Lisr);
TCNT1 = 0x0;
TDATA1 = t->TM_DATA;
TMOD = t->TM_MODE;
if(t->TM_OUT_PORT) IOPCON |= (1<<30); /* enable Timer1 out */
Enable_Int(nTIMER1_INT);
}
else
{
Disable_Int(nTIMER0_INT);
SysSetInterrupt(nTIMER0_INT, t->TIMER_Lisr);
TCNT0 = 0x0;
TDATA0 = t->TM_DATA;
TMOD = t->TM_MODE;
if(t->TM_OUT_PORT) IOPCON |= (1<<29); /* enable Timer1 out */
Enable_Int(nTIMER0_INT);
}
}
/*************************************************************************/
/* */
/* NAME : TimeStopCheck(timer device) */
/* */
/* FUNCTIONS : Start and stop check timer using no interrupt. */
/* This is useful in Dhrystone program. */
/* */
/* EXAMPLES : */
/* TimeStopCheck(TIMER_DEV0); */
/* This function's return value is the timer counter */
/* register value. */
/* */
/* VARIABLES USED */
/* */
/* HISTORY */
/* */
/* NAME DATE REMARKS */
/* */
/* in4maker 06-07-1999 Created initial version 1.0 */
/* */
/*************************************************************************/
uint32 TimeStopCheck(int TIMER_DEV)
{
float TIME ;
if (TIMER_DEV)
{
TIME = (float)TCNT1 ;
Timer1Stop() ;
}
else
{
TIME = (float)TCNT0 ;
Timer0Stop() ;
}
TIME = (float)(0xFFFFFFFF - TIME) ;
//Print("\r +------ Time Stop : %d\r",(int)TIME);
return (TIME-1);
}
/*************************************************************************/
/* */
/* NAME : PrtSysTime(timer device, timer display string) */
/* */
/* FUNCTIONS : Hour:Minutes:second of the timer device will be displyed */
/* */
/* EXAMPLES : */
/* PrtSysTime(TIMER_DEV0,"Total test time"); */
/* */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -