STM32 PWM 模式
时间:10-02
整理:3721RD
点击:
本来应该输出方波 为什么出现这种情况?
代码如下:
#include "pwm.h"
#include "led.h"
#include "usart.h"
//TIM14 PWM2?·?3?ê??ˉ
//PWMê?3?3?ê??ˉ
//arr£o×??ˉ??×°?μ
//psc£oê±?ó?¤·??μêy
void TIM14_PWM_Init(u32 arr,u32 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM14,ENABLE); //TIM14ê±?óê1?ü
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); //ê1?üPORTFê±?ó
GPIO_PinAFConfig(GPIOA,GPIO_PinSource7,GPIO_AF_TIM14); //GPIO?′ó??a?¨ê±?÷14
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7; //GPIO
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //?′ó?1|?ü
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; //?ù?è100MHz
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //í?íì?′ó?ê?3?
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //é?à-
GPIO_Init(GPIOA,&GPIO_InitStructure); //3?ê??ˉ
TIM_TimeBaseStructure.TIM_Prescaler=psc; //?¨ê±?÷·??μ
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up; //?òé???êy?£ê?
TIM_TimeBaseStructure.TIM_Period=arr; //×??ˉ??×°???μ
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_div1;
TIM_TimeBaseInit(TIM14,&TIM_TimeBaseStructure);//3?ê??ˉ?¨ê±?÷14
//3?ê??ˉTIM14 Channel1 PWM?£ê?
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //?????¨ê±?÷?£ê?:TIM??3??í?èμ÷???£ê?2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //±è??ê?3?ê1?ü
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //ê?3???D?:TIMê?3?±è????D?μí
TIM_OC1Init(TIM14, &TIM_OCInitStructure); //?ù?YT???¨μ?2?êy3?ê??ˉíaéèTIM1 4OC1
TIM_OC1PreloadConfig(TIM14, TIM_OCPreload_Enable); //ê1?üTIM14?úCCR1é?μ??¤×°????′??÷
TIM_ARRPreloadConfig(TIM14,ENABLE);//ARPEê1?ü
TIM_Cmd(TIM14, ENABLE); //ê1?üTIM14
}
int main(void)
{
//u16 led0pwmval=0;
//u8 dir=1;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//éè???μí3?D??ó??è??·?×é2
delay_init(168); //3?ê??ˉ?óê±oˉêy
uart_init(115200);//3?ê??ˉ′??ú2¨ì??ê?a115200
TIM14_PWM_Init(500-1,32-1); //84M/84=1Mhzμ???êy?μ?ê,??×°???μ500£??ùò?PWM?μ?ê?a 1M/500=2Khz.
TIM_SetCompare1(TIM14,250);
}
代码如下:
#include "pwm.h"
#include "led.h"
#include "usart.h"
//TIM14 PWM2?·?3?ê??ˉ
//PWMê?3?3?ê??ˉ
//arr£o×??ˉ??×°?μ
//psc£oê±?ó?¤·??μêy
void TIM14_PWM_Init(u32 arr,u32 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM14,ENABLE); //TIM14ê±?óê1?ü
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); //ê1?üPORTFê±?ó
GPIO_PinAFConfig(GPIOA,GPIO_PinSource7,GPIO_AF_TIM14); //GPIO?′ó??a?¨ê±?÷14
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7; //GPIO
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //?′ó?1|?ü
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; //?ù?è100MHz
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //í?íì?′ó?ê?3?
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //é?à-
GPIO_Init(GPIOA,&GPIO_InitStructure); //3?ê??ˉ
TIM_TimeBaseStructure.TIM_Prescaler=psc; //?¨ê±?÷·??μ
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up; //?òé???êy?£ê?
TIM_TimeBaseStructure.TIM_Period=arr; //×??ˉ??×°???μ
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_div1;
TIM_TimeBaseInit(TIM14,&TIM_TimeBaseStructure);//3?ê??ˉ?¨ê±?÷14
//3?ê??ˉTIM14 Channel1 PWM?£ê?
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //?????¨ê±?÷?£ê?:TIM??3??í?èμ÷???£ê?2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //±è??ê?3?ê1?ü
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //ê?3???D?:TIMê?3?±è????D?μí
TIM_OC1Init(TIM14, &TIM_OCInitStructure); //?ù?YT???¨μ?2?êy3?ê??ˉíaéèTIM1 4OC1
TIM_OC1PreloadConfig(TIM14, TIM_OCPreload_Enable); //ê1?üTIM14?úCCR1é?μ??¤×°????′??÷
TIM_ARRPreloadConfig(TIM14,ENABLE);//ARPEê1?ü
TIM_Cmd(TIM14, ENABLE); //ê1?üTIM14
}
int main(void)
{
//u16 led0pwmval=0;
//u8 dir=1;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//éè???μí3?D??ó??è??·?×é2
delay_init(168); //3?ê??ˉ?óê±oˉêy
uart_init(115200);//3?ê??ˉ′??ú2¨ì??ê?a115200
TIM14_PWM_Init(500-1,32-1); //84M/84=1Mhzμ???êy?μ?ê,??×°???μ500£??ùò?PWM?μ?ê?a 1M/500=2Khz.
TIM_SetCompare1(TIM14,250);
}
你的pwm通道和配置的复用功能的io,匹配上了吗,还有就是,你都把定时器重新配置了一遍,为什么还要引用pwm.h文件
还有,你的main函数怎么是int类型的,没看到有返回值啊
stm32 好像只有8个定时器,你的TIM14是哪里来的?
main函数里边加个while(1)
TIM14 15 16 17是高级定时器,要加上TIM_CtrlPWMOutputs(TIM14, ENABLE);这句
TIM14_PWM_Init(500-1,32-1); //84M/84=1Mhzμ???êy?μ?ê,??×°???μ500£??ùò?P 敢为频率是如何计算的,谢谢
1