;**************************************************************************
; Input1.ASM
;
; This is a simple 4 bit counter, writing the result to PORTB...
;
; Switches are connected to RA0 and RA1 for up, down...
;
;**************************************************************************

        LIST      P=16F84, R=DEC
        __FUSES   _XT_OSC &  _WDT_OFF & _CP_OFF & _PWRTE_ON
        include   "P16F84.inc"

;--------------------------------------------------------------------------
; Variables
;--------------------------------------------------------------------------

Ram             EQU     h'0C'

Count           EQU     Ram+0



;--------------------------------------------------------------------------
; Program Code
;--------------------------------------------------------------------------

                ORG     0               ;reset vector
Start           call    Init            ;Initialise hardware

                clrf    Count           ;Reset Count

Loop            movfw   Count           ;Move Count into W
                movwf   PORTB           ;Write W to PORTB

;Check for switches...

                btfss   PORTA,0         ;Is "UP" switch pressed?
                incf    Count,f         ;  Yes, so Count = Count + 1

                btfss   PORTA,1         ;Is "UP" switch pressed?
                decf    Count,f         ;  Yes, so Count = Count - 1

;Now check for limits...

                movlw   d'16'           ;
                xorwf   Count,w         ;W = Zero if Count = 16
                btfsc   STATUS,Z        ;Is Z-flag set?
                clrf    Count           ;  Yes, so count had reached 16 - reset
                                        ;  No, ok...

                movlw   d'255'          ;
                xorwf   Count,w         ;W = Zero if Count = 255 ("-1")
                movlw   d'15'           ;Setup W - Note this doesn't affect Z
                btfsc   STATUS,Z        ;Is Z-flag set?
                movwf   Count           ;  Yes, so count had reached "-1"
                                        ;  No, ok...

                goto    Loop            ;Keep on looping...



;--------------------------------------------------------------------------
; Subroutines
;--------------------------------------------------------------------------

;*****Init - set up all ports, make unused ports outputs

Init            clrf    PORTA           ;all of porta low
                clrf    PORTB           ;all of portb low

                bsf     STATUS, RP0     ;change to bank1
                movlw   b'00000011'     ;
                movwf   TRISA           ;all of porta outputs except RA0,1
                clrf    TRISB           ;all of portb outputs
                bcf     STATUS, RP0     ;back to bank0

                return


                END                     ;Stop assembling here

