📄 timer.c
字号:
//-+---+---++-+--+-++-+++--++--+++
#include "std.h"
#include "snds.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)
{
if(TIMER_DEV) // for TIMER 1
{
Disable_Int(nTIMER1_INT);
SysSetInterrupt(nTIMER1_INT, tm1isr);
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
}
else // for TIMER0
{
Disable_Int(nTIMER0_INT);
SysSetInterrupt(nTIMER0_INT, tm0isr);
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.
// ***********************************************************************
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 : tm0isr()
// FUNCTIONS : Timer0 interrupt service routine.
// EXAMPLES :
// SysSetInterrupt(nTIMER0_INT, tm0isr);
// ***********************************************************************
void tm0isr(void)
{
clk_tick0++;
if(clk_tick0 == TICKS_PER_SECOND)
{
clk_tick0 = 0;
if(tm0.tm_sec++ > 59)
{
tm0.tm_sec = 0;
if(tm0.tm_min++ > 59)
{
tm0.tm_min = 0;
if(tm0.tm_hour++ > 23)
{
tm0.tm_hour = 0;
if(tm0.tm_mday++ > 30)
{
tm0.tm_mday = 0;
if(tm0.tm_mon++ > 11)
{
tm0.tm_mon = 0;
tm0.tm_year++;
}
}
}
}
}
// 4 means digit number for LED display
IOPDATA = ~(1<<tm0.tm_sec%4);
}
}
// ***********************************************************************
// NAME : tm1isr()
// FUNCTIONS : Timer1 interrupt service routine.
// EXAMPLES :
// SysSetInterrupt(nTIMER1_INT, tm1isr);
// ***********************************************************************
void tm1isr(void)
{
clk_tick1++;
if(clk_tick1 == TICKS_PER_SECOND)
{
clk_tick1 = 0;
if(tm1.tm_sec++ > 59)
{
tm1.tm_sec = 0;
if(tm1.tm_min++ > 59)
{
tm1.tm_min = 0;
if(tm1.tm_hour++ > 23)
{
tm1.tm_hour = 0;
if(tm1.tm_mday++ > 30)
{
tm1.tm_mday = 0;
if(tm1.tm_mon++ > 11)
{
tm1.tm_mon = 0;
tm1.tm_year++;
}
}
}
}
}
// 4 means digit number for LED display
IOPDATA = ~(16<<tm1.tm_sec%4);
}
}
// ***********************************************************************
// 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
// ***********************************************************************
//-+---+---++-+--+-++-+++--++--+++
//被我改名为TimerSetup(TM_PARAM *t)
void TimerSetup(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);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -