Skip to content

Instantly share code, notes, and snippets.

@ajarmst
Created March 2, 2022 21:53
Show Gist options
  • Save ajarmst/3fa1ec3c2328b1ab0c258d1845e9d25a to your computer and use it in GitHub Desktop.
Save ajarmst/3fa1ec3c2328b1ab0c258d1845e9d25a to your computer and use it in GitHub Desktop.
/////////////////////////////////////////////////////////////////////////////
// Basic HC9S12 Output Compare Timer Functionality
/////////////////////////////////////////////////////////////////////////////
#include <hidef.h> /* common defines and macros */
#include "derivative.h" /* derivative-specific definitions */
#include "pll.h"
#include "swled.h"
/////////////////////////////////////////////////////////////////////////////
// Main Entry
/////////////////////////////////////////////////////////////////////////////
void main(void)
{
// main entry point - these two lines must appear first
_DISABLE_COP();
EnableInterrupts;
/////////////////////////////////////////////////////////////////////////////
// one-time initializations
/////////////////////////////////////////////////////////////////////////////
PLL_To20MHz();
(void) SWLInit();
//Initialize Timer. OC0
// Turn the thing on
TSCR1 = 0b10000000; //TSCR1_TEN is bit 7. Turn it on. 7.3.2.6
// Set Prescale of 128
TSCR2 = 7; //0b00000111; //Timer rate = BUSSPD/2^7 = 20 MHz/128 = 156.25 KHz (6.4 us tick)
// Use channel 0 OC
TIOS |= 0b00000001; //1
// Toggle the pin for channel 0 (pin 9)
TCTL2 = 0b0000001;
//Timer for OC0 is currently set up. Let's use it.
// 1 - Clear any pending flags
TFLG1 = 1; // Yes, 1 clears. Not 0
//Arm for an event in 100 ms
TC0 = TCNT + 15625;// 100 ms / 6.4 us = 15625
//Main loop
for (;;)
{
// Nothing else to do right now, so just wait for the timer
while (!TFLG1); //Spin till you see the 1 on the flag
TFLG1 = 1; //Clear flag
TC0 = TC0 + 15625;
(void) SWLToggle(SWLGreen); //Toggle the green LED so I know it's working
}
}
/////////////////////////////////////////////////////////////////////////////
// Functions
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
// Interrupt Service Routines
/////////////////////////////////////////////////////////////////////////////
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment