幸福的拾荒者 发表于 2017-4-5 13:11:22

STM32 PWM输出问题

请问有没有知道这个程序有什么问题吗,下载到芯片一直没有PWM输出,不知道为什么,请教一下,谢谢。。。。

yygkqzh 发表于 2017-5-5 21:45:42

找到问题所在了 ,少下面这一句,添加后就好了,不光要使能定时器、GPIO,还要使能功能复用
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);

wenyangzeng 发表于 2017-5-5 07:25:08

Period比Pulse小,就没有输出。

时光虫子 发表于 2017-5-5 08:30:25

看你上面代码没有看到问题,你最好一步一步仿真看看到底怎么回事

eagle0754 发表于 2017-5-5 08:38:54

官方例子看下
void TIM_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;

/* TIM10 clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM10, ENABLE);

/* GPIOF clock enable */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE);

/* GPIOF Configuration: TIM10 CH1 (PF6) */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;
GPIO_Init(GPIOF, &GPIO_InitStructure);

/* Connect TIM pins to AF3 */
GPIO_PinAFConfig(GPIOF, GPIO_PinSource6, GPIO_AF_TIM10);
}

int main(void)
{
/*!< At this stage the microcontroller clock setting is already configured,
       this is done through SystemInit() function which is called from startup
       file (startup_stm32f2xx.s) before to branch to application main.
       To reconfigure the default setting of SystemInit() function, refer to
       system_stm32f2xx.c file
   */

/* TIM Configuration */
TIM_Config();

/* ---------------------------------------------------------------------------
    TIM10 Configuration: generate 1 PWM signal:
   
    In this example TIM10 input clock (TIM10CLK) is set to 2 * APB2 clock (PCLK2),
    since APB2 prescaler is different from 1.   
      TIM10CLK = 2 * PCLK2
      PCLK2 = HCLK / 2
      => TIM10CLK = HCLK = SystemCoreClock
         
    To get TIM10 counter clock at 20 MHz, the prescaler is computed as follows:
       Prescaler = (TIM10CLK / TIM10 counter clock) - 1
       Prescaler = (SystemCoreClock /20 MHz) - 1
                                             
    To get TIM10 output clock at 30 KHz, the period (TIM10_ARR) is computed as follows:
       ARR = (TIM10 counter clock / TIM10 output clock) - 1
         = 665
                  
    TIM10 Channel1 duty cycle = (TIM10_CCR1/ TIM10_ARR)* 100 = 50%

    Note:
   SystemCoreClock variable holds HCLK frequency and is defined in system_stm32f2xx.c file.
   Each time the core clock (HCLK) changes, user had to call SystemCoreClockUpdate()
   function to update SystemCoreClock variable value. Otherwise, any configuration
   based on this variable will be incorrect.   
--------------------------------------------------------------------------- */   


/* Compute the prescaler value */
PrescalerValue = (uint16_t) (SystemCoreClock / 20000000) - 1;

/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period = 665;
TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

TIM_TimeBaseInit(TIM10, &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(TIM10, &TIM_OCInitStructure);

TIM_OC1PreloadConfig(TIM10, TIM_OCPreload_Enable);

TIM_ARRPreloadConfig(TIM10, ENABLE);

/* TIM10 enable counter */
TIM_Cmd(TIM10, ENABLE);

while (1)
{}
}

队长shiwo 发表于 2017-5-5 08:56:49

从上面代码看不出PWM输出函数

无薪税绵 发表于 2017-5-5 09:05:59

本帖最后由 无薪税绵 于 2017-5-5 09:07 编辑

给你一个参考:
http://www.cnblogs.com/jiwangbujiu/p/5616376.html

toofree 发表于 2017-5-5 09:47:43

本帖最后由 toofree 于 2017-5-5 09:50 编辑

问题暂未看出来。

IO模式后面设置过了,刚没看到

harvardx 发表于 2017-5-5 10:10:25

搞清楚实质. 脉宽 ,周期, 脉宽是通过match寄存器实现.

cldym 发表于 2017-5-5 10:29:17

问题暂未看出来。

sting 发表于 2017-5-5 10:37:53

CCR1 = ??
页: [1] 2 3
查看完整版本: STM32 PWM输出问题