现在两路PWM输出都有了,但是为什么占空比设置一样输出的电压不一样,还有之前TIM9的调用函数名为TIM9_PWM_Init()时 PE6一直输出高电平,怎么调占空比都没用
还有的就是TIM14占空比设置的低的时候例如 TIM_SetCompare1(TIM14,20); 输出的电压接近于 TIM_SetCompare1(TIM14,200);输出的电压
#include "stm32f4xx.h"
#include "gpio.h"
#include "delay.h"
#include "pwm.h"
#include "xj.h"
#include "usart.h"
int main(void)
{
delay_init(168); //3õê¼»ˉÑóê±oˉêy
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,84-1);
TIM9_CH2_PWM_Init(500-1,84-1);
while(1)
{
TIM_SetCompare1(TIM14,200);
TIM_SetCompare2(TIM9,400); //DT¸Ä±è½ÏÖ죬DT¸ÄÕ¼¿Õ±è
}
}
void TIM14_PWM_Init(u32 arr,u32 psc)
{
//′Ë2¿·ÖDèêÖ¶ˉDT¸ÄIO¿úéèÖÃ
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM14,ENABLE); //TIM14ê±Öóê1Äü
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE); //ê1ÄüPORTFê±Öó
GPIO_PinAFConfig(GPIOF,GPIO_PinSource9,GPIO_AF_TIM14); //GPIOF9¸′óÃÎa¶¨ê±Æ÷14
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //GPIOF9
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(GPIOF,&GPIO_InitStructure); //3õê¼»ˉPF9
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
}
void TIM9_CH2_PWM_Init(u32 arr,u32 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM9,ENABLE); //TIM9ê±Öóê1Äü
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE); //ê1ÄüPORTAê±Öó
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; //GPIOA3
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(GPIOE,&GPIO_InitStructure); //3õê¼»ˉPA3
GPIO_PinAFConfig(GPIOE,GPIO_PinSource6,GPIO_AF_TIM9); //GPIOA3¸′óÃ붨ê±Æ÷9 AF3
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(TIM9,&TIM_TimeBaseStructure);//3õê¼»ˉ¶¨ê±Æ÷9
//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_High; //êä3ö¼«DÔ:TIMêä3ö±è½Ï¼«DÔ¸ß
TIM_OC2Init(TIM9, &TIM_OCInitStructure); //¸ù¾YTÖ¸¶¨μÄ2Îêy3õê¼»ˉíaéèTIM9 OC2
TIM_OC2PreloadConfig(TIM9, TIM_OCPreload_Enable); //ê1ÄüTIM9ÔúCCR2éÏμÄԤװÔؼÄ′æÆ÷
TIM_ARRPreloadConfig(TIM9,ENABLE);//ARPEê1Äü
TIM_Cmd(TIM9, ENABLE); //ê1ÄüTIM9
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>