是TI的TIVA C LAUNCHPAD TM4C123GXL 经测试,PD0的波形和PB6一致,PD1的波形和PB7的一致。全部源码如下:
#include
#include
#include
#include
#include "hw_memmap.h"
#include "hw_nvic.h"
#include "hw_types.h"
#include "hw_sysctl.h"
#include "hw_pwm.h"
#include "gpio.h"
#include "interrupt.h"
#include "pin_map.h"
#include "sysctl.h"
#include "pwm.h"
void PwmInit(void);
void SetPwm(unsigned long channel_1,unsigned long channel_2,unsigned long channel_3,unsigned long channel_4);
int main()
[
SysCtlClockSet(SYSCTL_SYSDIV_2_5 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ);
IntPriorityGroupingSet(2);
PwmInit();
SetPwm(10,20,30,50);
while(1)
[]
]
void PwmInit(void)
[
// Enable the peripherals used by this program
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
SysCtlPeripheralEnable(SYSCTL_PERIPH_PWM0);
SysCtlPeripheralEnable(SYSCTL_PERIPH_PWM1);
GPIOPinTypePWM(GPIO_PORTB_BASE, GPIO_PIN_6 | GPIO_PIN_7);
GPIOPinTypePWM(GPIO_PORTF_BASE, GPIO_PIN_2 | GPIO_PIN_3);
GPIOPinConfigure(GPIO_PB6_M0PWM0);
GPIOPinConfigure(GPIO_PB7_M0PWM1);
GPIOPinConfigure(GPIO_PF2_M1PWM6);
GPIOPinConfigure(GPIO_PF3_M1PWM7);
//Configure PWM Clock to match system
SysCtlPWMClockSet(SYSCTL_PWMDIV_8);
PWMGenConfigure(PWM0_BASE,PWM_GEN_0, PWM_GEN_MODE_UP_DOWN| PWM_GEN_MODE_NO_SYNC);
PWMGenConfigure(PWM1_BASE,PWM_GEN_3, PWM_GEN_MODE_UP_DOWN | PWM_GEN_MODE_NO_SYNC);
PWMGenPeriodSet(PWM0_BASE,PWM_GEN_0, 25000);
PWMGenPeriodSet(PWM1_BASE,PWM_GEN_3, 25000);
PWMPulseWidthSet(PWM0_BASE,PWM_OUT_1,0);
PWMPulseWidthSet(PWM1_BASE,PWM_OUT_6,0);
PWMPulseWidthSet(PWM1_BASE,PWM_OUT_7,0);
// Enable the PWM generator
PWMGenEnable(PWM0_BASE, PWM_GEN_0);
PWMGenEnable(PWM1_BASE, PWM_GEN_3);
// Turn on the Output pins
PWMOutputState(PWM0_BASE, (PWM_OUT_0_BIT |PWM_OUT_1_BIT), true);
PWMOutputState(PWM1_BASE, (PWM_OUT_6_BIT |PWM_OUT_7_BIT), true);
]
void SetPwm(unsigned long channel_1,unsigned long channel_2,unsigned long channel_3,unsigned long channel_4)
[
PWMPulseWidthSet(PWM0_BASE, PWM_OUT_0, channel_1*250);
PWMPulseWidthSet(PWM0_BASE, PWM_OUT_1, channel_2*250);
PWMPulseWidthSet(PWM1_BASE, PWM_OUT_6, channel_3*250);
PWMPulseWidthSet(PWM1_BASE, PWM_OUT_7, channel_4*250);
]
|
|
2020-4-6 17:54:05
评论
举报
|
|
|