单片机输出pwm波在项目中是恨常用的一个功能,今天就总结一下用STM32F103C8T6单片机输出16路 pwm波。
使用keil5软件,用标准库函数来实现。
首先看定时器初始化
void TIM1_PWM_Init(u16 arr, u16 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
TIM_OCInitTypeDef TIM_OCInitSturcture;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_TIM1, ENABLE);
//初始化IO口
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);
//设置 TIM ARR PSC
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_TimeBaseInitStructure.TIM_RepetitionCounter = 0x00;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseInitStructure);
//TIM1_CH1 PWM1 模式
TIM_OCInitSturcture.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitSturcture.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitSturcture.TIM_OCPolarity = TIM_OCPolarity_High; //输出高电平有效
TIM_OC1Init(TIM1, &TIM_OCInitSturcture);
//TIM1_CH2 PWM1 模式
TIM_OCInitSturcture.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitSturcture.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitSturcture.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC2Init(TIM1, &TIM_OCInitSturcture);
//TIM1_CH3 PWM2 模式
TIM_OCInitSturcture.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitSturcture.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitSturcture.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC3Init(TIM1, &TIM_OCInitSturcture);
//TIM1_CH4 PWM2 模式
TIM_OCInitSturcture.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitSturcture.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitSturcture.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC4Init(TIM1, &TIM_OCInitSturcture);
TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable); //TIM1_CH1 使能预装载寄存器
TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Enable); //TIM1_CH2 使能预装载寄存器
TIM_OC3PreloadConfig(TIM1, TIM_OCPreload_Enable); //TIM1_CH3 使能预装载寄存器
TIM_OC4PreloadConfig(TIM1, TIM_OCPreload_Enable); //TIM1_CH4 使能预装载寄存器
TIM_Cmd(TIM1, ENABLE); //使能计数器
TIM_CtrlPWMOutputs(TIM1, ENABLE); //主输出使能
}
初始化定时1的4个通道,对应IO口为 PA8,PA9,PA10,PA11,将这4个口都设置为PWM输出口。入口参数arr用来设置定时器的自动重装载值,psc用于设置时钟分频系数。时钟默认为72MHZ。
下来初始化定时器2
void TIM2_PWM_Init(u16 arr, u16 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); //使能定时器2时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA , ENABLE); //使能GPIO时钟
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//初始化TIM2
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(TIM2, &TIM_TimeBaseInitStructure);
//初始化TIM2_CH1 PWM 模式
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC1Init(TIM2, &TIM_OCInitStructure);
//初始化TIM2_CH2 PWM 模式
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC2Init(TIM2, &TIM_OCInitStructure);
//初始化TIM2_CH3 PWM 模式
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC3Init(TIM2, &TIM_OCInitStructure);
//初始化TIM2_CH4 PWM 模式
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC4Init(TIM2, &TIM_OCInitStructure);
//使能TIM2在CCR上的预装载寄存器
TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Enable);
TIM_OC2PreloadConfig(TIM2, TIM_OCPreload_Enable);
TIM_OC3PreloadConfig(TIM2, TIM_OCPreload_Enable);
TIM_OC4PreloadConfig(TIM2, TIM_OCPreload_Enable);
//使能TIM2
TIM_Cmd(TIM2, ENABLE);
}
这里要注意的是,定时器1是高级定时器,定时器2、3、4是通用定时器。只有高级定时器有重复计数寄存器,而通过定时器没有重复计数寄存器。定时器2初始化时,比定时器1少一行代码,这里要注意一下。
TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0x00;
重复计数寄存器主要作用如下:
其余设置方法和定时器1基本一样,定时器2的4个通道对应的IO口为PA0,PA1,PA2,PA3.将这四个口设置为PWM输出模式。
入口参数arr用来设置定时器的自动重装载值,psc用于设置时钟分频系数。时钟默认为72MHZ。
下来设置定时器3
void TIM3_PWM_Init(u16 arr, u16 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //使能定时器3时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE); //使能GPIO时钟
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_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
//初始化TIM3
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);
//初始化TIM3_CH1 PWM 模式
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC1Init(TIM3, &TIM_OCInitStructure);
//初始化TIM3_CH2 PWM 模式
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC2Init(TIM3, &TIM_OCInitStructure);
//初始化TIM3_CH3 PWM 模式
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC3Init(TIM3, &TIM_OCInitStructure);
//初始化TIM3_CH4 PWM 模式
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC4Init(TIM3, &TIM_OCInitStructure);
//使能TIM3在CCR上的预装载寄存器
TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);
TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);
TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Enable);
TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Enable);
//使能TIM3
TIM_Cmd(TIM3, ENABLE);
}
设置方法和定时器2一样,定时器3的4个通道对应的IO口为PA6,PA7,PB0,PB1。将这四个口设置为PWM输出模式。
入口参数arr用来设置定时器的自动重装载值,psc用于设置时钟分频系数。时钟默认为72MHZ。
最后设置定时器4
void TIM4_PWM_Init(u16 arr, u16 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); //使能定时器4时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB , ENABLE); //使能GPIO时钟
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
//初始化TIM4
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(TIM4, &TIM_TimeBaseInitStructure);
//初始化TIM4_CH1 PWM 模式
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
上一篇:STM32F103单片机输出相位可调PWM波
下一篇:STM103单片机串口输出函数printf重映射
推荐阅读最新更新时间:2024-11-10 11:11
设计资源 培训 开发板 精华推荐
- SG117A 1.5A 三端可调式遥感稳压器典型应用
- 【CW32】无刷电机开发板
- 用于微处理器复位电路的 NCP301HSN30T1 3V 电压检测器的典型应用
- miniFOC控制板STMF405RGT6
- 使用 ROHM Semiconductor 的 BA25BC0FP-E2 的参考设计
- DEMO9S08RG60E,MC9S08RG60 S08RG、RC、RD 或 RE 系列微控制器的演示板
- LTC6262HDC 230 uA 电源电流运算放大器的典型应用
- 使用 Diodes Incorporated 的 PT8A 3517D 的参考设计
- 具有关断功能的 LT1129IQ-3.3 5V 低压差稳压器电源的典型应用电路
- AM1G-0509SZ 9V 1 瓦 DC-DC 转换器的典型应用