; Source created with Parsic Version 3.x

; at              : 16.01.2000 15:19:52


; Project File    : E:\DATEN\MPLAB\HOLM\NIVEAUV2.PIC

; Project Version : 1.0.18

; Created         : 17.08.1999 18:52:00

; Last change     : 16.01.2000 15:11:22

               PROCESSOR 16C711

               INCLUDE P16C711.INC

               RADIX DEC


               __IDLOCS H'0100'

               __CONFIG _CP_OFF & _PWRTE_ON & _WDT_ON & _BODEN_ON & _XT_OSC

; Diverses

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


; Variable

ADC0               = 12               ; located in Bank 0, 1
SYS_TMP1               = 13               ; located in Bank 0, 1
ADC1               = 14               ; located in Bank 0, 1
ADC2               = 15               ; located in Bank 0, 1
ADC3               = 16               ; located in Bank 0, 1
S0               = 17               ; located in Bank 0, 1
SYS_WSAV               = 18               ; located in Bank 0, 1
SYS_SSAV               = 19               ; located in Bank 0, 1
TR1               = 20               ; located in Bank 0, 1
TR1_HI               = 21               ; located in Bank 0, 1
S1               = 22               ; located in Bank 0, 1
ZV1               = 23               ; located in Bank 0, 1
ZV1_HI               = 24               ; located in Bank 0, 1
ZV1_FD               = 25               ; located in Bank 0, 1
MF1               = 26               ; located in Bank 0, 1
MF1_HI               = 27               ; located in Bank 0, 1
ZV2               = 28               ; located in Bank 0, 1
ZV2_HI               = 29               ; located in Bank 0, 1
ZV2_FD               = 30               ; located in Bank 0, 1
BSTUNDEN               = 31               ; located in Bank 0, 1
BSTUNDEN_HI               = 32               ; located in Bank 0, 1
SYS_TMP2               = 33               ; located in Bank 0, 1
SYS_TMP3               = 34               ; located in Bank 0, 1
SYS_TMP4               = 35               ; located in Bank 0, 1
SYS_TMP5               = 36               ; located in Bank 0, 1
SYS_TMP6               = 37               ; located in Bank 0, 1
SYS_TMP7               = 38               ; located in Bank 0, 1
TR2               = 39               ; located in Bank 0, 1
TR2_HI               = 40               ; located in Bank 0, 1
RAM               = 41               ; 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 TR1,F
               GOTO SYS_INT_LABEL_0
               INCF TR1_HI,F

SYS_INT_LABEL_0
               INCFSZ MF1,F
               GOTO SYS_INT_LABEL_1
               INCF MF1_HI,F

SYS_INT_LABEL_1
               INCFSZ TR2,F
               GOTO SYS_INT_LABEL_2
               INCF TR2_HI,F

SYS_INT_LABEL_2
               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


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

SYSDIV16
               MOVFW SYS_TMP3               ; Division durch 0 ?
               IORWF SYS_TMP4,W
               BTFSC STATUS,ZERO
               GOTO SYSDIV16_ERROR

               CLRF SYS_TMP5               ; Ergebnis löschen
               CLRF SYS_TMP6
               MOVLW 1
               MOVWF SYS_TMP7
SYSDIV16_0
               BTFSC SYS_TMP4,7               ; OP2 linksbündig
               GOTO SYSDIV16_1
               INCF SYS_TMP7,F
               BCF STATUS,CARRY
               RLF SYS_TMP3,F
               RLF SYS_TMP4,F
               GOTO SYSDIV16_0

SYSDIV16_1
               MOVFW SYS_TMP4               ; OP1 und OP2 vergleichen
               SUBWF SYS_TMP2,W               ; F-W -> W
               BTFSS STATUS,ZERO
               GOTO SYSDIV16_2
               MOVFW SYS_TMP3
               SUBWF SYS_TMP1,W               ; F-W -> W

SYSDIV16_2
               BTFSS STATUS,CARRY
               GOTO SYSDIV16_3
               MOVFW SYS_TMP4               ; OP1-OP2
               SUBWF SYS_TMP2,F               ; F-W -> F
               MOVFW SYS_TMP3
               SUBWF SYS_TMP1,F               ; F-W -> F
               BTFSS STATUS,CARRY
               DECF SYS_TMP2,F
               BSF STATUS,CARRY
SYSDIV16_3
               RLF SYS_TMP5,F               ; Ergebnis aktualisieren
               RLF SYS_TMP6,F
               RRF SYS_TMP4,F               ; OP2 nach rechts schieben
               RRF SYS_TMP3,F
               DECFSZ SYS_TMP7,F               ; Zähler -1
               GOTO SYSDIV16_1

               RETURN

