Hi,
I am using the TM4C123GXL's PD0 and PD1 to drive 2 DC servo motors. Below is the code I used to setup the clock, PWM and port. I also use the buttons on the Tiva C Launchapd to move the DC servos to a particular position based on the cutting position. After the initial cutting position 0, I use one of the buttons to advance the cutting position to 1. However, it doesn't work. The servos get stuck in a particular position and it doesn't move regardless of which button I press. The program works just fine when I run in debug mode with break point inside the IF statement that is monitoring the buttons. I can't figure out why it works in Debug, but not in Run mode.
Thanks,
Charlie
#include <stdint.h>
#include <stdbool.h>
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "driverlib/sysctl.h"
#include "driverlib/gpio.h"
#include "driverlib/debug.h"
#include "driverlib/pwm.h"
#include "driverlib/pin_map.h"
#include "inc/hw_gpio.h"
#include "driverlib/rom.h"
// 20 ms @ 50 Hz
#define PWM_FREQUENCY 50
int main(void)
{
volatile uint32_t ui32Load;
volatile uint32_t ui32PWMClock;
volatile uint8_t ui8Adjust1, ui8Adjust2;
int cutting_position; // Position
cutting_position = 0;
ui8Adjust1 = 72; // subtract 3
ui8Adjust2 = 77; // add 2
ROM_SysCtlClockSet(SYSCTL_SYSDIV_5|SYSCTL_USE_PLL|SYSCTL_OSC_MAIN|SYSCTL_XTAL_16MHZ);
ROM_SysCtlPWMClockSet(SYSCTL_PWMDIV_64);
ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_PWM1);
ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD);
ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
ROM_GPIOPinTypePWM(GPIO_PORTD_BASE, GPIO_PIN_0);
ROM_GPIOPinConfigure(GPIO_PD0_M1PWM0);
ROM_GPIOPinTypePWM(GPIO_PORTD_BASE, GPIO_PIN_1);
ROM_GPIOPinConfigure(GPIO_PD1_M1PWM1);
// Buttons are associated with Port F
HWREG(GPIO_PORTF_BASE + GPIO_O_LOCK) = GPIO_LOCK_KEY;
HWREG(GPIO_PORTF_BASE + GPIO_O_CR) |= 0x01;
HWREG(GPIO_PORTF_BASE + GPIO_O_LOCK) = 0;
ROM_GPIODirModeSet(GPIO_PORTF_BASE, GPIO_PIN_4|GPIO_PIN_0, GPIO_DIR_MODE_IN);
ROM_GPIOPadConfigSet(GPIO_PORTF_BASE, GPIO_PIN_4|GPIO_PIN_0, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD_WPU);
ui32PWMClock = SysCtlClockGet() / 64;
ui32Load = (ui32PWMClock / PWM_FREQUENCY) - 1;
PWMGenConfigure(PWM1_BASE, PWM_GEN_0, PWM_GEN_MODE_DOWN);
PWMGenPeriodSet(PWM1_BASE, PWM_GEN_0, ui32Load);
PWMGenConfigure(PWM1_BASE, PWM_GEN_1, PWM_GEN_MODE_DOWN);
PWMGenPeriodSet(PWM1_BASE, PWM_GEN_1, ui32Load);
ROM_PWMPulseWidthSet(PWM1_BASE, PWM_OUT_0, ui8Adjust1 * ui32Load / 1000);
ROM_PWMPulseWidthSet(PWM1_BASE, PWM_OUT_1, ui8Adjust2 * ui32Load / 1000);
ROM_PWMOutputState(PWM1_BASE, PWM_OUT_0_BIT | PWM_OUT_1_BIT, true);
ROM_PWMGenEnable(PWM1_BASE, PWM_GEN_0);
ROM_PWMGenEnable(PWM1_BASE, PWM_GEN_1);
while(1)
{
if(ROM_GPIOPinRead(GPIO_PORTF_BASE,GPIO_PIN_4)==0x00) // SW1 CCW
{
//ui8Adjust1--;
cutting_position--;
//if (ui8Adjust1 < 25)
if (cutting_position <= 1 )
{
//ui8Adjust1 = 25;
cutting_position = 1;
ui8Adjust1 = 101;
ui8Adjust2 = 57;
}
else if (cutting_position == 2)
{
ui8Adjust1 = 64;
ui8Adjust2 = 69;
}
else if (cutting_position == 3)
{
ui8Adjust1 = 81;
ui8Adjust2 = 90;
}
else if (cutting_position == 4)
{
ui8Adjust1 = 78;
ui8Adjust2 = 108;
}
else if (cutting_position == 5)
{
ui8Adjust1 = 79;
ui8Adjust2 = 58;
}
ROM_PWMPulseWidthSet(PWM1_BASE, PWM_OUT_0, ui8Adjust1 * ui32Load / 1000); //Motor 1
ROM_PWMPulseWidthSet(PWM1_BASE, PWM_OUT_1, ui8Adjust2 * ui32Load / 1000); //Motor 2
ROM_SysCtlDelay(10000);
} //if
if(ROM_GPIOPinRead(GPIO_PORTF_BASE,GPIO_PIN_0)==0x00) // SW2 CW
{
//ui8Adjust1++;
//if (ui8Adjust1 > 125)
cutting_position++;
if (cutting_position >= 5 )
{
cutting_position = 5;
ui8Adjust1 = 79;
ui8Adjust2 = 58;
}
else if (cutting_position == 4)
{
ui8Adjust1 = 78;
ui8Adjust2 = 108;
}
else if (cutting_position == 3)
{
ui8Adjust1 = 81;
ui8Adjust2 = 90;
}
else if (cutting_position == 2)
{
ui8Adjust1 = 64;
ui8Adjust2 = 69;
}
else if (cutting_position == 1)
{
ui8Adjust1 = 101;
ui8Adjust2 = 57;
}
ROM_PWMPulseWidthSet(PWM1_BASE, PWM_OUT_0, ui8Adjust1 * ui32Load / 1000);//Motor 1
ROM_PWMPulseWidthSet(PWM1_BASE, PWM_OUT_1, ui8Adjust2 * ui32Load / 1000);//Motor 2
ROM_SysCtlDelay(10000);
}
} // while
} // main