A port data sampler


This code reads samples from the three highest bits of port 1. It stores consecutive samples in a circular buffer. If all the samples in the buffer are the same, it returns the value of the sample in D7..D5 with D0 set to indicate a stable value.

This is part of a bit-banging scheme to read data on the processor to peripheral bus of an analog cellphone.

;*********************************************************************
;* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
; FILE:   $Workfile:   MBSAMPL.A51  $
; 
; AUTHOR:   Timothy Fox
; 
; PURPOSE:
;
;
;********************************************************************
;
; --------------------------------------------------------------------
; Version control history:

;  
;* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

; $NOMOD51
; $INCLUDE (..\INC\REG52.INC)
; $INCLUDE (..\INC\EQUATE.INC)
$REGISTERBANK(0,1,2,3)

    NAME  MBSAMPLE
    ?MBSAMPL  SEGMENT CODE

    RSEG  ?MBSAMPL
    USING 2

; EXTRN BIT   (TD,CD,RDA)          ; P1
EXTRN DATA (PhData1,PhData2,PhData5)

R0B2  DATA  010H
R1B2  DATA  011H
R2B2  DATA  012H
R3B2  DATA  013H
R4B2  DATA  014H
R5B2  DATA  015H
R6B2  DATA  016H
R7B2  DATA  017H

;mbsample   equ PhData1
;debounce_array equ PhData2
;//                       2..4
;dba_limit      equ PhData5

    PUBLIC  InitMBSampler
InitMBSampler:
    ;sets constant values into R0 in bank 2, and dba_limit 
    push    psw
    mov     psw,#010H
    push    acc

    mov     a,#PhData2 ; get the _address_ of the debounce_array 
    mov     R0,a
    add     a,#3
    mov     PhData5,a   ; R0 must always be <= PhData5
    pop     acc
    pop     psw
    ret

    PUBLIC  Read_MBUS_state
Read_MBUS_state:
    ;Bank 2 Register usage
    ;R0 = pointer to byte array in immediate memory
    ;R4 = loop counter
    ;
    push    psw
    mov     psw,#010H ; force Bank 2
    push    acc

    mov     acc,p1      ; get a sample of DATA1..3
    anl     acc,#0E0H   ; mask off everything but DATA1..3
    mov     @R0,acc     ; store this sample
    inc     R0
    mov     acc,R0
    cjne    a,PhData5,nowrap
    mov     R0,#PhData2 ; start of debounce array

nowrap:
    push    R0B2  ; save rotating pointer (debounce array index)
    ; now see if all the samples match
    mov     R0,#PhData2 ; point to the start of the debounce array
    mov     R4,#2 ; R4 is now the loop repeat down-counter
debounce_loop:
    mov     a,@R0
    inc     R0
    xrl     a,@R0  ; compare with the next sample
    jnz     nomatch
    djnz    R4,debounce_loop
    ; all samples match - reload a sample and set D0
    mov     acc,@R0
    orl     acc,#1
nomatch:
    pop     R0B2
    mov     PhData1,acc
    pop     acc
    pop     psw
    ret


    END