

;ram
  OldButtons
  Buttons
  DownedButtons
  Pos1
  Pos2



BUTN  equ  PORTA, 3
CA0   equ  PORTB, 0  
CA1   equ  PORTA, 4
B1F   equ   0
B2F   equ   1

// ------------------------------------------------

macro mSelDigitOne
  bcf CA1
  bsf CA0
endm

macro mSelDigitTwo
  bcf CA1
  bsf CA0
endm

macro mNoDigits
  movlw 0xFF
  movwf PORTB
  bsf   CA1
endm

macro mTestButtonOne
  bsf  Buttons, B1F   ; assume down
  btfsc BUTN ; was it up?
    bcf Buttons, B1F  ; if so, correct assumption
endm

macro mTestButtonTwo
  bsf  Buttons, B2F
  btfsc BUTN
    bcf Buttons, B2F
endm

// -----------------------------------------------------

PutDigitOne:
  NoDigits
  movf  SegsOne, W
  movwf PORTB
  SelDigitOne
 return

PutDigitTwo:
  NoDigits
  movf  SegsTwo, W
  movwf PORTB
  SelDigitTwo
 return

ClrPos:
  clrf Pos1
  clrf Pos2
 return


IncPos:     
   incf    Pos1, F     ; inc first place
   movlw   d'10'       ; decimal rollover?
   subwf   Pos1, W     
   btfsc   STATUS, Z   ; if so, do complex 
      goto  IncMore
  return              ; otherwise carry on
IncMore:
   incf    Pos2, F     ; next digit
   clrf    Pos1        ; and clear first digit
  return
   

DecPos:
   iorwf   Pos1, F     ; test for zero
   btfsc   STATUS, Z
     goto DecMore      ; if its zero, do more
   decf    Pos1, F     ; otherwise just dec
  return              ; and return
DecMore:
   movlw   d'9'        ; preset first digit to 9
   movwf   Pos1        
   decf    Pos2, F     ; decrement second place
  return


ConvertDigits:
   movf   Pos1, W
   call   TransDigit
   movwf  SegsOne
   movf   Pos2, W
   call   TransDigit
   movwf  SegsTwo
  return


; test for buttons that just went down.
CheckButnDown:
   movf  Buttons,    W   ; load new button stati
   xorwf OldButtons, W   ; find out what changed
   andwf Buttons,    W   ; mask off things that are now high (hising edges)
   movwf DownedButtons   ; save result

   movf  Buttons, W      ; update history
   movwf OldButtons
 return


init:
  mNoDigits
  call ClrPos

mainloup:
  call PutDigitOne
  mTestButtonOne
  call PutDigitTwo
  mTestButtonTwo
  btfsc DownedButtons, B1F
     call IncPos
  btfsc DownedButtons, B2F
     call DecPos
  call ConvertDigits  

goto mainloup



transdigit:
  addwf PCL, F
  ;        abcdefg
  retlw  b'00000001' ; 0  abcdef
  retlw  b'10011111' ; 1   bc
  retlw  b'00100101' ; 2  ab de g
  retlw  b'00001101' ; 3  abcd  g
  retlw  b'10011001' ; 4   bc  fg
  retlw  b'01001001' ; 5  a cd fg
  retlw  b'01000001' ; 6  a cdefg
  retlw  b'00011011' ; 7  abc  f
  retlw  b'00000000' ; 8  abcdefg
  retlw  b'00001001' ; 9  abcd fg
  retlw  b'11111101' ; -        g  

 

