📄 timer.c
字号:
Disable_Int(nTIMER0_INT);
Disable_Int(nTIMER1_INT);
if(reconf) /* timer0 reconfiguration */
SysSetInterrupt(nTIMER0_INT, tm0isr);
else
SysSetInterrupt(nTIMER0_INT, WatchDog0Lisr);
SysSetInterrupt(nTIMER1_INT, WatchDog1Lisr);
/* TIMER 0 used to system basic timer */
TDATA0 = t_data_ms(ONE_SECOND/TICKS_PER_SECOND);
TCNT0 = 0x0;
TMOD = 0;
/* TIMER 1 used to system watchdog timer */
WatchDogSetup();
Enable_Int(nTIMER0_INT); /* Timer0 interrupt enable */
Enable_Int(nTIMER1_INT); /* Timer1 interrupt enable */
/* TIMER RUN enable */
TimerStart(TIMER_DEV0);
TimerStart(TIMER_DEV1);
}
/* Watchdog timer start */
void WatchDogSetup(void)
{
IOPDATA &= ~(1<<17); /* Output 0 to P17(TOUT1) */
IOPMOD |= (1<<17); /* Enable P17 to output port */
/* TOUT1 will be cleared */
TCNT1 = 0; /* Clear timer counter register */
TDATA1 = t_data_ms(3*ONE_SECOND); /* Set watchdog time to 3sec */
TMOD = 0; /* Set timer mode : */
/* Interval mode,timer start */
WatchDogStatus = 0;
}
void WatchDogReload(void)
{
TimerStop(TIMER_DEV1);
Disable_Int(nTIMER1_INT);
TDATA1 = t_data_ms(3*ONE_SECOND); /* Set watchdog time to 3sec */
WatchDogStatus = 0;
Enable_Int(nTIMER1_INT); /* Timer0 interrupt enable */
TimerStart(TIMER_DEV1);
}
void WatchDog1Lisr(void)
{
#ifdef WDT_HW_RESET
Print("\n\r\r@@System hardware reset.\r");
Print("\nReset signal came from TOUT1 as reset input\r");
/* This for the System hardware reset */
/* P17 have been merged system reset input pin */
IOPDATA &= ~(1<<17); /* Output 0 to P17(TOUT1) */
IOPMOD |= (1<<17); /* P17 output mode enable */
#else
Print("\n\r\r@@System software reset.\r");
Print("\nSystem will be reboot by boot ROM.\r");
return;
#endif
WatchDogStatus = 1;
}
void WatchDog0Lisr(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);
}
WatchDogReload(); /* WatchDog timer value will be re-loaded */
}
void TimerTest(void)
{
U8 items;
do {
TimerTestItem(); // Main memu for cache test
do {
Print("\n\rSelect Number?_");
items = get_byte();
}while(is_space(items));
if(is_xdigit(to_upper(items))) {
switch(to_upper(items)) {
case '1': rtc_run(); break;
case '2': WatchDogTest(); break;
case '3': tmConfig(); break;
default : break;
}
}
Print("\n\rPress any key to continue.\r");
while(!is_space(get_byte()));
} while((items!='q')&&(items!='Q'));
}
void TimerTestItem()
{
Print("\n##########################################\r");
Print("\n# TIMER TEST ITEMS #\r");
Print("\n##########################################\r");
Print("\n# 1.Running Timer 0,1 #\r");
Print("\n# 2.Watchdog Timer function(Timer1) Test.#\r");
Print("\n# 3.View the timer configurations. #\r");
Print("\n# Q.Type Q for exit timer test. #\r");
Print("\n##########################################\r");
}
void rtc_run()
{
uint8 dev; /* for to select timer device 0,1 */
/* Select timer device. Timer0 or Timer 1 */
do {
Print("\n\r\r$ Select timer[0/1]_");
dev = get_byte();
} while(dev != '1' && dev != '0');
Print("\n###########################################\r");
Print("\n# You select timer default value for test #\r");
Print("\n###########################################\r");
if(dev=='1') {
tm_init(TIMER_DEV1,(ONE_SECOND/TICKS_PER_SECOND));
TimerStart(TIMER_DEV1);
}
else {
tm_init(TIMER_DEV0,(ONE_SECOND/TICKS_PER_SECOND));
TimerStart(TIMER_DEV0);
}
/* -------------------------*/
/* Now Start timer for test */
/* -------------------------*/
Print("\n\rTo exit, enter the Esc key on keyboard.\r");
Print("\n\r\r>> Now, the timer is Running......\r\r");
IOPDATA = 0xff; // All LED OFF
while(1)
{
if(dev == '1') PrtSysTime(TIMER_DEV1,"[TIMER1] Current time");
else PrtSysTime(TIMER_DEV0,"[TIMER0] Current time");
}
}
void WatchDogTest()
{
uint8 ch;
Print("\n\r\rWatchDog Timer function test.\r\r");
Print("\nTimer0 used as system timer.\r");
Print("\nTimer1 used as watchdog timer.\r");
Print("\nWatchdog timer counter value will be initialized at \r");
Print("\nsystem timer interrupt service routine repeatedly. \r");
Print("\nIf you want to make the system hangup situation,\r");
Print("\n\rEnter the character 'R'.\r");
WatchDogFunc(0);
Print("\n\r\r Now, watchdog & system timer is running.....\r");
while(1)
{
ch = get_byte(); put_byte(ch); /* for echo */
if(ch != 'r')
{
Print("\n\rTIMER0 reconfigured with no watchdog Init function.\r");
/* TIMER 0 re-configured with no watchdog Initialize function */
WatchDogFunc(1);
Print("\n\rNow, wathdog timer interrupt occurred...\r");
}
}
}
void tmConfig(void)
{
Print("\n\r<<---------- TIMER 0 STATUS ----------->>\r");
if(TMOD & TM0_RUN)
Print("\n>>TMOD[0]: Timer0,Start enabled.\r");
else
Print("\n>>TMOD[0]: Timer0,Disabled.\r");
if(TMOD & TM0_TOGGLE)
Print("\n>>TMOD[1]: Timer0,Toggle mode.\r");
else
Print("\n>>TMOD[1]: Timer0,Interval mode.\r");
if(TMOD & TM0_OUT_1)
Print("\n>>TMOD[2]: Timer0,Initial TOUT0 is 1.\r");
else
Print("\n>>TMOD[2]: Timer0,Initial TOUT0 is 0.\r");
Print("\n>>TDATA0 = 0x%08x\r",TDATA0);
Print("\n>>TCNT0= 0x%08x \r",TCNT0);
if((IOPCON & 0x40000000) && (IOPMOD & 0x10000))
Print("\n>>Timer0,TOUT0 will be monitored at PORT16.\r");
else
Print("\n>>Timer0,TOUT0 output disabled.\r");
if(INTMODE & TIMER0_INT)
Print("\n>>Timer0 is in FIQ mode.\r");
else
Print("\n>>Timer0 is in IRQ mode.\r");
if(~INTMASK & TIMER0_INT)
Print("\n>>Timer0 interrupt enabled.\r");
else
Print("\n>>Timer0 interrupt disabled.\r");
Print("\n-----------------------------------------\r");
Print("\n\r<<---------- TIMER 1 STATUS ----------->>\r");
if(TMOD & TM1_RUN)
Print("\n>>TMOD[1]: Timer1,Start enabled.\r");
else
Print("\n>>TMOD[1]: Timer1,Disabled.\r");
if(TMOD & TM1_TOGGLE)
Print("\n>>TMOD[1]: Timer1,Toggle mode.\r");
else
Print("\n>>TMOD[1] Timer1,Interval mode.\r");
if(TMOD & TM1_OUT_1)
Print("\n>>TMOD[2]: Timer1,Initial TOUT1 is 1.\r");
else
Print("\n>>TMOD[2]: Timer1,Initial TOUT1 is 0.\r");
Print("\n>>TDATA1 = 0x%08x\r",TDATA1);
Print("\n>>TCNT1= 0x%08x\r",TCNT1);
if((IOPCON & 0x80000000) && (IOPMOD & 0x20000))
Print("\n>>Timer1,TOUT1 will be monitored at PORT17.\r");
else
Print("\n>>Timer1,TOUT1 output disabled.\r");
if(INTMODE & TIMER1_INT)
Print("\n>>Timer1 is in FIQ mode.\r");
else
Print("\n>>Timer1 is in IRQ mode.\r");
if(~INTMASK & TIMER1_INT)
Print("\n>>Timer1 interrupt enabled.\r");
else
Print("\n>>Timer1 interrupt disabled.\r");
Print("\n-----------------------------------------\r\r");
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -