//TIM1 分频
#define TIM1_DIV1 (1-1)
#define TIM1_DIV2 (2-1)
#define TIM1_DIV4 (4-1)
#define TIM1_DIV8 (8-1)
#define TIM1_DIV9 (9-1)
#define TIM1_DIV18 (18-1)
#define TIM1_DIV72 (72-1)
//*************************************
// 函数名:TIM1_NVIC_Configuration
// 描述 :TIM1中断优先级配置
// 输入 :无
// 输出 :无
//***************************************
void TIM1_NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_IRQn ; //TIM1更新中断
// NVIC_InitStructure.NVIC_IRQChannel = TIM1_CC_IRQn ; //TIM1比较中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
//*************************************
// 函数名称:TIM1_BASEInitSys
// 函数功能:TIM1基本初始化 用作定时器
// 入口参数:计数个数 分频数
// 出口参数:无
// 返回值 :无
//***************************************
void TIM1_BASEInitSys(int16_t Tcon,uint16_t psc)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE);
TIM_DeInit(TIM1); //复位定时器
TIM_TimeBaseStructure.TIM_Period = Tcon; //定时器初始值
TIM_TimeBaseStructure.TIM_Prescaler = psc ; //时钟预分频
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; // 时钟分割
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数模式
TIM_TimeBaseInit(TIM1,&TIM_TimeBaseStructure); //初始化定时器的值
TIM_ClearFlag(TIM1,TIM_FLAG_Update); //清除定时器中断标志
// TIM_ITConfig(TIM1,TIM_IT_Update,ENABLE); //使能中断
// 通道1 2 3 配置为PWM1模式
TIM1->CCMR1 = 0X6C6C ; //
TIM1->CCMR2 = 0X006C ;
// 使能比较通道1 2 3 且1电平为有效电平
TIM1->CCER = 0X0011 ;
//
TIM1->CCR1 = 0 ;
TIM1->CCR2 = 0 ;
TIM1->CCR3 = 0 ;
TIM1->CCR4 = 0 ;
TIM_Cmd(TIM1,ENABLE); //开启时钟
}
//----实例应用-----
TIM1_NVIC_Configuration() ; //开启更新中断
TIM1_BASEInitSys(125,TIM1_DIV72) ; //72M/72=1M 定时125U
stm32f103 TIM1初始化--定时器应用
最新推荐文章于 2024-09-29 18:01:33 发布