用開發(fā)板提供的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);
? ? ?}
}