
;**********************************************************************
; Filename: F628a_h_bridge2.asm
; Date:  OCT 2, 2016 
; Author: Lewis Loflin lewis@bvu.net
; http://www.bristolwatch.com/k150/hb2.htm
; PIC16F628A operates a H-Bridge motor comtrol.                                                     
;**********************************************************************


	list      p=16f628A           ; list directive to define processor
	#include <p16F628A.inc>       ; processor specific variable definitions
	errorlevel  -302              ; suppress message 302 from list file

__CONFIG   _CP_OFF & _LVP_OFF & _BOREN_OFF & _MCLRE_OFF & _WDT_OFF & _PWRTE_ON & _INTOSC_OSC_NOCLKOUT 

; for 16F628A only	
; Use  _INTOSC_OSC_NOCLKOUT  for 
; internal 4 mHz osc and no ext reset, use pin RA5 as an input
; Use _HS_OSC for a 16 mHz ext crystal. 
; Use _XT_OSC for 4 mHz ext crystal. Page 95 in spec sheet. 

;INPUTS
SW1		EQU     H'07'		;SW1 is triggering RB7
SW2		EQU     H'06'		;SW2 is triggering RB6
SW3		EQU     H'05'		;SW3 is triggering RB5
SW4		EQU     H'04'		;SW4 is triggering RB4
;OUTPUTS
LD0		EQU	H'00'		;LD1 is connected to RB0
LD1		EQU	H'01'		;LD2 is connected to RB1
LD2		EQU	H'02'		;LD3 is connected to RB2
LD3		EQU	H'03'		;LD4 is connected to RB3


DELAY_count1	EQU H'20'
DELAY_count2	EQU H'21'
DELAY_count3	EQU H'22'


	ORG     0x000             ; processor reset vector
	goto    SETUP              ; go to beginning of program

;**********************************************************************

SETUP ; init PIC16F628A

	movlw	0x07		; Turn comparators off and enable pins for I/O 
	movwf	CMCON
	BSF	STATUS, RP0		    ;Jump to bank 1 of PIC16F84 to access special registers.
	MOVLW	B'00001111'		;Config Port A, Low nibble = Ra0 -> Ra3 are inputs, high nibble not implemented.
	MOVWF	TRISA			;Set I/O configuration for PORTA
	MOVLW	B'11110000'		;Config Port B, Ra7 - RA4 are input, Ra3 - Ra0 are outputs.
	MOVWF	TRISB			;Set I/O configuration for PORTB

	BCF OPTION_REG, 7 ; enable PORTB pull ups
	BCF	STATUS,RP0  ;Jump back to bank 0 of PIC.
	MOVLW 0xF0
	MOVWF PORTB ; pull ups PB4-PB7 on
	CALL OFF ; clear outputs
	GOTO LOOP

LOOP ;Loop starts here !!!

	BTFSS	PORTB, SW1	; Test if SW1 is pressed ?
	GOTO $+2
	GOTO $+3
	CALL OFF
	BSF PORTB, LD0
	
	BTFSS	PORTB, SW2	; Test if SW2 is pressed ?
	GOTO $+2
	GOTO $+3
	CALL OFF
	BSF PORTB, LD1


	BTFSS	PORTB, SW3
	CALL OFF

	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
	CALL DELAY_500mS
	RETURN


	    END  ;End of source code.
