pwm_init.c

来自「基于Cortex-M3的全自动焊接机」· C语言 代码 · 共 57 行

C
57
字号


#include "hw_types.h"
#include "hw_memmap.h"
#include "gpio.h"
#include  <hw_sysctl.h>
#include  <hw_gpio.h>
#include  <sysctl.h>
#include  "pwm.h"
#include  "pwm_init.h"

#define  SysCtlPeriEnable       SysCtlPeripheralEnable
#define  SysCtlPeriDisable      SysCtlPeripheralDisable
#define  GPIOPinTypeIn          GPIOPinTypeGPIOInput
#define  GPIOPinTypeOut         GPIOPinTypeGPIOOutput

extern unsigned int ulPeriod;


void pwm_init(void)
{
    SysCtlPWMClockSet(SYSCTL_PWMDIV_1);//PWM clock is processor clock /1

    SysCtlPeripheralEnable(SYSCTL_PERIPH_PWM);
    SysCtlPeripheralEnable(motor1_use_pwm_group);
    
    SysCtlPeripheralEnable(motor2_use_pwm_group);
    
    SysCtlPeripheralEnable(motor3_use_pwm_group);

    GPIOPinTypePWM(motor1_base, motor1_pwm_forward);
    GPIOPinTypePWM(motor1_base, motor1_pwm_reverse);
    
    GPIOPinTypePWM(motor2_base, motor2_pwm_forward);
    GPIOPinTypePWM(motor2_base, motor2_pwm_reverse);
    
    GPIOPinTypePWM(motor3_base, motor3_pwm_forward);
    GPIOPinTypePWM(motor3_base, motor3_pwm_reverse);
    
    ulPeriod = SysCtlClockGet()/ 1000;
    //ulPeriod = 2 0000;
    PWMGenConfigure(PWM_BASE, PWM_GEN_0,
                    PWM_GEN_MODE_UP_DOWN | PWM_GEN_MODE_NO_SYNC);
    PWMGenPeriodSet(PWM_BASE, PWM_GEN_0, ulPeriod);
    
    PWMGenConfigure(PWM_BASE, PWM_GEN_1,
                    PWM_GEN_MODE_UP_DOWN | PWM_GEN_MODE_NO_SYNC);
    PWMGenPeriodSet(PWM_BASE, PWM_GEN_1, ulPeriod);
    
    PWMGenConfigure(PWM_BASE, PWM_GEN_2,
                    PWM_GEN_MODE_UP_DOWN | PWM_GEN_MODE_NO_SYNC);
    PWMGenPeriodSet(PWM_BASE, PWM_GEN_2, ulPeriod);
    
    PWMGenEnable(PWM_BASE, PWM_GEN_0);
    PWMGenEnable(PWM_BASE, PWM_GEN_1);
    PWMGenEnable(PWM_BASE, PWM_GEN_2);
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?