This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

TM4C123GH6PM: Having trouble setting PWM period

Part Number: TM4C123GH6PM


Hello,

I'm trying to make an LED blink on and off for 1 sec. with PWM. But no matter what I write in PWMGenPeriodSet and PWMPulseWidthSet, the LED is continuously on and never turns off. The code is below. Please help I really need this. Thanks in advance.

#include <stdint.h>
#include <stdbool.h>
#include <math.h>
#include "inc/hw_types.h"
#include "inc/hw_memmap.h"
#include "inc/hw_gpio.h"
#include "driverlib/sysctl.h"
#include "driverlib/gpio.h"
#include "driverlib/interrupt.h"
#include <inc/hw_ints.h>
#include "driverlib/timer.h"
#include "driverlib/pwm.h"
#include "driverlib/pin_map.h"
#include "driverlib/adc.h"
#include "driverlib/debug.h"


int main(void)
{

SysCtlClockSet(SYSCTL_SYSDIV_4|SYSCTL_USE_PLL|SYSCTL_XTAL_16MHZ|SYSCTL_OSC_MAIN);

SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF); 
SysCtlPeripheralEnable(SYSCTL_PERIPH_PWM1);

SysCtlPWMClockSet(SYSCTL_PWMDIV_1);

HWREG(GPIO_PORTF_BASE + GPIO_O_LOCK) = GPIO_LOCK_KEY;
HWREG(GPIO_PORTF_BASE + GPIO_O_CR) = 0x01;

GPIOPinConfigure(GPIO_PF1_M1PWM5);
GPIOPinConfigure(GPIO_PF2_M1PWM6);
GPIOPinConfigure(GPIO_PF3_M1PWM7);

GPIOPinTypePWM(GPIO_PORTF_BASE,GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3);

PWMGenConfigure(PWM1_BASE,PWM_GEN_2,PWM_GEN_MODE_DOWN|PWM_GEN_MODE_NO_SYNC); 
PWMGenConfigure(PWM1_BASE,PWM_GEN_3,PWM_GEN_MODE_DOWN|PWM_GEN_MODE_NO_SYNC); 

PWMGenPeriodSet(PWM1_BASE,PWM_GEN_2,16000000);
PWMGenPeriodSet(PWM1_BASE,PWM_GEN_3,16000000);

PWMPulseWidthSet(PWM1_BASE,PWM_OUT_5,8000000);
PWMPulseWidthSet(PWM1_BASE,PWM_OUT_6,8000000);
PWMPulseWidthSet(PWM1_BASE,PWM_OUT_7,8000000);

PWMGenEnable(PWM1_BASE,PWM_GEN_2);
PWMGenEnable(PWM1_BASE,PWM_GEN_3);

PWMOutputState(PWM1_BASE, (PWM_OUT_5_BIT|PWM_OUT_6_BIT|PWM_OUT_7_BIT), true); 


while(1)
{

}


}

  • Found it. The timer is only 16 bits so the maximum value you can enter in the PWMGenPeriod set and hence PWMPulseWidthSet is 65,535. The LED will turn on and off in miliseconds. To increase the period you can increase the clock DIV of system clock and pwm clock.

  • Hello Zahid,

    Glad you were able to find the issue and thanks for sharing the explanation as another community member may find that useful in the future.

    By the way in TivaWare 2.2.0.295 we have an API, GPIOUnlockPin, for GPIO like PF0 if you need to unlock them.

    Best Regards,

    Ralph Jacobi