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.

TM4C123GXL: Setting up periodic timer

Hi guys,


I'm trying to set up a timer that triggers an ISR every 200 milliseconds. I'm a little less familiar with timers but after reading through the datasheet and the tivaware library I think this is a periodic timer? To get it to trigger every 200 ms I set the Load to 200 * TicksPerMS, which I've pound defined as 40000 since I'm using a 40 mhz clock. I'm using that the value in the TimerLoadSet command is the value you want in ticks.  Do I need to make the timer count up to this value, or will it count down from this loaded value and then automatically start again?

I've got a few lines of code which I think is enough to get me started. If I'm making any big errors please let me know, thanks.

void isr_timer(void){
	TimerIntClear(TIMER0_BASE, TIMER_TIMA_TIMEOUT);
	//insert code to poll the accelerometer
}

void timer_init(void){
	TimerConfigure(TIMER0_BASE, TIMER_CFG_PERIODIC); //Set as periodic timer counting up ( I think)
	TimerLoadSet(TIMER0_BASE, TIMER_A, 200*TicksPerMS); //
	TimerIntRegister(TIMER0_BASE, TIMER_A, isr_timer);
	TimerIntEnable(TIMER0_BASE, TIMER_TIMA_TIMEOUT);
	
	TimerEnable(TIMER0_BASE, TIMER_A);
	
	
}

  • Hello Zero_PD

    When the timer is in periodic mode, it shall reload automatically. The count up or down depends on the TimerConfigure's 2nd parameter which in this case is a down count.

    Regards
    Amit
  • EDIT: False alarm, I hadn't included the SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0) command! But why, in the example code, are all the lines of code preceded by ROM? Such as ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0)?

    Thanks Amit. My code is hanging for some reason, so I must be doing something wrong. I added the call to timer_init into my setup function, and when it did my code started freezing. I added some print statements to figure out exactly where I was getting stuck. It turns out I go through my init routine, get into timer_init, print "0" and never get to print "1". So for some reason the TimerConfigure(TIMER0_BASE, TIMER_CFG_PERIODIC) line is hanging my code. Do you have any clue what might be going wrong here?

    I tried commenting out the TimerConfigure line, but then I get stuck at the TImerLoadSet line. Could there be something wrong with the timer library? I'm using Keil Uvision and not seeing anything wrong, so it's pretty odd.

    void timer_init(void){
    	printf("0");
    	TimerConfigure(TIMER0_BASE, TIMER_CFG_PERIODIC); //Set as periodic timer counting up ( I think)
    	printf("1");
    	TimerLoadSet(TIMER0_BASE, TIMER_A, 200*TicksPerMS); //
    	printf("2");
    	TimerIntRegister(TIMER0_BASE, TIMER_A, isr_timer);
    	printf("3");
    	TimerIntEnable(TIMER0_BASE, TIMER_TIMA_TIMEOUT);
    	printf("4");
    	TimerEnable(TIMER0_BASE, TIMER_A);
    }

  • Hello Zero_PD,

    Did you enable the clock to the peripheral?

    Regards
    Amit
  • Hi Amit, check the "edit" to my post. I must've been typing it up as you commented. All's well and thank you!
  • Hello Zero_PD,

    The TM4C devices have most of the basic API's in ROM as well. As a result the code instead of placing the API's in Flash, now link to the API's in the ROM. This frees up space for more application code to be written in flash.

    Regards
    Amit