include "regs11.lib" ' ' File: ir_testB.bas ' Tom Dickens 1/15/1999 ' 68HC11 SBASIC program to talk with a Sharp GP2D02 ' IR Distance module. ' ' Simple driving program to display resulting distance values ' on the C port and also through the RS-232 serial link. ' This can be seen using the command from PCBUG11. ' ' Physical Hookpup: ' From HC11 to GP2D02, Port A7 with voltage divider ' (two 1K (or 10K) resistors) ' ' HC11 port A7 ----/\/\/\---+---/\/\/\---Ground ' 1K | 1K ' +---> to Vin on GP2D02 ' From GP2D02 to HC11, Port A3 ' '======================================================================== ' Declarations '======================================================================== declare val declare n '======================================================================== ' Main '======================================================================== main: pokeb baud, $30 ' 9600 baud pokeb sccr2, $0c ' turn on SCI print "\n\r\n\rIR obstacle detection:\n\r" print "\n\r Code by Tom Dickens & Karl Lunt\n\r" pokeb ddrc, 255 ' set port C for output gosub irinit ' prepare IR do gosub readir ' get a reading printx "\rDistance: "; val; " "; pokeb portc, val loop '======================================================================== ' ' readir get reading from IR sensor ' ' This routine reads an 8-bit value from the IR sensor ' and saves it in the global variable val. ' '======================================================================== readir: pokeb porta, peekb(porta) and $7f ' pull A7 low waituntil porta, $04 ' loop until A2 goes high val = 0 ' start with nothing for n=0 to 7 ' do eight bits... val = val + val ' need to wait a bit before read pokeb porta, peekb(porta) or $80 ' Set A7 high pokeb porta, peekb(porta) and $7f ' Set A7 low if peekb(porta) and $04 <> 0 ' Read A2 val = val + 1 endif next pokeb porta, peekb(porta) or $80 ' Set A7 high if val < $50 val = 0 endif return '======================================================================== ' ' irinit IR ranging system initialization ' '======================================================================== irinit: pokeb pactl, peekb(pactl) or $80 ' A7 is output pokeb porta, peekb(porta) or $80 ' start with A7 high return