; Source created with Parsic Version 3.x

; at              : 16.01.2000 15:47:16


; Project File    : E:\DATEN\DELPHI\PARSIC\INTERNET\BSP\BSP1EE84.PIC

; Project Version : 1.0.15

; Created         : 15.06.1999 01:36:00

; Last change     : 16.01.2000 15:45:47

	PROCESSOR 16F84

	INCLUDE P16F84.INC

	RADIX DEC


	__IDLOCS H'0100'

	__CONFIG _CP_OFF & _PWRTE_ON & _WDT_OFF & _XT_OSC

; Diverses

ZERO	EQU Z	; ZERO-Flag
CARRY	 EQU C	; CARRY-Flag


; Variable

ZAEHLER_EE	= 12	; located in Bank 0, 1
EE1_FD	= 13	; located in Bank 0, 1
SYS_TMP1	= 14	; located in Bank 0, 1
MF1	= 15	; located in Bank 0, 1
MF1_HI	= 16	; located in Bank 0, 1
SYS_WSAV	= 17	; located in Bank 0, 1
SYS_SSAV	= 18	; located in Bank 0, 1
S0	= 19	; located in Bank 0, 1
ZAEHLER_EE_FD	= 20	; located in Bank 0, 1
RAM	= 21	; located in Bank 0, 1

	ERRORLEVEL -302	; disable warning (Bankswitching)
	ERRORLEVEL -306	; disable warning (Pageswitching)

;****************************************
;Programmanfang
;****************************************

	ORG 0

	CLRF STATUS	; switch to RAM-Bank 0
	MOVLW HIGH SYS_INIT	; set ROM-Bank
	MOVWF PCLATH
	GOTO SYS_INIT

;****************************************
;Interrupt-Adress
;****************************************

	MOVWF SYS_WSAV	; Register sichern
	SWAPF STATUS,W
	CLRF STATUS	; BANK0
	MOVWF SYS_SSAV

	INCFSZ MF1,F
	GOTO SYS_INT_LABEL_0
	INCF MF1_HI,F

SYS_INT_LABEL_0
	BCF INTCON,T0IF	; Interrupt quitieren

EXITINT

	SWAPF SYS_SSAV,W	; Register zurück
	MOVWF STATUS
	SWAPF SYS_WSAV,F
	SWAPF SYS_WSAV,W

	RETFIE	; RETurn From Interrupt and Enable GIE


;****************************************
; Subroutines
;****************************************


EEINT_WRITE
	MOVFW INDF
EEINT_WRITE_0
	BCF STATUS,RP0	; Bank 0
	MOVWF EEDATA
	BSF STATUS,RP0	; Bank 1
	BSF EECON1,WREN

EEINT_DISINT
	BCF INTCON,GIE
	BTFSC INTCON,GIE
	GOTO EEINT_DISINT

	MOVLW H'55'
	MOVWF EECON2
	MOVLW H'AA'
	MOVWF EECON2

	BSF EECON1,WR	; start write
	BSF INTCON,GIE

EEINT_WRITE_1
	BTFSC EECON1,WR	; ready ?
	GOTO EEINT_WRITE_1	; no
	BCF EECON1,WREN

	BCF STATUS,RP0	; Bank 0
	INCF EEADR,F
	INCF FSR,F
	DECFSZ SYS_TMP1,F
	GOTO EEINT_WRITE

	RETURN

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

EEINT_READ
	BSF STATUS,RP0	; Bank 1
	BSF EECON1,RD
	BCF STATUS,RP0	; Bank 0
	MOVFW EEDATA
	MOVWF INDF
	INCF EEADR,F
	INCF FSR,F
	DECFSZ SYS_TMP1,F
	GOTO EEINT_READ
	RETURN

;***************************************************
; EE-Modul -> EE1
;***************************************************

WRITE_EE1
	MOVLW 0
	BCF STATUS,RP0	; Bank 0
	MOVWF EEADR

	BCF STATUS,IRP	; vars - located in Bank 0
	MOVLW 1
	MOVWF SYS_TMP1
	MOVLW LOW ZAEHLER_EE
	MOVWF FSR

	CALL EEINT_WRITE


	RETURN

