STM32 定时器浅谈_1
时间:11-09
来源:互联网
点击:
分频器TIM3_PSC=63 TIM3_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //计数器向上计数模式 TIM3_CR1[4]=0TIM3_TimeBaseStructure.TIM_Period =0xffff; //自动重装载寄存器TIM3_APR TIM3_TimeBaseStructure.TIM_ClockDivision = 0x0; //时钟分频因子 TIM3_CR1[9:8]=00TIM_TimeBaseInit(TIM3,&TIM3_TimeBaseStructure); //写TIM3各寄存器参数TIM3_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle ; //TIM3_CCMR1[14:12]=011 翻转 当TIM3_CCR2=TIM3_CNT时,翻转OC2REF的电平 // TIM3_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2 ;TIM3_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //输入/捕获2输出允许 OC2信号输出到对应的输出引脚PB5TIM3_OCInitStructure.TIM_Pulse =CCR2_Val; //若CC1通道配置为输出:CCR2是装入当前捕获/比较2 TIM3_CCR2寄存器的值(预装载值)。//当前捕获/比较寄存器包含了与计数器TIM3_CNT比较的值,并且在OC端口上输出信号TIM3_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //输出极性 低电平有效 TIM3_CCER[5]=1;TIM_OC2Init(TIM3, &TIM3_OCInitStructure);TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Disable); //TIM3_CCMR1[1]=0 禁止TIM3_CCR2寄存器的预装载功能,可随时写入TIM3_CCR2//且新值马上起作用 TIM_Cmd(TIM3,ENABLE); //启动定时器3 TIM3_CR1[0]=1; TIM_ITConfig(TIM3,TIM_IT_CC2,ENABLE); //TIM3_DIER[2]=1 允许捕获/比较2中断}
it.c
当我们把tim.c中的TIM3_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle ;
it.c
void TIM3_IRQHandler(void){if(TIM_GetITStatus(TIM3,TIM_IT_CC2)!=RESET){TIM_ClearITPendingBit (TIM3,TIM_IT_CC2);capture1=TIM_GetCapture2(TIM3);TIM_SetCompare2(TIM3,capture1+CCR2_Val); }}
当我们把tim.c中的TIM3_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle ;
换成:TIM3_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2 ;时可以看到占空一个近似于1:2的占空比波形图
stm32定时 相关文章:
- 使用STM32的定时器进行输入脉冲的计数(12-02)
- STM32定时器中断设置方法(12-02)
- STM32 定时器产生PWM彻底应用(12-02)
- 转两个stm32定时器的使用代码(11-30)
- stm32高级定时器重新学习(11-30)
- 关于stm32的定时器主从模式概念(11-30)