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.

TM4C1231H6PZ: PWM initialization with prescalar

Part Number: TM4C1231H6PZ

Hi,

I am using PWM to control LCD backlight brightness - see init function below.  I was having an issue where the output pin was turning on after this function, even though I expected it to be off (to be specific, the pin was turning on immediately after the ROM_TimerControlLevel() call).  

I played around with the order of these ROM calls, and I figured out that the issue was fixed if I moved the ROM_TimerMatchSet() and ROM_TimerPrescaleMatchSet() calls after the ROM_TimerConfigure() call.  Does this make sense?  Do the MatchSet() functions need to be after the TimerConfigure in order for them to work properly?  Just want to make sure I got to the root of the problem, and didn't just fix it by accident.

static void LCD_BacklightInit(void){

UINT32 timerPeriod;
UINT8 timerPrescaler;

backlightLoadValue = SysCtlClockGet() / BACKLIGHT_PWM_FREQ_HZ;
timerPeriod = backlightLoadValue & 0xFFFF; //lowest 4 bytes
timerPrescaler = backlightLoadValue >> 16;

ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER2);
ROM_TimerDisable(TIMER2_BASE, TIMER_B);
ROM_TimerMatchSet(TIMER2_BASE, TIMER_B, 0); // Initially off
ROM_TimerPrescaleMatchSet(TIMER2_BASE, TIMER_B, 0);ROM_GPIOPinConfigure(GPIO_PF5_T2CCP1);
ROM_GPIOPinTypeTimer(GPIO_PORTF_BASE, GPIO_PIN_5);
ROM_TimerConfigure(TIMER2_BASE, TIMER_CFG_SPLIT_PAIR | TIMER_CFG_B_PWM);
ROM_TimerControlLevel(TIMER2_BASE, TIMER_B, TRUE); //Inverse PWM output, enables simpler duty cycle set function
ROM_TimerLoadSet(TIMER2_BASE, TIMER_B, timerPeriod);
ROM_TimerPrescaleSet(TIMER2_BASE, TIMER_B, timerPrescaler);
ROM_TimerEnable(TIMER2_BASE, TIMER_B);
}

  • That's an interesting point - and indeed - on occasion the sequence of function calls is important. I'm travelling - cannot repeat your test (and don't have that MCU) yet would not your effort be eased by, "Removing the Prescaler? If I recall correctly - the Timer enabled PWM encompasses 16 bits - surely that's sufficient for an LCD backlight "Dimming" control - don't you agree?
  • Thanks for the reply. I should have mentioned that my system clock is 80MHz, and PWM frequency is 800Hz. So as is, the load value needs more than 16 bits (80MHz / 800 Hz = 0x186A0). For the dimming control, you are right - I could increase the PWM frequency > 1220Hz and only require 16 bits (and thus no prescaler).

    I am also planning to use similar code to enable PWM on digital outputs, and I'll need to allow frequencies below 1220Hz. So with a sysClock at 80Mhz, I believe I'll need the prescaler. In this case, I'll really need to make sure my outputs aren't turning on upon PWM initialization.
  • Firm/I have past been "in" the Flat-Screen business. (LCD, VFD, OLED etc.) While 800Hz will work - we usually ran far lower in frequency - say 200Hz - sometimes even lower. (I'm assuming yours is the more modern Led backlight - not CFL nor Electro-Luminescent.)

    You may "keep" your 80MHz system clock - and still realize "sub 250Hz backlight PWM" by employing a PWM Generator rather than the Timer controlled PWM. The PWM Generator enables extensive system-clock division - and the 16 bits should easily meet your full range requirements.

    Another advantage of the PWM Generator - you gain far greater control of the PWM frequency - which enables you to prevent, "Beat Frequency" (visual disturbances) between the PWMing backlight and the Display's scanning, row multiplexer.     PWM Generator demands greater "Set-Up/Config" (I doubt that will be an issue for you - yet we can assist) and indeed offers many added features - unattainable via the much simpler Timer-PWM.

  • Thanks for the info on the "beat frequency".  I was seeing this at lower frequencies, but I didn't realize what it was.  I thought I just needed a higher frequency.

    Unfortunately it looks like the TM4C1231H6PZ does not have the PWM Generator.  At this point the hardware design is locked in, so I'll forced to use the timer PWM.  The tolerance of the PWM is not critical, so I think it'll be ok.  It seems to be working fine with the change in the initialization order, as described in the original post.  

  • OMG - you are correct - NO PWM Generators appear! Certainly - you chose this device for GPIO and price...

    I'm uncertain if that code sequence is the best - or only - fix. Cannot you review the key/critical Timer registers - and determine "just when/where" your (differently) sequenced code - creates illegal register entries? I've a different MCU - perhaps what you're noting is MCU centric? (i.e. Krazy-Making)

    I'm curious - but cannot get access to our boards till tomorrow. As I recall - my past PWM via Timer's "Set-Up/Config" was intuitive - and worked "out of the box."

    And... having just searched & found that (past) code - I find that "intuitive & worked" both resulted from use of the PWM Generator. We did employ several Timers - but NOT to generate PWM.    Ratz!