Skip to content

Instantly share code, notes, and snippets.

@eggie5
Created April 13, 2012 00:02
Show Gist options
  • Select an option

  • Save eggie5/2372019 to your computer and use it in GitHub Desktop.

Select an option

Save eggie5/2372019 to your computer and use it in GitHub Desktop.
/* RealTime Clock.
* Global variable msCount is incremented every millisecond by Timer
* channel 5 Interrupt Service Routine.
* Loop in main displays msCount<15:12> on the module LEDs
* and msCount<11:4> project board LEDs.
* Note that when a word is assigned to a byte variable, the low 8 bits
* of the word are assigned to the byte.
*/
#include <hidef.h> /* common defines and macros */
#include <MC9S12C128.h> /* derivative information */
void initializePorts(void);
volatile word msCount = 0;
int i=0;
byte patterns[]={0x00,0x01,0x03,0x07,0x0F,0x1F,0x3F,0x7F,0xFF,0x7F,0x1F,0x0F,0x07,0x03,0x01,0x00};
word last_time=0;
volatile word delay=0x0080;
void readButtons()
{
word delay_increment=0x10;
if(PTM_PTM4 == 0) //up
{
while(PTM_PTM4==0) { ; }
if(delay_increment<= delay)
{
delay-=delay_increment;
}
else
{
delay=0;
}
}
else if(PTM_PTM5==0)//down
{
while (PTM_PTM5==0) { ; }
delay+=delay_increment;
}
}
void main(void) {
initializePorts();
EnableInterrupts; // Equivalent to asm CLI
for(;;) {
// Display msCount<11:4> on board LEDs
// PORTA = msCount >> 4;
readButtons();
// Display msCount<15:12> on module LEDs
PORTB = ~(msCount >> 8);
}
}
void leds()
{
word delta_from_last_update=msCount-last_time;
if(delta_from_last_update>=delay)
{
PORTA=patterns[i];
i++;
last_time=msCount;
}
if(i>13)
{
i=0;
}
}
/////////////////// Timer5 Interrupt Service Routine /////////////
void interrupt 13 ISR_Timer5(void) {
msCount++;
leds();
TC5 += 1000; // Interrupt again in 1 ms
TFLG1 = 0x20; // Clear Timer5 Flag; will not interrupt
// again if flag is not cleared.
}
void initializePorts() {
DDRA = 0xFF; // PTA<7:0> - outputs (LEDs)
DDRB = 0xF0; // Make PTB<7:4> outputs
TIOS = 0x20; // Make IOC5 an output. It is NECCESSARY to make
// a timer bit an output (not of the chip, but of the
// internal Timer Block) in order to have it generate
// an interrupt request.
TSCR1 = 0x80; // Set TEN = 1 (Enable timer)
TIE = 0x20; // Turn on Timer5 interrupt; disable others
TSCR2 = 0x01; // Set Timer prescaler bits PR<2:0> to 001
// and disable Timer Overflow interrupt.
// Timer counter clock = BusClock / 2 = 1MHz
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment