Skip to content

Instantly share code, notes, and snippets.

@robertinant
Last active April 5, 2024 02:06
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save robertinant/10398194 to your computer and use it in GitHub Desktop.
Save robertinant/10398194 to your computer and use it in GitHub Desktop.
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
Copy link

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
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