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: How to create the delays in microseconds and nanoseconds using the timer??

Part Number: TM4C123GH6PM

Hii

I'm working on the LCD display interfacing where in the datasheet of the LCD its mentioned that  I have to give delays in microseconds, milliseconds and in nanoseconds. According to the sysctl.c driver, sysctldelay() is not accurate. That is why i tried to give delay using timer0 I'm attaching  the code below. I was able to apply 10 millisecond delay now I want to create 160 microsecond delay and 400 nanoseconds delay so how can create macro and nanoseconds delay using timer0 can anyone please help me to do so?    

				void timer0A_delayMs(int ttime)
				{
					int i;
					SYSCTL->RCGCTIMER |= 1;     /* enable clock to Timer Block 0 */
				 
					TIMER0->CTL = 0;            /* disable Timer before initialization */
					TIMER0->CFG = 0x04;         /* 16-bit option */
					TIMER0->TAMR = 0x02;        /* periodic mode and down-counter */
					TIMER0->TAILR = 50000 - 1;  /* (Using 50Mhz clock) Timer A interval load value register */
					TIMER0->ICR = 0x1;          /* clear the TimerA timeout flag*/
					TIMER0->CTL |= 0x01;        /* enable Timer A after initialization */
				 
					for(i = 0; i < ttime; i++)
					{ 
						while ((TIMER0->RIS & 0x1) == 0) ;      /* wait for TimerA timeout flag */
						TIMER0->ICR = 0x1;      /* clear the TimerA timeout flag */
					}
				}

Regards

Omkar

  • First, if you are not using interrupts, the ROM_SysCtlDelay() function is accurate. If you are using interrupts, polling the time like you have done above can suffer giving a longer delay than expected, but only if an interrupt routine is being serviced when the timer expires. Finally, each method has an inherent precision. If you are operating the TM4C123 device at 50MHz, the precision of ROM_SysCtlDelay() is 3*(1/50MHz) or 60nS. The precision of the timer is 20nS, but the polling in the while loop and the surrounding for loop will make the precision worse than for ROM_SysCtlDelay(). If the delays required by the display are minimum delays, then the lack of precision is OK.

    Now, to change the delay time, of your function to uS, use "50-1" instead of "50000-1". To do nanoseconds, use ROM_SysCtlDelay() if a minimum value is required.

    Finally, we strongly recommend that you not do direct register writes as you have done in the sample code. We recommend that you use the TivaWare library of functions to setup the timer and other peripherals. 

  • Hii

    Thank you so much sir for the detailed explanation now I'm cleared about this.

    Regards

    Omkar

  • The below code is what I have written. For  a 120 MHz processor! (TM4C1294)

    I have not timed it yet, but it seems to work fine.

    I have used Timer 3, as at least Timer0 seems to be used for something, when I start using various packages for LAN.

    void Timer3AIntHandler(void)
    {
        //
        // Clear the timer interrupt flag.
        //
        TimerIntClear(TIMER3_BASE, TIMER_TIMA_TIMEOUT);
    
        ... Do something ...
    }
    
    void Setup() {
    
        //Timer 3A
        SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER3);
        TimerConfigure(TIMER3_BASE, TIMER_CFG_SPLIT_PAIR | TIMER_CFG_A_ONE_SHOT);
        TimerLoadSet(TIMER3_BASE, TIMER_A, 1800); //15 µs at 120 MHz
    
        TimerIntRegister(TIMER3_BASE, TIMER_A, Timer3AIntHandler);
    
        IntMasterEnable();
    
        TimerIntEnable(TIMER3_BASE, TIMER_TIMA_TIMEOUT);
        IntEnable(INT_TIMER3A);
    
    }
    
    void main() {
    
        .....
    
        Setup();
    
        .....
    
        while (???) {
    
    
            if (??) 
               TimerEnable(TIMER3_BASE, TIMER_A);  //Interrupt happens 15 us later
    
        }
    }

  • Hii Ja

    I will try and let you know thank you for the reply.

    Regards

    Omkar