首先,我们要先向工程文件Driver中添加STC32G_Timer.c,STC32G_Timer_Isr.c和STC32G_NVIC.c这三个文件,其中STC32G_Timer.c中定义了定时器初始化函数Timer_Inilize();STC32G_NVIC.c文件中定义了定时器中断初始化的相关函数NVIC_Timer0_Init();STC32G_Timer_Isr.c文件中定义了各种的中断函数,我们主要将想要实现的操作写进它给的对应的函数中就行。
初始化定时器0(其他的定时器初始化操作也已经写出,删掉注释就可使用),EA=1是开总开关操作,可添加到主函数中:
//========================================================================
// 定时器配置
//========================================================================
void Timer_config(void)
{
TIM_InitTypeDef TIM_InitStructure; //结构定义
TIM_InitStructure.TIM_Mode = TIM_16BitAutoReload; //指定工作模式, TIM_16BitAutoReload,TIM_16Bit,TIM_8BitAutoReload,TIM_16BitAutoReloadNoMask
TIM_InitStructure.TIM_ClkSource = TIM_CLOCK_1T; //指定时钟源, TIM_CLOCK_1T,TIM_CLOCK_12T,TIM_CLOCK_Ext
TIM_InitStructure.TIM_ClkOut = DISABLE; //是否输出高速脉冲, ENABLE或DISABLE
TIM_InitStructure.TIM_Value = (u16)(65536UL - (MAIN_Fosc / 1000UL)); //中断频率, 1000次/秒
TIM_InitStructure.TIM_Run = ENABLE; //是否初始化后启动定时器, ENABLE或DISABLE
Timer_Inilize(Timer0,&TIM_InitStructure); //初始化Timer0 Timer0,Timer1,Timer2,Timer3,Timer4
NVIC_Timer0_Init(ENABLE,Priority_0); //中断使能, ENABLE/DISABLE; 优先级(低到高) Priority_0,Priority_1,Priority_2,Priority_3
EA = 1;
// //定时器1做16位自动重装, 中断频率为20000HZ,中断函数从P6.6取反输出10KHZ方波信号.
// TIM_InitStructure.TIM_Mode = TIM_16BitAutoReload; //指定工作模式, TIM_16BitAutoReload,TIM_16Bit,TIM_8BitAutoReload,TIM_16BitAutoReloadNoMask
// TIM_InitStructure.TIM_ClkSource = TIM_CLOCK_1T; //指定时钟源, TIM_CLOCK_1T,TIM_CLOCK_12T,TIM_CLOCK_Ext
// TIM_InitStructure.TIM_ClkOut = DISABLE; //是否输出高速脉冲, ENABLE或DISABLE
// TIM_InitStructure.TIM_Value = 65536UL - (MAIN_Fosc / 20000); //中断频率, 20000次/秒
// TIM_InitStructure.TIM_Run = ENABLE; //是否初始化后启动定时器, ENABLE或DISABLE
// Timer_Inilize(Timer1,&TIM_InitStructure); //初始化Timer1 Timer0,Timer1,Timer2,Timer3,Timer4
// NVIC_Timer1_Init(ENABLE,Priority_0); //中断使能, ENABLE/DISABLE; 优先级(低到高) Priority_0,Priority_1,Priority_2,Priority_3
// //定时器2做16位自动重装, 中断频率为10000HZ,中断函数从P6.5取反输出5KHZ方波信号.
// TIM_InitStructure.TIM_ClkSource = TIM_CLOCK_1T; //指定时钟源, TIM_CLOCK_1T,TIM_CLOCK_12T,TIM_CLOCK_Ext
// TIM_InitStructure.TIM_ClkOut = DISABLE; //是否输出高速脉冲, ENABLE或DISABLE
// TIM_InitStructure.TIM_Value = 65536UL - (MAIN_Fosc / 10000); //初值
// TIM_InitStructure.TIM_Run = ENABLE; //是否初始化后启动定时器, ENABLE或DISABLE
// Timer_Inilize(Timer2,&TIM_InitStructure); //初始化Timer2 Timer0,Timer1,Timer2,Timer3,Timer4
// NVIC_Timer2_Init(ENABLE,NULL); //中断使能, ENABLE/DISABLE; 无优先级
// //定时器3做16位自动重装, 中断频率为100HZ,中断函数从P6.4取反输出50HZ方波信号.
// TIM_InitStructure.TIM_ClkSource = TIM_CLOCK_12T; //指定时钟源, TIM_CLOCK_1T,TIM_CLOCK_12T,TIM_CLOCK_Ext
// TIM_InitStructure.TIM_ClkOut = ENABLE; //是否输出高速脉冲, ENABLE或DISABLE
// TIM_InitStructure.TIM_Value = 65536UL - (MAIN_Fosc / (100*12)); //初值
// TIM_InitStructure.TIM_Run = ENABLE; //是否初始化后启动定时器, ENABLE或DISABLE
// Timer_Inilize(Timer3,&TIM_InitStructure); //初始化Timer3 Timer0,Timer1,Timer2,Timer3,Timer4
// NVIC_Timer3_Init(ENABLE,NULL); //中断使能, ENABLE/DISABLE; 无优先级
// //定时器4做16位自动重装, 中断频率为50HZ,中断函数从P6.3取反输出25HZ方波信号.
// TIM_InitStructure.TIM_ClkSource = TIM_CLOCK_12T; //指定时钟源, TIM_CLOCK_1T,TIM_CLOCK_12T,TIM_CLOCK_Ext
// TIM_InitStructure.TIM_ClkOut = ENABLE; //是否输出高速脉冲, ENABLE或DISABLE
// TIM_InitStructure.TIM_Value = 65536UL - (MAIN_Fosc / (50*12)); //初值
// TIM_InitStructure.TIM_Run = ENABLE; //是否初始化后启动定时器, ENABLE或DISABLE
// Timer_Inilize(Timer4,&TIM_InitStructure); //初始化Timer4 Timer0,Timer1,Timer2,Timer3,Timer4
// NVIC_Timer4_Init(ENABLE,NULL); //中断使能, ENABLE/DISABLE; 无优先级
}
以下是STC32G_Timer_Isr.c文件定时器0的中断调用函数,我的操作是实现led灯的亮灭,led灯接在P10上。
//========================================================================
// 函数: Timer0_ISR_Handler
// 描述: Timer0中断函数.
// 参数: none.
// 返回: none.
// 版本: V1.0, 2020-09-23
//========================================================================
void Timer0_ISR_Handler (void) interrupt TMR0_VECTOR //进中断时已经清除标志
{
tt++;
if(tt>=1000)
{
P10=~P10;
tt=0;
}
}
除此之外,我们在开发过程中有时也会需要停止定时器,库函数也为我们提供了相应的宏定义可以直接调用:
//========================================================================
// 定时器0设置
//========================================================================
/* 0,1: 定时器0用做定时器, 12T/1T; 2: 定时器0用做计数器 */
#define Timer0_CLK_Select(n) do{if(n == 0) T0x12 = 0, T0_CT = 0; \
if(n == 1) T0x12 = 1, T0_CT = 0; \
if(n == 2) T0_CT = 1; \
}while(0)
#define Timer0_CLK_Output(n) T0CLKO = n /* T0 溢出脉冲在T0脚输出使能 */
#define Timer0_Run(n) (n==0?(TR0 = 0):(TR0 = 1)) /* 定时器0计数使能 */
#define Timer0_Stop() TR0 = 0 /* 禁止定时器0计数 */
#define T0_Load(n) TH0 = (n) / 256, TL0 = (n) % 256
//========================================================================
// 定时器1设置
//========================================================================
/* 0,1: 定时器1用做定时器, 12T/1T; 2: 定时器1用做计数器 */
#define Timer1_CLK_Select(n) do{if(n == 0) T1x12 = 0, T1_CT = 0; \
if(n == 1) T1x12 = 1, T1_CT = 0; \
if(n == 2) T1_CT = 1; \
}while(0)
#define Timer1_CLK_Output(n) T1CLKO = n /* T1 溢出脉冲在T1脚输出使能 */
#define Timer1_Run(n) (n==0?(TR1 = 0):(TR1 = 1)) /* 定时器1计数使能 */
#define Timer1_Stop() TR1 = 0 /* 禁止定时器1计数 */
#define T1_Load(n) TH1 = (n) / 256, TL1 = (n) % 256
//========================================================================
// 定时器2设置
//========================================================================
/* 0,1: 定时器2用做定时器, 12T/1T; 2: 定时器2用做计数器 */
#define Timer2_CLK_Select(n) do{if(n == 0) T2x12 = 0, T2_CT = 0; \
if(n == 1) T2x12 = 1, T2_CT = 0; \
if(n == 2) T2_CT = 1; \
}while(0)
#define Timer2_CLK_Output(n) T2CLKO = n /* T2 溢出脉冲在T2脚输出使能 */
#define Timer2_Run(n) (n==0?(T2R = 0):(T2R = 1)) /* 定时器2计数使能 */
#define Timer2_Stop() T2R = 0 /* 禁止定时器2计数 */
#define T2_Load(n) T2H = (n) / 256, T2L = (n) % 256
//========================================================================
// 定时器3设置
//========================================================================
/* 0,1: 定时器3用做定时器, 12T/1T; 2: 定时器3用做计数器 */
#define Timer3_CLK_Select(n) do{if(n == 0) T3x12 = 0, T3_CT = 0; \
if(n == 1) T3x12 = 1, T3_CT = 0; \
if(n == 2) T3_CT = 1; \
}while(0)
#define Timer3_CLK_Output(n) T3CLKO = n /* T3 溢出脉冲在T3脚输出使能 */
#define Timer3_Run(n) (n==0?(T3R = 0):(T3R = 1)) /* 定时器3计数使能 */
#define Timer3_Stop() T3R = 0 /* 禁止定时器3计数 */
#define T3_Load(n) T3H = (n) / 256, T3L = (n) % 256
//========================================================================
// 定时器4设置
//========================================================================
/* 0,1: 定时器4用做定时器, 12T/1T; 2: 定时器4用做计数器 */
#define Timer4_CLK_Select(n) do{if(n == 0) T4x12 = 0, T4_CT = 0; \
if(n == 1) T4x12 = 1, T4_CT = 0; \
if(n == 2) T4_CT = 1; \
}while(0)
#define Timer4_CLK_Output(n) T4CLKO = n /* T4 溢出脉冲在T4脚输出使能 */
#define Timer4_Run(n) (n==0?(T4R = 0):(T4R = 1)) /* 定时器4计数使能 */
#define Timer4_Stop() T4R = 0 /* 禁止定时器4计数 */
#define T4_Load(n) T4H = (n) / 256, T4L = (n) % 256
以定时器0为例子,我们可以用Timer0_Run(0)或者Timer0_Stop()让定时器0停止计数,调用Timer0_Run(1)重新开始计数。相同的,关闭和开启中断也是一样的:
//========================================================================
// 定时器中断设置
//========================================================================
#define Timer0_Interrupt(n) (n==0?(ET0 = 0):(ET0 = 1)) /* Timer0中断使能 */
#define Timer1_Interrupt(n) (n==0?(ET1 = 0):(ET1 = 1)) /* Timer1中断使能 */
#define Timer2_Interrupt(n) (n==0?(ET2 = 0):(ET2 = 1)) /* Timer2中断使能 */
#define Timer3_Interrupt(n) (n==0?(ET3 = 0):(ET3 = 1)) /* Timer3中断使能 */
#define Timer4_Interrupt(n) (n==0?(ET4 = 0):(ET4 = 1)) /* Timer4中断使能 */
因为这次的程序比较简单,例程就不放在资源了