; Quelltext erstellt mit Easy-PIC Version 2.16

; am : 10.06.1999 22:33:30


; Projekt Datei   : E:\DATEN\DELPHI\EPICWIN\INTERNET\BSP\MODELL3.PIC

; Projekt Version : 2.0.13

; angelegt        : 14.03.1999 21:15:00

; letzte Änderung : 10.06.1999 22:22:00

	PROCESSOR 16C84

	RADIX DEC


	__IDLOCS H'0200'

	__CONFIG B'11101'

W EQU 0
F EQU 1

; Bit Belegung STATUS-Register

RP0   EQU 5 ; Register-Page 0
ZERO  EQU 2 ; ZERO-Flag
DC    EQU 1 ; HALF-CARRY-Flag
CARRY EQU 0 ; CARRY-Flag

; Bit Belegung INTCON-Register

GIE  EQU 7 ; Global  -Interrupt-Enable
EEIE EQU 6 ; EE-Write-Interrupt-Enable
TOIE EQU 5 ; Timer0  -Interrupt-Enable
INTE EQU 4 ; Extern  -Interrupt-Enable
RBIE EQU 3 ; Port B Changed-Interrupt-Enable
T0IF EQU 2 ; Timer0 Interrupt-Flag
INTF EQU 1 ; Extern-Interrupt-Flag
RBIF EQU 0 ; Port B-Interrupt-Flag

; RAM Bank 0

INDF   EQU 0
TMR0   EQU 1
PCL    EQU 2
STATUS EQU 3
FSR    EQU 4
RA     EQU 5
RB     EQU 6
EEDATA EQU 8
EEADR  EQU 9
PCLATH EQU 10
INTCON EQU 11

; RAM Bank 1

OPTIONR EQU 1
TRISA   EQU 5
TRISB   EQU 6
EECON1  EQU 8
EECON2  EQU 9

; Variable

	VARIABLE RAM = 12

SYS_WSAV	EQU RAM
RAM += 1

SYS_SSAV	EQU RAM
RAM += 1

TMR0H	EQU RAM
RAM += 1

RBSTATUS	EQU RAM
RAM += 1

RBCHANGED	EQU RAM
RAM += 1

RBTMP	EQU RAM
RAM += 1

RB4STARTTIME	EQU RAM
RAM += 1

RB4STARTTIME_HI	EQU RAM
RAM += 1

RB4TIME	EQU RAM
RAM += 1

RB4TIME_HI	EQU RAM
RAM += 1

MF1_LO	EQU RAM
RAM += 1

MF1_HI	EQU RAM
RAM += 1

MF1_FD	EQU RAM
RAM += 1

S0	EQU RAM
RAM += 1

SYS_TMP1	EQU RAM
RAM += 1

SYS_TMP2	EQU RAM
RAM += 1

ZR1	EQU RAM
RAM += 1

ZR1_FD	EQU RAM
RAM += 1

ZR2	EQU RAM
RAM += 1

ZR2_FD	EQU RAM
RAM += 1



;****************************************
;Programmanfang
;****************************************

	ORG 0

	CLRF STATUS	; RAM-Bank 0
	BCF INTCON,GIE	; Interrupt sperren
	CLRWDT
	GOTO SYS_INIT

;****************************************
;Interrupt-Adresse (fest vorgegeben)
;****************************************

	ORG 4

	BTFSC INTCON,RBIF	; RBIF ?
	GOTO RBIF_INTERRUPT	; Ja

	MOVWF SYS_WSAV	; Register sichern
	SWAPF STATUS,W
	BCF STATUS,RP0	; BANK0
	MOVWF SYS_SSAV

	INCF TMR0H,F	; High-Byte von TMR0

	INCFSZ MF1_LO,F
	GOTO SYS_INT_LABEL_0
	INCF MF1_HI,F

SYS_INT_LABEL_0
	BCF INTCON,T0IF	; Interrupt quitieren

	BTFSC INTCON,RBIF	; RBIF ?
	GOTO RBIF_INTERRUPT2

	SWAPF SYS_SSAV,W	; Register zurück
	MOVWF STATUS
	SWAPF SYS_WSAV,F
	SWAPF SYS_WSAV,W

	RETFIE

RBIF_INTERRUPT
	MOVWF SYS_WSAV	; Register sichern
	SWAPF STATUS,W
	BCF STATUS,RP0	; BANK0
	MOVWF SYS_SSAV