SYSDIV16_ERROR
               MOVLW 255
               MOVWF SYS_TMP5
               MOVWF SYS_TMP6
               RETURN


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


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

               MOVLW 3               ; all digital
               MOVWF ADCON1

; Richtung setzen

               MOVLW B'11111111'
               MOVWF TRISA

               MOVLW B'10000111'
               MOVWF TRISB

; init var

               CLRF ADC0
               CLRF SYS_TMP1
               CLRF ADC1
               CLRF ADC2
               CLRF ADC3
               CLRF S0
               CLRF SYS_WSAV
               CLRF SYS_SSAV
               CLRF TR1
               CLRF TR1_HI
               CLRF S1
               CLRF ZV1
               CLRF ZV1_HI
               CLRF ZV1_FD
               CLRF MF1
               CLRF MF1_HI
               CLRF ZV2
               CLRF ZV2_HI
               CLRF ZV2_FD
               CLRF BSTUNDEN
               CLRF BSTUNDEN_HI
               CLRF SYS_TMP2
               CLRF SYS_TMP3
               CLRF SYS_TMP4
               CLRF SYS_TMP5
               CLRF SYS_TMP6
               CLRF SYS_TMP7
               CLRF TR2
               CLRF TR2_HI

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

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

MAIN

               CLRWDT

;***************************************************
; Read  -> AN0
;***************************************************

               BSF STATUS,RP0               ; Bank 1
               BCF ADCON1,0
               BSF TRISA,0
               MOVLW B'11000001'               ; CH0
               BCF STATUS,RP0               ; Bank 0
               MOVWF ADCON0
               MOVLW 5
               MOVWF SYS_TMP1

SYS_ADC_LABEL_0
               DECFSZ SYS_TMP1,F
               GOTO SYS_ADC_LABEL_0

               BSF ADCON0,GO               ; Wandlung starten

SYS_ADC_LABEL_1

               BTFSC ADCON0,GO               ; Schon fertig ?
               GOTO SYS_ADC_LABEL_1               ; Sprung, wenn nicht

               MOVFW ADRES               ; Wert holen
               MOVWF ADC0               ; und speichern

               MOVLW 3
               BSF STATUS,RP0               ; Bank 1
               MOVWF ADCON1
               MOVLW B'11111111'
               MOVWF TRISA
               
;***************************************************
; Read  -> AN1
;***************************************************

               BCF ADCON1,0
               BSF TRISA,1
               MOVLW B'11001001'               ; CH1
               BCF STATUS,RP0               ; Bank 0
               MOVWF ADCON0
               MOVLW 5
               MOVWF SYS_TMP1

SYS_ADC_LABEL_2
               DECFSZ SYS_TMP1,F
               GOTO SYS_ADC_LABEL_2

               BSF ADCON0,GO               ; Wandlung starten

SYS_ADC_LABEL_3

               BTFSC ADCON0,GO               ; Schon fertig ?
               GOTO SYS_ADC_LABEL_3               ; Sprung, wenn nicht

               MOVFW ADRES               ; Wert holen
               MOVWF ADC1               ; und speichern

               MOVLW 3
               BSF STATUS,RP0               ; Bank 1
               MOVWF ADCON1
               MOVLW B'11111111'
               MOVWF TRISA
               
;***************************************************
; Read  -> AN2
;***************************************************

               CLRF ADCON1
               BSF TRISA,2
               MOVLW B'11010001'               ; CH2
               BCF STATUS,RP0               ; Bank 0
               MOVWF ADCON0
               MOVLW 5
               MOVWF SYS_TMP1

SYS_ADC_LABEL_4
               DECFSZ SYS_TMP1,F
               GOTO SYS_ADC_LABEL_4

               BSF ADCON0,GO               ; Wandlung starten

SYS_ADC_LABEL_5

               BTFSC ADCON0,GO               ; Schon fertig ?
               GOTO SYS_ADC_LABEL_5               ; Sprung, wenn nicht

               MOVFW ADRES               ; Wert holen
               MOVWF ADC2               ; und speichern

               MOVLW 3
               BSF STATUS,RP0               ; Bank 1
               MOVWF ADCON1
               MOVLW B'11111111'
               MOVWF TRISA
               
;***************************************************
; Read  -> AN3
;***************************************************

               CLRF ADCON1
               BSF TRISA,3
               MOVLW B'11011001'               ; CH3
               BCF STATUS,RP0               ; Bank 0
               MOVWF ADCON0
               MOVLW 5
               MOVWF SYS_TMP1

SYS_ADC_LABEL_6
               DECFSZ SYS_TMP1,F
               GOTO SYS_ADC_LABEL_6

               BSF ADCON0,GO               ; Wandlung starten

