2019-03-24 09:08发布
hbwangliang 发表于 2014-8-13 08:59 你在网上找到了两路的pwm输出,就是不知道怎么从其他引脚输出哈.................
最多设置5个标签!
是的~
#include <stdbool.h>
#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"
#define GPIO_PB6_M0PWM0 0x00011804
#define GPIO_PB7_M0PWM1 0x00011C04
#define GPIO_PB4_M0PWM2 0x00011004
#define GPIO_PB5_M0PWM3 0x00011404
void PWM_init(void)
{
//PWM时钟配置:64分频
SysCtlPWMClockSet(SYSCTL_PWMDIV_64);
//使能PWM0模块
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
//使能PWM0模块
SysCtlPeripheralEnable(SYSCTL_PERIPH_PWM0);
//GPIO引脚配置
GPIOPinConfigure(GPIO_PB7_M0PWM1);
GPIOPinConfigure(GPIO_PB6_M0PWM0);
GPIOPinConfigure(GPIO_PB5_M0PWM3);
GPIOPinConfigure(GPIO_PB4_M0PWM2);
//配置引脚为PWM功能
GPIOPinTypePWM(GPIO_PORTB_BASE, GPIO_PIN_7);
GPIOPinTypePWM(GPIO_PORTB_BASE, GPIO_PIN_6);
GPIOPinTypePWM(GPIO_PORTB_BASE, GPIO_PIN_5);
GPIOPinTypePWM(GPIO_PORTB_BASE, GPIO_PIN_4);
//配置PWM发生器0和发生器1:加减计数不分频
PWMGenConfigure(PWM0_BASE, PWM_GEN_0,PWM_GEN_MODE_DOWN | PWM_GEN_MODE_NO_SYNC);
PWMGenConfigure(PWM0_BASE, PWM_GEN_1,PWM_GEN_MODE_DOWN | PWM_GEN_MODE_NO_SYNC);
//设置PWM发生器1的频率,时钟频率/pwm分频数/n,80M/64/3125=400hz
PWMGenPeriodSet(PWM0_BASE, PWM_GEN_0, 3125);
PWMGenPeriodSet(PWM0_BASE, PWM_GEN_1, 3125);
//设置PWM0/PWM1输出的脉冲宽度
PWMPulseWidthSet(PWM0_BASE, PWM_OUT_0, 1200);
PWMPulseWidthSet(PWM0_BASE, PWM_OUT_1, 1200);
PWMPulseWidthSet(PWM0_BASE, PWM_OUT_2, 1200);
PWMPulseWidthSet(PWM0_BASE, PWM_OUT_3, 1200);
//使能PWM发生器
PWMGenEnable(PWM0_BASE, PWM_GEN_0);
PWMGenEnable(PWM0_BASE, PWM_GEN_1);
//使能PWM0和PWM1的输出
PWMOutputState(PWM0_BASE, (PWM_OUT_0_BIT), true);
PWMOutputState(PWM0_BASE, (PWM_OUT_1_BIT), true);
PWMOutputState(PWM0_BASE, (PWM_OUT_2_BIT), true);
PWMOutputState(PWM0_BASE, (PWM_OUT_3_BIT), true);
}
void GPIOPin_init(void)
{
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF); //使能启动GPIOF
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD); //使能启动GPIOD
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA); //使能启动GPIOA
GPIOPinTypeGPIOInput(GPIO_PORTD_BASE,GPIO_PIN_6|GPIO_PIN_2); //设置GPIOD_PIN2、6 为输入
GPIOPinTypeGPIOInput(GPIO_PORTA_BASE,0x3C); //设置GPIOA_PIN2-7 为输入
GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE,GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4);
GPIODirModeSet(GPIO_PORTD_BASE,GPIO_PIN_6|GPIO_PIN_2,GPIO_DIR_MODE_IN);
GPIODirModeSet(GPIO_PORTA_BASE,0x3C,GPIO_DIR_MODE_IN);
GPIOPadConfigSet(GPIO_PORTD_BASE,GPIO_PIN_6|GPIO_PIN_2,GPIO_STRENGTH_2MA,GPIO_PIN_TYPE_STD_WPU);//上拉
GPIOPadConfigSet(GPIO_PORTA_BASE,0x3C,GPIO_STRENGTH_2MA,GPIO_PIN_TYPE_STD_WPU);
}
void StartElCo(void)
{
uint32_t h0=1250,h1=1250,h2=1250,h3=1250,i;
SysCtlDelay(48000000);//2s
for(i=0;i<4;i++)
{
SysCtlDelay(2400000); //100ms
if(h0<1643)
{
h0+=110;
h1+=110;
h2+=110;
h3+=110;
}
if(h0>=1643)
{
h0=1643;
h1=1643;
h2=1643;
h3=1643;
}
PWMPulseWidthSet(PWM0_BASE, PWM_OUT_0, h0);
PWMPulseWidthSet(PWM0_BASE, PWM_OUT_1, h1);
PWMPulseWidthSet(PWM0_BASE, PWM_OUT_2, h2);
PWMPulseWidthSet(PWM0_BASE, PWM_OUT_3, h3);
}
// SysCtlDelay(24000000); //1s
}
int main(void)
{
//系统和时钟设置
SysCtlClockSet(SYSCTL_SYSDIV_2_5|SYSCTL_USE_PLL|SYSCTL_XTAL_16MHZ|SYSCTL_OSC_MAIN);
PWM_init(); //PWM中断
StartElCo(); //启动电调
while(1);
}
一周热门 更多>