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: Timer 0 Subtimer B

Part Number: TM4C123GH6PM

Hi,

I am new to TMC4C123GH6PM & Using the Launchpad.

I have Understood the Usage of Timer0 Sub timer A for generating Desired Frequency & Duty Cycle.

In the same way I Tried to use Timer0 Sub timer B for Frequency Generation But I could not Succeed even after spending couple of hours.

I request to help on this Problem. 

/* -----------------------          Include Files       --------------------- */
#include <stdint.h>                     // Library of Standard Integer Types
#include <stdbool.h>                    // Library of Standard Boolean Types
#include "inc/tm4c123gh6pm.h"           // Definitions for interrupt and register assignments on Tiva C
#include "inc/hw_memmap.h"              // Macros defining the memory map of the Tiva C Series device
#include "inc/hw_types.h"               // Defines common types and macros
#include "driverlib/sysctl.h"           // Defines and macros for System Control API of DriverLib
#include "driverlib/interrupt.h"        // Defines and macros for NVIC Controller API of DriverLib
#include "driverlib/gpio.h"             // Defines and macros for GPIO API of DriverLib
#include "driverlib/timer.h"            // Defines and macros for Timer API of driverLib

/* -----------------------      Global Variables        --------------------- */
uint32_t ui32Period;                    // Variable to store the period to be inputted to Timer0
uint32_t ui32IntFrequency = 0x00000009; // Variable to determine the Blinking Frequency of LEDs

/* -----------------------      Function Prototypes     --------------------- */
void Timer0AIntHandler(void);            // The prototype of the ISR for Timer0 Interrupt
void Timer0BIntHandler(void);            // The prototype of the ISR for Timer0 Interrupt


/* -----------------------          Main Program        --------------------- */
int main(void){
    // Set the System clock to 80MHz and enable the clock for peripheral Port F and Timer0
    SysCtlClockSet(SYSCTL_SYSDIV_2_5 | SYSCTL_USE_PLL | SYSCTL_XTAL_16MHZ | SYSCTL_OSC_MAIN);
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);



    SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0);

    // Set the PF1, PF2, PF3 as output and configure Timer0 to run in periodic mode
    GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE,  GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3 | GPIO_PIN_4 );



    TimerConfigure(TIMER0_BASE, TIMER_CFG_A_PERIODIC);
    TimerConfigure(TIMER0_BASE, TIMER_CFG_B_PERIODIC);

    //TIMER0

    //TIMER0A
    // Calculate the Time period for 50% duty cycle while switching at "ui32IntFreqency" Hz.
    ui32Period = (SysCtlClockGet() / ui32IntFrequency) / 2;
    // Load the Timer with the calculated period.
    TimerLoadSet(TIMER0_BASE, TIMER_A, ui32Period - 1);

    //TIMER0B
    // Calculate the Time period for 50% duty cycle while switching at "ui32IntFreqency" Hz.
    ui32Period = (SysCtlClockGet() / ui32IntFrequency) / 2;
    // Load the Timer with the calculated period.
    TimerLoadSet(TIMER0_BASE, TIMER_B, ui32Period - 1);

    // Enable the Interrupt specific vector associated with Timer0A
    IntEnable(INT_TIMER0A);
    // Enables a specific event within the timer to generate an interrupt
    TimerIntEnable(TIMER0_BASE, TIMER_TIMA_TIMEOUT);

    // Enable the Interrupt specific vector associated with Timer0A
    IntEnable(INT_TIMER0B);
    // Enables a specific event within the timer to generate an interrupt
    TimerIntEnable(TIMER0_BASE, TIMER_TIMB_TIMEOUT);

    // FOR ALL TIMERS
    // Master interrupt enable API for all interrupts
    IntMasterEnable();
    // Enable the Timer
    TimerEnable(TIMER0_BASE, TIMER_A);
    TimerEnable(TIMER0_BASE, TIMER_B);

    while (1);
}

/* -----------------------      Function Definition     --------------------- */
void Timer0AIntHandler(void)
{   // The ISR for Timer0 Interrupt Handling
    // Clear the timer interrupt
    TimerIntClear(TIMER0_BASE, TIMER_TIMA_TIMEOUT);
    // Read the current state of the GPIO pin and write back the opposite state
    if (GPIOPinRead(GPIO_PORTF_BASE, GPIO_PIN_1)){
        // GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3, 0);
        GPIOPinWrite(GPIO_PORTF_BASE,  GPIO_PIN_1,0);
    } else{
        GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1, GPIO_PIN_1);
    }
}


void Timer0BIntHandler(void)
{   // The ISR for Timer0 Interrupt Handling
    // Clear the timer interrupt
    TimerIntClear(TIMER0_BASE, TIMER_TIMB_TIMEOUT);
    // Read the current state of the GPIO pin and write back the opposite state
    if (GPIOPinRead(GPIO_PORTF_BASE, GPIO_PIN_2)){
        // GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3, 0);
        GPIOPinWrite(GPIO_PORTF_BASE,  GPIO_PIN_2,0);
    } else{
        GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_2, GPIO_PIN_2);
    }
}

  • > TimerConfigure(TIMER0_BASE, TIMER_CFG_A_PERIODIC);
    > TimerConfigure(TIMER0_BASE, TIMER_CFG_B_PERIODIC);

    At reset, Timer0A is configured as a 32-bit timer, and Timer0B (effectively) doesn't exist. To split the timer into 2x16-bit timers, you need something like

    TimerConfigure(TIMER0_BASE, TIMER_CFG_SPLIT_PAIR |TIMER_CFG_A_PERIODIC |TIMER_CFG_B_PERIODIC );

    --------

    As soon as you do this, you'll notice that 80M/9/2 doesn't fit in 16 bits. You'll need to apply a prescale value (GPTMTxPR, 8 bits wide) to get the load value in range. [Ref data sheet (SPMS376E) Table 11-5].

    I don't have all my materials here, but I expect there's an applicable example in the TivaWare Examples suite.

  • Hi, 

    Thanks for the Quick Response. I will try this & update.

  • Hi,

    Thanks a lot. It is working.

    Great Help.

    with regards,

    Ravi