; Source created with Parsic Version 3.x

; at              : 16.01.2000 14:39:09


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

; Project Version : 1.0.48

; Created         : 17.08.1999 18:52:00

; Last change     : 16.01.2000 14:39:06

               PROCESSOR 16C71

               INCLUDE P16C71.INC

               RADIX DEC


               __IDLOCS H'0100'

               __CONFIG _CP_OFF & _PWRTE_ON & _WDT_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
ZV1               = 22               ; located in Bank 0, 1
ZV1_HI               = 23               ; located in Bank 0, 1
ZV1_FD               = 24               ; located in Bank 0, 1
MF1               = 25               ; located in Bank 0, 1
MF1_HI               = 26               ; located in Bank 0, 1
ZV2               = 27               ; located in Bank 0, 1
ZV2_HI               = 28               ; located in Bank 0, 1
ZV2_FD               = 29               ; located in Bank 0, 1
BSTUNDEN               = 30               ; located in Bank 0, 1
BSTUNDEN_HI               = 31               ; located in Bank 0, 1
SYS_TMP2               = 32               ; located in Bank 0, 1
SYS_TMP3               = 33               ; located in Bank 0, 1
SYS_TMP4               = 34               ; located in Bank 0, 1
SYS_TMP5               = 35               ; located in Bank 0, 1
SYS_TMP6               = 36               ; located in Bank 0, 1
SYS_TMP7               = 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
               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
               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'01111111'
               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 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

               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,0               ; Ausgang gesetzt ?
               GOTO LABEL_ST1_MH               ; dann mit Hysterese ?

               MOVFW ADC1
               GOTO LABEL_ST1_TEST

LABEL_ST1_MH

               MOVFW ADC1
               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,0               ; if greater or equal
               GOTO LABEL_ST1_ENDE

LABEL_ST1_RESET
               BCF S0,0               ; if less
LABEL_ST1_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 S0,1
               GOTO LABEL_TR1_CLR
               BSF S0,1
               GOTO LABEL_TR1_ENDE
LABEL_TR1_CLR
               BCF S0,1
LABEL_TR1_ENDE

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

               BTFSC S0,0               ; Reset ?
               GOTO LABEL_ZV1_COUNT
               CLRF ZV1
               CLRF ZV1_HI
               GOTO LABEL_ZV1_SETOUT

LABEL_ZV1_COUNT

; Test auf positive Flanke

               BTFSS S0,1
               GOTO LABEL_ZV1_SETFD
               BTFSC ZV1_FD,0
               GOTO LABEL_ZV1_SETFD

               BSF ZV1_FD,0


               BTFSC S0,2               ; 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 S0,1
               GOTO LABEL_ZV1_EXIT
               BCF ZV1_FD,0

LABEL_ZV1_EXIT

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

               BCF S0,2               ; immer Low

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

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

               BTFSS STATUS,ZERO               ; Wenn ZERO gesetzt LOW-Byte testen
               GOTO LABEL_IF1_CHECK
               MOVLW LOW 180
               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,3
               GOTO LABEL_IF1_EXIT

LABEL_IF1_FALSE
               BCF S0,3

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

               BTFSS S0,3               ; Eingang auf High ?
               GOTO LABEL_MF1_CHKTIME               ; Nein

; RESET

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

LABEL_MF1_CHKTIME

               BTFSS S0,6               ; 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,6
LABEL_MF1_EXIT

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

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

               MOVFW ADC0
               GOTO LABEL_ST2_TEST

LABEL_ST2_MH

               MOVLW 10               ; Hysterese
               SUBWF ADC0,W
LABEL_ST2_TEST

               SUBWF ADC2,W
               BTFSS STATUS,CARRY
               GOTO LABEL_ST2_RESET

               BSF S0,7               ; if greater or equal
               GOTO LABEL_ST2_ENDE

LABEL_ST2_RESET
               BCF S0,7               ; if less
LABEL_ST2_ENDE

;***************************************************
; ODER - Gatter -> UG1
;***************************************************

LABEL_UG1_1
               BTFSC S0,7
               GOTO LABEL_UG1_TRUE
LABEL_UG1_2
               BTFSC S0,6
               GOTO LABEL_UG1_TRUE
               BCF STATUS,RP0               ; Bank 0

LABEL_UG1_FALSE
               BCF PORTB,7
               GOTO LABEL_UG1_ENDE
LABEL_UG1_TRUE
               BCF STATUS,RP0               ; Bank 0
               BSF PORTB,7
LABEL_UG1_ENDE
;***************************************************
; Zähler -> ZV2
;***************************************************

               BTFSS PORTB,0               ; Reset ?
               GOTO LABEL_ZV2_COUNT
               CLRF ZV2
               CLRF ZV2_HI
               GOTO LABEL_ZV2_SETOUT

LABEL_ZV2_COUNT

; Test auf positive Flanke

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

               BSF ZV2_FD,0


               BTFSC S0,5               ; 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,4
               GOTO LABEL_ZV2_EXIT
               BCF ZV2_FD,0

LABEL_ZV2_EXIT

;***************************************************
; UND - Gatter -> UG2
;***************************************************

LABEL_UG2_1
               BTFSS S0,1
               GOTO LABEL_UG2_FALSE
LABEL_UG2_2
               BCF STATUS,RP0               ; Bank 0
               BTFSS PORTB,7
               GOTO LABEL_UG2_FALSE

LABEL_UG2_TRUE
               BSF S0,4
               GOTO LABEL_UG2_ENDE
LABEL_UG2_FALSE
               BCF S0,4
LABEL_UG2_ENDE
;***************************************************
; GND
;***************************************************

               BCF S0,5               ; immer Low

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


               GOTO MAIN


               END

