ttl_web 发表于 2016-10-29 16:09:11

没有PWM输出

本帖最后由 ttl_web 于 2016-11-1 16:36 编辑

请教以下代码,始终无法看到PWM输出//问题:没PWM输出
//TIM3产生1路PWM
//stm32f103vet6

#include "stm32f10x.h"
void RCC_conf()
{
      RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
      RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
      RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
}
void GPIO_conf()
{
      GPIO_InitTypeDef GPIO_InitStructure;      
      
      GPIO_InitStructure.GPIO_Pin=GPIO_Pin_6;
      GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
      GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
      GPIO_Init(GPIOC,&GPIO_InitStructure);
      
}
void TIM3_conf() {
      TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
      TIM_OCInitTypeDef TIM_OCInitStructure;
      
      GPIO_PinRemapConfig(GPIO_FullRemap_TIM3,ENABLE);
      
      TIM_TimeBaseStructure.TIM_Period=900-1;
      TIM_TimeBaseStructure.TIM_Prescaler=0;
      TIM_TimeBaseStructure.TIM_ClockDivision=0;
      TIM_TimeBaseStructure.TIM_CounterMode=
                TIM_CounterMode_Up;
      TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStructure);
      
      TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM1;
      TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;
      TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High;
      TIM_OC1Init(TIM3,&TIM_OCInitStructure);      
      TIM_OC1PreloadConfig(TIM3,TIM_OCPreload_Enable);
      
      TIM_Cmd(TIM3,ENABLE);      
}

int main()
{
      //SystemInit();
      RCC_conf();
      GPIO_conf();
      TIM3_conf();

      while (1)
      {
               
      }
}

andrewz 发表于 2016-10-29 19:06:30

有个MOE位不知道LZ用了没?



peter001 发表于 2016-10-30 03:19:56

跟踪一下,看看是不是gpio模式不对

haifeng-388081 发表于 2016-10-30 12:12:26

用示波器看看。

samhong 发表于 2016-10-31 00:30:34

签到,好多内容,谢谢!

jackeyt 发表于 2016-10-31 08:54:21

你都没有给TIM3->CCR1赋值 ,当然没有东西输出了。

ttl_web 发表于 2016-10-31 11:34:52

jackeyt 发表于 2016-10-31 08:54
你都没有给TIM3->CCR1赋值 ,当然没有东西输出了。

CREN 已使能

ttl_web 发表于 2016-10-31 14:26:23

//已看到PWM

//TIM3产生1路PWM
//stm32f103vet6

#include "stm32f10x.h"
void RCC_conf()
{
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
}
void GPIO_conf()
{
        GPIO_InitTypeDef GPIO_InitStructure;       
       
        GPIO_InitStructure.GPIO_Pin=GPIO_Pin_6;
        GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
        GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
        GPIO_Init(GPIOC,&GPIO_InitStructure);
       
}
void TIM3_conf() {
        TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
        TIM_OCInitTypeDef TIM_OCInitStructure;
       
        GPIO_PinRemapConfig(GPIO_FullRemap_TIM3,ENABLE);
       
        TIM_TimeBaseStructure.TIM_Period=900-1;
        TIM_TimeBaseStructure.TIM_Prescaler=0;
        TIM_TimeBaseStructure.TIM_ClockDivision=0;
        TIM_TimeBaseStructure.TIM_CounterMode=
                TIM_CounterMode_Up;
        TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStructure);
       
        TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM1;
        TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;
        TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High;
        TIM_OCInitStructure.TIM_Pulse=100;
        TIM_OC1Init(TIM3,&TIM_OCInitStructure);       
        TIM_OC1PreloadConfig(TIM3,TIM_OCPreload_Enable);
        TIM_ARRPreloadConfig(TIM3,ENABLE);
        TIM_Cmd(TIM3,ENABLE);       
}

int main()
{
        //SystemInit();
        RCC_conf();
        GPIO_conf();
        TIM3_conf();

        while (1)
        {
               
        }
}

ttl_web 发表于 2016-10-31 14:28:06

jackeyt 发表于 2016-10-31 08:54
你都没有给TIM3->CCR1赋值 ,当然没有东西输出了。

谢谢点拨

xhzheng 发表于 2016-10-31 16:59:02

限流电阻太大了吧,即使有,也看不清楚;
直接短路1K电阻看看,可以直接驱动LED的;

页: [1] 2
查看完整版本: 没有PWM输出