求助!!!CH32V103 TIM3设置PWM无输出,同样的配置TIM1可以

用开发板提供的PWM例程修改的TIM3 PWM无输出,TIM1就可以


/********************************** (C) COPYRIGHT *******************************

* File Name          : main.c

* Author             : WCH

* Version            : V1.0.0

* Date               : 2020/04/30

* Description        : Main program body.

*******************************************************************************/


/*

 *@Note

 PWM输出例程:

 TIM1_CH1(PA8)

 本例程演示 TIM_CH1(PA8) 引脚在 PWM模式1和PWM模式2 下,输出 PWM。

 

*/


#include "debug.h"


/* PWM Output Mode Definition */

#define PWM_MODE1   0

#define PWM_MODE2   1


/* PWM Output Mode Selection */

//#define PWM_MODE PWM_MODE1

#define PWM_MODE PWM_MODE2


/*******************************************************************************

* Function Name  : TIM1_PWMOut_Init

* Description    : Initializes TIM1 PWM output.

* Input          : arr: the period value.

*                  psc: the prescaler value.

* ccp: the pulse value.

* Return         : None

*******************************************************************************/

void TIM1_PWMOut_Init( u16 arr, u16 psc, u16 ccp )

{

GPIO_InitTypeDef GPIO_InitStructure;

TIM_OCInitTypeDef TIM_OCInitStructure;

TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;


RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA | RCC_APB2Periph_TIM1, ENABLE );


GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_11;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_Init( GPIOA, &GPIO_InitStructure );


TIM_TimeBaseInitStructure.TIM_Period = arr;

TIM_TimeBaseInitStructure.TIM_Prescaler = psc;

TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;

TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;

TIM_TimeBaseInit( TIM1, &TIM_TimeBaseInitStructure);


#if (PWM_MODE == PWM_MODE1)

  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;


#elif (PWM_MODE == PWM_MODE2)

TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;


#endif


TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;

TIM_OCInitStructure.TIM_Pulse = ccp;

TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;

TIM_OC1Init( TIM1, &TIM_OCInitStructure );

TIM_OC4Init(TIM1,&TIM_OCInitStructure);

TIM_CtrlPWMOutputs(TIM1, ENABLE );

TIM_OC1PreloadConfig( TIM1, TIM_OCPreload_Enable );

    TIM_OC4PreloadConfig( TIM1, TIM_OCPreload_Enable );

TIM_ARRPreloadConfig( TIM1, ENABLE );

TIM_Cmd( TIM1, ENABLE );

}



void TIM3_PWMOut_Init( u16 arr, u16 psc, u16 ccp )

{

    GPIO_InitTypeDef GPIO_InitStructure;

    TIM_OCInitTypeDef TIM_OCInitStructure;

    TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;

    TIM_InternalClockConfig(TIM3); //选择时钟为内部时钟

    RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB|RCC_APB1Periph_TIM3, ENABLE );


    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7;

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;

    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

    GPIO_Init( GPIOA, &GPIO_InitStructure );

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1;

    GPIO_Init( GPIOB, &GPIO_InitStructure );


    TIM_TimeBaseInitStructure.TIM_Period = arr;

    TIM_TimeBaseInitStructure.TIM_Prescaler = psc;

    TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;

    TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;

    TIM_TimeBaseInit( TIM3, &TIM_TimeBaseInitStructure);


#if (PWM_MODE == PWM_MODE1)

  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;


#elif (PWM_MODE == PWM_MODE2)

    TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;


#endif


    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;

    TIM_OCInitStructure.TIM_Pulse = ccp;

    TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;

    TIM_OC1Init( TIM3, &TIM_OCInitStructure );

    TIM_OC2Init(TIM3,&TIM_OCInitStructure);

    TIM_OC3Init(TIM3,&TIM_OCInitStructure);

    TIM_OC4Init(TIM3,&TIM_OCInitStructure);

    TIM_CtrlPWMOutputs(TIM3, ENABLE );

    TIM_OC1PreloadConfig( TIM3, TIM_OCPreload_Enable );

    TIM_OC2PreloadConfig( TIM3, TIM_OCPreload_Enable );

    TIM_OC3PreloadConfig( TIM3, TIM_OCPreload_Enable );

    TIM_OC4PreloadConfig( TIM3, TIM_OCPreload_Enable );

    //TIM_ARRPreloadConfig( TIM3, ENABLE );

    TIM_Cmd( TIM3, ENABLE );

}



/*******************************************************************************

* Function Name  : main

* Description    : Main program.

* Input          : None

* Return         : None

*******************************************************************************/

int main(void)

{

     u16 pwmval=0;

     u8 a=1;

     Delay_Init();

     USART_Printf_Init(115200);

     printf("SystemClk:%d\r\n",SystemCoreClock);


     TIM1_PWMOut_Init( 899, 0, 1000 );

     TIM3_PWMOut_Init( 899, 0, 500 );


     while(1)

     {

         Delay_Ms(1);

         if(a) pwmval++;

         else  pwmval--;

         if(pwmval >500) a =0;

         if(pwmval==0) a =1;

         TIM_SetCompare1(TIM1, pwmval);

         TIM_SetCompare4(TIM1, pwmval);

         TIM_SetCompare1(TIM3, pwmval);

         TIM_SetCompare2(TIM3, pwmval);

         TIM_SetCompare3(TIM3, pwmval);

         TIM_SetCompare4(TIM3, pwmval);


     }

}



您好,注意看下面截图圈出位置,TIM3时钟使能错误,修改过来再试试,麻烦仔细检查一下程序。

image.png


j_0051.gif就是这里的问题,修改后解决了,谢谢谢大佬,祝您新年快乐,虎年大吉!


只有登录才能回复,可以选择微信账号登录