本帖最后由 RISCVLAR 于 2021-2-22 15:18 编辑
CH32V103应用教程——PWM输出-8路
在智能小车应用当中,我们通常需要使用PWM实现对电机的控制。本章教程主要使用TIM1和TIM2的共8个通道产生8路PWM输出。
1、TIM简介及相关函数介绍 关于TIM以及PWM的简介前面章节已经介绍,在此不再赘述。
2、硬件设计 本章教程使用TIM1和TIM2的共8个通道产生8路PWM输出,需要用到8个GPIO引脚,具体对应引脚见程序注释。
3、软件设计 本章教程主要进行简单的8路PWM输出,具体程序如下: pwm.h文件 #ifndef __PWM_H
#define __PWM_H
#include "ch32v10x_conf.h"
void PWM_GPIO_Configuration(void);
void PWM_TIM_Configuration(void);
#endif
pwm.h文件主要进行函数的声明; pwm.c文件
#include "pwm.h"
//PWM输出通道对应GPIO引脚初始化配置
//此处使用TIM1和TIM2,对应通道分别为下:
//TIM2:CH1(PA0) CH2(PA1) CH3(PA2) CH4(PA3)
//TIM1:CH1(PA8) CH2(PA9) CH3(PA10) CH4(PA11)
void PWM_GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA , ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE); //打开管脚复用AFIO时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
//此处PA9引脚既作为USART_TX,又作为TIM1_CH2,需要进行复用操作
GPIO_PinRemapConfig(GPIO_FullRemap_TIM1,ENABLE); //更改指定管脚的映射。
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|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);
}
//定时器PWM输出配置
void PWM_TIM_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_BaseInitStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_BaseInitStructure.TIM_Period = 1000-1;
TIM_BaseInitStructure.TIM_Prescaler = 72-1;
TIM_BaseInitStructure.TIM_ClockDivision = 0;
TIM_BaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_BaseInitStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM1, &TIM_BaseInitStructure);
TIM_ARRPreloadConfig(TIM1, ENABLE);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_Pulse = 200;
TIM_OC1Init(TIM1, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);
TIM_OCInitStructure.TIM_Pulse = 400;
TIM_OC2Init(TIM1, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Enable);
TIM_OCInitStructure.TIM_Pulse = 600;
TIM_OC3Init(TIM1, &TIM_OCInitStructure);
TIM_OC3PreloadConfig(TIM1, TIM_OCPreload_Enable);
TIM_OCInitStructure.TIM_Pulse = 800;
TIM_OC4Init(TIM1, &TIM_OCInitStructure);
TIM_OC4PreloadConfig(TIM1, TIM_OCPreload_Enable);
TIM_BaseInitStructure.TIM_Period = 1000-1;
TIM_BaseInitStructure.TIM_Prescaler = 72-1;
TIM_BaseInitStructure.TIM_ClockDivision = 0;
TIM_BaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_BaseInitStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM2, &TIM_BaseInitStructure);
TIM_ARRPreloadConfig(TIM2, ENABLE);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_Pulse =100;
TIM_OC1Init(TIM2, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Enable);
TIM_OCInitStructure.TIM_Pulse =300;
TIM_OC2Init(TIM2, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM2, TIM_OCPreload_Enable);
TIM_OCInitStructure.TIM_Pulse =500;
TIM_OC3Init(TIM2, &TIM_OCInitStructure);
TIM_OC3PreloadConfig(TIM2, TIM_OCPreload_Enable);
TIM_OCInitStructure.TIM_Pulse =700;
TIM_OC4Init(TIM2, &TIM_OCInitStructure);
TIM_OC4PreloadConfig(TIM2, TIM_OCPreload_Enable);
TIM_Cmd(TIM1, ENABLE);
TIM_Cmd(TIM2, ENABLE);
TIM_CtrlPWMOutputs(TIM1, ENABLE);
TIM_CtrlPWMOutputs(TIM2, ENABLE);
}
pwm.c文件主要进行定时器的PWM输出配置,与应用教程第八章PWM输出类似,主要进行8个通道的配置,具体过程及讲解可参考第八章。此外,此处还需要注意GPIO引脚的复用。 main.c文件 int main(void)
{
Delay_Init();
//USART_Printf_Init(115200);
//printf("SystemClk:%d\r\n",SystemCoreClock);
PWM_GPIO_Configuration();
PWM_TIM_Configuration();
while(1)
{
//TIM_SetCompare1(TIM1,500); //可对占空比进行修改
}
}
main.c文件主要进行函数初始化。此外,若要对PWM输出占空比进行修改,可使用TIM_SetComparex函数(x为1、2、3、4)修改占空比。
4、下载验证 将编译好的程序下载到开发版并复位,PWM输出波形如下:
|