C PWM输出在STM32F10x OPEN107V开发板上不工作
您好,我有“stm32f10x open107V开发板”,我修改了制造商提供的PWM代码,但我没有在开发板上提供的LED上获得任何PWM输出,请任何人帮助获取以下代码。 portB(GPIOB)上的GPIO_引脚0,1,14,15是开发板上给出的Led引脚。代码是无错误的,链接时没有错误。我不明白问题出在哪里C PWM输出在STM32F10x OPEN107V开发板上不工作,c,microcontroller,stm32,C,Microcontroller,Stm32,您好,我有“stm32f10x open107V开发板”,我修改了制造商提供的PWM代码,但我没有在开发板上提供的LED上获得任何PWM输出,请任何人帮助获取以下代码。 portB(GPIOB)上的GPIO_引脚0,1,14,15是开发板上给出的Led引脚。代码是无错误的,链接时没有错误。我不明白问题出在哪里 /** -----------------------------------------------------------------*/ #incl
/**
-----------------------------------------------------------------*/
#include "stm32f10x_gpio.h"
#include "stm32f10x_rcc.h"
#include "stm32f10x_tim.h"
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
uint16_t CCR1_Val = 333;
uint16_t CCR2_Val = 249;
uint16_t CCR3_Val = 166;
uint16_t CCR4_Val = 83;
uint16_t PrescalerValue ;
/* Private function prototypes -----------------------------------------------*/
void RCC_Configuration(void);
void GPIO_Configuration(void);
/* Private functions ---------------------------------------------------------*/
/**
int main(void)
{
RCC_Configuration();
/*GPIO Configuration */
GPIO_Configuration();
PrescalerValue =(72000000 / 24000000) - 1;
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period = 665;
TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue;
TIM_TimeBaseStructure.TIM_ClockDivision = 4;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
/* PWM1 Mode configuration: Channel1 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = CCR1_Val;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC1Init(TIM3, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);
/* PWM1 Mode configuration: Channel2 */
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = CCR2_Val;
TIM_OC2Init(TIM3, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);
/* PWM1 Mode configuration: Channel3 */
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = CCR3_Val;
TIM_OC3Init(TIM3, &TIM_OCInitStructure);
TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Enable);
/* TIM3 enable counter */
TIM_Cmd(TIM3, ENABLE);
while (1)
{}
}
void RCC_Configuration(void)
{
/* TIM3 clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
/* GPIOA and GPIOB clock enable */
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/*GPIOB Configuration: TIM3 channel1, 2, 3 and 4 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_14|GPIO_Pin_15 ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_PinRemapConfig(GPIO_FullRemap_TIM3, ENABLE);
}
首先,如果您查看STM32F107数据表(这是我假设您拥有的MCU),PB0和PB1引脚可以映射到TIM3_CH3和TIM3_CH4,但PB14和PB15不能映射到任何TIM3通道——只能映射到TIM1通道。至少我的数据表版本见第3节表5 其次,您正在使用AFIO重新映射功能,调用了
GPIO\u PinRemapConfig()
。实际上连接到LED的两个引脚(PB0和PB1)不需要重新映射,而其他两个引脚(PB14和PB15)无论您做什么都无法重新映射到TIM3通道
具体地说,您只需取消对GPIO\u PinRemapConfig()
的调用,就可以在两个引脚(PB0和PB1)上获得PWM输出,而代码更改最少。至于PB14和PB15,您需要配置TIM1,这将需要大量额外的代码。不幸的是,您无法将这些引脚映射到TIM3