打印
[应用相关]

stm32输出pwm的问题

[复制链接]
224|1
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
jcky001|  楼主 | 2021-2-23 15:59 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
使用STM32f103的tim1_ch1输出pwm,我在keil里面仿真时,PA8脚输出的波形是需要的方波波形,但是放到硬件时,我测芯片PA8脚值时,始终是0.25V,是什么问题呢



  • void TIM1_config(u32 Cycle)
  • {
  •     GPIO_InitTypeDef GPIO_InitStructure;
  •     TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
  •     TIM_OCInitTypeDef  TIM_OCInitStructure;
  •     RCC_APB2PeriphclockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_TIM1|RCC_APB2Periph_GPIOB , ENABLE);
  •     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;                   //TIM1_CH1 PA8
  •     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  •     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  •     GPIO_Init(GPIOA, &GPIO_InitStructure);
  •     TIM_TimeBaseStructure.TIM_Period = Cycle-1;
  •     TIM_TimeBaseStructure.TIM_Prescaler =71;
  •     TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
  •     TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  •     TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
  •     TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
  •     TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
  •     TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  •           TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
  •     TIM_OCInitStructure.TIM_Pulse = Cycle/2-1;
  •     TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
  •     TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_Low;
  •     TIM_OC1Init(TIM1, &TIM_OCInitStructure);
  •     TIM_SelectMasterSlaveMode(TIM1, TIM_MasterSlaveMode_Enable);
  •     TIM_SelectOutputtrigger(TIM1, TIM_TRGOSource_Update);
  •     TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);
  •     TIM_ARRPreloadConfig(TIM1, ENABLE);
  • }
  • void TIM4_config(u32 PulseNum)
  • {
  •     TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
  •     NVIC_InitTypeDef NVIC_InitStructure;
  •     RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
  •     TIM_TimeBaseStructure.TIM_Period = PulseNum-1;
  •     TIM_TimeBaseStructure.TIM_Prescaler =0;
  •     TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
  •     TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  •     TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
  •     TIM_SelectInputTrigger(TIM4, TIM_TS_ITR0);
  •     //TIM_InternalClockConfig(TIM4);
  •     TIM4->SMCR|=0x07;
  •     //TIM_ITRxExternalClockConfig(TIM4, TIM_TS_ITR0);
  •     //TIM_ARRPreloadConfig(TIM4, ENABLE);
  •     TIM_ITConfig(TIM4,TIM_IT_Update,DISABLE);
  •    // NVIC_PriorityGroupConfig(NVIC_PriorityGroup_3);
  •     NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;
  •     NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  •     NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  •     NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  •     NVIC_Init(&NVIC_InitStructure);
  • }
  • void Pulse_output(u32 Cycle,u32 PulseNum)
  • {
  •     TIM4_config(PulseNum);
  •     TIM_Cmd(TIM4, ENABLE);
  •     TIM_ClearITPendingBit(TIM4,TIM_IT_Update);
  •     TIM_ITConfig(TIM4,TIM_IT_Update,ENABLE);
  •     TIM1_config(Cycle);
  •     TIM_Cmd(TIM1, ENABLE);
  •     TIM_CtrlPWMOutputs(TIM1, ENABLE);
  • }
  • void TIM4_IRQHandler(void)
  • {
  •     if (TIM_GetITStatus(TIM4, TIM_IT_Update) != RESET)
  •     {
  •         TIM_ClearITPendingBit(TIM4, TIM_IT_Update);
  •         TIM_CtrlPWMOutputs(TIM1, DISABLE);
  •         TIM_Cmd(TIM1, DISABLE);
  •         TIM_Cmd(TIM4, DISABLE);
  •         TIM_ITConfig(TIM4, TIM_IT_Update, DISABLE);
  •     }
  • }



[color=rgb(51, 102, 153) !important]复制代码







  • int main(void)
  • {
  •         ChipHalInit();
  •         ChipOutHalInit();
  •         DA_Init();
  • //  tmc2660_init();
  •         TIM_Cmd(TIM2,ENABLE);
  •   NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
  •   Pulse_output(1000,8000);
  •         while(1)
  •         {}
  • }


[color=rgb(51, 102, 153) !important]复制代码


使用特权

评论回复
沙发
香水城| | 2021-2-24 08:30 | 只看该作者
检查下硬件线路 是否发生了电平嵌位而锁在电平。

或者说管脚配置方面有问题,GJPIO没配置好,包括复用配置。

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

1504

主题

4518

帖子

6

粉丝