1.头文件与变量定义
#include "tiva_pwm.h" // PWM
2.初始化
// PWM 初始化,频率为1000,占空比为0
M1PWM7_init(, ); // 具体函数内容
void M1PWM7_init(uint32_t freq, float duty)
{
//设置PWM时钟为系统时钟的1分频
SysCtlPWMClockSet(SYSCTL_PWMDIV_1);
SysCtlPeripheralEnable(SYSCTL_PERIPH_PWM1);
//配置引脚为PWM功能
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
GPIOPinConfigure(GPIO_PF3_M1PWM7);
GPIOPinTypePWM(GPIOF, Pin3); //M1PWM7
//配置 PWM1 Generator3·发生器
PWMGenConfigure(PWM1_BASE, PWM_GEN_3, PWM_GEN_MODE_UP_DOWN|PWM_GEN_MODE_NO_SYNC);
//配置 PWM1 Generator3 周期
PWMGenPeriodSet(PWM1_BASE, PWM_GEN_3, SysCtlClockGet()/freq - );
//配置 PWM1 Generator3 占空比
PWMPulseWidthSet(PWM1_BASE, PWM_OUT_7, PWMGenPeriodGet(PWM1_BASE, PWM_GEN_3)*duty - );
//使能 PWM1 输出
PWMOutputState(PWM1_BASE, PWM_OUT_7_BIT, true);
//使能 PWM1 发生器模块
PWMGenEnable(PWM1_BASE, PWM_GEN_3);
}
改变占空比
M1PWM7_set_duty(duty); // 具体函数内容
void M1PWM7_set_duty(float duty)
{
//配置 PWM1 Generator3 占空比
PWMPulseWidthSet(PWM1_BASE, PWM_OUT_7, PWMGenPeriodGet(PWM1_BASE, PWM_GEN_3)*duty - );
PWMOutputState(PWM1_BASE, PWM_OUT_7_BIT, true);
//使能 PWM1 ·发生器模块
PWMGenEnable(PWM1_BASE, PWM_GEN_3);
}
3.调用示例
*由于PF3对应的是扩展板上的白色LED灯,灭灯操作为
PWMOutputState(PWM1_BASE, PWM_OUT_7_BIT, false); // 灭灯
4.管脚