RBIF_INTERRUPT2
	BCF INTCON,RBIF	; Interrupt quitieren

; Timer stoppen
	BSF STATUS,RP0	; BANK1
	BSF OPTIONR,5	; externer takt
	BCF STATUS,RP0

	MOVFW RB	; testen welcher Eingang sich geändert hat
	MOVWF RBTMP
	XORWF RBSTATUS,W
	MOVWF RBCHANGED
	MOVFW RBTMP
	MOVWF RBSTATUS

	BTFSC INTCON,T0IF	; Timer Interrupt ?
	INCF TMR0H,F	; High-Byte von TMR0
	BCF INTCON,T0IF	; Interrupt quitieren

TESTRB4
	BTFSC RBCHANGED,4	; RB4
	GOTO CHECKRB4
	GOTO EXITINT	 ; Oh

CHECKRB4	BTFSC RBSTATUS,4
	GOTO CHECKRB4START

	MOVFW RB4STARTTIME_HI
	SUBWF TMR0H,W
	MOVWF RB4TIME_HI
	MOVFW RB4STARTTIME
	SUBWF TMR0,W
	MOVWF RB4TIME
	BTFSS STATUS,CARRY
	DECF RB4TIME_HI,F

	GOTO EXITINT

CHECKRB4START
	MOVFW TMR0H
	MOVWF RB4STARTTIME_HI
	MOVFW TMR0
	ADDLW D'225'
	MOVWF RB4STARTTIME
	BTFSS STATUS,CARRY
	DECF RB4STARTTIME_HI,F

	GOTO EXITINT
EXITINT
	BSF STATUS,RP0	; BANK1
	BCF OPTIONR,5	; interner Takt
	BCF STATUS,RP0	; BANK0

	SWAPF SYS_SSAV,W	; Register zurück
	MOVWF STATUS
	SWAPF SYS_WSAV,F
	SWAPF SYS_WSAV,W

	RETFIE	; RETurn From Interrupt and Enable GIE

;****************************************
;Initialisierung
;****************************************


SYS_INIT

	MOVLW B'00101000'
	MOVWF INTCON

; Ausgangs-Pegel setzen, bevor die Richtung gesetzt wird

	MOVLW B'00000000'
	MOVWF RA

	MOVLW B'00000000'
	MOVWF RB

	BSF STATUS,RP0	; RAM-BANK 1

	MOVLW 136
	MOVWF OPTIONR


; Richtung setzen

	MOVLW B'11111100'
	MOVWF TRISA

	MOVLW B'11111111'
	MOVWF TRISB

	BCF STATUS,RP0	; RAM-BANK 0

; Variable initialisieren

	CLRF SYS_WSAV
	CLRF SYS_SSAV
	CLRF TMR0H
	CLRF RBSTATUS
	CLRF RBCHANGED
	CLRF RBTMP
	CLRF RB4STARTTIME
	CLRF RB4STARTTIME_HI
	CLRF RB4TIME
	CLRF RB4TIME_HI
	CLRF MF1_LO
	CLRF MF1_HI
	CLRF MF1_FD
	MOVLW B'00000101'
	MOVWF S0
	CLRF SYS_TMP1
	CLRF SYS_TMP2
	CLRF ZR1
	CLRF ZR1_FD
	CLRF ZR2
	CLRF ZR2_FD

	CLRF TMR0
	BSF INTCON,GIE	; Interrupt freigeben

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

MAIN

	CLRWDT

;***************************************************
; Puls-Geber -> MF1
;***************************************************

; Test auf positive Flanke

	BTFSC MF1_FD,0
	GOTO LABEL_MF1_CHKTIME
	BTFSS RBTMP,4
	GOTO LABEL_MF1_CHKTIME


LABEL_MF1_RESET

	CLRF MF1_LO
	CLRF MF1_HI
	BSF MF1_FD,1
	BCF S0,0

LABEL_MF1_CHKTIME

; Läuft der Impuls ?

	BTFSS MF1_FD,1
	GOTO LABEL_MF1_SETFD

	MOVLW 7
	SUBWF MF1_HI,W
	BTFSS STATUS,CARRY
	GOTO LABEL_MF1_SETFD
	MOVLW 161
	SUBWF MF1_LO,W
	BTFSS STATUS,CARRY
	GOTO LABEL_MF1_SETFD
LABEL_MF1_TO

	BCF MF1_FD,1
	BSF S0,0

