* * File: ir_test.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 an LCD display connected to port C (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 * * Addresses of RAM Display Tables: RAM_Display_1 EQU $10 RAM_Display_2 EQU $21 DIS_END EQU $32 * ORG $F800 ORG $B600 lds #$00FF * IR init ldx #$1000 bset $26,x %10000000 ; Set A7 as an output bset 0,x %10000000 ; Set A7 to High jsr LCDInit * Top: IR_Test: jsr Read_IR_Distance jsr B_to_D_Hex std RAM_Display_1+$0E jsr Write_LCD bra IR_Test *======================================================================== * The LCD is connected to port C with the following bit assignments: * V+ :LCD2 * Gnd :LCD1 * Vref:LCD3 * C0 :LCD11 data I/O to LCD data bit 4 * C1 :LCD12 data I/O to LCD data bit 5 * C2 :LCD13 data I/O to LCD data bit 6 * C3 :LCD14 data I/O to LCD data bit 7 * C4 :LCD4 RS (register select) L=control, H=data * C5 :LCD5 R/W H=read, L=write * C6 :LCD6 E (enable) H is enabled, transaction on H->L edge. * C7 -unused- * *======================================================================== * LCD RAM usage: * $00-$01 Mode variables * * MODE_EH $00 Mode (data/control) with Enable High * MODE_EL $01 Mode (data/control) with Enable Low * * $DF-E1 JMP Instruction for timer 1 interrupt (In boot ROM) * $E2-FF Stack (30 bytes: RTI uses 9 bytes) * *======================================================================== * Addresses of RAM variables: MODE_EH EQU $00 MODE_EL EQU $01 *======================================================================== LCDInit: ldx #$400f ; set up Control Mode stx MODE_EH ldab #$FF stab $1007 ; set port C, all bits for output ldab #$04 LOOPB: ldaa #$03 jsr Write_Nibble ldx #$0480 ; 1152 loops * 3.5 uS = 4.032 mS LOOPY1: dex ; 4 cycles = 2 uS bne LOOPY1 ; 3 cycles + ? = 1.5 uS decb bne LOOPB ldx #LCD_Init_Table *======================================================================== MoreInit: ldaa $00,x beq LCD_Init_Done bsr Write_C_Byte inx bra MoreInit LCD_Init_Done: CopyLCDTable: * Copy the LCD Display Tables to RAM ldx #RAM_Display_1 ; To ldy #Display_1 ; From Copy_More: ldaa $00,y staa $00,x inx iny cpx #DIS_END bne Copy_More bsr Write_LCD rts Write_LCD: pshx ldx #RAM_Display_1 bsr Write_Line_1 ldx #RAM_Display_2 bsr Write_Line_2 pulx rts *======================================================================== LCD_Init_Table: fcb $28 ; Data length (bit 4) = 4-bit, number of lines (bit 2) = 2 fcb $08 ; Display on (bit 2), cursor on (bit 1), blink cursor (bit 0) fcb $01 ; Clear display fcb $06 ; Entry mode, I/D (bit 1) inc/dec, S (bit) display shift fcb $0C ; Display on (bit 2), cursor on (bit 1), blink cursor (bit 0) fcb $00 *======================================================================== Wait4BF: Delay: pshb ldb #$10 ; 16 loops * 2.5 uS = 40 uS d_loop: decb ; 2 cycles = 1.0 uS bne d_loop ; 3 cycles = 1.5 uS pulb rts *======================================================================== Write_Line_1: * ; x points to start of NULL terminated line psha pshx ldaa #$80 ; Line 1 in Display Data RAM bra Write_Line *------------------------------------------------------------------------ Write_Line_2: psha pshx ldaa #$A8 ; Line 2 in Display Data RAM ($80 + 40 ($28)) *------------------------------------------------------------------------ Write_Line: bsr Write_C_Byte Next_Character: lda $00,x beq Write_Line_Done bsr Write_D_Byte inx bra Next_Character Write_Line_Done: pulx pula rts *======================================================================== Write_D_Byte: pshx ldx #$501F ; Masks for Data mode bra Write_LCD_Byte *------------------------------------------------------------------------ Write_C_Byte: pshx ldx #$400F ; Masks for Control mode *------------------------------------------------------------------------ Write_LCD_Byte: stx MODE_EH pulx psha lsra lsra lsra lsra bsr Wait4BF bsr Write_Nibble pula psha bsr Write_Nibble cmpa #$01 ; if clear, then we need to wait 1.64 mS bne Not_Clear ldaa #$20 ; 32 * 50uS = 1.6 mS Do_Not_Clear: bsr Wait4BF deca bne Do_Not_Clear Not_Clear: pula rts *======================================================================== * Write nibble is low 4 bits of A Write_Nibble: psha anda #$0F ; set upper 4 bits to zero oraa MODE_EH ; E=H, R/W=L, RS=H or L staa $1003 anda MODE_EL ; E=L, R/W=L, RS=H or L staa $1003 pula rts *======================================================================== * LCD Display tables Display_1: * 0123456789ABCDEF fcc "IR Robot:Dist=ns" fcb $00 Display_2: * 0123456789ABCDEF fcc "Tom Dickens....." fcb $00 *======================================================================== * 8-bit distance value returned in B * Read_IR_Distance: ldx #$1000 bclr 0,x %10000000 Wait_For_Distance brclr 0,x %00000100 Wait_For_Distance ; wait for IR unit sei psha pshx 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 IR_Value_Good: pulx pula cli rts *======================================================================== 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 *======================================================================== * Convert the value in the B register as 2 ASCII hex bytes. *======================================================================== B_to_D_Hex: pshx pshy pshb lsrb lsrb lsrb lsrb ldy #WA2D_Table ; 4-bits to HEX aby ldaa $00,y * pulb andb #$0f ldy #WA2D_Table aby ldab $00,y puly pulx rts WA2D_Table: fcc "0123456789ABCDEF"