STM32一定时器产生不同频率的PWM
SPI1-> 定时器中断中,根据这个参数,改变频率。
#include "stm32libstm32f10x.h"
#include "hal.h"
volatile u16 Key_Value=1000;
int main(void)
{
ChipHalInit();
ChipOutHalInit();
while(1)
{
}
}
extern volatile u16 Key_Value;
u16 capture=0;
void TIM3_IRQHandler(void)
{
if (TIM_GetITStatus(TIM3, TIM_IT_CC2) != RESET)
{
capture = TIM_GetCapture2(TIM3);
}
}
void TIM3_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
TIM_TimeBaseStructure.TIM_Prescaler = 5;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_Period = 65535;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_div1;
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0x0;
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //正向通道有效
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Disable;//反向通道无效
TIM_OCInitStructure.TIM_Pulse = Key_Value;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
TIM_OC2Init(TIM3,&TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Disable);
TIM_Cmd(TIM3,ENABLE);
//TIM_CtrlPWMOutputs(TIM1,ENABLE);
TIM_ITConfig(TIM3, TIM_IT_CC2 , ENABLE);
}
注意:在计算PWM频率的时候,TIMx的时钟都是72Mhz,分频后,因为翻转两次才能形成一个PWM波,因为,PWM的频率是捕获改变频率的1/2。
STM32定时器不同频率PW 相关文章:
- Windows CE 进程、线程和内存管理(11-09)
- RedHatLinux新手入门教程(5)(11-12)
- uClinux介绍(11-09)
- openwebmailV1.60安装教学(11-12)
- Linux嵌入式系统开发平台选型探讨(11-09)
- Windows CE 进程、线程和内存管理(二)(11-09)