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.

CCS/TM4C123GH6PM: TIMER INTERRUPT

Part Number: TM4C123GH6PM

Tool/software: Code Composer Studio

Hi,

   Once again thanks for all to guide me in various doubt. Now i am working in the timer part and generated the interrupt from the timer which toggle the GPIO pin. I have one doubt interrupt working fine but i want interrupt in exactly 1microsecond and it need to toggle the GPIO exactly on the same time. Can any one guide me how to do these in the timer interrupt with clear explanation.

Regards

NiranjanCD

//*****************************************************************************
//
// timers.c - Timers example.
//
// Copyright (c) 2012-2016 Texas Instruments Incorporated.  All rights reserved.
// Software License Agreement
// 
// Texas Instruments (TI) is supplying this software for use solely and
// exclusively on TI's microcontroller products. The software is owned by
// TI and/or its suppliers, and is protected under applicable copyright
// laws. You may not combine this software with "viral" open-source
// software in order to form a larger program.
// 
// THIS SOFTWARE IS PROVIDED "AS IS" AND WITH ALL FAULTS.
// NO WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT
// NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. TI SHALL NOT, UNDER ANY
// CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
// DAMAGES, FOR ANY REASON WHATSOEVER.
// 
// This is part of revision 2.1.3.156 of the EK-TM4C123GXL Firmware Package.
//
//*****************************************************************************

#include <stdint.h>
#include <stdbool.h>
#include "inc/hw_ints.h"
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "driverlib/debug.h"
#include "driverlib/fpu.h"
#include "driverlib/gpio.h"
#include "driverlib/interrupt.h"
#include "driverlib/pin_map.h"
#include "driverlib/rom.h"
#include "driverlib/sysctl.h"
#include "driverlib/timer.h"
#include "driverlib/uart.h"
#include "utils/uartstdio.h"

//*****************************************************************************
//
//! \addtogroup example_list
//! <h1>Timer (timers)</h1>
//!
//! This example application demonstrates the use of the timers to generate
//! periodic interrupts.  One timer is set up to interrupt once per second and
//! the other to interrupt twice per second; each interrupt handler will toggle
//! its own indicator on the display.
//!
//! UART0, connected to the Virtual Serial Port and running at 115,200, 8-N-1,
//! is used to display messages from this application.
//
//*****************************************************************************

//*****************************************************************************
//
// Flags that contain the current value of the interrupt indicator as displayed
// on the UART.
//
//*****************************************************************************

//uint32_t g_ui32Flags;

static unsigned char test[4] = { 0x00, 0x01, 0x10, 0x11 };


//*****************************************************************************
//
// The error routine that is called if the driver library encounters an error.
//
//*****************************************************************************
#ifdef DEBUG
void
__error__(char *pcFilename, uint32_t ui32Line)
{
}
#endif

//*****************************************************************************
//
// The interrupt handler for the first timer interrupt.
//
//*****************************************************************************
void
Timer0IntHandler(void)
{
	UARTprintf("#\n");

    //
    // Clear the timer interrupt.
    //
    ROM_TimerIntClear(TIMER0_BASE, TIMER_TIMA_TIMEOUT);

    GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_4, test[0]);

    GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_4, test[2]);

    /*ROM_IntMasterDisable();

    UARTprintf("#\n");

    ROM_IntMasterEnable();*/


}

//*****************************************************************************
//
// The interrupt handler for the second timer interrupt.
//
//*****************************************************************************
void
Timer1IntHandler(void)
{
	UARTprintf("$\n");

    //
    // Clear the timer interrupt.
    //
    ROM_TimerIntClear(TIMER1_BASE, TIMER_TIMA_TIMEOUT);

    GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_4, test[1]);

    GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_4, test[3]);

    /*ROM_IntMasterDisable();

    UARTprintf("$\n");

    ROM_IntMasterEnable();*/

}

//*****************************************************************************
//
// Configure the UART and its pins.  This must be called before UARTprintf().
//
//*****************************************************************************
void
ConfigureUART(void)
{
    //
    // Enable the GPIO Peripheral used by the UART.
    //
    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);

    //
    // Enable UART0
    //
    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);

    //
    // Configure GPIO Pins for UART mode.
    //
    ROM_GPIOPinConfigure(GPIO_PA0_U0RX);
    ROM_GPIOPinConfigure(GPIO_PA1_U0TX);
    ROM_GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);

    //
    // Use the internal 16MHz oscillator as the UART clock source.
    //
    UARTClockSourceSet(UART0_BASE, UART_CLOCK_PIOSC);

    //
    // Initialize the UART for console I/O.
    //
    UARTStdioConfig(0, 115200, 16000000);
}

