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.

TM4C123: Timer

Other Parts Discussed in Thread: EK-TM4C123GXL, TM4C123GH6PM

Hi everyone,

The example code for Tiva C LaunchPad Kit (EK-TM4C123GXL ) following:

#include <stdint.h>
#include <stdbool.h>
#include "inc/tm4c123gh6pm.h"
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "driverlib/sysctl.h"
#include "driverlib/interrupt.h"
#include "driverlib/gpio.h"
#include "driverlib/timer.h"
#include "driverlib/pin_map.h"

int main(void)
{
uint32_t ui32Period;
uint32_t ui32DutyCycle;
//Clock Setup
SysCtlClockSet(SYSCTL_SYSDIV_5|SYSCTL_USE_PLL|SYSCTL_XTAL_16MHZ|SYSCTL_OSC_MAIN);
ui32Period = 1000;
ui32DutyCycle = 0;
//PWM
//GPIO Configuration
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3);
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3, 0);
// Configure PB6 as T0CCP0
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
GPIOPinConfigure(GPIO_PF1_T0CCP1);
GPIOPinTypeTimer(GPIO_PORTF_BASE, GPIO_PIN_1);
//Timer Configuration
SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0);
TimerConfigure(TIMER0_BASE, TIMER_CFG_SPLIT_PAIR|TIMER_CFG_B_PWM);
TimerLoadSet(TIMER0_BASE, TIMER_B, ui32Period -1);
TimerMatchSet(TIMER0_BASE, TIMER_B, ui32DutyCycle);
// PWM
TimerEnable(TIMER0_BASE, TIMER_B);

while(1)
{
TimerMatchSet(TIMER0_BASE, TIMER_B, ui32DutyCycle++);
if(ui32DutyCycle >= ui32Period - 1)
ui32DutyCycle = 0;
SysCtlDelay(50000);
}
}

With above configuration for timer, will it count down or count up? 

***********

In the document TM4C123G_LaunchPad_Workshop_Workbook.pdf , a sentence to explain  for TimerLoadSet(TIMER0_BASE, TIMER_B, ui32Period -1);  following: 

Note that you have to subtract one from the timer period since the interrupt fires at the zero count.

I don't really understand :(  Someone can explain it clearly, pls?!

**********

In the description of TimerLoadSet() and TimerMatchSet function, the parameters ui32Value is the load value and match value.

So, what are load value and match value?

Thanks so much and best regard!

  • Hello Long,

    In PWM Mode the timer always count down. Please look at the note for description of the GPTMTxMR.TxDIR (where x is A or B, and in your case B) in the data sheet.

    For the second point, assume a counter load value of 5, the timer has to count for 5 clocks. If you load the value of 5 and the timer interrupt comes at 0, the effective count is 6 clock cycles. Thus by subtracting 1 from the counter load value you will get the load value worth of count.

    The Load Value is the start or end value for the counter in down or up count mode. The match value is a value between Load and 0 for which an action can be taken like an interrupt or trigger or Pin toggle (PWM Mode).

    Regards

    Amit

  • Thanks for your complete answer, Mr/Ms Amit!

    Regards!

    Long Pham