|
///////////////////////////////////////////////////////////////////////////// |
|
// 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" |
|
|
|
void Wait10ms(); //Wait for 10 ms |
|
int DebouncedButtonState(eSWLSwitch butt); // return the current debounced state of the button |
|
|
|
///////////////////////////////////////////////////////////////////////////// |
|
// Main Entry |
|
///////////////////////////////////////////////////////////////////////////// |
|
void main(void) |
|
{ |
|
int LastState = 0; //Previous button state |
|
int CurrState = 0; //The current button state |
|
// 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; |
|
//Main loop |
|
for (;;) |
|
{ |
|
CurrState = DebouncedButtonState(SWLMiddle); |
|
if(CurrState != LastState) //Button has changed |
|
{ |
|
//Either button was pressed or button was released |
|
if(CurrState)//Pressed! |
|
SWLSet(SWLAll); |
|
else |
|
SWLClear(SWLAll); |
|
//else //Released! |
|
//Do nothing |
|
} |
|
LastState = CurrState; |
|
} |
|
} |
|
|
|
///////////////////////////////////////////////////////////////////////////// |
|
// Functions |
|
///////////////////////////////////////////////////////////////////////////// |
|
void Wait10ms() //Wait for 10 ms |
|
{ |
|
//Timer for OC0 is currently set up. Let's use it. |
|
// 1 - Clear any pending flags |
|
TFLG1 |= 0x00000001; // Yes, 1 clears. Not 0 |
|
|
|
//Arm for an event in 100 ms |
|
TC0 = TCNT + 1563;// 10 ms / 6.4 us = 1563 |
|
|
|
while(!(TFLG1 & 0x00000001)); |
|
} |
|
int DebouncedButtonState(eSWLSwitch butt) // return the current debounced state of the button |
|
{ |
|
int firstButtonState = 0; |
|
int secondButtonState = 0; |
|
do |
|
{ |
|
firstButtonState = SWLRead(butt); |
|
Wait10ms(); |
|
secondButtonState = SWLRead(butt); |
|
} while (firstButtonState != secondButtonState); |
|
return firstButtonState; |
|
} |
|
///////////////////////////////////////////////////////////////////////////// |
|
// Interrupt Service Routines |
|
///////////////////////////////////////////////////////////////////////////// |