' timers1.bas ' ' This file is written for the 68HC12 by Kevin Ross (kevinro@nwlink.com) ' An article in the Seattle Robotics Society Encoder accompanies this ' file http://www.seattlerobotics.org/encoder/nov97/68hc12.html ' ' It is written in SBASIC. ' To compile this file, you need to invoke the following commands: ' ' sbasic timers.bas /cf000 /v0000 /m6812 > timers.asm ' as12 first.asm ' include "regs12.lib" ' PORT definitions and constants declare TicFlags ' ' Program execution starts at the Main: label, as shown here. ' Main: ' ' This program isn't going to use the COP. It must be turned of ' right away, otherwise it might reset the chip. ' pokeb copctl,0 ' ' Next, initialize the serial port and print a message saying we started. ' ' Enable serial port poke sc0bdh, 52 ' Set for 9600 baud pokeb sc0cr2,$0c ' Enable transceiver print "timers.bas has started" ' ' Setup channels 0 and 1 to be TOC channels. ' Setup channels 2 and 3 to be TIC channels. ' pokeb tios,%00000011 ' ' Set channels 0 and 1 to toggle their output pins when the compare happens ' pokeb tctl2, %00000101 ' ' Set the prescalar to slow the clock way way down. In fact, we want to ' have the TOC's happen every 262 milliseconds, so you can see the pins ' toggle using an LED or a logic probe. Check out the prescalar selection ' table in the 68HC812A4 Technical Summary to see how to change the rate. ' pokeb tmsk2,%00110101 ' ' Set the compares to trigger when TCNT == 0 and $8000 ' poke tc0,0 poke tc1,$8000 ' ' Turn on the timer, disable it during background debug mode ' pokeb tscr, %10100000 ' ' The TOC channels are up and running. Now configure the TIC channels 2 and 3 ' ' We are interested in capturing any changes in channel 2, or any rising ' edge in channel 3 ' pokeb tctl4,%01110000 do ' ' This loop is going to sit and spin, testing to see when a TIC ' event happens. When the TIC triggers, it sets a bit in TFLG1 ' to indicate that a value is ready. To clear the flag indicating ' the TIC has happened, we write a 1 to its position in TFLG1 ' TicFlags = peekb(tflg1) if TicFlags AND $04 <> 0 printx "TIC2: ",peek(tc2) pokeb tflg1,peekb(tflg1) OR $04 endif if TicFlags AND $08 <> 0 printx "TIC3: ",peek(tc3) pokeb tflg1,peekb(tflg1) OR $08 endif loop end