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.

TM4C1294NCPDT: How to configure a clock of 50 Hz with timers?

Part Number: TM4C1294NCPDT

I need to configure a display that updates its values with f = 50Hz. Since the CPU works with 16Mhz and the prescaler has a max. value of 256, the min. clock rate I can compute is 62.5 kHz. How do you go about this? Do you concatenate several timers? If I would take the output of Timer0 and put it into Timer1 with another prescaler of 256, I would get 244.140625. Timer2 and another prescaler of 5, would result in 48.828125 Hz, which would be acceptable. Is this how you do it or is there an easier and better way?

  • No, if the goal is to create an interrupt at 50Hz (50 times a second), you can simply use the timer in full width (32-bit) mode and run it at full system clock speed with the appropriate load value. The timer counts down from 2,400,000 (120,000,000 / 50, or 0x00249F00). It then generates an interrupt, reloads the counter and starts over again.

    #include <stdbool.h>
    #include <stdint.h>
    #include "inc/hw_ints.h"
    #include "inc/hw_memmap.h"
    #include "inc/hw_types.h"
    #include "inc/hw_gpio.h"
    #include "driverlib/interrupt.h"
    #include "driverlib/rom.h"
    #include "driverlib/rom_map.h"
    #include "driverlib/sysctl.h"
    #include "driverlib/timer.h"
    #include "driverlib/gpio.h"
    
    uint32_t g_ui32Flags;
    volatile uint8_t Timer_flag=0;
    
    void
    Timer0IntHandler(void)
    {
        //
        // Clear the timer interrupt.
        //
        ROM_TimerIntClear(TIMER0_BASE, TIMER_TIMA_TIMEOUT);
    
       Timer_flag = 1;
    
    }
    
    /**
     * main.c
     */
    int main(void)
    {
        uint32_t ui32SysClock;
        uint8_t test_Flags;
    
        //
        // Run from the PLL at 120 MHz.
        //
        ui32SysClock = SysCtlClockFreqSet((SYSCTL_XTAL_25MHZ |
                                               SYSCTL_OSC_MAIN | SYSCTL_USE_PLL |
                                               SYSCTL_CFG_VCO_480), 120000000);
    
        //
        // Enable the peripherals used by this project.
        //
        ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0);
    
        ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
        ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
        ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
        ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOJ);
    
        ROM_GPIOPinTypeGPIOInput(GPIO_PORTA_BASE, GPIO_PIN_0);
        ROM_GPIOPinTypeGPIOOutput(GPIO_PORTA_BASE, GPIO_PIN_1);
        ROM_GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_1, GPIO_PIN_1);
    
        ROM_GPIOPinTypeGPIOInput(GPIO_PORTB_BASE, GPIO_PIN_0);
        ROM_GPIOPinTypeGPIOOutput(GPIO_PORTB_BASE, GPIO_PIN_1);
        ROM_GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_1, GPIO_PIN_1);
    
        ROM_GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_0 | GPIO_PIN_1);
        ROM_GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_0 | GPIO_PIN_1, 0);
    
        ROM_GPIOPinTypeGPIOOutput(GPIO_PORTJ_BASE, GPIO_PIN_0 | GPIO_PIN_1);
        ROM_GPIOPinWrite(GPIO_PORTJ_BASE, GPIO_PIN_0 | GPIO_PIN_1, 0);
    
        //
        // Enable processor interrupts.
        //
        ROM_IntMasterEnable();
    
        ROM_TimerConfigure(TIMER0_BASE, TIMER_CFG_PERIODIC);
        ROM_TimerLoadSet(TIMER0_BASE, TIMER_A, ui32SysClock/50); // 50Hz
    
        ROM_TimerIntClear(TIMER0_BASE, TIMER_TIMA_TIMEOUT);
        ROM_TimerIntEnable(TIMER0_BASE, TIMER_TIMA_TIMEOUT);
    
        ROM_IntEnable(INT_TIMER0A);
        ROM_TimerEnable(TIMER0_BASE, TIMER_A);
    
        //
        // Loop forever while the timers run.
        //
        while(1)
        {
            if( Timer_flag )
            {
                Timer_flag = 0;
                if( test_Flags )
                {
                    test_Flags = 0;
                    ROM_GPIOPinWrite(GPIO_PORTJ_BASE, GPIO_PIN_0, GPIO_PIN_0);
                    ROM_GPIOPinWrite(GPIO_PORTJ_BASE, GPIO_PIN_1, 0);
                }else
                {
                    test_Flags = 1;
                    ROM_GPIOPinWrite(GPIO_PORTJ_BASE, GPIO_PIN_0, 0);
                    ROM_GPIOPinWrite(GPIO_PORTJ_BASE, GPIO_PIN_1, GPIO_PIN_1);
                }
            }
        }
    }