/*---------------------------------------------------------------------------------------------------------*/
/* */
/* Copyright (c) Nuvoton Technology Corp. All rights reserved. */
/* */
/*---------------------------------------------------------------------------------------------------------*/
/*en:************************************************************************************
This demo is used for testing timer0~timer1, in period and one-shot mode, using
external 12M clock.
Run the demo, select a timer(timer0~timer1) follow wizard, then select timer
mode(period and one-shot) in the same way, now you can see the result print
through Hyper-terminal.
****************************************************************************************/
/*中:************************************************************************************
该程序用于测试定时器0~定时器3,周期和单次模式,使用外部12MHz作为时钟源。
超级终端115200波特率可显示输出信息。
根据输出的信息选择要测试的定时器和工作方式,相应结果也会在超级终端显示出来。
****************************************************************************************/
#include "Common.h"
static uint32_t TMR_Tick = 0;
static uint32_t TMR_Shot = 0;
static uint8_t TMR_Select_Flag = 0;
static uint8_t TMR_Mode_Select_Flag = 0;
static void TMR_Select(void);
static void TMR_Mode_Select(void);
void TMR0_Init(void);
void TMR1_Init(void);
void TMR2_Init(void);
void TMR3_Init(void);
int main(void)
{
Un_Lock_Reg(); //中:解锁被保护的寄存器位,以便用户访问//en:Unlock protected register bits, so that user can access them
PWRCON |= XTL12M_EN; //中:使能外部12MHz晶振//en:Enable external 12MHz crystal
while((CLKSTATUS & XTL12M_STB) == 0); //中:等12M晶振时钟稳定//en:Wait until external 12M crystal stable
CLKSEL0 = (CLKSEL0 & (~HCLK)) | HCLK_12M; //中:选外部12MHz晶振为系统时钟//en:Select 12M as system clock
Lock_Reg(); //中:重新锁被保护的寄存器位//en:Re-lock protected register bits
UART_Init();
while(1)
{
TMR_Tick = 0;
TMR_Shot = 0;
TMR_Select(); //中:定时器选择//en:Timer select
TMR_Select_Flag = getchar();
printf("%c\n",TMR_Select_Flag);
switch(TMR_Select_Flag)
{
case '1':
TMR0_Init();
break;
case '2':
TMR1_Init();
break;
case '3':
TMR2_Init();
break;
case '4':
TMR3_Init();
break;
default:
break;
}
switch(TMR_Mode_Select_Flag)
{
case '1':
while(TMR_Tick != 100);
break;
case '2':
while(TMR_Shot != 1);
break;
default:
break;
}
}
}
/*en:************************************************************************************
Description: Timer select
****************************************************************************************/
/*中:************************************************************************************
描述 : Timer选择
****************************************************************************************/
static void TMR_Select (void)
{
printf("\n\n");
printf("Please select a timer for test...\n");
printf("+-----------------------------------------------------------+\n");
printf("| Timer0 - [1] |\n");
printf("| Timer1 - [2] |\n");
printf("| Timer2 - [3] |\n");
printf("| Timer3 - [4] |\n");
printf("+-----------------------------------------------------------+\n");
printf("Select key : ");
}
/*en:************************************************************************************
Description: Timer mode select
****************************************************************************************/
/*中:************************************************************************************
描述 : Timer模式选择
****************************************************************************************/
static void TMR_Mode_Select(void)
{
printf("\n\n");
printf("Please select timer mode...\n");
printf("+-----------------------------------------------------------+\n");
printf("| 10ms period mode - [1] |\n");
printf("| 1s one-shot mode - [2] |\n");
printf("+-----------------------------------------------------------+\n");
printf("Select key : ");
}
/*en:************************************************************************************
Description: Initialize timer0
****************************************************************************************/
/*中:************************************************************************************
描述 : Timer0初始化
****************************************************************************************/
void TMR0_Init(void)
{
IPRSTC2 |= TMR0_RST; //中:复位Timer0外设//en:Reset timer0 module
IPRSTC2 &= ~TMR0_RST;
APBCLK |= TMR0_CLKEN; //中:使能Tmier0时钟源//en:Enable timer0 clock
CLKSEL1 = (CLKSEL1 & (~TM0_CLK)) | TM0_12M; //中:选择12M做为Timer0时钟源//en:Select external 12M as timer0 clock source
TMR_Mode_Select();
TMR_Mode_Select_Flag = getchar();
printf("%c\n",TMR_Mode_Select_Flag);
switch(TMR_Mode_Select_Flag)
{
case '1':
TCSR0 = TCSR0 & (~TMR_MODE) | MODE_PERIOD; //中:周期模式//en:Period mode
TCSR0 = TCSR0 & 0xFFFFFF00; //中:不分频//en:No division
TCMPR0 = 120000; //中:中断周期//en:Time period
break;
case '2':
TCSR0 = TCSR0 & (~TMR_MODE) | MODE_ONE_SHOT; //中:一次定时模式//en:One-shot mode
TCSR0 = TCSR0 & 0xFFFFFF00; //中:不分频//en:No division
TCMPR0 = 12000000; //中:定时时间//en:Expired time
break;
default:
break;
}
TCSR0 |= TMR_IE; //中:使能中断//en:Enable timer0 interrupt
NVIC_ISER = TMR0_INT; //中:使能中断向量//en:Enable NVIC TMR0 interrupt
TCSR0 |= CRST; //中:复位Timer0计数器//en:Reset timer0 counter
TCSR0 |= CEN; //中:使能Timer0开始计数//en:Start timer0
}
/*en:************************************************************************************
Description: Timer0 ISR routine
****************************************************************************************/
/*中:************************************************************************************
描述 : Timer0中断处理函数
****************************************************************************************/
void TMR0_IRQHandler(void)
{
TISR0 |= TMR_TIF; //中:清中断标志//en:Clear timer0 interrupt flag
switch(TMR_Mode_Select_Flag)
{
case '1':
TMR_Tick++;
printf("Timer0 10ms tick %d\n", TMR_Tick);
if (TMR_Tick == 100)
{
TCSR0 &= ~CEN; //中:关定时器0//en:Stop timer0
printf("\nTimer0 10ms period test is over\n");
}
break;
case '2':
printf("\nTimer0 1s one-shot test is over\n");
TMR_Shot = 1;
break;
default:
break;
}
}
/*en:************************************************************************************
Description: Initialize timer1
****************************************************************************************/
/*中:************************************************************************************
描述 : Timer1初始化
****************************************************************************************/
void TMR1_Init(void)
{
IPRSTC2 |= TMR1_RST; //中:复位Timer1外设//en:Reset timer1 module
IPRSTC2 &= ~TMR1_RST;
APBCLK |= TMR1_CLKEN; //中:使能Tmier1时钟源//en:Enable timer1 clock source
CLKSEL1 = (CLKSEL1 & (~TM1_CLK)) | TM1_12M; //中:选择12M做为Timer1时钟源//en:Select external 12M as timer1 clock source
TMR_Mode_Select();
TMR_Mode_Select_Flag = getchar();
printf("%c\n",TMR_Mode_Select_Flag);
switch(TMR_Mode_Select_Flag)
{
case '1':
TCSR1 = TCSR1 & (~TMR_MODE) | MODE_PERIOD; //中:周期模式//en:Period mode
TCSR1 = TCSR1 & 0xFFFFFF00; //中:不分频//en:No division
TCMPR1 = 120000; //中:中断周期//en:Time period
break;
case '2':
TCSR1 = TCSR1 & (~TMR_MODE) | MODE_ONE_SHOT; //中:一次定时模式//en:One-shot mode
TCSR1 = TCSR1 & 0xFFFFFF00; //中:不分频//en:No division
TCMPR1 = 12000000; //中:定时时间//en:Expired time
break;
default:
break;
}
TCSR1 |= TMR_IE; //中:使能中断//en:Enable timer1 interrupt
NVIC_ISER = TMR1_INT; //中:使能中断向量//en:Enable NVIC TMR1 interrupt
TCSR1 |= CRST; //中:复位Timer1计数器//en:Reset timer1 counter
TCSR1 |= CEN; //中:使能Timer1开始计数//en:Start timer1
}
/*en:************************************************************************************
Description: Timer1 ISR routine
****************************************************************************************/
/*中:************************************************************************************
描述 : Timer1中断处理函数
****************************************************************************************/
void TMR1_IRQHandler(void)
{
TISR1 |= TMR_TIF; //中:清中断标志//en:Clear timer1 interrupt flag
switch(TMR_Mode_Select_Flag)
{
case '1':
TMR_Tick++;
printf("Timer1 10ms tick %d\n", TMR_Tick);
if (TMR_Tick == 100)
{
TCSR1 &= ~CEN; //中:关定时器1//en:Stop timer1
printf("\nTimer1 10ms period test is over\n");
}
break;
case '2':
printf("\nTimer1 1s one-shot test is over\n");
TMR_Shot = 1;
break;
default:
break;
}
}
/*en:************************************************************************************
Description: Initialize timer2
****************************************************************************************/
/*中:************************************************************************************
描述 : Timer2初始化
****************************************************************************************/
void TMR2_Init(void)
{
IPRSTC2 |= TMR2_RST; //中:复位Timer2外设//en:Reset timer2 module
IPRSTC2 &= ~TMR2_RST;
APBCLK |= TMR2_CLKEN; //中:使能Tmier2时钟源//en:Enable timer2 clock
CLKSEL1 = (CLKSEL1 & (~TM2_CLK)) | TM2_12M; //中:选择12M做为Timer2时钟源//en:Select external 12M as timer2 clock source
TMR_Mode_Select();
TMR_Mode_Select_Flag = getchar();
printf("%c\n",TMR_Mode_Select_Flag);
switch(TMR_Mode_Select_Flag)
{
case '1':
TCSR2 = TCSR2 & (~TMR_MODE) | MODE_PERIOD; //中:周期模式//en:Period mode
TCSR2 = TCSR2 & 0xFFFFFF00; //中:不分频//en:No division
TCMPR2 = 120000; //中:中断周期//en:Time period
break;
case '2':
TCSR2 = TCSR2 & (~TMR_MODE) | MODE_ONE_SHOT; //中:一次定时模式//en:One-shot mode
TCSR2 = TCSR2 & 0xFFFFFF00; //中:不分频//en:No division
TCMPR2 = 12000000; //中:定时时间//en:Expired time
break;
default:
break;
}
TCSR2 |= TMR_IE; //中:使能中断//en:Enable timer2 interrupt
NVIC_ISER = TMR2_INT; //中:使能中断向量//en:Enable NVIC TMR2 interrupt
TCSR2 |= CRST; //中:复位Timer2计数器//en:Reset timer2 counter
TCSR2 |= CEN; //中:使能Timer2开始计数//en:Start timer2
}
/*en:************************************************************************************
Description: Timer2 ISR routine
****************************************************************************************/
/*中:************************************************************************************
描述 : Timer2中断处理函数
****************************************************************************************/
void TMR2_IRQHandler(void)
{
TISR2 |= TMR_TIF; //中:清中断标志//en:Clear timer2 interrupt flag
switch(TMR_Mode_Select_Flag)
{
case '1':
TMR_Tick++;
printf("Timer2 10ms tick %d\n", TMR_Tick);
if (TMR_Tick == 100)
{
TCSR2 &= ~CEN; //中:关定时器2//en:Stop timer2
printf("\nTimer2 10ms period test is over\n");
}
break;
case '2':
printf("\nTimer2 1s one-shot test is over\n");
TMR_Shot = 1;
break;
default:
break;
}
}
/*en:************************************************************************************
Description: Initialize timer3
****************************************************************************************/
/*中:************************************************************************************
描述 : Timer3初始化
****************************************************************************************/
void TMR3_Init(void)
{
IPRSTC2 |= TMR3_RST; //中:复位Timer3外设//en:Reset timer3 module
IPRSTC2 &= ~TMR3_RST;
APBCLK |= TMR3_CLKEN; //中:使能Tmier3时钟源//en:Enable timer3 clock
CLKSEL1 = (CLKSEL1 & (~TM3_CLK)) | TM3_12M; //中:选择12M做为Timer3时钟源//en:Select external 12M as timer3 clock source
TMR_Mode_Select();
TMR_Mode_Select_Flag = getchar();
printf("%c\n",TMR_Mode_Select_Flag);
switch(TMR_Mode_Select_Flag)
{
case '1':
TCSR3 = TCSR3 & (~TMR_MODE) | MODE_PERIOD; //中:周期模式//en:Period mode
TCSR3 = TCSR3 & 0xFFFFFF00; //中:不分频//en:No division
TCMPR3 = 120000; //中:中断周期//en:Time period
break;
case '2':
TCSR3 = TCSR3 & (~TMR_MODE) | MODE_ONE_SHOT; //中:一次定时模式//en:One-shot mode
TCSR3 = TCSR3 & 0xFFFFFF00; //中:不分频//en:No division
TCMPR3 = 12000000; //中:定时时间//en:Expired time
break;
default:
break;
}
TCSR3 |= TMR_IE; //中:使能中断//en:Enable timer3 interrupt
NVIC_ISER = TMR3_INT; //中:使能中断向量//en:Enable NVIC TMR3 interrupt
TCSR3 |= CRST; //中:复位Timer3计数器//en:Reset timer3 counter
TCSR3 |= CEN; //中:使能Timer3开始计数//en:Start timer3
}
/*en:************************************************************************************
Description: Timer3 ISR routine
****************************************************************************************/
/*中:************************************************************************************
描述 : Timer3中断处理函数
****************************************************************************************/
void TMR3_IRQHandler(void)
{
TISR3 |= TMR_TIF; //中:清中断标志//en:Clear timer3 interrupt flag
switch(TMR_Mode_Select_Flag)
{
case '1':
TMR_Tick++;
printf("Timer3 10ms tick %d\n", TMR_Tick);
if (TMR_Tick == 100)
{
TCSR3 &= ~CEN; //中:关定时器3//en:Stop timer3
printf("\nTimer3 10ms period test is over\n");
}
break;
case '2':
printf("\nTimer3 1s one-shot test is over\n");
TMR_Shot = 1;
break;
default:
break;
}
}
|