; Source created with Parsic Version 3.x

; at              : 11.06.2004 16:03:38


; Project File    : c:\PROGRA~1\PARSIC\SAYICI~1\SAYICI_9.PIC

; Project Version : 1.0.6

; Created         : 10.06.2004 17:51:00

; Last change     : 11.06.2004 16:03:33

	PROCESSOR 16F84A

	INCLUDE P16F84A.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

SYS_TMP1	= 12	; located in Bank 0, 1
S0	= 13	; located in Bank 0, 1
SYS_WSAV	= 14	; located in Bank 0, 1
SYS_SSAV	= 15	; located in Bank 0, 1
TR1	= 16	; located in Bank 0, 1
TR1_HI	= 17	; located in Bank 0, 1
S1	= 18	; located in Bank 0, 1
ZV1	= 19	; located in Bank 0, 1
ZV1_FD	= 20	; located in Bank 0, 1
ZV2	= 21	; located in Bank 0, 1
ZV2_FD	= 22	; located in Bank 0, 1
ZV3	= 23	; located in Bank 0, 1
ZV3_FD	= 24	; located in Bank 0, 1
ZV4	= 25	; located in Bank 0, 1
ZV4_FD	= 26	; located in Bank 0, 1
ZV5	= 27	; located in Bank 0, 1
ZV5_FD	= 28	; located in Bank 0, 1
TR2	= 29	; located in Bank 0, 1
TR2_HI	= 30	; located in Bank 0, 1
S2	= 31	; located in Bank 0, 1
MF1	= 32	; located in Bank 0, 1
MF1_HI	= 33	; located in Bank 0, 1
MF1_FD	= 34	; located in Bank 0, 1
MF2	= 35	; located in Bank 0, 1
MF2_HI	= 36	; located in Bank 0, 1
MF2_FD	= 37	; located in Bank 0, 1
RAM	= 38	; 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 TR1,F
	GOTO SYS_INT_LABEL_0
	INCF TR1_HI,F

SYS_INT_LABEL_0
	INCFSZ TR2,F
	GOTO SYS_INT_LABEL_1
	INCF TR2_HI,F

SYS_INT_LABEL_1
	INCFSZ MF1,F
	GOTO SYS_INT_LABEL_2
	INCF MF1_HI,F

SYS_INT_LABEL_2
	INCFSZ MF2,F
	GOTO SYS_INT_LABEL_3
	INCF MF2_HI,F

SYS_INT_LABEL_3

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
;****************************************


;****************************************
;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 143
	BSF STATUS,RP0	; Bank 1
	MOVWF OPTION_REG


; Richtung setzen

	MOVLW B'11111111'
	MOVWF TRISA

	MOVLW B'00000000'
	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


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

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

MAIN

;***************************************************
; Multiplexer -> MX1
;***************************************************

	CLRF SYS_TMP1
	MOVFW ZV1
	BTFSS STATUS,ZERO
	GOTO LABEL_MX1_CHECKCONTROL1

	MOVFW ZV2
	MOVWF S0
	GOTO LABEL_MX1_END

LABEL_MX1_CHECKCONTROL1
	INCF SYS_TMP1,F
	MOVFW ZV1
	SUBWF SYS_TMP1,W
	BTFSS STATUS,ZERO
	GOTO LABEL_MX1_CHECKCONTROL2

	MOVFW ZV3
	MOVWF S0
	GOTO LABEL_MX1_END

LABEL_MX1_CHECKCONTROL2
	INCF SYS_TMP1,F
	MOVFW ZV1
	SUBWF SYS_TMP1,W
	BTFSS STATUS,ZERO
	GOTO LABEL_MX1_CHECKCONTROL3

	MOVFW ZV4
	MOVWF S0
	GOTO LABEL_MX1_END

LABEL_MX1_CHECKCONTROL3
	INCF SYS_TMP1,F
	MOVFW ZV1
	SUBWF SYS_TMP1,W
	BTFSS STATUS,ZERO
	GOTO LABEL_MX1_CHECKCONTROL4

	MOVFW ZV5
	MOVWF S0
	GOTO LABEL_MX1_END

LABEL_MX1_CHECKCONTROL4