SYS_ADC_LABEL_7

               BTFSC ADCON0,GO               ; Schon fertig ?
               GOTO SYS_ADC_LABEL_7               ; Sprung, wenn nicht

               MOVFW ADRES               ; Wert holen
               MOVWF ADC3               ; und speichern

               MOVLW 3
               BSF STATUS,RP0               ; Bank 1
               MOVWF ADCON1
               MOVLW B'11111111'
               MOVWF TRISA
               
;***************************************************
; Schmitt-Trigger 8-Bit -> ST1
;***************************************************

               BTFSC S0,4               ; Ausgang gesetzt ?
               GOTO LABEL_ST1_MH               ; dann mit Hysterese ?

               MOVFW ADC2
               GOTO LABEL_ST1_TEST

LABEL_ST1_MH

               MOVFW ADC2
               MOVWF SYS_TMP1
               MOVFW ADC3               ; Hysterese
               SUBWF SYS_TMP1,W
LABEL_ST1_TEST

               SUBWF ADC0,W
               BTFSS STATUS,CARRY
               GOTO LABEL_ST1_RESET

               BSF S0,4               ; if greater or equal
               GOTO LABEL_ST1_ENDE

LABEL_ST1_RESET
               BCF S0,4               ; if less
LABEL_ST1_ENDE

;***************************************************
; Schmitt-Trigger 8-Bit -> ST2
;***************************************************

               BTFSC S0,3               ; Ausgang gesetzt ?
               GOTO LABEL_ST2_MH               ; dann mit Hysterese ?

               MOVFW ADC1
               GOTO LABEL_ST2_TEST

LABEL_ST2_MH

               MOVFW ADC1
               MOVWF SYS_TMP1
               MOVFW ADC3               ; Hysterese
               SUBWF SYS_TMP1,W
LABEL_ST2_TEST

               SUBWF ADC0,W
               BTFSS STATUS,CARRY
               GOTO LABEL_ST2_RESET

               BSF S0,3               ; if greater or equal
               GOTO LABEL_ST2_ENDE

LABEL_ST2_RESET
               BCF S0,3               ; if less
LABEL_ST2_ENDE

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

               MOVLW 7               ; Test auf >=
               SUBWF TR1_HI,W               ;F-W -> W
               BTFSS STATUS,CARRY
               GOTO LABEL_TR1_ENDE
               MOVLW 161
               SUBWF TR1,W               ;F-W -> W
               BTFSS STATUS,CARRY
               GOTO LABEL_TR1_ENDE

LABEL_TR1_RESET
               CLRF TR1               ; reset time
               CLRF TR1_HI               ; reset time

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

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

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

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

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

LABEL_IF1_TRUE
               BSF PORTB,4
               GOTO LABEL_IF1_EXIT

LABEL_IF1_FALSE
               BCF PORTB,4

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

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

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

LABEL_IF2_CHECK
               BTFSS STATUS,CARRY               ; Test auf -> >
               GOTO LABEL_IF2_FALSE
               BTFSC STATUS,ZERO
               GOTO LABEL_IF2_FALSE

LABEL_IF2_TRUE
               BSF S1,2
               GOTO LABEL_IF2_EXIT

LABEL_IF2_FALSE
               BCF S1,2

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

               BTFSS S1,2               ; Reset ?
               GOTO LABEL_ZV1_COUNT
               CLRF ZV1
               CLRF ZV1_HI
               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

               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

               BTFSC S1,0
               GOTO LABEL_ZV1_EXIT
               BCF ZV1_FD,0

LABEL_ZV1_EXIT

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

               BCF S1,1               ; immer Low

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

               BCF STATUS,RP0               ; Bank 0
               BTFSS PORTB,4               ; Eingang auf High ?
               GOTO LABEL_MF1_CHKTIME               ; Nein

; RESET

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

LABEL_MF1_CHKTIME

               BTFSS S0,0               ; Ausgang schon auf Low ?
               GOTO LABEL_MF1_EXIT               ; Ja

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

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

               BCF S0,0
LABEL_MF1_EXIT

;***************************************************
; ODER - Gatter -> UG3
;***************************************************

LABEL_UG3_1
               BCF STATUS,RP0               ; Bank 0
               BTFSC PORTB,0
               GOTO LABEL_UG3_TRUE
LABEL_UG3_2
               BTFSC S0,0
               GOTO LABEL_UG3_TRUE

LABEL_UG3_FALSE
               BCF S1,3
               GOTO LABEL_UG3_ENDE
LABEL_UG3_TRUE
               BSF S1,3
LABEL_UG3_ENDE
;***************************************************
; Zähler -> ZV2
;***************************************************

               BCF STATUS,RP0               ; Bank 0
               BTFSS PORTB,1               ; Reset ?
               GOTO LABEL_ZV2_COUNT
               CLRF ZV2
               CLRF ZV2_HI
               GOTO LABEL_ZV2_SETOUT

