' A small test program to exercise the A/D functions on NanoCore12 modules ' This file adapted on 1/16/2009 by Carl Barnes for NanoCore12 from code written by Kevin Ross ' It is written in SBASIC. ' To compile this file, you need to invoke the following commands: ' sbasic NCadtest.bas /c4000 /v3800 /s3f80 /m6812 > NCadtest.asm ' as12 NCadtest.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 "reg9s12c.lib" ' Port definitions and constants for 9S12C family of MCUs ' Single channel scan takes a single argument, which is the channel number to ' be read. It returns an eight-sample averaged 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 atdctl3, %01000000 + pop() ' S8CM pokeb atdctl5, 0 'start conversion 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(tcnthi) do j = peek(atdstat0) loop while ( j AND $8000) = 0 j = peek(tcnthi) printx "Single Channel scan took " ; j - i ; " TCNT clocks " j = 0 for i = 0 to 7 j = j + peekb(atddr0h + 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 up A/D modes pokeb atdctl3, %01000000 'set for 8 conversions pokeb atdctl5, %00010000 'and MULTichannel conversion j = peek(tcnthi) ' Wait for scan to be completed do loop while (peek(atdstat0) AND $8000) = 0 i = peek(tcnthi) printx "MultiChannel took " ; i - j ; " TCNT clocks" i = pop() j = pop() return main: ' Enable serial port poke scibdh, 26 ' Set for 9600 baud pokeb scicr2,$0c ' Enable transceiver pokeb tscr1,$80 ' Turn on the timer TCNT print print "****####****" print "Myadtest 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) pokeb atdctl4, %10000101 ' select 8-bit mode ' ' 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(atddr0h + lshft(i)) printx "Channel 0x" ; i ; " = 0x" ; NewValue next endif loop