LABEL_MX1_END

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

	BTFSC S0,0
	GOTO LABEL_BV1_0
	BCF STATUS,RP0	; Bank 0
	BCF PORTB,7
	GOTO LABEL_BV1_0_END
LABEL_BV1_0
	BCF STATUS,RP0	; Bank 0
	BSF PORTB,7
LABEL_BV1_0_END

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

	BTFSC S0,1
	GOTO LABEL_BV1_1
	BCF PORTB,6
	GOTO LABEL_BV1_1_END
LABEL_BV1_1
	BSF PORTB,6
LABEL_BV1_1_END

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

	BTFSC S0,2
	GOTO LABEL_BV1_2
	BCF PORTB,5
	GOTO LABEL_BV1_2_END
LABEL_BV1_2
	BSF PORTB,5
LABEL_BV1_2_END

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

	BTFSC S0,3
	GOTO LABEL_BV1_3
	BCF PORTB,4
	GOTO LABEL_BV1_3_END
LABEL_BV1_3
	BSF PORTB,4
LABEL_BV1_3_END

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


;***************************************************
; Timer -> TR1
;***************************************************

	MOVLW 0	; Test auf >=
	SUBWF TR1_HI,W	;F-W -> W
	BTFSS STATUS,ZERO
	GOTO LABEL_TR1_CHECK
	MOVLW 8
	SUBWF TR1,W	;F-W -> W
LABEL_TR1_CHECK
	BTFSS STATUS,CARRY
	GOTO LABEL_TR1_ENDE

LABEL_TR1_RESET
	MOVLW 0	; Test auf >=
	SUBWF TR1_HI,F	;F-W -> F
	MOVLW 8
	SUBWF TR1,F	;F-W -> F
	BTFSS STATUS,CARRY
	DECF TR1_HI,F

LABEL_TR1_SET	; toggel output
	BTFSC S1,0
	GOTO LABEL_TR1_CLR
	BSF S1,0
	GOTO LABEL_TR1_ENDE
LABEL_TR1_CLR
	BCF S1,0
LABEL_TR1_ENDE

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

	BTFSS S1,2	; Reset ?
	GOTO LABEL_ZV1_COUNT
	CLRF ZV1
	GOTO LABEL_ZV1_SETOUT

LABEL_ZV1_COUNT

; Test auf positive Flanke

	BTFSS S1,0
	GOTO LABEL_ZV1_SETFD
	BTFSC ZV1_FD,0
	GOTO LABEL_ZV1_SETFD

	BSF ZV1_FD,0


	BTFSC S1,1	; Vorwärts oder Rückwärts ?
	GOTO LABEL_ZV1_BACKWARD

	INCF ZV1,F
	GOTO LABEL_ZV1_SETOUT

LABEL_ZV1_BACKWARD
	DECF ZV1,F

LABEL_ZV1_SETOUT


LABEL_ZV1_SETFD

; Test auf negative Flanke

	BTFSC S1,0
	GOTO LABEL_ZV1_EXIT
	BCF ZV1_FD,0

LABEL_ZV1_EXIT

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

	BCF S1,1	; immer Low

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

	MOVLW 0
	SUBWF ZV1,W	; F - W -> W

LABEL_IF1_CHECK
	BCF STATUS,RP0	; Bank 0
	BTFSS STATUS,ZERO	; Test auf -> =
	GOTO LABEL_IF1_FALSE

LABEL_IF1_TRUE
	BSF PORTB,0
	GOTO LABEL_IF1_EXIT

LABEL_IF1_FALSE
	BCF PORTB,0

LABEL_IF1_EXIT
;***************************************************
; Vergleich -> IF2
;***************************************************

	MOVLW 1
	SUBWF ZV1,W	; F - W -> W

LABEL_IF2_CHECK
	BCF STATUS,RP0	; Bank 0
	BTFSS STATUS,ZERO	; Test auf -> =
	GOTO LABEL_IF2_FALSE

LABEL_IF2_TRUE
	BSF PORTB,1
	GOTO LABEL_IF2_EXIT

LABEL_IF2_FALSE
	BCF PORTB,1

LABEL_IF2_EXIT
;***************************************************
; Vergleich -> IF3
;***************************************************

	MOVLW 2
	SUBWF ZV1,W	; F - W -> W