;***************************************************
; EE-Modul -> EE1
;***************************************************

READ_EE1
	MOVLW 0
	BCF STATUS,RP0	; Bank 0
	MOVWF EEADR

	BCF STATUS,IRP	; vars - located in Bank 0
	MOVLW 1
	MOVWF SYS_TMP1
	MOVLW LOW ZAEHLER_EE
	MOVWF FSR

	CALL EEINT_READ


	RETURN

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

;****************************************
; Tables
;****************************************


;****************************************
;Initialisierung
;****************************************


SYS_INIT

	MOVLW B'00100000'
	MOVWF INTCON

; Ausgangs-Pegel setzen, bevor die Richtung gesetzt wird

	BCF STATUS,RP0	; Bank 0
	MOVLW B'00000000'
	MOVWF PORTA

	MOVLW B'00000000'
	MOVWF PORTB

	MOVLW 15
	BSF STATUS,RP0	; Bank 1
	MOVWF OPTION_REG


; Richtung setzen

	MOVLW B'11111111'
	MOVWF TRISA

	MOVLW B'00000000'
	MOVWF TRISB

; init var

	CLRF ZAEHLER_EE
	CLRF EE1_FD
	CLRF SYS_TMP1
	CLRF MF1
	CLRF MF1_HI
	CLRF SYS_WSAV
	CLRF SYS_SSAV
	CLRF S0
	CLRF ZAEHLER_EE_FD


	CALL READ_EE1	; read vars from internal EE

	BCF STATUS,RP0	; Bank 0
	CLRF TMR0
	BCF INTCON,T0IF
	BSF INTCON,GIE	; Interrupt freigeben

;**********************************
;******* Haupt-Programm ***********
;**********************************

MAIN

;***************************************************
; Anzug-Verzögerung -> MF1
;***************************************************

	BCF STATUS,RP0	; Bank 0
	BTFSC PORTA,0	; Eingang auf High ?
	GOTO LABEL_MF1_CHKTIME	; Ja

; RESET

	CLRF MF1
	CLRF MF1_HI
	BCF S0,0
	GOTO LABEL_MF1_EXIT	; raus

LABEL_MF1_CHKTIME

	BTFSC S0,0	; Ausgang schon auf High ?
	GOTO LABEL_MF1_EXIT	; Ja

	MOVLW 0
	SUBWF MF1_HI,W
	BTFSS STATUS,CARRY
	GOTO LABEL_MF1_EXIT

	MOVLW 195
	SUBWF MF1,W
	BTFSS STATUS,CARRY
	GOTO LABEL_MF1_EXIT

	BSF S0,0
LABEL_MF1_EXIT

;***************************************************
; Zähler -> ZAEHLER_EE
;***************************************************

	BTFSS S0,1	; Reset ?
	GOTO LABEL_ZAEHLER_EE_COUNT
	CLRF ZAEHLER_EE
	GOTO LABEL_ZAEHLER_EE_SETOUT

LABEL_ZAEHLER_EE_COUNT

; Test auf positive Flanke

	BTFSS S0,0
	GOTO LABEL_ZAEHLER_EE_SETFD
	BTFSC ZAEHLER_EE_FD,0
	GOTO LABEL_ZAEHLER_EE_SETFD

	BSF ZAEHLER_EE_FD,0


	BTFSC S0,1	; Vorwärts oder Rückwärts ?
	GOTO LABEL_ZAEHLER_EE_BACKWARD

	INCF ZAEHLER_EE,F
	GOTO LABEL_ZAEHLER_EE_SETOUT

LABEL_ZAEHLER_EE_BACKWARD
	DECF ZAEHLER_EE,F

LABEL_ZAEHLER_EE_SETOUT

; Output Q0

	BTFSS ZAEHLER_EE,0
	GOTO LABEL_ZAEHLER_EE_SETOUT0
	BCF STATUS,RP0	; Bank 0
	BSF PORTB,0
	GOTO LABEL_ZAEHLER_EE_SETOUT0_END
LABEL_ZAEHLER_EE_SETOUT0
	BCF STATUS,RP0	; Bank 0
	BCF PORTB,0
LABEL_ZAEHLER_EE_SETOUT0_END

; Output Q1

	BTFSS ZAEHLER_EE,1
	GOTO LABEL_ZAEHLER_EE_SETOUT1
	BSF PORTB,1
	GOTO LABEL_ZAEHLER_EE_SETOUT1_END
LABEL_ZAEHLER_EE_SETOUT1
	BCF PORTB,1
LABEL_ZAEHLER_EE_SETOUT1_END

; Output Q2

	BTFSS ZAEHLER_EE,2
	GOTO LABEL_ZAEHLER_EE_SETOUT2
	BSF PORTB,2
	GOTO LABEL_ZAEHLER_EE_SETOUT2_END
LABEL_ZAEHLER_EE_SETOUT2
	BCF PORTB,2
LABEL_ZAEHLER_EE_SETOUT2_END

; Output Q3

	BTFSS ZAEHLER_EE,3
	GOTO LABEL_ZAEHLER_EE_SETOUT3
	BSF PORTB,3
	GOTO LABEL_ZAEHLER_EE_SETOUT3_END
LABEL_ZAEHLER_EE_SETOUT3
	BCF PORTB,3
LABEL_ZAEHLER_EE_SETOUT3_END

; Output Q4

	BTFSS ZAEHLER_EE,4
	GOTO LABEL_ZAEHLER_EE_SETOUT4
	BSF PORTB,4
	GOTO LABEL_ZAEHLER_EE_SETOUT4_END
LABEL_ZAEHLER_EE_SETOUT4
	BCF PORTB,4
LABEL_ZAEHLER_EE_SETOUT4_END

; Output Q5

	BTFSS ZAEHLER_EE,5
	GOTO LABEL_ZAEHLER_EE_SETOUT5
	BSF PORTB,5
	GOTO LABEL_ZAEHLER_EE_SETOUT5_END
LABEL_ZAEHLER_EE_SETOUT5
	BCF PORTB,5
LABEL_ZAEHLER_EE_SETOUT5_END

; Output Q6

	BTFSS ZAEHLER_EE,6
	GOTO LABEL_ZAEHLER_EE_SETOUT6
	BSF PORTB,6
	GOTO LABEL_ZAEHLER_EE_SETOUT6_END
LABEL_ZAEHLER_EE_SETOUT6
	BCF PORTB,6
LABEL_ZAEHLER_EE_SETOUT6_END

; Output Q7

	BTFSS ZAEHLER_EE,7
	GOTO LABEL_ZAEHLER_EE_SETOUT7
	BSF PORTB,7
	GOTO LABEL_ZAEHLER_EE_SETOUT7_END
LABEL_ZAEHLER_EE_SETOUT7
	BCF PORTB,7
LABEL_ZAEHLER_EE_SETOUT7_END


LABEL_ZAEHLER_EE_SETFD

; Test auf negative Flanke

	BTFSC S0,0
	GOTO LABEL_ZAEHLER_EE_EXIT
	BCF ZAEHLER_EE_FD,0

LABEL_ZAEHLER_EE_EXIT

;***************************************************
; GND
;***************************************************

	BCF S0,1	; immer Low

;***************************************************
; EE-Modul -> EE1
;***************************************************


; Test of falling edge

	BTFSC S0,0
	GOTO LABEL_EE1_SETFD
	BTFSS EE1_FD,0
	GOTO LABEL_EE1_SETFD
	BCF EE1_FD,0


	CALL WRITE_EE1

LABEL_EE1_SETFD

; test input

	BTFSS S0,0
	GOTO LABEL_EE1_EXIT
	BSF EE1_FD,0

LABEL_EE1_EXIT

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


	GOTO MAIN

	ORG H'2100'

	DE 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
	DE 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
	DE 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
	DE 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0

	END

