1、timer.c文件
#include "STM32Lib\\stm32f10x.h"
void TIM2_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
u16 CCR1_Val = 4000;
u16 CCR2_Val = 2000;
u16 CCR3_Val = 1000;
u16 CCR4_Val = 500;
/* TIM2 clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
/* 基础设置*/
TIM_TimeBaseStructure.TIM_Period = 10000; //计满值
TIM_TimeBaseStructure.TIM_Prescaler = 7200-1; //预分频,此值+1为分频的除数
TIM_TimeBaseStructure.TIM_ClockDivision = 0x0; //
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
/* 比较通道1*/
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Inactive; //输出比较非主动模式
TIM_OCInitStructure.TIM_Pulse = CCR1_Val;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //极性为正
TIM_OC1Init(TIM2, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Disable); //禁止OC1重装载,其实可以省掉这句,因为默认是4路都不重装的.
/*比较通道2 */
TIM_OCInitStructure.TIM_Pulse = CCR2_Val;
TIM_OC2Init(TIM2, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM2, TIM_OCPreload_Disable);
/* 比较通道3 */
TIM_OCInitStructure.TIM_Pulse = CCR3_Val;
TIM_OC3Init(TIM2, &TIM_OCInitStructure);
TIM_OC3PreloadConfig(TIM2, TIM_OCPreload_Disable);
/* 比较通道4 */
TIM_OCInitStructure.TIM_Pulse = CCR4_Val;
TIM_OC4Init(TIM2, &TIM_OCInitStructure);
TIM_OC4PreloadConfig(TIM2, TIM_OCPreload_Disable);
/*使能预装载*/
TIM_ARRPreloadConfig(TIM2, ENABLE);
/*预先清除所有中断位*/
TIM_ClearITPendingBit(TIM2, TIM_IT_CC1 | TIM_IT_CC2 | TIM_IT_CC3 | TIM_IT_CC4|TIM_IT_Update);
/* 4个通道和溢出都配置中断*/
TIM_ITConfig(TIM2, TIM_IT_CC1 | TIM_IT_CC2 | TIM_IT_CC3 | TIM_IT_CC4|TIM_IT_Update, ENABLE);
/* 允许TIM2开始计数 */
TIM_Cmd(TIM2, ENABLE);
}
void TIM3_Configuration(u16 p,u16 psc)
{
RCC->APB1ENR|=1<<1;//TIM3时钟使能
//自动装载寄存器
TIM3->ARR=p; //设定定时器自动重装值
//PSC预分频寄存器
TIM3->PSC=psc; //设定定时器的分频系数
TIM3->DIER|=1<<0; //允许更新中断
TIM3->DIER|=1<<6; //允许触发中断
TIM3->CR1|=0X01; //使能定时器3(这里面包括计数方向为向上计数)
}
#if 0
void TIM4_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
/* TIM4 clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
/* 基础设置*/
TIM_TimeBaseStructure.TIM_Period = 10000; //计满值
TIM_TimeBaseStructure.TIM_Prescaler = 7200-1; //预分频,此值+1为分频的除数
TIM_TimeBaseStructure.TIM_ClockDivision = 0x0; //
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
/*使能预装载*/
TIM_ARRPreloadConfig(TIM4, ENABLE);
/*预先清除所有中断位*/
TIM_ClearITPendingBit(TIM4, TIM_IT_Update);
TIM_ITConfig(TIM4, TIM_IT_Update, ENABLE);
/* 允许TIM2开始计数 */
TIM_Cmd(TIM4, ENABLE);
}
#else
void TIM_Configuration(u16 p,u16 psc)
{
RCC->APB1ENR|=1<<2;//TIM4时钟使能
//自动装载寄存器
TIM4->ARR=p; //设定定时器自动重装值
//PSC预分频寄存器
TIM4->PSC=psc; //设定定时器的分频系数
TIM4->DIER|=1<<0; //允许更新中断
TIM4->DIER|=1<<6; //允许触发中断
TIM4->CR1|=0X01; //使能定时器3(这里面包括计数方向为向上计数)
}
#endif
上程序中,定时器2被配置成多路捕获模式,定时器3是直接操作寄存器进行配置的。
定时器4用了两种配置方式,使用固件库和直接操作寄存器。可以切换。效果一样。
需要注意的是,stm32103RBT6的通用定时器只有2、3、4.(没有5)
|