; Source created with Parsic Version 3.x

; at              : 03.03.2000 00:15:25


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

; Project Version : 1.4.39

; Created         : 14.03.1999 21:16:00

; Last change     : 02.03.2000 23:45:00

	PROCESSOR 16C711

	INCLUDE P16C711.INC

	RADIX DEC


	__IDLOCS H'0104'

	__CONFIG _CP_OFF & _PWRTE_ON & _WDT_ON & _BODEN_ON & _XT_OSC

; Diverses

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


; Variable

S2	= 12	; located in Bank 0, 1
TMR0H	= 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
ADC0	= 18	; located in Bank 0, 1
ADC0_HI	= 19	; located in Bank 0, 1
SYS_TMP1	= 20	; located in Bank 0, 1
S1	= 21	; located in Bank 0, 1
S1_HI	= 22	; located in Bank 0, 1
SYS_TMP2	= 23	; located in Bank 0, 1
SYS_TMP3	= 24	; located in Bank 0, 1
SYS_TMP4	= 25	; located in Bank 0, 1
SYS_TMP5	= 26	; located in Bank 0, 1
SYS_TMP6	= 27	; located in Bank 0, 1
SYS_TMP7	= 28	; located in Bank 0, 1
S0	= 29	; located in Bank 0, 1
S0_HI	= 30	; located in Bank 0, 1
RAM	= 31	; 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

	INCF TMR0H,F

	INCFSZ TR1,F
	GOTO SYS_INT_LABEL_0
	INCF TR1_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


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


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

; init var

	CLRF S2
	CLRF TMR0H
	CLRF SYS_WSAV
	CLRF SYS_SSAV
	CLRF TR1
	CLRF TR1_HI
	CLRF ADC0
	CLRF ADC0_HI
	CLRF SYS_TMP1
	CLRF S1
	CLRF S1_HI
	CLRF SYS_TMP2
	CLRF SYS_TMP3
	CLRF SYS_TMP4
	CLRF SYS_TMP5
	CLRF SYS_TMP6
	CLRF SYS_TMP7
	CLRF S0
	CLRF S0_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
	
;***************************************************
; RS-FLIP-FLOP -> RS1
;***************************************************

	BTFSS S2,2
	GOTO LABEL_RS1_RESET
	BTFSS S2,0
	GOTO LABEL_RS1_ENDE
	BSF S2,1	; Ausgang setzen
	GOTO LABEL_RS1_ENDE
LABEL_RS1_RESET
	BCF S2,1	; Ausgang zurücksetzen
LABEL_RS1_ENDE

;***************************************************
; UND - Gatter -> UG1
;***************************************************

LABEL_UG1_1
	BTFSC S2,1
	GOTO LABEL_UG1_FALSE
LABEL_UG1_2
	BTFSS S2,2
	GOTO LABEL_UG1_FALSE

LABEL_UG1_TRUE
	BSF S2,0
	GOTO LABEL_UG1_ENDE
LABEL_UG1_FALSE
	BCF S2,0
LABEL_UG1_ENDE
;***************************************************
; Impuls -> PO1
;***************************************************

	BTFSS S2,0
	GOTO LABEL_PO1_EXIT

	MOVLW 250
	SUBWF S0,W
	BTFSC STATUS,ZERO
	GOTO LABEL_PO1_0
	BTFSS STATUS,CARRY
	GOTO LABEL_PO1_0

	MOVLW 250
	MOVWF S0

LABEL_PO1_0
	MOVFW S0
	IORWF S0_HI,W
	BTFSC STATUS,ZERO
	GOTO LABEL_PO1_EXIT
	BCF STATUS,RP0	; Bank 0
	CLRF TMR0
	CLRF TMR0H

	BSF PORTB,0

LABEL_PO1_1
	CLRWDT
	MOVFW S0_HI
	SUBWF TMR0H,W
	BTFSS STATUS,ZERO
	GOTO LABEL_PO1_1

LABEL_PO1_2
	BCF INTCON,GIE
	BTFSC INTCON,GIE
	GOTO LABEL_PO1_2

LABEL_PO1_3
	MOVFW S0
	SUBWF TMR0,W
	BTFSS STATUS,CARRY
	GOTO LABEL_PO1_3

	BCF PORTB,0

	CLRF S0
	CLRF S0_HI
	BSF INTCON,GIE

LABEL_PO1_EXIT

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

	MOVLW 0	; Test auf >=
	SUBWF TR1_HI,W	;F-W -> W
	BTFSS STATUS,CARRY
	GOTO LABEL_TR1_ENDE
	MOVLW 39
	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 S2,2
	GOTO LABEL_TR1_CLR
	BSF S2,2
	GOTO LABEL_TR1_ENDE
LABEL_TR1_CLR
	BCF S2,2
LABEL_TR1_ENDE

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

	MOVFW ADC0
	MOVWF SYS_TMP1
	MOVFW ADC0_HI
	MOVWF SYS_TMP2
	MOVLW LOW 4
	MOVWF SYS_TMP3
	MOVLW HIGH 4
	MOVWF SYS_TMP4
	CALL SYSMUL16
	MOVFW SYS_TMP5
	MOVWF S1
	MOVFW SYS_TMP6
	MOVWF S1_HI

LABEL_AF1_EXIT
;***************************************************
;AF2
;***************************************************

	MOVFW S1_HI
	ADDLW HIGH 900
	MOVWF S0_HI
	MOVFW S1
	ADDLW LOW 900
	MOVWF S0
	BTFSC STATUS,CARRY
	INCF S0_HI,F

LABEL_AF2_EXIT
;****************************************


	GOTO MAIN


	END

