Monday, April 13, 2009

68HCS12 Heartbeat

It's often useful to have a flashing LED in embedded systems to verify the system is 'running' and healthy. I coded up a 500ms heartbeat using the Motorolla 68HCS12 in C. See below.

#include "vectors12.h"
#include "ioregs12.h"
#include "bits.h" // custom header containing bit values in hex

#define INTERRUPT void __attribute__ ((interrupt))
#define sei() __asm__ __volatile__ ( "sei" )
#define cli() __asm__ __volatile__ ( "cli" )
#define wai() __asm__ __volatile__ ( "wai" )

volatile unsigned int hbcount;

INTERRUPT rti_interrupt(void);

int main(void)
{
// Initialization
sei(); // diable interrupts
UserRTI = (unsigned int) &rti_interrupt;
RTICTL = 0x40; // set RTICTL for 1ms interrupt
CRGINT = BIT8; // enable RTI interrupts
DDRT |= BIT8; // or equal
cli(); // clear interrupt flag

while (1)
{ // idle loop
wai();
};
return 0;
}

INTERRUPT rti_interrupt(void)
{
CRGFLG = 0x80; // acknoledge RTI & clear interrupt flag
hbcount++;
if (hbcount >= 489) // Reset hbcount every 500ms
{
hbcount = 0;
PTT = PTIT ^ BIT8; // exclusive or PTT
}
}