【TIVA C Launchpad 学习笔记三】利用PWM模块产生两路方波
[复制链接]
/*******************************************
开发坏境:CCSv5
程序功能:利用PWM模块产生两路方波
程序说明:使用PWM模块0,PWM发生器0,输出为PWM0和PWM1管脚,分别为PB6,PB7
编程者:Linchpin
********************************************/
#include
#include
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "driverlib/sysctl.h"
#include "driverlib/rom.h"
#include "driverlib/gpio.h"
#include "driverlib/pwm.h"
#include "driverlib/fpu.h"
#include "driverlib/pin_map.h"
int main (void)
{
//使能FPU
FPUEnable();
FPULazyStackingEnable();
//设置系统时钟为80MHz
SysCtlClockSet(SYSCTL_SYSDIV_2_5 |SYSCTL_USE_PLL|SYSCTL_OSC_MAIN |SYSCTL_XTAL_16MHZ);
SysCtlPeripheralEnable(SYSCTL_PERIPH_PWM0);//使能PWM0模块
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);//使能PWM0和PWM1输出所在GPIO
GPIOPinTypePWM(GPIO_PORTB_BASE, GPIO_PIN_6);
GPIOPinTypePWM(GPIO_PORTB_BASE, GPIO_PIN_7);//配置PH0/PH1为PWM功能
GPIOPinConfigure(GPIO_PB6_M0PWM0); //#define GPIO_PB6_M0PWM0 0x00011804
GPIOPinConfigure(GPIO_PB7_M0PWM1); //#define GPIO_PB7_M0PWM1 0x00011C04
SysCtlPWMClockSet(SYSCTL_PWMDIV_1); // PWM时钟配置:不分频
//配置PWM发生器0:加减计数,不同步
PWMGenConfigure(PWM0_BASE,PWM_GEN_0,PWM_GEN_MODE_UP_DOWN| PWM_GEN_MODE_NO_SYNC);
//设置PWM发生器1的频率,时钟频率/PWM分频数/n,80M/1/800=100KHZ
PWMGenPeriodSet(PWM0_BASE, PWM_GEN_0, 800);
//设置PWM0/PWM1输出的脉冲宽度
PWMPulseWidthSet(PWM0_BASE, PWM_OUT_0, 400);//50%占空比
PWMPulseWidthSet(PWM0_BASE, PWM_OUT_1, 200);//25%占空比
//使能PWM0和PWM1的输出
PWMOutputState(PWM0_BASE, (PWM_OUT_0_BIT |PWM_OUT_1_BIT), true);
//使能PWM发生器
PWMGenEnable(PWM0_BASE, PWM_GEN_0);
while(1);
}
|