LABEL_IF3_CHECK
	BCF STATUS,RP0	; Bank 0
	BTFSS STATUS,ZERO	; Test auf -> =
	GOTO LABEL_IF3_FALSE

LABEL_IF3_TRUE
	BSF PORTB,2
	GOTO LABEL_IF3_EXIT

LABEL_IF3_FALSE
	BCF PORTB,2

LABEL_IF3_EXIT
;***************************************************
; Vergleich -> IF4
;***************************************************

	MOVLW 3
	SUBWF ZV1,W	; F - W -> W

LABEL_IF4_CHECK
	BCF STATUS,RP0	; Bank 0
	BTFSS STATUS,ZERO	; Test auf -> =
	GOTO LABEL_IF4_FALSE

LABEL_IF4_TRUE
	BSF PORTB,3
	GOTO LABEL_IF4_EXIT

LABEL_IF4_FALSE
	BCF PORTB,3

LABEL_IF4_EXIT
;***************************************************
; Zähler -> ZV2
;***************************************************

	BTFSS S1,3	; Reset ?
	GOTO LABEL_ZV2_COUNT
	CLRF ZV2
	GOTO LABEL_ZV2_SETOUT

LABEL_ZV2_COUNT

; Test auf positive Flanke

	BTFSS S2,5
	GOTO LABEL_ZV2_SETFD
	BTFSC ZV2_FD,0
	GOTO LABEL_ZV2_SETFD

	BSF ZV2_FD,0


	BTFSC S2,2	; Vorwärts oder Rückwärts ?
	GOTO LABEL_ZV2_BACKWARD

	INCF ZV2,F
	GOTO LABEL_ZV2_SETOUT

LABEL_ZV2_BACKWARD
	DECF ZV2,F

LABEL_ZV2_SETOUT


LABEL_ZV2_SETFD

; Test auf negative Flanke

	BTFSC S2,5
	GOTO LABEL_ZV2_EXIT
	BCF ZV2_FD,0

LABEL_ZV2_EXIT

;***************************************************
; Zähler -> ZV3
;***************************************************

	BTFSS S1,4	; Reset ?
	GOTO LABEL_ZV3_COUNT
	CLRF ZV3
	GOTO LABEL_ZV3_SETOUT

LABEL_ZV3_COUNT

; Test auf positive Flanke

	BTFSS S1,3
	GOTO LABEL_ZV3_SETFD
	BTFSC ZV3_FD,0
	GOTO LABEL_ZV3_SETFD

	BSF ZV3_FD,0


	BTFSC S2,1	; Vorwärts oder Rückwärts ?
	GOTO LABEL_ZV3_BACKWARD

	INCF ZV3,F
	GOTO LABEL_ZV3_SETOUT

LABEL_ZV3_BACKWARD
	DECF ZV3,F

LABEL_ZV3_SETOUT


LABEL_ZV3_SETFD

; Test auf negative Flanke

	BTFSC S1,3
	GOTO LABEL_ZV3_EXIT
	BCF ZV3_FD,0

LABEL_ZV3_EXIT

;***************************************************
; Zähler -> ZV4
;***************************************************

	BTFSS S1,5	; Reset ?
	GOTO LABEL_ZV4_COUNT
	CLRF ZV4
	GOTO LABEL_ZV4_SETOUT

LABEL_ZV4_COUNT

; Test auf positive Flanke

	BTFSS S1,4
	GOTO LABEL_ZV4_SETFD
	BTFSC ZV4_FD,0
	GOTO LABEL_ZV4_SETFD

	BSF ZV4_FD,0


	BTFSC S2,0	; Vorwärts oder Rückwärts ?
	GOTO LABEL_ZV4_BACKWARD

	INCF ZV4,F
	GOTO LABEL_ZV4_SETOUT

LABEL_ZV4_BACKWARD
	DECF ZV4,F

LABEL_ZV4_SETOUT


LABEL_ZV4_SETFD

; Test auf negative Flanke

	BTFSC S1,4
	GOTO LABEL_ZV4_EXIT
	BCF ZV4_FD,0

LABEL_ZV4_EXIT