//*****************************************************************************
//
// This example application demonstrates the use of the timers to generate
// periodic interrupts.
//
//*****************************************************************************
int
main(void)
{
    //
    // Enable lazy stacking for interrupt handlers.  This allows floating-point
    // instructions to be used within interrupt handlers, but at the expense of
    // extra stack usage.
    //
    ROM_FPULazyStackingEnable();

    //
    // Set the clocking to run directly from the crystal.
    //
    //ROM_SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN |
    					//SYSCTL_XTAL_10MHZ);

    SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN |				//10MHz clock frequency
    				SYSCTL_XTAL_10MHZ);

    //
    // Initialize the UART and write status.
    //
    ConfigureUART();

    UARTprintf("\033[2JTimers example\n");
    UARTprintf("T1: 0  T2: 0");

    //
    // Enable the GPIO port that is used for the on-board LED.
    //
    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);

    //
    // Enable the GPIO pins for the LED (PF1 & PF2).
    //
    ROM_GPIOPinTypeGPIOOutput(GPIO_PORTB_BASE,  GPIO_PIN_4);


    //
    // Enable the peripherals used by this example.
    //
    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0);
    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER1);

    //
    // Enable processor interrupts.
    //
    ROM_IntMasterEnable();

    //
    // Configure the two 32-bit periodic timers.
    //
    ROM_TimerConfigure(TIMER0_BASE, TIMER_CFG_PERIODIC);
    ROM_TimerConfigure(TIMER1_BASE, TIMER_CFG_PERIODIC);
    TimerLoadSet(TIMER0_BASE, TIMER_A, ROM_SysCtlClockGet());
    TimerLoadSet(TIMER1_BASE, TIMER_A, ROM_SysCtlClockGet());

    //
    // Setup the interrupts for the timer timeouts.
    //
    ROM_IntEnable(INT_TIMER0A);
    ROM_IntEnable(INT_TIMER1A);
    ROM_TimerIntEnable(TIMER0_BASE, TIMER_TIMA_TIMEOUT);
    ROM_TimerIntEnable(TIMER1_BASE, TIMER_TIMA_TIMEOUT);

    //
    // Enable the timers.
    //
    ROM_TimerEnable(TIMER0_BASE, TIMER_A);
    ROM_TimerEnable(TIMER1_BASE, TIMER_A);

    //
    // Loop forever while the timers run.
    //
    while(1)
    {

    }
}

  • niranjan kumar85 said:
    TimerLoadSet(TIMER0_BASE, TIMER_A, ROM_SysCtlClockGet());

    This code line above set the Timer0 to interrupt every 1 second. So divide the ROM_SysCtlClockGet() by 1000000 for the Timer0 to interrupt every 1 us or 0.000001 seconds.

    Also your question is more appropriate at TI TM4C Forum. 

    -kel

  • Markel Robregado said:
    Also your question is more appropriate at TI TM4C Forum. 

    Agreed. I will move it to that forum.

    Thanks!

    ki

  • niranjan kumar85 said:
    want interrupt in exactly 1microsecond and it need to toggle the GPIO exactly on the same time.

    This may be a language issue - I am unclear as to what (poster's word now) "exactly" is being asked.

    • Are you asking for a continuous flow of Timer Interrupts - each/every one arriving at 1µS intervals?     If so - have you (and earlier poster) "thought this through?"    You must "service" that interrupt - and the "combination of interrupt response time (latency) PLUS the time w/in the ISR" - must not exceed (that same) 1µS!   (such is doubtful - is it not?)
    • Do you seek to, "toggle a GPIO" w/in that Timer interrupt?    If so - does not the MCU require (some) time to execute that "GPIO toggle" instruction - thus violating your requirement of "exactly?"    (i.e. the toggle will always occur AFTER the ISR is entered - it cannot be "coincident" (exact)!

    Your read of the MCU manual should reveal the "best case" response of "Interrupt Request" to "Interrupt Service."     And you realize that (other) interrupts may coexist - and compete w/your Timer - for the MCU's attention - do you not?     There are strategies which may "elevate" your chosen interrupt so that it "dominates" all others - thus receives the fastest service - and may (even) intrude upon (another) interrupt "in process."

    Might you respond to the 2 listed uncertainties - and the (apparent) impossibility of meeting your NEED(?) to, "Toggle the GPIO @ exactly the same time as the Interrupt."

    Never/ever mentioned is poster requirement for such behavior - often those w/serious experience/history - may conceive a "different means" (perhaps superior) of meeting (any) specified objective...   (and of course - cannot - when such (need) is withheld!)

  • Hi,

       My Query is wheather we can able to generate a timer interrupt for 1microsecond and it is possible to toggle a GPIO on the interrupt. Microcontroller need to run in 80Mhz or less than that.

    Regards

    NiranjanCD

  • Hi,
    Continuous interrupt generation for 1microsecond
  • Still not fully clear! Are you seeking a continuous series of MCU interrupts - each one 1µS apart?

    If that describes your requirement - AND you seek (still) to "toggle a GPIO" - have you made the (earlier) suggested effort to determine "IF" that is possible? You've been silent in that regard.

    Once again - when poster's prescribe their "solution method" - yet cannot fully achieve its execution - that poster "method" may deserve (some) rethink.    (i.e. exploit the experience of others!)

    If the (real) goal is a reliable, toggling GPIO output (@ 1MHz) - would not a "Timer ordered into PWM mode - (better) achieve that objective?"    (that so as the Timer's output is "automated" - thus "avoids" interrupt latency & code execution penalties...)