LABEL_ZV2_COUNT

; Test auf positive Flanke

               BTFSS S0,2
               GOTO LABEL_ZV2_SETFD
               BTFSC ZV2_FD,0
               GOTO LABEL_ZV2_SETFD

               BSF ZV2_FD,0


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

               INCFSZ ZV2,F
               GOTO LABEL_ZV2_SETOUT
               INCF ZV2_HI,F
               GOTO LABEL_ZV2_SETOUT

LABEL_ZV2_BACKWARD
               DECF ZV2,F
               INCFSZ ZV2,W
               GOTO LABEL_ZV2_SETOUT
               DECF ZV2_HI,F

LABEL_ZV2_SETOUT


LABEL_ZV2_SETFD

; Test auf negative Flanke

               BTFSC S0,2
               GOTO LABEL_ZV2_EXIT
               BCF ZV2_FD,0

LABEL_ZV2_EXIT

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

               BCF S0,1               ; immer Low

;***************************************************
; UND - Gatter -> UG6
;***************************************************

LABEL_UG6_1
               BTFSS S1,0
               GOTO LABEL_UG6_FALSE
LABEL_UG6_2
               BCF STATUS,RP0               ; Bank 0
               BTFSS PORTB,6
               GOTO LABEL_UG6_FALSE

LABEL_UG6_TRUE
               BSF S0,2
               GOTO LABEL_UG6_ENDE
LABEL_UG6_FALSE
               BCF S0,2
LABEL_UG6_ENDE
;***************************************************
;AF1
;***************************************************

               MOVFW ZV2
               MOVWF SYS_TMP1
               MOVFW ZV2_HI
               MOVWF SYS_TMP2
               MOVLW LOW 3600
               MOVWF SYS_TMP3
               MOVLW HIGH 3600
               MOVWF SYS_TMP4
               CALL SYSDIV16
               MOVFW SYS_TMP5
               MOVWF BSTUNDEN
               MOVFW SYS_TMP6
               MOVWF BSTUNDEN_HI

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

LABEL_LG3_1
               BTFSC S0,4
               GOTO LABEL_LG3_TRUE
LABEL_LG3_2
               BTFSC S0,3
               GOTO LABEL_LG3_TRUE

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

LABEL_LG4_1
               BTFSS S0,5
               GOTO LABEL_LG4_FALSE
LABEL_LG4_2
               BTFSC S1,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
;***************************************************
; RS-FLIP-FLOP -> RS1
;***************************************************

               BTFSC S1,3
               GOTO LABEL_RS1_RESET
               BTFSS S0,4
               GOTO LABEL_RS1_ENDE
               BSF S0,6               ; Ausgang setzen
               GOTO LABEL_RS1_ENDE
LABEL_RS1_RESET
               BCF S0,6               ; Ausgang zurücksetzen
LABEL_RS1_ENDE

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

               MOVLW 3               ; Test auf >=
               SUBWF TR2_HI,W               ;F-W -> W
               BTFSS STATUS,CARRY
               GOTO LABEL_TR2_ENDE
               MOVLW 209
               SUBWF TR2,W               ;F-W -> W
               BTFSS STATUS,CARRY
               GOTO LABEL_TR2_ENDE

LABEL_TR2_RESET
               CLRF TR2               ; reset time
               CLRF TR2_HI               ; reset time

LABEL_TR2_SET               ; toggel output
               BTFSC S0,7
               GOTO LABEL_TR2_CLR
               BSF S0,7
               GOTO LABEL_TR2_ENDE
LABEL_TR2_CLR
               BCF S0,7
LABEL_TR2_ENDE

;***************************************************
; UND - Gatter -> LG5
;***************************************************

LABEL_LG5_1
               BTFSS S0,6
               GOTO LABEL_LG5_FALSE
LABEL_LG5_2
               BTFSS S0,7
               GOTO LABEL_LG5_FALSE

LABEL_LG5_TRUE
               BCF STATUS,RP0               ; Bank 0
               BSF PORTB,5
               GOTO LABEL_LG5_ENDE
LABEL_LG5_FALSE
               BCF STATUS,RP0               ; Bank 0
               BCF PORTB,5
LABEL_LG5_ENDE
;***************************************************
; UND - Gatter -> LG6
;***************************************************

LABEL_LG6_1
               BTFSS S0,7
               GOTO LABEL_LG6_FALSE
LABEL_LG6_2
               BTFSS PORTB,2
               GOTO LABEL_LG6_FALSE

LABEL_LG6_TRUE
               BSF PORTB,3
               GOTO LABEL_LG6_ENDE
LABEL_LG6_FALSE
               BCF STATUS,RP0               ; Bank 0
               BCF PORTB,3
LABEL_LG6_ENDE
;****************************************


               GOTO MAIN


               END

