想用QEI调节PWM占空比
signed long j=0;
#include <stdbool.h>
#include <stdint.h>
#include "inc/hw_ints.h"
#include "inc/hw_memmap.h"
#include "driverlib/gpio.h"
#include "driverlib/interrupt.h"
#include "driverlib/pin_map.h"
#include "driverlib/pwm.h"
#include "driverlib/sysctl.h"
#include "inc/hw_gpio.h"
#include "inc/hw_types.h"
#include "inc/hw_memmap.h"
#include "driverlib/qei.h"
//*******************************************************************
//PWM初始化
int
PWMInit (void)
{
//
// PWM不分频
//
SysCtlPWMClockSet(SYSCTL_PWMDIV_1);
//使能M1PWM用到的PF,和RESET对应的PB
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);//使能B端口,PB2、PB3分别对应RESET_AB,RESET_CD
GPIOPinTypeGPIOOutput(GPIO_PORTB_BASE, GPIO_PIN_2|GPIO_PIN_3);//配置2,3管脚为输出
//配置PF0,PF1分别为M1PWM4,M1PWM5
GPIOPinConfigure(GPIO_PF0_M1PWM4);
GPIOPinConfigure(GPIO_PF1_M1PWM5);
// 配置PF0,PF1为PWM的输出管脚
GPIOPinTypePWM(GPIO_PORTF_BASE, GPIO_PIN_0|GPIO_PIN_1);
//
// Configure the PWM1 to count down without synchronization.
//配置GEN2递减计数模式不同步模式
PWMGenConfigure(PWM1_BASE, PWM_GEN_2,
PWM_GEN_MODE_DOWN | PWM_GEN_MODE_NO_SYNC);
//设置PWM周期
PWMGenPeriodSet(PWM1_BASE, PWM_GEN_2,100);
//设置PWM占空比
PWMPulseWidthSet(PWM1_BASE, PWM_OUT_4, 1);
PWMPulseWidthSet(PWM1_BASE, PWM_OUT_5, 1);
//使能PWM输出状态
PWMOutputState(PWM1_BASE, PWM_OUT_4_BIT| PWM_OUT_5_BIT, true);
GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_2, 0x1<<2);//H2管脚置高 对应RESETAB
GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_3, 0x1<<3);//H3管脚置高 对应RESETCD
//
// 使能PWM发生器
PWMGenEnable(PWM1_BASE, PWM_GEN_2);
return(0);
}
//********************************************************************
//QEI1初始化
int
QEIInit (void)
{
GPIOPinTypeQEI(GPIO_PORTC_BASE,GPIO_PIN_5|GPIO_PIN_6);//配置PC5,PC6为PhA1,PhB1
QEIConfigure(QEI1_BASE,(QEI_CONFIG_CAPTURE_A_B|QEI_CONFIG_NO_RESET|
QEI_CONFIG_QUADRATURE|QEI_CONFIG_NO_SWAP),100);//使用A,B通道共共4个边沿计算位置
QEIEnable(QEI1_BASE);//使能正交编码器QEI1
return(0);
}
//**************************************************************************
//GPIOC中断处理函数
int
GPIOCIntHandler(void)
{
signed long Direction;
GPIOIntClear(GPIO_PORTC_BASE,GPIO_INT_PIN_5);//清除中断状态
GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_2, 0x1<<0);//B2管脚置0 对应RESETAB
GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_3, 0x1<<0);//B3管脚置0 对应RESETCD
PWMGenDisable(PWM1_BASE, PWM_GEN_2 );//禁能PWM发生器
//禁能PWM输出状态
PWMOutputState(PWM1_BASE, PWM_OUT_4_BIT| PWM_OUT_5_BIT, false);
j=QEIPositionGet(QEI1_BASE);
if (QEIDirectionGet(QEI1_BASE))
{
j++;
}
else
{
j--;
}
if (j<0)
{
j=0;
}
else if (0<j<100)
{
j=j;
}
else
{
j=100;
}
SysCtlDelay(100);//延时
PWMPulseWidthSet(PWM1_BASE, PWM_OUT_4, j);
PWMPulseWidthSet(PWM1_BASE, PWM_OUT_5, j);
//使能PWM输出状态
PWMOutputState(PWM1_BASE, PWM_OUT_4_BIT| PWM_OUT_5_BIT, true);
GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_2, 0x1<<2);//H2管脚置高 对应RESETAB
GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_3, 0x1<<3);//H3管脚置高 对应RESETCD
//
// 使能PWM发生器
PWMGenEnable(PWM1_BASE, PWM_GEN_2);
return(0);
}
//**************************************************************************
//主函数
int main (void)
{
SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN |
SYSCTL_XTAL_16MHZ); // 设置时钟频率为16MHZ
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);//使能GPIOB
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOC);//使能GPIOC
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);//使能GPIOF
SysCtlPeripheralEnable(SYSCTL_PERIPH_PWM1); //使能M1PWM外设
SysCtlPeripheralEnable(SYSCTL_PERIPH_QEI1);//使能QEI外设
PWMInit ();
QEIInit ();
QEIPositionSet(QEI1_BASE,50);//更新参考点位置为50
j=QEIPositionGet(QEI1_BASE);
GPIOIntTypeSet(GPIO_PORTC_BASE,GPIO_PIN_5,GPIO_RISING_EDGE);//设置GPIO PC5为上升边沿中断
GPIOIntEnable(GPIO_PORTC_BASE,GPIO_INT_PIN_5);//设置GPIO PC5所在管脚中断
IntEnable(INT_GPIOC);//使能GPIOC端口中断
IntMasterEnable();//使能处理器中断
while(1)
{
}
}
每次选择正交编码,PWM占空比只加不减
一周热门 更多>