;***************************************************
; Zähler -> ZV5
;***************************************************

	BTFSS S1,6	; Reset ?
	GOTO LABEL_ZV5_COUNT
	CLRF ZV5
	GOTO LABEL_ZV5_SETOUT

LABEL_ZV5_COUNT

; Test auf positive Flanke

	BTFSS S1,5
	GOTO LABEL_ZV5_SETFD
	BTFSC ZV5_FD,0
	GOTO LABEL_ZV5_SETFD

	BSF ZV5_FD,0


	BTFSC S1,7	; Vorwärts oder Rückwärts ?
	GOTO LABEL_ZV5_BACKWARD

	INCF ZV5,F
	GOTO LABEL_ZV5_SETOUT

LABEL_ZV5_BACKWARD
	DECF ZV5,F

LABEL_ZV5_SETOUT


LABEL_ZV5_SETFD

; Test auf negative Flanke

	BTFSC S1,5
	GOTO LABEL_ZV5_EXIT
	BCF ZV5_FD,0

LABEL_ZV5_EXIT

;***************************************************
; Timer -> TR2
;***************************************************

	MOVLW 7	; Test auf >=
	SUBWF TR2_HI,W	;F-W -> W
	BTFSS STATUS,ZERO
	GOTO LABEL_TR2_CHECK
	MOVLW 161
	SUBWF TR2,W	;F-W -> W
LABEL_TR2_CHECK
	BTFSS STATUS,CARRY
	GOTO LABEL_TR2_ENDE

LABEL_TR2_RESET
	MOVLW 7	; Test auf >=
	SUBWF TR2_HI,F	;F-W -> F
	MOVLW 161
	SUBWF TR2,F	;F-W -> F
	BTFSS STATUS,CARRY
	DECF TR2_HI,F

LABEL_TR2_SET	; toggel output
	BTFSC S2,4
	GOTO LABEL_TR2_CLR
	BSF S2,4
	GOTO LABEL_TR2_ENDE
LABEL_TR2_CLR
	BCF S2,4
LABEL_TR2_ENDE

;***************************************************
; Vergleich -> IF5
;***************************************************

	MOVLW 9
	SUBWF ZV2,W	; F - W -> W

LABEL_IF5_CHECK
	BTFSS STATUS,CARRY	; Test auf -> >
	GOTO LABEL_IF5_FALSE
	BTFSC STATUS,ZERO
	GOTO LABEL_IF5_FALSE

LABEL_IF5_TRUE
	BSF S1,3
	GOTO LABEL_IF5_EXIT

LABEL_IF5_FALSE
	BCF S1,3

LABEL_IF5_EXIT
;***************************************************
; Vergleich -> IF6
;***************************************************

	MOVLW 9
	SUBWF ZV3,W	; F - W -> W

LABEL_IF6_CHECK
	BTFSS STATUS,CARRY	; Test auf -> >
	GOTO LABEL_IF6_FALSE
	BTFSC STATUS,ZERO
	GOTO LABEL_IF6_FALSE

LABEL_IF6_TRUE
	BSF S1,4
	GOTO LABEL_IF6_EXIT

LABEL_IF6_FALSE
	BCF S1,4

LABEL_IF6_EXIT
;***************************************************
; Vergleich -> IF7
;***************************************************

	MOVLW 9
	SUBWF ZV4,W	; F - W -> W

LABEL_IF7_CHECK
	BTFSS STATUS,CARRY	; Test auf -> >
	GOTO LABEL_IF7_FALSE
	BTFSC STATUS,ZERO
	GOTO LABEL_IF7_FALSE

LABEL_IF7_TRUE
	BSF S1,5
	GOTO LABEL_IF7_EXIT

LABEL_IF7_FALSE
	BCF S1,5

LABEL_IF7_EXIT
;***************************************************
; Vergleich -> IF8
;***************************************************

	MOVLW 9
	SUBWF ZV5,W	; F - W -> W

LABEL_IF8_CHECK
	BTFSS STATUS,CARRY	; Test auf -> >
	GOTO LABEL_IF8_FALSE
	BTFSC STATUS,ZERO
	GOTO LABEL_IF8_FALSE

