是可以的,给你个代码看看
#include "stm32f10x.h"
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
uint16_t CCR1_Val = 250;
uint16_t CCR2_Val = 500;
uint16_t CCR3_Val = 750;
uint16_t CCR4_Val = 0;
uint16_t PrescalerValue = 0;
voidRCC_Configuration(void); //时钟配置
voidGPIO_Configuration(void); //管脚配置
void Delay (__IO uint32_tnCount); //延时函数
int main(void)
{
RCC_Configuration();
GPIO_Configuration();
PrescalerValue = (uint16_t) (SystemCoreClock /24000000) - 1;
TIM_TimeBaseStructure.TIM_Period =1000; //周期
TIM_TimeBaseStructure.TIM_Prescaler =PrescalerValue; //分频
TIM_TimeBaseStructure.TIM_ClockDivision =0; //时钟分割
TIM_TimeBaseStructure.TIM_CounterMode =TIM_CounterMode_Up;
//计数模式
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //初始TIM3
/*************************** 通道1 ********************************/
TIM_OCInitStructure.TIM_OCMode =TIM_OCMode_PWM2; //PWM2
TIM_OCInitStructure.TIM_OutputState =TIM_OutputState_Enable; //PWM功能使能
TIM_OCInitStructure.TIM_Pulse =CCR1_Val; //写比较值(占空比
TIM_OCInitStructure.TIM_OCPolarity =TIM_OCPolarity_Low; //置高
TIM_OC1Init(TIM3, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);
/****************************** 通道2 ******************************/
/* PWM1 Mode configuration: Channel2 */
TIM_OCInitStructure.TIM_OutputState =TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = CCR2_Val;
TIM_OCInitStructure.TIM_OCPolarity =TIM_OCPolarity_High;
TIM_OC2Init(TIM3, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);
/******************************* 通道3 *********************************/
/* PWM1 Mode configuration: Channel3 */
TIM_OCInitStructure.TIM_OutputState =TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = CCR3_Val;
TIM_OC3Init(TIM3, &TIM_OCInitStructure);
TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Enable);
/****************************** 通道4 *********************************/
/* PWM1 Mode configuration: Channel4 */
TIM_OCInitStructure.TIM_OutputState =TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = CCR4_Val;
TIM_OC4Init(TIM3, &TIM_OCInitStructure);
TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM3,ENABLE); //
TIM_Cmd(TIM3,ENABLE); //使能计数
while (1)
{
CCR4_Val++;
if(CCR4_Val==1000)CCR4_Val=0;
TIM_SetCompare4(TIM3,CCR4_Val); //占空比调节
|