通道1 | TIM1_CH1 | PA8 |
通道1 | TIM1_CH1N | PB13 |
通道2 | TIM1_CH2 | PA9 |
通道2 | TIM1_CH2N | PB14 |
通道3 | TIM1_CH3 | PA10 |
通道3 | TIM1_CH3N | PB15 |
通道4 | TIM1_CH4 | PA11 |
通道1 | TIM8_CH1 | PC6 |
通道1 | TIM8_CH1N | PA7 |
通道2 | TIM8_CH2 | PC7 |
通道2 | TIM8_CH2N | PB0 |
通道3 | TIM8_CH3 | PC8 |
通道3 | TIM8_CH3N | PB1 |
通道4 | TIM8_CH4 | PC9 |
/*************************************************************
Function : PWM_GPIO_Init
Description: TIM1对应通道引脚初始化
Input : none
return : none
*************************************************************/
static void PWM_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE);
//TIM1的TIM_CH1/TIM_CH2/TIM_CH3/TIM1_CH4对应的引脚(PA9引脚与串口1的TX引脚复用,记得关闭串口1后在使用)
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;//设置成复用推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//TIM1的TIM1_CH1N/TIM1_CH2N/TIM1_CH3N对应的引脚
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
GPIO_Init(GPIOB, &GPIO_InitStructure);
//TIM8的TIM8_CH1/TIM8_CH2/TIM8_CH3/TIM8_CH4对应的引脚
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9;
GPIO_Init(GPIOC, &GPIO_InitStructure);
//TIM8的TIM8_CH1N对应的引脚
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//TIM8的TIM8_CH2N/TIM8_CH3N对应的引脚
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
u16 CCR1_Val = 2047;
u16 CCR2_Val = 1535;
u16 CCR3_Val = 1023;
u16 CCR4_Val = 511;
/*************************************************************
Function : PWM_TIM1_Init
Description: 定时器1的7路PWM初始化
Input : none
return : none
*************************************************************/
static void PWM_TIM1_Init(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
TIM_TimeBaseStructure.TIM_Prescaler = 0;//不预分频
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//增计数
TIM_TimeBaseStructure.TIM_Period = 4095;//定时器周期为4095
TIM_TimeBaseStructure.TIM_ClockDivision = 0;//时钟不分频
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;//TIM_OCMode_PWM1 PWM1/PWM2模式
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;//输出使能
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;//输出使能
TIM_OCInitStructure.TIM_Pulse = CCR1_Val;//设置通道1的跳变值
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;//有效电平为低电平
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;//有效电平为高电平
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;//空闲时的状态为有效电平
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;//空闲时的状态为无效电平
TIM_OC1Init(TIM1, &TIM_OCInitStructure);//初始化TIM1的通道1
TIM_OCInitStructure.TIM_Pulse = CCR2_Val;//设置通道2的跳变值
TIM_OC2Init(TIM1, &TIM_OCInitStructure);//初始化TIM1的通道2
TIM_OCInitStructure.TIM_Pulse = CCR3_Val;//设置通道3的跳变值
TIM_OC3Init(TIM1, &TIM_OCInitStructure);//初始化TIM1的通道3
TIM_OCInitStructure.TIM_Pulse = CCR4_Val;//初始化通道4的跳变值
TIM_OC4Init(TIM1, &TIM_OCInitStructure);//初始化TIM1的通道4
TIM_Cmd(TIM1, ENABLE);//使能定时器1
TIM_CtrlPWMOutputs(TIM1, ENABLE);//打开定时器主输出(一定要有这句话)
}
/*************************************************************
Function : PWM_TIM8_Init
Description: 定时器8的7路PWM初始化
Input : none
return : none
*************************************************************/
static void PWM_TIM8_Init(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM8, ENABLE);
TIM_TimeBaseStructure.TIM_Prescaler = 0;//不预分频
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//增计数
TIM_TimeBaseStructure.TIM_Period = 4095;//定时器周期为4095
TIM_TimeBaseStructure.TIM_ClockDivision = 0;//时钟不分频
TIM_TimeBaseInit(TIM8, &TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;//TIM_OCMode_PWM1 PWM1/PWM2模式
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //输出使能
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;//输出使能
TIM_OCInitStructure.TIM_Pulse = CCR1_Val;//设置通道1的跳变值
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;//有效电平为低电平
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;//有效电平为高电平
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;//空闲时的状态为有效电平
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;//空闲时的状态为无效电平
TIM_OC1Init(TIM8, &TIM_OCInitStructure);//初始化TIM8的通道1
TIM_OCInitStructure.TIM_Pulse = CCR2_Val;//设置通道2的跳变值
TIM_OC2Init(TIM8, &TIM_OCInitStructure);//初始化TIM8的通道2
TIM_OCInitStructure.TIM_Pulse = CCR3_Val;//设置通道3的跳变值
TIM_OC3Init(TIM8, &TIM_OCInitStructure);//初始化TIM8的通道3
TIM_OCInitStructure.TIM_Pulse = CCR4_Val;//初始化通道4的跳变值
TIM_OC4Init(TIM8, &TIM_OCInitStructure);//初始化TIM8的通道4
TIM_Cmd(TIM8, ENABLE);//使能定时器1
TIM_CtrlPWMOutputs(TIM8, ENABLE);//打开定时器主输出(一定要有这句话)
}
/*************************************************************
Function : PWM_Init
Description: PWM初始化
Input : none
return : none
*************************************************************/
void PWM_Init(void)
{
PWM_GPIO_Init();
PWM_TIM1_Init();
PWM_TIM8_Init();
}
#ifndef __7PWMOUTPUT_H__
#define __7PWMOUTPUT_H__
#include "stm32f10x.h"
void PWM_Init(void);
#endif
/*************************************************************
Function : main
Description: main入口
Input : none
return : none
*************************************************************/
int main(void)
{
BSP_Init();
PWM_Init();
PRINTF("\nmain() is running!\r\n");
while(1)
{
LED1_Toggle();
Delay_ms(1000);
}
}
上一篇:STM32 定时器输出比较时间模式
下一篇:STM32 GPIO的8中种配置模式
推荐阅读最新更新时间:2024-03-16 15:14
设计资源 培训 开发板 精华推荐
- 有奖直播:联咏安防监控方案及未来技术方向 报名开始啦!
- 【评论有礼】大话CC2650,从资料到例程、从应用到生态全方位讲解!
- Mouser 福利活动:邀请新用户下单,各得百元奖励!
- 直播已结束【借助 TI DLP 技术,开始您的下一代4K显示应用设计】
- 有奖直播:TI最新C2000实时控制器,在功率变换应用中实现高性能的成本优化型设计
- ADI公司再生能源—能源储存解决方案
- TI 工业月,灵感不设限!21天习惯养成记!打卡赢好礼啦!
- 有奖直播|TI 新一代Sitara™ AM62处理器革新人机交互——产品介绍和相关资源
- 【MPS有奖分享】便携式多功能电工台的核“芯”功能测试与结构设计
- 下载送好礼|人工智能物联网时代即将来临,您准备好了吗?