LABEL_IF8_TRUE
	BSF S1,6
	GOTO LABEL_IF8_EXIT

LABEL_IF8_FALSE
	BCF S1,6

LABEL_IF8_EXIT
;***************************************************
; GND
;***************************************************

	BCF S2,2	; immer Low

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

	BCF S2,1	; immer Low

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

	BCF S2,0	; immer Low

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

	BCF S1,7	; immer Low

;***************************************************
; Vergleich -> IF9
;***************************************************

	MOVLW 3
	SUBWF ZV1,W	; F - W -> W

LABEL_IF9_CHECK
	BTFSS STATUS,CARRY	; Test auf -> >
	GOTO LABEL_IF9_FALSE
	BTFSC STATUS,ZERO
	GOTO LABEL_IF9_FALSE

LABEL_IF9_TRUE
	BSF S1,2
	GOTO LABEL_IF9_EXIT

LABEL_IF9_FALSE
	BCF S1,2

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

	BTFSC S2,7
	GOTO LABEL_RS1_RESET
	BTFSS S2,6
	GOTO LABEL_RS1_ENDE
	BSF S2,3	; Ausgang setzen
	GOTO LABEL_RS1_ENDE
LABEL_RS1_RESET
	BCF S2,3	; Ausgang zurücksetzen
LABEL_RS1_ENDE

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

LABEL_LG1_1
	BTFSS S2,4
	GOTO LABEL_LG1_FALSE
LABEL_LG1_2
	BTFSS S2,3
	GOTO LABEL_LG1_FALSE

LABEL_LG1_TRUE
	BSF S2,5
	GOTO LABEL_LG1_ENDE
LABEL_LG1_FALSE
	BCF S2,5
LABEL_LG1_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
	BCF STATUS,RP0	; Bank 0
	BTFSS PORTA,3
	GOTO LABEL_MF1_SETFD


LABEL_MF1_RESET

	CLRF MF1
	CLRF MF1_HI
	BSF MF1_FD,1
	BSF S2,6

LABEL_MF1_CHKTIME

	MOVLW 0
	SUBWF MF1_HI,W
	BTFSS STATUS,ZERO
	GOTO LABEL_MF1_CHECK
	MOVLW 195
	SUBWF MF1,W
LABEL_MF1_CHECK
	BTFSS STATUS,CARRY
	GOTO LABEL_MF1_SETFD
LABEL_MF1_TO

	BCF MF1_FD,1
	BCF S2,6

LABEL_MF1_SETFD

	BCF STATUS,RP0	; Bank 0
	BTFSS PORTA,3
	GOTO LABEL_MF1_CLR
	BSF MF1_FD,0
	GOTO LABEL_MF1_ENDE
LABEL_MF1_CLR
	BCF MF1_FD,0

LABEL_MF1_ENDE

;***************************************************
; Mono-Flop -> MF2
;***************************************************

; Läuft der Impuls ?

	BTFSC MF2_FD,1
	GOTO LABEL_MF2_CHKTIME

; Test auf positive Flanke

	BTFSC MF2_FD,0
	GOTO LABEL_MF2_SETFD
	BCF STATUS,RP0	; Bank 0
	BTFSS PORTA,2
	GOTO LABEL_MF2_SETFD


LABEL_MF2_RESET

	CLRF MF2
	CLRF MF2_HI
	BSF MF2_FD,1
	BSF S2,7

LABEL_MF2_CHKTIME

	MOVLW 0
	SUBWF MF2_HI,W
	BTFSS STATUS,ZERO
	GOTO LABEL_MF2_CHECK
	MOVLW 195
	SUBWF MF2,W
LABEL_MF2_CHECK
	BTFSS STATUS,CARRY
	GOTO LABEL_MF2_SETFD
LABEL_MF2_TO

	BCF MF2_FD,1
	BCF S2,7

LABEL_MF2_SETFD

	BCF STATUS,RP0	; Bank 0
	BTFSS PORTA,2
	GOTO LABEL_MF2_CLR
	BSF MF2_FD,0
	GOTO LABEL_MF2_ENDE
LABEL_MF2_CLR
	BCF MF2_FD,0

LABEL_MF2_ENDE

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

	GOTO MAIN


	END

