Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Tiva C Timer Interrupts
#include <stdint.h>
#include <stdbool.h>
#define PART_TM4C123GH6PM
#include "inc/tm4c123gh6pm.h"
/*****************************
* LM4F120 - timer based blink
* Using TimerIntRegister
* 80 Mhz clock
*****************************/
#include "inc/hw_ints.h"
#include "driverlib/interrupt.h"
#include "driverlib/sysctl.h"
#include "driverlib/timer.h"
void initTimer()
{
ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0);
ROM_TimerConfigure(TIMER0_BASE, TIMER_CFG_PERIODIC); // 32 bits Timer
TimerIntRegister(TIMER0_BASE, TIMER_A, Timer0Isr); // Registering isr
ROM_TimerEnable(TIMER0_BASE, TIMER_A);
ROM_IntEnable(INT_TIMER0A);
ROM_TimerIntEnable(TIMER0_BASE, TIMER_TIMA_TIMEOUT);
}
void Timer0Isr(void)
{
ROM_TimerIntClear(TIMER0_BASE, TIMER_TIMA_TIMEOUT); // Clear the timer interrupt
digitalWrite(RED_LED, digitalRead(RED_LED) ^ 1); // toggle LED pin
}
void setup()
{
Serial.begin(9600);
pinMode(RED_LED,OUTPUT);
initTimer();
}
void loop()
{
unsigned long ulPeriod;
unsigned int Hz = 5; // frequency in Hz
ulPeriod = (SysCtlClockGet() / Hz)/ 2;
ROM_TimerLoadSet(TIMER0_BASE, TIMER_A,ulPeriod -1);
while (1)
{
Serial.println("While timer is running ...");
delay(100);
}
}
@tomjennings

This comment has been minimized.

Copy link

tomjennings commented Nov 16, 2017

thanks. it's been tough to find workable examples of timer code; i can read a datasheet but the macros and lib functions aren't well documented.

i actually needed a one shot; all it took to modify this is s/TIMER_CFG_PERIODIC/TIMER_CFG_ONE_SHOT/ and invoke ROM_TimerEnable (...) to fire off another cycle.

@MohammedKhandwawala

This comment has been minimized.

Copy link

MohammedKhandwawala commented May 20, 2018

Can you help with priority interrupts?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.