如果PWM的输出时钟和管脚配置都写在外部函数里面,比如rcc_configuration和gpio_configuration里面,将这两个函数放在PWM配置函数之前执行,下载到stm32f103里面,重新上电后PWM输出管脚会出问题。有遇到这种问题的么?
如图,若将GPIO配置函数放在PWM配置函数之前执行,下载后重新上电发现TIM1产生的PWM无输出了(PA8管脚)。
我就是先配置GPIO,再配置PWM的,没有问题啊,是不是你配置出错了,什么挂载时钟啊,引脚输出模式,
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10; //TIM1输出
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
TIM_DeInit(TIM1);
TIM_TimeBaseStructure.TIM_Prescaler = 2; //TIM基本初始化
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Down;//中央对齐计数模式
TIM_TimeBaseStructure.TIM_Period = 1499; //PWM 16K
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM1,&TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //TIM输出通道初始化
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Disable;
// TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Disable;
TIM_OCInitStructure.TIM_Pulse =1600; //通道赋无效值
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
// TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
// TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
TIM_OC1Init(TIM1,&TIM_OCInitStructure);
拿出部分参考参考
天地庄周马;江湖范蠡船。
个性签名还是放QQ号吧,2060347305,添加说明EEworld好友
我是把所有的时钟配置写在一起的,不是在定时器函数里面配置的
int main(void)
{
RCC_Configuration(); //系统时钟初始化
GPIO_Configuration();
TIM4_CH1PWM_Configuration(); //打开ctrl PWM 30HZ
TIM1_CH1PWM_Configuration(); //打开boost升压PWM 33.3kHZ
}
void RCC_Configuration(void)
{
SystemInit(); //系统时钟初始化
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE); //oled端口使能 PA端口
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); //ctrl PWM定时器挂接 30Hz
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); //GPIOB挂接在APB2上 ctrl PWM PB6
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE); //BOOST PWM定时器挂接 33KHz
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE); //打开APB2复用功能
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE); //USART3挂接在APB1上
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //TIM3时钟挂载在APB1上
}
void GPIO_Configuration(void) //PWM端口配置写在PWM发生函数中
{
GPIO_InitTypeDef GPIO_InitStructure;
/*oled端口配置*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//速度50MHz
GPIO_Init(GPIOA, &GPIO_InitStructure); //初始化GPIOA
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_11;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOB,&GPIO_InitStructure); //PB11用作蓝牙的rx口 浮空输入模式
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_Init(GPIOB,&GPIO_InitStructure); //PB10用作蓝牙的Tx口 推挽输出
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_1;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_Init(GPIOB,&GPIO_InitStructure); //PB1用作蓝牙的模式选择口 推挽输出
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_8;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOB,&GPIO_InitStructure); //PB8配置为开漏输出 蓝牙信息发送开关
}
void TIM4_CH1PWM_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
TIM_DeInit(TIM4);
TIM_InternalClockConfig(TIM4);
TIM_TimeBaseStructure.TIM_Period = 33333-1; //脉冲宽度
TIM_TimeBaseStructure.TIM_Prescaler = 72-1; //72:0x48 1MHZ
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //1分频:72MHZ
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_RepetitionCounter=0;////////////////////////////
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 33331;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC1Init(TIM4, &TIM_OCInitStructure); //30hz,导通2us
TIM_OC1PreloadConfig(TIM4, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM4, ENABLE);
TIM_Cmd(TIM4, ENABLE);
TIM_CtrlPWMOutputs(TIM4,ENABLE);
}
/*boost PWM端口函数 33.3khz*/
void TIM1_CH1PWM_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
TIM_DeInit(TIM1);
TIM_InternalClockConfig(TIM1);
TIM_TimeBaseStructure.TIM_Period = 270-1; //脉冲宽度
TIM_TimeBaseStructure.TIM_Prescaler = 8-1; //72:0x48 9MHZ
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //1分频:72MHZ
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_RepetitionCounter=0;////////////////////////////
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
/* TIM4 PWM2 Mode configuration: Channel1 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 108;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM_OC1Init(TIM1, &TIM_OCInitStructure); //33khz
TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM1, ENABLE);
TIM_Cmd(TIM1, ENABLE);
TIM_CtrlPWMOutputs(TIM1,ENABLE);
}
上面是代码,这样会使33k的PWM重新上电后没输出
void Pwminit_Tim1(void) //A8A9A10A11
{
RCC->APB2ENR|=0X800;
GPIOA->CRH&=0xffff0000;
GPIOA->CRH|=0xbbbb;
TIM1->ARR=40000;
TIM1->PSC=63;
TIM1->CCMR1|=0x6868;
TIM1->CCMR2|=0x6868;
TIM1->CCER|=0x1111;
TIM1->CR1=0x0080;
TIM1->BDTR|=0x8000;
TIM1->CR1|=0x01;
}
应该好用,ARR和PSC自己设置