;**************************************************************************
;*          K8048  Microchip PIC(tm) Programmer & Experiment board        *
;**************************************************************************
;*          (C) VELLEMAN Components,2003  All rights reserved             *
;*          Hardware & sample software by Pascal De Petter 	          *
;**************************************************************************
;*          Hardw. Rev: P8048'1          Softw. Rev:  1.02                *
;*          OSC.......: XT 4MHz Max.     POWER.....:  15V DC              *
;**************************************************************************

;**********************************************************************
; Filename: F84A_hb.asm
; Date:  OCT 2, 2016 
; Author: Lewis Loflin lewis@bvu.net
; http://www.bristolwatch.com/k150/hb1.htm
; PIC16F84A operates a H-Bridge motor comtrol.                                                     
;**********************************************************************
	

;==========================================================================
;
;       Configuration Bits
;
;==========================================================================

_CP_ON                       EQU     H'000F'
_CP_OFF                      EQU     H'3FFF'
_PWRTE_ON                    EQU     H'3FF7'
_PWRTE_OFF                   EQU     H'3FFF'
_WDT_ON                      EQU     H'3FFF'
_WDT_OFF                     EQU     H'3FFB'
_LP_OSC                      EQU     H'3FFC'
_XT_OSC                      EQU     H'3FFD'
_HS_OSC                      EQU     H'3FFE'
_RC_OSC                      EQU     H'3FFF'

	__CONFIG        _CP_OFF & _PWRTE_ON & _WDT_OFF & _XT_OSC 


;==========================================================================
;
;       Register Definitions
;
;==========================================================================

W                            EQU     H'0000'
F                            EQU     H'0001'

;----- Register Files------------------------------------------------------

INDF                         EQU     H'0000'
TMR0                         EQU     H'0001'
PCL                          EQU     H'0002'
STATUS                       EQU     H'0003'
FSR                          EQU     H'0004'
PORTA                        EQU     H'0005'
PORTB                        EQU     H'0006'
EEDATA                       EQU     H'0008'
EEADR                        EQU     H'0009'
PCLATH                       EQU     H'000A'
INTCON                       EQU     H'000B'

OPTION_REG                   EQU     H'0081'
TRISA                        EQU     H'0085'
TRISB                        EQU     H'0086'
EECON1                       EQU     H'0088'
EECON2                       EQU     H'0089'

;----- STATUS Bits --------------------------------------------------------

IRP                          EQU     H'0007'
RP1                          EQU     H'0006'
RP0                          EQU     H'0005'
NOT_TO                       EQU     H'0004'
NOT_PD                       EQU     H'0003'
Z                            EQU     H'0002'
DC                           EQU     H'0001'
C                            EQU     H'0000'

;----- INTCON Bits --------------------------------------------------------

GIE                          EQU     H'0007'
EEIE                         EQU     H'0006'
T0IE                         EQU     H'0005'
INTE                         EQU     H'0004'
RBIE                         EQU     H'0003'
T0IF                         EQU     H'0002'
INTF                         EQU     H'0001'
RBIF                         EQU     H'0000'

;----- OPTION_REG Bits ----------------------------------------------------

NOT_RBPU                     EQU     H'0007'
INTEDG                       EQU     H'0006'
T0CS                         EQU     H'0005'
T0SE                         EQU     H'0004'
PSA                          EQU     H'0003'
PS2                          EQU     H'0002'
PS1                          EQU     H'0001'
PS0                          EQU     H'0000'

;----- EECON1 Bits --------------------------------------------------------

EEIF                         EQU     H'0004'
WRERR                        EQU     H'0003'
WREN                         EQU     H'0002'
WR                           EQU     H'0001'
RD                           EQU     H'0000'

;==========================================================================
;
;       RAM Definition
;
;==========================================================================

        __MAXRAM H'CF'
        __BADRAM H'07', H'50'-H'7F', H'87'

;==========================================================================
;
;       Variables
;
;==========================================================================

;INPUTS
SW1		EQU     H'00'		;SW1 is triggering RA0
SW2		EQU     H'01'		;SW2 is triggering RA1
SW3		EQU     H'02'		;SW3 is triggering RA2
SW4		EQU     H'03'		;SW4 is triggering RA3
;OUTPUTS
LD1		EQU	H'00'		;LD1 is connected to RB0
LD2		EQU	H'01'		;LD2 is connected to RB1
LD3		EQU	H'02'		;LD3 is connected to RB2
LD4		EQU	H'03'		;LD4 is connected to RB3

SW5		EQU H'04'
SW6		EQU H'05'


DELAY_count1	EQU H'0D'
DELAY_count2	EQU H'0E'
DELAY_count3	EQU H'0F'

		ORG	0			;Setting Reset vector, reset vector address is 000h in PIC16F84
		GOTO	SETUP			;Jump to RESET procedure when boot-up PIC device.
	

SETUP						;Program starts here !!!		
		
	BSF	STATUS, RP0		  ;Jump to bank 1 of PIC16F84 to access special registers.
	MOVLW	B'00001111'	  ;Port A - RA0 - RA3 are inputs,
	MOVWF	TRISA		  ;Set I/O configuration for PORTA
	MOVLW	B'11110000'	  ;Port B, RB4 - RB7 are inputs, RB0 - RB3 are outputs.
	MOVWF	TRISB		  ;Set I/O configuration for PORTB
	BCF OPTION_REG, NOT_RBPU  ; enable PORTB pull ups
	
	BCF	STATUS,RP0  ;Jump back to bank 0 of PIC.
	BSF PORTB, SW6 ; pull up on PB5 switch N.O. wired to GND
	CALL OFF ; clear outputs B0-B3
	GOTO LOOP

LOOP ;Loop starts here !!!

	BTFSC	PORTA, SW1	; Test if SW1 is pressed ?
	GOTO $+2 ; yes SW1 LOW skip next line
	GOTO $+3 ; goto next switch check
	CALL OFF ; outputs off delay 500 mS
	BSF PORTB, 0 ; PB0 HIGH
	
	BTFSC	PORTA, SW2	; Test if SW2 is pressed ?
	GOTO $+2 ; yes SW2 LOW
	GOTO $+3 ; no goto next switch check
	CALL OFF ; output off delay 500 mS
	BSF PORTB, 1 ; PB1 HIGH

;	BTFSC	PORTA, SW3	; Test if SW3 is pressed ?
	BTFSS	PORTB, SW6 ; jumpered off board switch
	CALL OFF ; output off delay 500 mS

	GOTO  LOOP



DELAY_500uS
; formula 500uS => DELAY_holdCount1 = (500 - 3)/3 = 166
; time delay = 0 - 771 uS
	MOVLW d'164'
	MOVWF DELAY_count1
	DECFSZ DELAY_count1, f
	GOTO $-1
	RETURN

DELAY_100mS
	MOVLW d'200'
	MOVWF DELAY_count2
	CALL DELAY_500uS
	DECFSZ DELAY_count2
	GOTO $-2
	RETURN

DELAY_500mS
	MOVLW d'5' ; value X 100mS
	MOVWF DELAY_count3
	CALL DELAY_100mS
	DECFSZ DELAY_count3
	GOTO $-2
	RETURN

OFF 
	MOVLW 0xF0 
	ANDWF PORTB, F ; turn PB0-PB3 all LOW
	CALL DELAY_500mS
	RETURN


	    END  ;End of source code.


 

	
