Created
September 29, 2023 15:38
-
-
Save ajarmst/014c35378200604b0bc2ba169ae484cd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/********************************************* | |
rti.h | |
**********************************************/ | |
#ifndef RTIH | |
#define RTIH | |
#include <hidef.h> /* common defines and macros */ | |
#include "derivative.h" | |
extern volatile unsigned int rtiCount; // global counter incremented each time | |
//Note: extern just means that this variable exists but is defined elsewhere | |
extern volatile unsigned int rtiInterval; // How freqently (in ms) to run the callback | |
void RTIInit(void); //Set up RTI for 1 ms intervals, enable interrupts | |
void RTICallback(void(*fp)(void), unsigned int count); //Tell the RTI to call a function when triggered | |
#endif | |
/********************************************* | |
rti.c | |
**********************************************/ | |
#include "rti.h" | |
#include <hidef.h> /* common defines and macros */ | |
#include "derivative.h" | |
#include "swled.h" | |
//These were declared in my .h, here's my definitions | |
volatile unsigned int rtiCount = 0; | |
volatile unsigned int rtiInterval = 1; | |
void (*rtiHandler)(void) = 0; | |
void RTIInit(void) | |
{ | |
//Set RTI to decimal, divider 2000, mod8 counter (1 ms) | |
RTICTL = 0b10010111; | |
//Enable my interrupts | |
CRGINT |= CRGINT_RTIE_MASK; | |
} | |
//Set a handler that will get called every RTI count | |
void RTICallback(void(*fp)(void), unsigned int count) | |
{ | |
rtiHandler = fp; //Function to run | |
rtiInterval = count; // How many milliseconds between runs | |
} | |
//Special syntax for interupt handlers (unique to CodeWarrior, not standard C) | |
interrupt VectorNumber_Vrti void Vrti_ISR(void) | |
{ | |
CRGFLG = CRGFLG_RTIF_MASK; //clear flag; | |
++rtiCount; | |
if (!(rtiCount % rtiInterval)) | |
{ //If I have a valid pointer to a handler, call it. | |
if (rtiHandler) rtiHandler(); | |
rtiCount = 0; | |
} | |
} | |
/********************************************* | |
main.c | |
**********************************************/ | |
#include <hidef.h> /* common defines and macros */ | |
#include "derivative.h" /* derivative-specific definitions */ | |
#include "pll.h" | |
#include "swled.h" | |
#include "rti.h" | |
void LightsToggle(void); | |
void main(void) { | |
PLL_To20MHz(); | |
SWL_Init(); | |
RTIInit(); | |
RTICallback(LightsToggle, 200); //toggle lights every 200 ms | |
EnableInterrupts; | |
for(;;) { | |
//No reason to stay awake in between interrupts. | |
asm wai; | |
} /* loop forever */ | |
/* please make sure that you never leave main */ | |
} | |
void LightsToggle(void) | |
{ | |
SWL_ToggleLEDs(eAllLEDs); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment