; Source created with Parsic Version 3.x

; at              : 25.06.2004 17:43:09


; Project File    : c:\BELGEL~1\PARSIC\ÝLERÝ_~1\I__GERI.PIC

; Project Version : 1.0.23

; Created         : 17.05.2004 20:29:00

; Last change     : 25.06.2004 17:43:07

	PROCESSOR 16F84

	INCLUDE P16F84.INC

	RADIX DEC


	__IDLOCS H'0100'

	__CONFIG _CP_OFF & _PWRTE_ON & _WDT_OFF & _RC_OSC

; Diverses

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


; Variable

MF1_HI	= 12	; located in Bank 0, 1
S0_HI	= 13	; located in Bank 0, 1
ZV1_HI	= 14	; located in Bank 0, 1
EE1_FD	= 15	; located in Bank 0, 1
SYS_TMP1	= 16	; located in Bank 0, 1
SYS_TMP2	= 17	; located in Bank 0, 1
S0	= 18	; located in Bank 0, 1
MF1	= 19	; located in Bank 0, 1
SYS_WSAV	= 20	; located in Bank 0, 1
SYS_SSAV	= 21	; located in Bank 0, 1
MF1_FD	= 22	; located in Bank 0, 1
ZV1	= 23	; located in Bank 0, 1
ZV1_FD	= 24	; located in Bank 0, 1
SYS_TMP3	= 25	; located in Bank 0, 1
SYS_TMP4	= 26	; located in Bank 0, 1
SYS_TMP5	= 27	; located in Bank 0, 1
SYS_TMP6	= 28	; located in Bank 0, 1
SYS_TMP7	= 29	; located in Bank 0, 1
RAM	= 30	; 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
	GOTO SYS_INIT
	NOP
	NOP

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

	MOVWF SYS_WSAV	; save register
	SWAPF STATUS,W
	CLRF STATUS	; bank 0
	MOVWF SYS_SSAV

	BCF INTCON,T0IF	; Interrupt quitieren

	INCFSZ MF1,F
	GOTO SYS_INT_LABEL_0
	INCF MF1_HI,F

SYS_INT_LABEL_0

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


;****************************************
; Multiplikation 16 - Bit
; OP1 = SYS_TMP1,2
; OP2 = SYS_TMP3,4
;****************************************

SYSMUL16
	MOVLW 16	; set loopcounter
	MOVWF SYS_TMP7

	CLRF SYS_TMP5	; clear result
	CLRF SYS_TMP6

SYSMUL16_1
	RRF SYS_TMP4,F
	RRF SYS_TMP3,F

	BTFSS STATUS,CARRY
	GOTO SYSMUL16_2
	MOVFW SYS_TMP2	; add High-Byte
	ADDWF SYS_TMP6,F
	MOVFW SYS_TMP1	; add Low-Byte
	ADDWF SYS_TMP5,F
	BTFSC STATUS,CARRY	; carry ?
	INCF SYS_TMP6,F	; add 256 to result

SYSMUL16_2
	BCF STATUS,CARRY
	RLF SYS_TMP1,F
	RLF SYS_TMP2,F
	DECFSZ SYS_TMP7,F

	GOTO SYSMUL16_1

	RETURN

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


EEINT_WRITE
	MOVFW INDF
	ADDWF SYS_TMP2,F
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
	INCF EEADR,F
	CLRF SYS_TMP2

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

	CALL EEINT_WRITE

	MOVLW 0	; write checksum
	BCF STATUS,RP0	; Bank 0
	MOVWF EEADR
	INCF SYS_TMP1,F
	MOVLW LOW SYS_TMP2
	MOVWF FSR
	MOVFW INDF
	CALL EEINT_WRITE_0


	RETURN

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

READ_EE1
	MOVLW 0
	BCF STATUS,RP0	; Bank 0
	MOVWF EEADR
	INCF EEADR,F	; test checksum
	CLRF SYS_TMP2
	MOVLW 3
	MOVWF SYS_TMP1

READ_EE1_0
	BSF STATUS,RP0	; Bank 1
	BSF EECON1,RD
	BCF STATUS,RP0	; Bank 0
	MOVFW EEDATA
	ADDWF SYS_TMP2,F
	INCF EEADR,F
	DECFSZ SYS_TMP1,F
	GOTO READ_EE1_0

	MOVLW 0
	MOVWF EEADR
	BSF STATUS,RP0	; Bank 1
	BSF EECON1,RD
	BCF STATUS,RP0	; Bank 0
	MOVFW EEDATA
	SUBWF SYS_TMP2,W
	BTFSS STATUS,ZERO
	RETURN	; Checksum error

	INCF EEADR,F

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

	CALL EEINT_READ


	RETURN

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

;****************************************
;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'00001111'
	MOVWF TRISB

; init vars


; Clear RAM

	MOVLW 12
	MOVWF FSR
CR1
	CLRF INDF
	INCF FSR,F
	MOVFW FSR
	SUBLW 80
	BTFSS STATUS,ZERO
	GOTO CR1



	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

;***************************************************
; RS-FLIP-FLOP -> RS1
;***************************************************

	BCF STATUS,RP0	; Bank 0
	BTFSC PORTA,2
	GOTO LABEL_RS1_RESET
	BTFSS PORTA,4
	GOTO LABEL_RS1_ENDE
	BSF S0,0	; Ausgang setzen
	GOTO LABEL_RS1_ENDE
LABEL_RS1_RESET
	BCF S0,0	; Ausgang zurücksetzen
LABEL_RS1_ENDE

;***************************************************
; UND - Gatter -> LG1
;***************************************************

LABEL_LG1_1
	BTFSS S0,0
	GOTO LABEL_LG1_FALSE
LABEL_LG1_2
	BCF STATUS,RP0	; Bank 0
	BTFSC PORTB,4
	GOTO LABEL_LG1_FALSE

LABEL_LG1_TRUE
	BSF PORTB,7
	GOTO LABEL_LG1_ENDE
LABEL_LG1_FALSE
	BCF STATUS,RP0	; Bank 0
	BCF PORTB,7
LABEL_LG1_ENDE
;***************************************************
; UND - Gatter -> LG2
;***************************************************

LABEL_LG2_1
	BTFSC PORTB,7
	GOTO LABEL_LG2_FALSE
LABEL_LG2_2
	BTFSS S0,1
	GOTO LABEL_LG2_FALSE

LABEL_LG2_TRUE
	BSF PORTB,4
	GOTO LABEL_LG2_ENDE
LABEL_LG2_FALSE
	BCF STATUS,RP0	; Bank 0
	BCF PORTB,4
LABEL_LG2_ENDE
;***************************************************
; RS-FLIP-FLOP -> RS2
;***************************************************

	BTFSC PORTA,2
	GOTO LABEL_RS2_RESET
	BTFSS PORTA,3
	GOTO LABEL_RS2_ENDE
	BSF S0,1	; Ausgang setzen
	GOTO LABEL_RS2_ENDE
LABEL_RS2_RESET
	BCF S0,1	; Ausgang zurücksetzen
LABEL_RS2_ENDE

;***************************************************
; ODER - Gatter -> LG3
;***************************************************

LABEL_LG3_1
	BCF STATUS,RP0	; Bank 0
	BTFSC PORTB,7
	GOTO LABEL_LG3_TRUE
LABEL_LG3_2
	BTFSC PORTB,4
	GOTO LABEL_LG3_TRUE

LABEL_LG3_FALSE
	BCF S0,2
	GOTO LABEL_LG3_ENDE
LABEL_LG3_TRUE
	BSF S0,2
LABEL_LG3_ENDE
;***************************************************
; UND - Gatter -> LG4
;***************************************************

LABEL_LG4_1
	BTFSS S0,2
	GOTO LABEL_LG4_FALSE
LABEL_LG4_2
	BTFSS S0,3
	GOTO LABEL_LG4_FALSE

LABEL_LG4_TRUE
	BCF STATUS,RP0	; Bank 0
	BSF PORTB,6
	GOTO LABEL_LG4_ENDE
LABEL_LG4_FALSE
	BCF STATUS,RP0	; Bank 0
	BCF PORTB,6
LABEL_LG4_ENDE
;***************************************************
; UND - Gatter -> LG5
;***************************************************

LABEL_LG5_1
	BTFSC S0,3
	GOTO LABEL_LG5_FALSE
LABEL_LG5_2
	BTFSS S0,2
	GOTO LABEL_LG5_FALSE

LABEL_LG5_TRUE
	BSF PORTB,5
	GOTO LABEL_LG5_ENDE
LABEL_LG5_FALSE
	BCF STATUS,RP0	; Bank 0
	BCF PORTB,5
LABEL_LG5_ENDE
;***************************************************
; Mono-Flop -> MF1
;***************************************************

; Läuft der Impuls ?

	BTFSC MF1_FD,1
	GOTO LABEL_MF1_CHKTIME

; Test auf positive Flanke

	BTFSC MF1_FD,0
	GOTO LABEL_MF1_SETFD
	BTFSS S0,2
	GOTO LABEL_MF1_SETFD


LABEL_MF1_RESET

	CLRF MF1
	CLRF MF1_HI
	BSF MF1_FD,1
	BSF S0,3

LABEL_MF1_CHKTIME

	MOVFW S0_HI
	SUBWF MF1_HI,W
	BTFSS STATUS,ZERO	; Schon drüber ?
	GOTO LABEL_MF1_CHECK
	MOVFW S0
	SUBWF MF1,W
LABEL_MF1_CHECK
	BTFSS STATUS,CARRY	; Schon drüber ?
	GOTO LABEL_MF1_SETFD
LABEL_MF1_TO

	BCF MF1_FD,1
	BCF S0,3

LABEL_MF1_SETFD

	BTFSS S0,2
	GOTO LABEL_MF1_CLR
	BSF MF1_FD,0
	GOTO LABEL_MF1_ENDE
LABEL_MF1_CLR
	BCF MF1_FD,0

LABEL_MF1_ENDE

;***************************************************
; Zähler -> ZV1
;***************************************************

	BTFSS S0,5	; Reset ?
	GOTO LABEL_ZV1_COUNT
	CLRF ZV1
	CLRF ZV1_HI
	GOTO LABEL_ZV1_SETOUT

LABEL_ZV1_COUNT

; Test auf positive Flanke

	BCF STATUS,RP0	; Bank 0
	BTFSS PORTB,0
	GOTO LABEL_ZV1_SETFD
	BTFSC ZV1_FD,0
	GOTO LABEL_ZV1_SETFD

	BSF ZV1_FD,0


	BTFSC S0,4	; Vorwärts oder Rückwärts ?
	GOTO LABEL_ZV1_BACKWARD

	INCFSZ ZV1,F
	GOTO LABEL_ZV1_SETOUT
	INCF ZV1_HI,F
	GOTO LABEL_ZV1_SETOUT

LABEL_ZV1_BACKWARD
	DECF ZV1,F
	INCFSZ ZV1,W
	GOTO LABEL_ZV1_SETOUT
	DECF ZV1_HI,F

LABEL_ZV1_SETOUT


LABEL_ZV1_SETFD

; Test auf negative Flanke

	BCF STATUS,RP0	; Bank 0
	BTFSC PORTB,0
	GOTO LABEL_ZV1_EXIT
	BCF ZV1_FD,0

LABEL_ZV1_EXIT

;***************************************************
;AF1
;***************************************************

	MOVFW ZV1
	MOVWF SYS_TMP1
	MOVFW ZV1_HI
	MOVWF SYS_TMP2
	MOVLW LOW 1000
	MOVWF SYS_TMP3
	MOVLW HIGH 1000
	MOVWF SYS_TMP4
	CALL SYSMUL16
	MOVFW SYS_TMP5
	MOVWF S0
	MOVFW SYS_TMP6
	MOVWF S0_HI

LABEL_AF1_EXIT
;***************************************************
; GND
;***************************************************

	BCF S0,4	; immer Low

;***************************************************
; Vergleich -> IF1
;***************************************************

	MOVLW HIGH 20
	SUBWF ZV1_HI,W	; F - W -> W

	BTFSS STATUS,ZERO	; Wenn ZERO gesetzt LOW-Byte testen
	GOTO LABEL_IF1_CHECK
	MOVLW LOW 20
	SUBWF ZV1,W	; F - W -> W

LABEL_IF1_CHECK
	BTFSS STATUS,CARRY	; Test auf -> >
	GOTO LABEL_IF1_FALSE
	BTFSC STATUS,ZERO
	GOTO LABEL_IF1_FALSE

LABEL_IF1_TRUE
	BSF S0,5
	GOTO LABEL_IF1_EXIT

LABEL_IF1_FALSE
	BCF S0,5

LABEL_IF1_EXIT
;***************************************************
; Verteiler -> BV1
;***************************************************

	BTFSC ZV1,0
	GOTO LABEL_BV1_0
	BCF S0,6
	GOTO LABEL_BV1_0_END
LABEL_BV1_0
	BSF S0,6
LABEL_BV1_0_END

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


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


; Test of rising edge

	BTFSS S0,6
	GOTO LABEL_EE1_SETFD
	BTFSC EE1_FD,0
	GOTO LABEL_EE1_SETFD
	BSF EE1_FD,0


	CALL WRITE_EE1

LABEL_EE1_SETFD

; test input

	BTFSC S0,6
	GOTO LABEL_EE1_EXIT
	BCF 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

