如何让stm32产生多路输出 PWM 最好有代码

1:用一个定时器产生两路PWM输出2:用两个定时器各产生一路PWM输出... 1:用一个定时器产生两路PWM输出
2:用两个定时器各产生一路PWM输出
展开
 我来答
匿名用户
2015-10-08
展开全部
我自己用的24路,贴不上来,就贴前面两个,其他的照着写就行。

#include "pwm.h"

void Timer1PwmInit(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;

// RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE); //使能GPIO外设和AFIO复用功能模块时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);

TIM_TimeBaseStructure.TIM_Period = PWM_PERIOD;//
TIM_TimeBaseStructure.TIM_Prescaler = PRECALERS;//
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0x0000;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);

TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
TIM_OCInitStructure.TIM_OutputState= TIM_OutputState_Disable;//只输出互补
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
TIM_OCInitStructure.TIM_Pulse = 0;//CCR
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM_OCInitStructure.TIM_OCNPolarity= TIM_OCNPolarity_Low;
TIM_OCInitStructure.TIM_OCIdleState= TIM_OCIdleState_Set;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;

TIM_OC3Init(TIM1, &TIM_OCInitStructure);//CH3N PB15
TIM_OC3PreloadConfig(TIM1, TIM_OCPreload_Enable);

TIM_ARRPreloadConfig(TIM1, DISABLE); //预装载寄存器的内容被立即传送到影子寄存器
// TIM_ARRPreloadConfig(TIM1, ENABLE);
TIM_Cmd(TIM1, ENABLE);
TIM_CtrlPWMOutputs(TIM1, ENABLE);
}

//void SetLED5_W(u16 val)
//{
// TIM1->CCR1 = val;
//}
//void SetLED5_B(u16 val)
//{
// TIM1->CCR2 = val;
//}
//void SetLED5_G(u16 val)
//{
// TIM1->CCR3 = val;
//}
//void SetLED5_R(u16 val)
//{
// TIM1->CCR4 = val;
//}

void SetPwm4(u16 val)
{
TIM1->CCR3 = val;
}

//void Timer2PwmInit(void)
//{
// GPIO_InitTypeDef GPIO_InitStructure;
// TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
// TIM_OCInitTypeDef TIM_OCInitStructure;
//
// RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
// RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB |RCC_APB2Periph_AFIO, ENABLE); //使能GPIO外设和AFIO复用功能模块时钟
// AFIO->MAPR&=0XF8FFFFFF;
// AFIO->MAPR|=0X04000000;
//
// GPIO_PinRemapConfig(GPIO_FullRemap_TIM2, ENABLE); //Timer2完全重映射
//
// GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;
// GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
// GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
// GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIO
//
// GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3|GPIO_Pin_10|GPIO_Pin_11;
// GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
// GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
// GPIO_Init(GPIOB, &GPIO_InitStructure);//初始化GPIO
//
// //初始化TIM2
// TIM_TimeBaseStructure.TIM_Period = PWM_PERIOD;
// TIM_TimeBaseStructure.TIM_Prescaler =PRECALERS;
// TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割:TDTS = Tck_tim
// TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
// TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
//
// //初始化TIM2 Channel PWM模式
// TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
// TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
// TIM_OCInitStructure.TIM_Pulse = 0;//TIM->CCR = 0
// TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
//
//
// TIM_OC1Init(TIM2, &TIM_OCInitStructure);
// TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Enable); //使能TIM2在CCR1上的预装载寄存器
//
// TIM_OC2Init(TIM2, &TIM_OCInitStructure);
// TIM_OC2PreloadConfig(TIM2, TIM_OCPreload_Enable);
//
// TIM_OC3Init(TIM2, &TIM_OCInitStructure);
// TIM_OC3PreloadConfig(TIM2, TIM_OCPreload_Enable);
//
// TIM_OC4Init(TIM2, &TIM_OCInitStructure);
// TIM_OC4PreloadConfig(TIM2, TIM_OCPreload_Enable);
//
// TIM_ARRPreloadConfig(TIM2, ENABLE);
// TIM_Cmd(TIM2, ENABLE); //使能TIM2
//}

//void SetLED2_R(u16 val)
//{
// TIM2->CCR4 = val;
//}
//void SetLED2_G(u16 val)
//{
// TIM2->CCR3 = val;
//}
//void SetLED2_B(u16 val)
//{
// TIM2->CCR2 = val;
//}
//void SetLED2_W(u16 val)
//{
// TIM2->CCR1 = val;
//}
huzi2099
2015-09-28 · TA获得超过1172个赞
知道小有建树答主
回答量:1887
采纳率:82%
帮助的人:638万
展开全部
第一个好说,库里有例子
2个什么意思不理解.
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式