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.

Timer one shot mode problem

Hello,

Iam trying to operate Timer in one shot mode, turn on the on-board led after 1sec.

I wrote the code as follows:

I made the period is 1 sec so after time-out interrupt initialized, I turn on the led in the ISR.

#include <stdint.h>
#include <stdbool.h>
#include "inc/tm4c1294ncpdt.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"
//////////////////////////////////////////////////////////////////////////
int count = 0;
int main(void)
{
	uint32_t ui32Period;
	uint32_t ui32SysClkFreq;

	//SET SYSTEM CLK
	ui32SysClkFreq = SysCtlClockFreqSet((SYSCTL_XTAL_25MHZ | SYSCTL_OSC_MAIN | SYSCTL_USE_PLL | SYSCTL_CFG_VCO_480), 120000000);

	//ENABLE PERIPHERALS
	SysCtlPeripheralEnable(SYSCTL_PERIPH_GPION);
	SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0);

	//CONFIGURE PERIPHERALS
	GPIOPinTypeGPIOOutput(GPIO_PORTN_BASE,GPIO_PIN_1);
	TimerConfigure(TIMER0_BASE, TIMER_CFG_SPLIT_PAIR | TIMER_CFG_A_ONE_SHOT);

	//SET THE PERIOD OF THE TIMER
	ui32Period = ui32SysClkFreq;
	TimerLoadSet(TIMER0_BASE, TIMER_A, ui32Period -1);

	//ENABLE INTERRUPTS
	IntEnable(INT_TIMER0A);
	TimerIntEnable(TIMER0_BASE, TIMER_TIMA_TIMEOUT);
	IntMasterEnable();

	//ENABLE TIMERS
	TimerEnable(TIMER0_BASE, TIMER_A);

	

	while(1)
	{
	}

}
//ISR
void Timer0IntHandler(void)
{
	// Clear the timer interrupt flag
	TimerIntClear(TIMER0_BASE, TIMER_TIMA_TIMEOUT);

	GPIOPinWrite(GPIO_PORTN_BASE,GPIO_PIN_1,0x2);
}

Thanks in advance.

Regards,

Badr.

  • Hello Badr,

    Did you map the Timer0IntHandler in the startup_ccs.c file, if the issue is that the interrupt does not fire?

    Regards

    Amit

  • Hi Badr,

    You have configured the timer as TIMER_CFG_SPLIT_PAIR | TIMER_CFG_A_ONE_SHOT - but on TM4C129x split timers are only 16 bits wide, so further loading the initial value with system clock frequency writes garbage, maximum possible in this case is 0xFFFF. 

    If you need one second, configure as a single timer, 32 bits; that supports the constant passed/calculated by you.

    Petrei

  • Thanks Amit,

    Thanks Petrei,

    Yes, I mapped it Amit.

    So, max time-out in split configuration for 120MHz configured clock is 2^16/120MHz = .54ms ?

    Is there a way to learn API functions other than Examples or libraries?

    Regards,

    Badr.

  • Hi,

    Yes, you are right, .54ms! 

    About learning: some of us worked with many (older) micro controllers - and for them no driver libraries were available. More, you need to know every bit and every register to be able to debug. 

    Here a big help is with driverlib, allows you to write code faster. Of coarse there is a learning time, usually one/two projects. But this is somewhat canceled if you are not able to debug when needed - you must know every bit/register to be able to recognize anomalies - so while learning, and before writing down something, read the source code of every function. And the user manual.

    Petrei

  • Thanks,

    I got it.

    Regards,

    Badr.

  • Just as poster Petrei states - our group too worked w/several (past) 8/16 bit MCUs in the "dark era" - when no such driver libraries roamed the earth.  Driver Library very likely had much to do with past, founding firm being acquired by this vendor.

    May I suggest that you use these resources in combination - to best speed & enhance your MCU mastery?

    a) Peripheral Driver Library Guide

    b) MCU manual - open to chapter of interest

    c) code examples targeting your specific MCU - past found @ examples\peripherals\

    d) driverlib open to the c source file aimed at your, "peripheral du jour"  (i.e. gpio.c, pwm.c, timers.c etc.)  Your careful study of this code will provide huge boosts in your understanding/capability.

    Petrei & I fully agree - rush is rarely good - unless you're really smart it's most always best to "confirm" your new knowledge by writing small, simple program - and then observing outputs and key MCU registers.  Single stepping your code is ideal, here.

    And - as always - avoid the (universal) desire to, "create your own/custom" project.  Instead - choose a known-good, factory supplied project - which has some overlap to your interest/project.  Your goal should be MCU mastery - not so much IDE maven...  (those existing, factory projects have all the "dirty-work" & special "gotchas" managed for you.  There is a, "Wisdom to the herd" - and successful herd members stay w/in those safe, existing herd boundaries...)

  • Thanks cb1_mobile,

     

    That's really precious information to me.

     

    Regards,

    Badr.


  • @Badr,

    Thank you - rarely do Petrei/I/others receive such neat thanks.  Appreciated.

    Individual MCU manual chapters are quite large.  What we find helpful is to, "copy/paste key portions" of each chapter into a smaller, more manageable, "Word" (or similar) document.  Be sure to keep that chapter segregation discipline - so that you can quickly/easily recover critical data.  You may also log the sample programs - and your results - keyed again to each MCU chapter.  (recall that what seems, "obvious" today - will likely, "be far less so" in the future - hate when that happens!)

    If you'd be so kind to give quick "glance" @ my post (above) "Forum Guidelines to aid posters..." you may find other items of interest & recommendations for further forum success.   Bon chance, mon ami - et merci...

  • Thanks cb1,

    Iam writing down paper notes while Iam reading from the datasheet but, I should try copying it to boost learning speed.

    Also I know that it used to be a hard thing to learn MCU in years earlier but, also I think that was the right way to learn :).

    I just consider you all as my teacher even you answer by one word, thanks.

    Regards,

    Badr.