PWM函数

PWM相关底层API函数使用说明。

头文件的引入

#include  "pwm.h"

PWM_IO初始化(常用定时器参数初始化)

//该函数用于PWM_IO初始化(常用定时器参数初始化)
//TIM8,TIM2,TIM4,TIM5为板子上16个口的定时器
//每四个io口属于一个定时器,每个io口对应一个定时器的一条通道
void TIM8_Init(void);
void TIM2_Init(void);
void TIM4_Init(void);
void TIM5_Init(void);
//该函数用于PWM_MOTOR初始化(常用定时器参数初始化)
//TIM1为板子上4个PWM电调口的定时器
void TIM1_Init(void);

PWM_IO初始化(自定义TIM定时器初始化)

//该函数用于PWM_IO初始化(自定义TIM定时器初始化)
//TIM8,TIM2,TIM4,TIM5为板子上16个口的定时器
//该初始化为用户自定义PWM口的初始化
//PWM_Period为自动重载值,PWM_Prescaler为时钟预分频值,PWM_Pluse为初始化比较值
//PWM_CounterMode为PWM计数模式,PWM_OCMode为PWM通道的模式,PWM_OCPolarity为输出极性
//PWM_CounterMode可用值:PWM_CounterMode_Up,PWM_CounterMode_Down
//PWM_OCMode可用值:PWMO_OCMode_PWM1PWM_OCMode_PWM2
//PWM_OCPolarity可用值:PWM_OCPolarity_High,PWM_OCPolarity_Low,
void TIM8_Config(int PWM_Period,int PWM_Prescaler,int PWM_Pluse,PWM_CounterMode_t PWM_CounterMode,PWM_OCMode_t PWM_OCMode,PWM_OCPolarity_t PWM_OCPolarity);
void TIM2_Config(int PWM_Period,int PWM_Prescaler,int PWM_Pluse,PWM_CounterMode_t PWM_CounterMode,PWM_OCMode_t PWM_OCMode,PWM_OCPolarity_t PWM_OCPolarity);
void TIM4_Config(int PWM_Period,int PWM_Prescaler,int PWM_Pluse,PWM_CounterMode_t PWM_CounterMode,PWM_OCMode_t PWM_OCMode,PWM_OCPolarity_t PWM_OCPolarity);
void TIM5_Config(int PWM_Period,int PWM_Prescaler,int PWM_Pluse,PWM_CounterMode_t PWM_CounterMode,PWM_OCMode_t PWM_OCMode,PWM_OCPolarity_t PWM_OCPolarity);
//该函数用于PWM_MOTOR初始化(自定义TIM定时器初始化)
//TIM1为板子上PWM电调4个口的定时器
//该初始化为PWM电调4个口的定时器的初始化
//PWM_Period为自动重载值,PWM_Prescaler为时钟预分频值,PWM_Pluse为初始化比较值
//PWM_CounterMode为PWM计数模式,PWM_OCMode为PWM通道的模式,PWM_OCPolarity为输出极性
//PWM_CounterMode可用值:PWM_CounterMode_Up,PWM_CounterMode_Down
//PWM_OCMode可用值:PWMO_OCMode_PWM1PWM_OCMode_PWM2
//PWM_OCPolarity可用值:PWM_OCPolarity_High,PWM_OCPolarity_Low,
void TIM1_Config(int PWM_Period,int PWM_Prescaler,int PWM_Pluse,\PWM_CounterMode_t PWM_CounterMode,PWM_OCMode_t PWM_OCMode,PWM_OCPolarity_t PWM_OCPolarity);

PWM_IO输出PWM控制(初始化IO口为PWM模式)

//该函数用于PWM_IO输出PWM控制(初始化IO口为PWM模式)
//PWM_Channel对应板子上16个IO口通道,psc为频率设置,pwm为脉宽数据
//PWM_Channel可用值:PD12,PD13,PD14,PD15,PH10,PH11,PH12,PI0,PA0,PA1,PA2,PA3,PI5,PI6,PI7,PI2
void Pwm_Write(PWM_Channel_t PWM_Channel,uint16_t psc,uint16_t pwm);

PWM串口回调

//PWM串口通讯协议回调函数,参数对应串口通讯协议参数
void PWM_Serial_Callback(char *type, int channel, int width, double rate){}

示例

//控制IO输出指定PWM波
#include "pwm.h"//引入头文件 "pwm.h"
void user1_main(void)
{
	//初始化所有PWM口,设置为PWM向下计数模式
	PWM_Io_Init(ALL_PWM_Channel,Mode_PWM_Down);
	while(1)
	{
		Pwm_Write(PI2,50,2000);//设置PI2口pwm波输出,输出频率为50hz,脉宽为2000ms的pwm波
		delay_ms(1);
	}
}