LABEL_MF1_SETFD

	BTFSS RBTMP,4
	BCF MF1_FD,0
	BTFSC RBTMP,4
	BSF MF1_FD,0

	CLRWDT

;***************************************************
; Schmitt-Trigger 16-Bit -> ST1
;***************************************************

	MOVLW LOW 1700
	MOVWF SYS_TMP1
	MOVLW HIGH 1700
	MOVWF SYS_TMP2

	BTFSS S0,1	; Ausgang gesetzt ?
	GOTO LABEL_ST1_TEST	; Ohne Hysterese

	MOVLW HIGH 100
	SUBWF SYS_TMP2,F
	MOVLW LOW 100
	SUBWF SYS_TMP1,F
	BTFSS STATUS,CARRY
	DECF SYS_TMP2,F

LABEL_ST1_TEST
	MOVFW SYS_TMP2
	SUBWF RB4TIME_HI,W

	BTFSS STATUS,ZERO
	GOTO LABEL_ST1_TEST2

	MOVFW SYS_TMP1
	SUBWF RB4TIME,W

LABEL_ST1_TEST2
	BTFSS STATUS,CARRY
	GOTO LABEL_ST1_RESET

LABEL_ST1_SET
	BSF S0,1	; Wenn größer oder gleich Ausgang setzen
	GOTO LABEL_ST1_ENDE

LABEL_ST1_RESET
	BCF S0,1	; Wenn kleiner Ausgang zurücksetzen
LABEL_ST1_ENDE

	CLRWDT

;***************************************************
; Zähler -> ZR1
;***************************************************

	BTFSS S0,0	; Reset ?
	GOTO LABEL_ZR1_COUNT
	CLRF ZR1
	GOTO LABEL_ZR1_SETOUT

LABEL_ZR1_COUNT

; Test auf positive Flanke

	BTFSC ZR1_FD,0
	GOTO LABEL_ZR1_SETFD
	BTFSS S0,1
	GOTO LABEL_ZR1_SETFD

	INCF ZR1,F

LABEL_ZR1_SETOUT

; Ausgang Q0

	BTFSS ZR1,0
	BCF RA,0
	BTFSC ZR1,0
	BSF RA,0

LABEL_ZR1_SETFD

	BTFSS S0,1
	BCF ZR1_FD,0
	BTFSC S0,1
	BSF ZR1_FD,0

	CLRWDT

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

	MOVLW LOW 1400
	MOVWF SYS_TMP1
	MOVLW HIGH 1400
	MOVWF SYS_TMP2

	BTFSC S0,2	; Ausgang gesetzt ?
	GOTO LABEL_ST2_TEST	; Ohne Hysterese

	MOVLW HIGH 100
	SUBWF SYS_TMP2,F
	MOVLW LOW 100
	SUBWF SYS_TMP1,F
	BTFSS STATUS,CARRY
	DECF SYS_TMP2,F

LABEL_ST2_TEST
	MOVFW SYS_TMP2
	SUBWF RB4TIME_HI,W

	BTFSS STATUS,ZERO
	GOTO LABEL_ST2_TEST2

	MOVFW SYS_TMP1
	SUBWF RB4TIME,W

LABEL_ST2_TEST2
	BTFSS STATUS,CARRY
	GOTO LABEL_ST2_RESET

LABEL_ST2_SET
	BCF S0,2	; Wenn größer oder gleich Ausgang zurücksetzen
	GOTO LABEL_ST2_ENDE

LABEL_ST2_RESET
	BSF S0,2	; Wenn kleiner Ausgang zurücksetzen
LABEL_ST2_ENDE

	CLRWDT

;***************************************************
; Zähler -> ZR2
;***************************************************

	BTFSS S0,0	; Reset ?
	GOTO LABEL_ZR2_COUNT
	CLRF ZR2
	GOTO LABEL_ZR2_SETOUT

LABEL_ZR2_COUNT

; Test auf positive Flanke

	BTFSC ZR2_FD,0
	GOTO LABEL_ZR2_SETFD
	BTFSS S0,2
	GOTO LABEL_ZR2_SETFD

	INCF ZR2,F

LABEL_ZR2_SETOUT

; Ausgang Q0

	BTFSS ZR2,0
	BCF RA,1
	BTFSC ZR2,0
	BSF RA,1

LABEL_ZR2_SETFD

	BTFSS S0,2
	BCF ZR2_FD,0
	BTFSC S0,2
	BSF ZR2_FD,0

	CLRWDT

	GOTO MAIN


	END

