1、STM32 笔记(一)TIM 模块定时器向上溢出 /Step2.中断 NVIC 设置:允许中断,设置优先级NVIC_InitStructure.NVIC_IRQChannel =TIM1_UP_IRQChannel; /更新事件NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; /抢占优先级 0NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; /响应优先级 1NVIC_InitStructure.NVIC_IRQChannelCmd =ENABLE; /允许中断NVIC_Init
2、( /写入设置/Step3.TIM1 模块设置voidTIM_Configuration(void)TIM_TimeBaseInitTypeDef TIM_BaseInitStructure;TIM_OCInitTypeDef TIM_OCInitStructure;/TIM1使用内部时钟/TIM_InternalClockConfig(TIM1);/TIM1 基本设置/设置预分频器分频系数 71,即 APB2=72M,TIM1_CLK=72/72=1MHz/TIM_Period( TIM1_ARR)=1000 ,计数器向上计数到 1000 后产生更新事件,计数值归零/向上计数模式/TIM_R
3、epetitionCounter(TIM1_RCR)=0,每次向上溢出都产生更新事件TIM_BaseInitStructure.TIM_Period = 1000;TIM_BaseInitStructure.TIM_Prescaler = 71;TIM_BaseInitStructure.TIM_ClockDivision = 0;TIM_BaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;TIM_BaseInitStructure.TIM_RepetitionCounter = 0;TIM_TimeBaseInit(TIM1,/清中断
4、,以免一启用中断后立即产生中断TIM_ClearFlag(TIM1, TIM_FLAG_Update);/使能 TIM1 中断源TIM_ITConfig(TIM1, TIM_IT_Update, ENABLE);/TIM1 总开关:开启TIM_Cmd(TIM1, ENABLE);/Step4.中断服务子程序:voidTIM1_UP_IRQHandler(void)GPIOC-ODR = (14); /闪灯TIM_ClearITPendingBit(TIM1, TIM_FLAG_Update); /清中断下面是输出比较功能实现 TIM1_CH1 管脚输出指定频率的脉冲:C 语言: TIM1 模块
5、实现输出比较,自动翻转并触发中断/Step1.启动 TIM1,同时还要注意给相应功能管脚启动时钟RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);/Step2.PA.8 口设置为 TIM1 的 OC1 输出口GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;GPIO_InitStructure.GPIO_Spee
6、d = GPIO_Speed_50MHz;GPIO_Init(GPIOA, /Step3.使能 TIM1 的输出比较匹配中断NVIC_InitStructure.NVIC_IRQChannel = TIM1_CC_IRQChannel;NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;NVIC_Init(/Step4.TIM 模块设置voi
7、dTIM_Configuration(void)TIM_TimeBaseInitTypeDef TIM_BaseInitStructure;TIM_OCInitTypeDef TIM_OCInitStructure;/TIM1 基本计数器设置TIM_BaseInitStructure.TIM_Period = 0xffff; /这里必须是 65535 TIM_BaseInitStructure.TIM_Prescaler = 71; /预分频 71,即 72 分频,得 1MTIM_BaseInitStructure.TIM_ClockDivision = 0;TIM_BaseInitStruc
8、ture.TIM_CounterMode = TIM_CounterMode_Up;TIM_BaseInitStructure.TIM_RepetitionCounter = 0;TIM_TimeBaseInit(TIM1,/TIM1_OC1 模块设置TIM_OCStructInit( TIM_OCInitStructure.TIM_OCMode =TIM_OCMode_Toggle; /管脚输出模式:翻转TIM_OCInitStructure.TIM_Pulse = 2000; /翻转周期:2000 个脉冲TIM_OCInitStructure.TIM_OutputState =TIM_Ou
9、tputState_Enable; /使能 TIM1_CH1 通道TIM_OCInitStructure.TIM_OCPolarity =TIM_OCPolarity_High; /输出为正逻辑TIM_OC1Init(TIM1,/写入配置/清中断TIM_ClearFlag(TIM1, TIM_FLAG_CC1);/TIM1 中断源设置,开启相应通道的捕捉比较中断TIM_ITConfig(TIM1, TIM_IT_CC1, ENABLE);/TIM1 开启TIM_Cmd(TIM1, ENABLE);/通道输出使能TIM_CtrlPWMOutputs(TIM1, ENABLE);Step5.中断服务子程序voidTIM1_CC_IRQHandler(void)u16capture;if(TIM_GetITStatus(TIM1,TIM_IT_CC1) = SET)TIM_ClearITPendingBit(TIM1, TIM_IT_CC1 );capture = TIM_GetCapture1(TIM1);TIM_SetCompare1(TIM1, capture + 2000);/这里解释下:/将 TIM1_CCR1 的值增加 2000,使得下一个 TIM 事件也需要 2000 个脉冲,/另一种方式是清零脉冲计数器/TIM_SetCounter(TIM2,0x0000);