* * File: ir_testC.asm * Tom Dickens 1/6/1998 * 68HC11 assembly language to talk with a Sharp GP2D02 * IR Distance module. * * Simple driving program to display resulting distance values * on the C port (in hex). * * Subroutines included: * IR_init * Arguments: -none- * Purpose: Initialize I/O direction for GP2D02 * * Read_IR_Distance: * Arguments: -none- * Return Value: Register B, 8-bit distance value. * Purpose: Get a current distance reading from the * GP2D02 detector. * * 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 * *======================================================================== * MAIN *======================================================================== ORG $B600 ; use $F800 for an 'E2 bsr IR_init ldaa #$ff staa $1007 ; set port C to be outputs, it defaults to inputs Top: bsr Read_IR_Distance stab $1003 ldx #$4000 ; Need to delay for IR timing. Delay dex bne Delay bra Top *======================================================================== *======================================================================== * IR_init * Arguments: -none- * Purpose: Initialize I/O direction for GP2D02 *======================================================================== IR_init: pshx ldx #$1000 bset $26,x %10000000 ; Set A7 as an output bset 0,x %10000000 ; Set A7 to High pulx rts *======================================================================== * Read_IR_Distance: * Arguments: -none- * Return Value: Register B, 8-bit distance value. * Purpose: Get a current distance reading from the * GP2D02 detector. *======================================================================== Read_IR_Distance: pshx psha ldx #$1000 bclr 0,x %10000000 Wait_For_Distance brclr 0,x %00000100 Wait_For_Distance ; wait for IR unit sei ; disable interrupts for timing clrb bsr Get_IR_Bit bsr Get_IR_Bit bsr Get_IR_Bit bsr Get_IR_Bit bsr Get_IR_Bit bsr Get_IR_Bit bsr Get_IR_Bit bsr Get_IR_Bit bset 0,x %10000000 cmpb #$50 ; minimum good value bpl IR_Value_Good ldab #$00 ; too far, set value to 0 IR_Value_Good: pula pulx cli rts *======================================================================== * Get_IR_Bit: * Arguments: Register B, current accumlated value. * Return: Register B, new accumlated value. * Purpose: Get a single data bit from the GP2D02 detector. * Assumptions: X is set to $1000. *======================================================================== Get_IR_Bit: bset 0,x %10000000 ; HIGH bclr 0,x %10000000 ; LOW nop ; delay to allow data to stabilize nop ; Note: Additional NOP needed for 2 out of 6 of my test sensors ldaa 0,x ; data in A2 lsra ; data in A1 lsra ; data in A0 lsra ; value from A2 is now in C, roll into B rolb rts