' ' A small test program to exercise the A/D functions on ' a 68HC12 ' ' This file is written for the 68HC12 by Kevin Ross (kevinro@nwlink.com) ' It is written in SBASIC. ' To compile this file, you need to invoke the following commands: ' ' sbasic adtest.bas /cf000 /v0000 /m6812 > adtest.asm ' as12 adtest.asm ' ' declare NewValue declare CurrentChannel ' ' The following three variables are used as temporaries ' in the code. You must preserve them across function calls ' declare i declare j declare k include "regs12.lib" ' ' Single channel scan takes a single argument, which is the pin number to ' be read. It returns an eight sample average value. This function ' removes the arguement from the stack before returning. ' SingleChannelScan: ' ' Perform a single channel A/D read cycle. By setting the ' S8CM bit, the single channel is sampled 8 times consecutively ' The low 3 bits determine which channel is going to be sampled ' and is derived from the argument passed to this function. ' ' The 8 results are stored in registers ADR0 thru ADR7 ' ' This provides a quick way of grabbing an average value ' ' Writing to ATDCTL5 will initiate the conversion ' pokeb atdctl5, %01000000 + pop() ' S8CM push j ' Save J across calls push i ' Save i across calls ' ' Wait for the SCF (Sequence Complete Flag) of ATDSTAT to be ' set. When it is set, then the result registers are valid. ' i = peek(tcnt) do j = peek(atdstat) loop while ( j AND $8000) = 0 j = peek(tcnt) printx "Single Channel scan took " ; j - i ; " TCNT clocks " j = 0 for i = 0 to 7 j = j + peekb(adr0h + lshft(i)) next j = j / 8 ' Restore i i = pop() ' The original j is on the top of the stack. We really want to have ' the average on the top of the stack, which is currently in the ' j variable. ' push j swap ' Now original j on top, average in second j = pop() ' Now average on top, j is restored ' Returns with the average on the top of the stack return pop() MultiChannelScan: push j push i ' ' Scan all 8 channels once then stop. The results are ' stored in ADR0-ADR7 ' ' Set atdctl5 for S8CM and MULT pokeb atdctl5, %01010000 j = peek(tcnt) ' Wait for scan to be completed do loop while (peek(atdstat) AND $8000) = 0 i = peek(tcnt) printx "MultiChannel took " ; i - j ; " TCNT clocks" i = pop() j = pop() return main: ' Turn off the COP pokeb copctl,0 ' Enable serial port poke sc0bdh, 26 ' Set for 19200 baud pokeb sc0cr2,$0c ' Enable transceiver pokeb tscr,$80 ' Turn on the timer TCNT print print "****####****" print "adtest12 running" ' ' Set up the A/D converter ' ' Setup is for 8 channel mode, continuous scan, multi-channel ' conversion, with results in ADR0 through ADR7 ' pokeb atdctl2, %10000000 ' ADPU is the power-up bit ' Must be done FIRST ' ' Press the number ( 0 - 7 ) on the terminal to get a reading from ' a particular channel. Press 'm' to get a multi-channel scan ' do i = inkey() i = i AND $ff if i >= '0' then if i <= '7' then ' Switch channel number CurrentChannel = i - $30 NewValue = USR(SingleChannelScan, CurrentChannel) printx "Single Channel 0x" ; CurrentChannel ; " = 0x" ; NewValue endif endif ' ' If the user has pressed 'm', then do a multi-channel scan ' if i = 'm' then printx "Multi-Channel scan: " gosub MultiChannelScan for i = 0 to 7 NewValue = peekb(adr0h + lshft(i)) printx "Channel 0x" ; i ; " = 0x" ; NewValue next endif loop