在看LM3S811用PWM控制舵机的时候,要产生一定占空比的PWM方波,经过自己的实验可以让舵机实现-90,-45,0,45,90的运转。但是自己看LM811的PWM的驱动程序源码的时候有几点要重视。在
void PWMGenConfigure(unsigned long ulBase, unsigned long ulGen,
unsigned long ulConfig)
{
//
// Check the arguments.
//
ASSERT(ulBase == PWM_BASE);
ASSERT(PWMGenValid(ulGen));
//
// Compute the generator's base address.
//
ulGen = PWM_GEN_BADDR(ulBase, ulGen);
//
// Change the global configuration of the generator.
//
HWREG(ulGen + PWM_O_X_CTL) = ((HWREG(ulGen + PWM_O_X_CTL) &
~(PWM_X_CTL_MODE | PWM_X_CTL_DEBUG |
PWM_X_CTL_LATCH | PWM_X_CTL_MINFLTPER |
PWM_X_CTL_FLTSRC | PWM_X_CTL_DBFALLUPD_M |
PWM_X_CTL_DBRISEUPD_M |
PWM_X_CTL_DBCTLUPD_M |
PWM_X_CTL_GENBUPD_M |
PWM_X_CTL_GENAUPD_M |
PWM_X_CTL_LOADUPD | PWM_X_CTL_CMPAUPD |
PWM_X_CTL_CMPBUPD)) | ulConfig);
//
// Set the individual PWM generator controls.
//
if(ulConfig & PWM_X_CTL_MODE)
{
//
// In up/down count mode, set the signal high on up count comparison
// and low on down count comparison (that is, center align the
// signals).
//
HWREG(ulGen + PWM_O_X_GENA) = (PWM_X_GENA_ACTCMPAU_ONE |
PWM_X_GENA_ACTCMPAD_ZERO);
HWREG(ulGen + PWM_O_X_GENB) = (PWM_X_GENB_ACTCMPBU_ONE |
PWM_X_GENB_ACTCMPBD_ZERO);
}
else
{
//
// In down count mode, set the signal high on load and low on count
// comparison (that is, left align the signals).
//
HWREG(ulGen + PWM_O_X_GENA) = (PWM_X_GENA_ACTLOAD_ONE |
PWM_X_GENA_ACTCMPAD_ZERO);
HWREG(ulGen + PWM_O_X_GENB) = (PWM_X_GENB_ACTLOAD_ONE |
PWM_X_GENB_ACTCMPBD_ZERO);
}
其中的两句源码 HWREG(ulGen + PWM_O_X_GENA) = (PWM_X_GENA_ACTCMPAU_ONE |
PWM_X_GENA_ACTCMPBU_ZERO);
在这里面有PWM_X_GENA_ACTCMPAU_ONE |PWM_X_GENA_ACTCMPBU_ZERO位或的操作,但是通过查hw_pwm.h我查到PWM_X_GENA_ACTCMPAU_ONE =0x00000030,PWM_X_GENA_ACTCMPBU_ZERO=0x00000020,但是位或以后不就是0x00000050;这样的话ActCmpAU=0x01,ActCmpAD=0x01,都是将输出信号反向。我认为它的驱动库有点问题。我把PWM_X_GENA_ACTCMPAD_ZERO<<2就可以实现一定的占空比得矩形波。我实验了可以。希望给大家的建议。
#include "hw_memmap.h"
#include "hw_types.h"
#include "sysctl.h"
#include "gpio.h"
#include "pwm.h"
#include "hw_ints.h"
#include "delay.h"
#define PH0_PWM2 GPIO_PIN_0
#define PH1_PWM3 GPIO_PIN_1
#define LED GPIO_PIN_6
unsigned int jiaodu[]={12000,16000,12000,8000};
unsigned int jiaodu1[]={12000,20000,12000,4000};
//unsigned int jiaodu1[]={148000,140000,148000,156000};
void H_T();
void Q_J();
/*********************************************************************************************************
** Function name: main()
** Descriptions: 主函数
** input parameters: NONE
** output parameters: NONE
** Returned value: NONE
*********************************************************************************************************/
int main (void)
{
SysCtlClockSet(SYSCTL_SYSDIV_1 | /* 配置6MHz外部晶振作为主时钟 */
SYSCTL_USE_OSC |
SYSCTL_OSC_MAIN |
SYSCTL_XTAL_8MHZ);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOH|SYSCTL_PERIPH_GPIOC); /* 使能PWM2和PWM3输出所在GPIO */
SysCtlPeripheralEnable(SYSCTL_PERIPH_PWM); /* 使能PWM模块 */
SysCtlPWMClockSet(SYSCTL_PWMDIV_1); /* PWM时钟配置:不分频 */
GPIOPinTypePWM(GPIO_PORTH_BASE, GPIO_PIN_0); /* PH0配置为PWM功能 */
GPIOPinTypePWM(GPIO_PORTH_BASE, GPIO_PIN_1); /* PH1配置为PWM功能 */
PWMGenConfigure(PWM_BASE, PWM_GEN_1, /* 配置PWM发生器1:加减计数 */
PWM_GEN_MODE_UP_DOWN | PWM_GEN_MODE_NO_SYNC);
PWMGenPeriodSet(PWM_BASE, PWM_GEN_1, 160000); /* 设置PWM发生器1的周期 */
PWMPulseWidthSet(PWM_BASE, PWM_OUT_2, jiaodu[0]); /* 设置PWM2输出的脉冲宽度 */
PWMPulseWidthSet(PWM_BASE, PWM_OUT_3, jiaodu[0]); /* 设置PWM3输出的脉冲宽度 */
// PWMSyncUpdate(PWM_BASE,PWM_GEN_1_BIT);
PWMOutputState(PWM_BASE, (PWM_OUT_2_BIT | PWM_OUT_3_BIT), true); /* 使能PWM2和PWM3的输出 */
PWMGenEnable(PWM_BASE, PWM_GEN_1); /* 使能PWM发生器1, */
GPIOPinTypeGPIOOutput(GPIO_PORTC_BASE,LED);
GPIOPinWrite(GPIO_PORTC_BASE,LED,0XFF);
/* 开始产生PWM方波 */
// PWMGenIntTrigEnable(PWM_BASE,PWM_GEN_1,PWM_INT_CNT_ZERO);
// PWMIntEnable(PWM_BASE,PWM_INT_GEN_1);
// IntEnable(INT_PWM1);
// IntMasterEnable();
while(1)
{
Q_J();
H_T();
}
}
void PWM_Generator_1_ISR (void)
{
const unsigned long ulTab[10] ={156000,148000,140000};
static unsigned long n = 0;
PWMGenIntClear(PWM_BASE,
PWM_GEN_1,
PWM_INT_CNT_ZERO);
PWMPulseWidthSet(PWM_BASE, PWM_OUT_2, ulTab[n]); /* 设置PWM2输出的周期 */
PWMPulseWidthSet(PWM_BASE, PWM_OUT_3, ulTab[2-n]); /* 设置PWM3输出的周期 */
GPIOPinWrite(GPIO_PORTC_BASE,LED,0);
n++;
if ( n >= 3 ) {
n = 0;
}
}
void Q_J()
{
unsigned char i=1,j=1;
PWMPulseWidthSet(PWM_BASE, PWM_OUT_2, jiaodu[i++]); /* 设置PWM2输出的周期 */
//PWMPulseWidthSet(PWM_BASE, PWM_OUT_3, jiaodu); /* 设置PWM3输出的周期 */
GPIOPinWrite(GPIO_PORTC_BASE,LED,0);
delay_ms(220);
PWMPulseWidthSet(PWM_BASE,PWM_OUT_3,jiaodu1[j++]);
GPIOPinWrite(GPIO_PORTC_BASE,LED,0XFF);
delay_ms(220);
PWMPulseWidthSet(PWM_BASE,PWM_OUT_2,jiaodu);
delay_ms(230);
3.17.1_PWM_Simple.rar
(138.49 KB)
}
void H_T()
{
unsigned char i=1,j=2;
PWMPulseWidthSet(PWM_BASE, PWM_OUT_2, jiaodu[i++]); /* 设置PWM2输出的周期 */
//PWMPulseWidthSet(PWM_BASE, PWM_OUT_3, jiaodu); /* 设置PWM3输出的周期 */
GPIOPinWrite(GPIO_PORTC_BASE,LED,0);
delay_ms(220);
PWMPulseWidthSet(PWM_BASE,PWM_OUT_3,jiaodu1[j++]);
GPIOPinWrite(GPIO_PORTC_BASE,LED,0XFF);
delay_ms(220);
PWMPulseWidthSet(PWM_BASE,PWM_OUT_2,jiaodu);
delay_ms(230);
}
} |