; Source created with Parsic Version 3.x

; at              : 2008.09.02. 21:05:04


; Project File    : C:\PROGRA~1\Parsic\Timers.PIC

; Project Version : 1.0.0

; Created         : 2008.09.02. 20:50:48

; Last change     : 2008.09.02. 21:03:35

	PROCESSOR 16F628

	INCLUDE P16F628.INC

	RADIX DEC


	__IDLOCS H'0100'

	__CONFIG _CP_OFF & _PWRTE_ON & _WDT_OFF & _BODEN_ON & _XT_OSC & _LVP_OFF & _MCLRE_ON

; Diverses

ZERO	EQU Z	; ZERO-Flag
CARRY	 EQU C	; CARRY-Flag


; Variable

ZV2	= 32	; located in Bank 0
EE1_FD	= 33	; located in Bank 0
TR1	= 34	; located in Bank 0
TR1_HI	= 35	; located in Bank 0
S0	= 36	; located in Bank 0
ZV1	= 37	; located in Bank 0
ZV1_FD	= 38	; located in Bank 0
OS1_FD	= 39	; located in Bank 0
ZV2_FD	= 40	; located in Bank 0
S1	= 41	; located in Bank 0
OS2_FD	= 42	; located in Bank 0
ZV3	= 43	; located in Bank 0
ZV3_FD	= 44	; located in Bank 0
OS3_FD	= 45	; located in Bank 0
RAM	= 46	; located in Bank 0
SYS_TMP1	= 112	; located in Bank 0, 1, 2, 3
SYS_WSAV	= 113	; located in Bank 0, 1, 2, 3
SYS_SSAV	= 114	; located in Bank 0, 1, 2, 3
SYS_FSAV	= 115	; located in Bank 0, 1, 2, 3

	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
	MOVFW FSR
	MOVWF SYS_FSAV


	BTFSS INTCON,T0IF	; TMR0 ?
	GOTO NOTMR0INT
	BCF INTCON,T0IF	; Interrupt quitieren

	BCF STATUS,RP0	; Bank 0
	BCF STATUS,RP1	; Bank 0
	INCFSZ TR1,F
	GOTO SYS_INT_LABEL_0
	INCF TR1_HI,F

SYS_INT_LABEL_0
NOTMR0INT


EXITINT

	MOVFW SYS_FSAV
	MOVWF FSR
	SWAPF SYS_SSAV,W	; Register zurück
	MOVWF STATUS
	SWAPF SYS_WSAV,F
	SWAPF SYS_WSAV,W

	RETFIE


;****************************************
; Subroutines
;****************************************


EEINT_WRITE
	MOVFW INDF
EEINT_WRITE_0
	BSF STATUS,RP0	; Bank 1
	BCF STATUS,RP1	; Bank 1
	MOVWF EEDATA
	BSF EECON1,WREN

EEINT_DISINT
	BCF INTCON,GIE
	BTFSC INTCON,GIE
	GOTO EEINT_DISINT

	MOVLW H'55'
	MOVWF EECON2
	MOVLW H'AA'
	MOVWF EECON2
	BSF EECON1,WR	; start write

	BSF INTCON,GIE

EEINT_WRITE_1
	BTFSC EECON1,WR	; ready ?
	GOTO EEINT_WRITE_1	; no
	BCF EECON1,WREN

	INCF EEADR,F
	INCF FSR,F
	DECFSZ SYS_TMP1,F
	GOTO EEINT_WRITE

	RETURN

;***************************************************

EEINT_READ
	BSF STATUS,RP0	; Bank 1
	BCF STATUS,RP1	; Bank 1
	BSF EECON1,RD
	MOVFW EEDATA
	MOVWF INDF
	INCF EEADR,F
	INCF FSR,F
	DECFSZ SYS_TMP1,F
	GOTO EEINT_READ
	RETURN

;***************************************************
; EE-Modul -> EE1
;***************************************************

WRITE_EE1
	MOVLW 0
	BSF STATUS,RP0	; Bank 1
	BCF STATUS,RP1	; Bank 1
	MOVWF EEADR

	BCF STATUS,IRP	; vars - located in Bank 0
	MOVLW 1
	MOVWF SYS_TMP1
	MOVLW LOW ZV2
	MOVWF FSR

	CALL EEINT_WRITE


	RETURN

;***************************************************
; EE-Modul -> EE1
;***************************************************

READ_EE1
	MOVLW 0
	BSF STATUS,RP0	; Bank 1
	BCF STATUS,RP1	; Bank 1
	MOVWF EEADR

	BCF STATUS,IRP	; vars - located in Bank 0
	MOVLW 1
	MOVWF SYS_TMP1
	MOVLW LOW ZV2
	MOVWF FSR

	CALL EEINT_READ


	RETURN

;***************************************************

;****************************************
;Initialisierung
;****************************************


SYS_INIT

	MOVLW B'00100000'
	MOVWF INTCON

; Set output before direction

	BCF STATUS,RP0	; Bank 0
	BCF STATUS,RP1	; Bank 0
	MOVLW B'00000000'
	MOVWF PORTA

	MOVLW B'00000000'
	MOVWF PORTB

	MOVLW 15
	BSF STATUS,RP0	; Bank 1
	MOVWF OPTION_REG


; Richtung setzen

	MOVLW B'11111111'
	MOVWF TRISA

	MOVLW B'11111100'
	MOVWF TRISB

	BCF STATUS,RP0	; Bank 0
	MOVLW 7	; Comparators off
	MOVWF CMCON
; init vars


; Clear RAM

	BCF STATUS,IRP
	MOVLW 32
	MOVWF FSR
CR1
	CLRF INDF
	INCF FSR,F
	BTFSS FSR,7
	GOTO CR1
	BSF FSR,5
CR2
	CLRF INDF
	INCF FSR,F
	MOVLW H'F0'
	SUBWF FSR,W
	BTFSS STATUS,ZERO
	GOTO CR2
	BSF STATUS,IRP
	MOVLW 32
	MOVWF FSR
CR3
	CLRF INDF
	INCF FSR,F
	MOVLW H'50'
	SUBWF FSR,W
	BTFSS STATUS,ZERO
	GOTO CR3



	CALL READ_EE1	; read vars from internal EE

	BCF STATUS,RP0	; Bank 0
	BCF STATUS,RP1	; Bank 0
	CLRF TMR0
	BCF INTCON,T0IF
	BSF INTCON,GIE	; Interrupt freigeben

;**********************************
;******* Haupt-Programm ***********
;**********************************

MAIN

;***************************************************
; Timer -> TR1
;***************************************************

	MOVLW 0	; Test auf >=
	BCF STATUS,RP0	; Bank 0
	BCF STATUS,RP1	; Bank 0
	SUBWF TR1_HI,W	;F-W -> W
	BTFSS STATUS,ZERO
	GOTO LABEL_TR1_CHECK
	MOVLW 20
	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 20
	SUBWF TR1,F	;F-W -> F
	BTFSS STATUS,CARRY
	DECF TR1_HI,F

LABEL_TR1_SET	; toggel output
	BTFSC S0,0
	GOTO LABEL_TR1_CLR
	BSF S0,0
	GOTO LABEL_TR1_ENDE
LABEL_TR1_CLR
	BCF S0,0
LABEL_TR1_ENDE

;***************************************************
; Zähler -> ZV1
;***************************************************

	BCF STATUS,RP0	; Bank 0
	BCF STATUS,RP1	; Bank 0
	BTFSS S0,5	; Reset ?
	GOTO LABEL_ZV1_COUNT
	CLRF ZV1
	GOTO LABEL_ZV1_SETOUT

LABEL_ZV1_COUNT

; Test auf positive Flanke

	BCF STATUS,RP0	; Bank 0
	BCF STATUS,RP1	; Bank 0
	BTFSS S0,3
	GOTO LABEL_ZV1_SETFD
	BTFSC ZV1_FD,0
	GOTO LABEL_ZV1_SETFD

	BSF ZV1_FD,0


	BTFSC S0,4	; Vorwärts oder Rückwärts ?
	GOTO LABEL_ZV1_BACKWARD

	INCF ZV1,F
	GOTO LABEL_ZV1_SETOUT

LABEL_ZV1_BACKWARD
	BCF STATUS,RP0	; Bank 0
	BCF STATUS,RP1	; Bank 0
	DECF ZV1,F

LABEL_ZV1_SETOUT


LABEL_ZV1_SETFD

; Test auf negative Flanke

	BCF STATUS,RP0	; Bank 0
	BCF STATUS,RP1	; Bank 0
	BTFSC S0,3
	GOTO LABEL_ZV1_EXIT
	BCF ZV1_FD,0

LABEL_ZV1_EXIT

;***************************************************
; UND - Gatter -> LG1
;***************************************************

LABEL_LG1_1
	BCF STATUS,RP0	; Bank 0
	BCF STATUS,RP1	; Bank 0
	BTFSS S0,0
	GOTO LABEL_LG1_FALSE
LABEL_LG1_2
	BTFSS S0,1
	GOTO LABEL_LG1_FALSE

LABEL_LG1_TRUE
	BSF S0,3
	GOTO LABEL_LG1_ENDE
LABEL_LG1_FALSE
	BCF STATUS,RP0	; Bank 0
	BCF STATUS,RP1	; Bank 0
	BCF S0,3
LABEL_LG1_ENDE
;***************************************************
; RS-FLIP-FLOP -> RS1
;***************************************************

	BTFSC S0,5
	GOTO LABEL_RS1_RESET
	BTFSS S0,2
	GOTO LABEL_RS1_ENDE
	BSF S0,1	; Ausgang setzen
	GOTO LABEL_RS1_ENDE
LABEL_RS1_RESET
	BCF STATUS,RP0	; Bank 0
	BCF STATUS,RP1	; Bank 0
	BCF S0,1	; Ausgang zurücksetzen
LABEL_RS1_ENDE

;***************************************************
; One Shot -> OS1
;***************************************************


	BCF STATUS,RP0	; Bank 0
	BCF STATUS,RP1	; Bank 0
	BTFSS PORTA,1
	GOTO LABEL_OS1_1
	BTFSC OS1_FD,0
	GOTO LABEL_OS1_1
	BSF OS1_FD,0
	BSF S0,2
	GOTO LABEL_OS1_2
LABEL_OS1_1
	BCF STATUS,RP0	; Bank 0
	BCF STATUS,RP1	; Bank 0
	BCF S0,2
	BTFSC PORTA,1
	GOTO LABEL_OS1_2
	BCF OS1_FD,0
LABEL_OS1_2
;***************************************************
; Schmitt-Trigger 8-Bit -> ST1
;***************************************************

	BCF STATUS,RP0	; Bank 0
	BCF STATUS,RP1	; Bank 0
	BTFSC S0,5	; Ausgang gesetzt ?
	GOTO LABEL_ST1_MH	; dann mit Hysterese ?

	MOVLW 100
	GOTO LABEL_ST1_TEST

LABEL_ST1_MH

	MOVLW 100
LABEL_ST1_TEST

	SUBWF ZV1,W
	BTFSS STATUS,CARRY
	GOTO LABEL_ST1_RESET

	BSF S0,5	; if greater or equal
	GOTO LABEL_ST1_ENDE

LABEL_ST1_RESET
	BCF S0,5	; if less
LABEL_ST1_ENDE

;***************************************************
; GND
;***************************************************

	BCF S0,4	; immer Low

;***************************************************
; UND - Gatter -> LG2
;***************************************************

LABEL_LG2_1
	BTFSS S0,1
	GOTO LABEL_LG2_FALSE

LABEL_LG2_TRUE
	BSF PORTB,0
	GOTO LABEL_LG2_ENDE
LABEL_LG2_FALSE
	BCF STATUS,RP0	; Bank 0
	BCF STATUS,RP1	; Bank 0
	BCF PORTB,0
LABEL_LG2_ENDE
;***************************************************
; Zähler -> ZV2
;***************************************************

	BTFSS S1,2	; Reset ?
	GOTO LABEL_ZV2_COUNT
	CLRF ZV2
	GOTO LABEL_ZV2_SETOUT

LABEL_ZV2_COUNT

; Test auf positive Flanke

	BCF STATUS,RP0	; Bank 0
	BCF STATUS,RP1	; Bank 0
	BTFSS S1,0
	GOTO LABEL_ZV2_SETFD
	BTFSC ZV2_FD,0
	GOTO LABEL_ZV2_SETFD

	BSF ZV2_FD,0


	BTFSC S1,1	; Vorwärts oder Rückwärts ?
	GOTO LABEL_ZV2_BACKWARD

	INCF ZV2,F
	GOTO LABEL_ZV2_SETOUT

LABEL_ZV2_BACKWARD
	BCF STATUS,RP0	; Bank 0
	BCF STATUS,RP1	; Bank 0
	DECF ZV2,F

LABEL_ZV2_SETOUT


LABEL_ZV2_SETFD

; Test auf negative Flanke

	BCF STATUS,RP0	; Bank 0
	BCF STATUS,RP1	; Bank 0
	BTFSC S1,0
	GOTO LABEL_ZV2_EXIT
	BCF ZV2_FD,0

LABEL_ZV2_EXIT

;***************************************************
; UND - Gatter -> LG3
;***************************************************

LABEL_LG3_1
	BCF STATUS,RP0	; Bank 0
	BCF STATUS,RP1	; Bank 0
	BTFSS S0,0
	GOTO LABEL_LG3_FALSE
LABEL_LG3_2
	BTFSS S0,6
	GOTO LABEL_LG3_FALSE

LABEL_LG3_TRUE
	BSF S1,0
	GOTO LABEL_LG3_ENDE
LABEL_LG3_FALSE
	BCF STATUS,RP0	; Bank 0
	BCF STATUS,RP1	; Bank 0
	BCF S1,0
LABEL_LG3_ENDE
;***************************************************
; RS-FLIP-FLOP -> RS2
;***************************************************

	BTFSC S1,2
	GOTO LABEL_RS2_RESET
	BTFSS S0,7
	GOTO LABEL_RS2_ENDE
	BSF S0,6	; Ausgang setzen
	GOTO LABEL_RS2_ENDE
LABEL_RS2_RESET
	BCF STATUS,RP0	; Bank 0
	BCF STATUS,RP1	; Bank 0
	BCF S0,6	; Ausgang zurücksetzen
LABEL_RS2_ENDE

;***************************************************
; One Shot -> OS2
;***************************************************


	BCF STATUS,RP0	; Bank 0
	BCF STATUS,RP1	; Bank 0
	BTFSS PORTA,2
	GOTO LABEL_OS2_1
	BTFSC OS2_FD,0
	GOTO LABEL_OS2_1
	BSF OS2_FD,0
	BSF S0,7
	GOTO LABEL_OS2_2
LABEL_OS2_1
	BCF STATUS,RP0	; Bank 0
	BCF STATUS,RP1	; Bank 0
	BCF S0,7
	BTFSC PORTA,2
	GOTO LABEL_OS2_2
	BCF OS2_FD,0
LABEL_OS2_2
;***************************************************
; Schmitt-Trigger 8-Bit -> ST2
;***************************************************

	BCF STATUS,RP0	; Bank 0
	BCF STATUS,RP1	; Bank 0
	BTFSC S1,2	; Ausgang gesetzt ?
	GOTO LABEL_ST2_MH	; dann mit Hysterese ?

	MOVFW ZV3
	GOTO LABEL_ST2_TEST

LABEL_ST2_MH

	MOVLW 0	; Hysterese
	SUBWF ZV3,W
LABEL_ST2_TEST

	SUBWF ZV2,W
	BTFSS STATUS,CARRY
	GOTO LABEL_ST2_RESET

	BSF S1,2	; if greater or equal
	GOTO LABEL_ST2_ENDE

LABEL_ST2_RESET
	BCF S1,2	; if less
LABEL_ST2_ENDE

;***************************************************
; GND
;***************************************************

	BCF S1,1	; immer Low

;***************************************************
; UND - Gatter -> LG4
;***************************************************

LABEL_LG4_1
	BTFSS S0,6
	GOTO LABEL_LG4_FALSE

LABEL_LG4_TRUE
	BSF PORTB,1
	GOTO LABEL_LG4_ENDE
LABEL_LG4_FALSE
	BCF STATUS,RP0	; Bank 0
	BCF STATUS,RP1	; Bank 0
	BCF PORTB,1
LABEL_LG4_ENDE
;***************************************************
; Zähler -> ZV3
;***************************************************

	BTFSS S1,1	; Reset ?
	GOTO LABEL_ZV3_COUNT
	CLRF ZV3
	GOTO LABEL_ZV3_SETOUT

LABEL_ZV3_COUNT

; Test auf positive Flanke

	BCF STATUS,RP0	; Bank 0
	BCF STATUS,RP1	; Bank 0
	BTFSS S1,3
	GOTO LABEL_ZV3_SETFD
	BTFSC ZV3_FD,0
	GOTO LABEL_ZV3_SETFD

	BSF ZV3_FD,0


	BTFSC S1,1	; Vorwärts oder Rückwärts ?
	GOTO LABEL_ZV3_BACKWARD

	INCF ZV3,F
	GOTO LABEL_ZV3_SETOUT

LABEL_ZV3_BACKWARD
	BCF STATUS,RP0	; Bank 0
	BCF STATUS,RP1	; Bank 0
	DECF ZV3,F

LABEL_ZV3_SETOUT


LABEL_ZV3_SETFD

; Test auf negative Flanke

	BCF STATUS,RP0	; Bank 0
	BCF STATUS,RP1	; Bank 0
	BTFSC S1,3
	GOTO LABEL_ZV3_EXIT
	BCF ZV3_FD,0

LABEL_ZV3_EXIT

;***************************************************
; EE-Modul -> EE1
;***************************************************


; Test of falling edge

	BCF STATUS,RP0	; Bank 0
	BCF STATUS,RP1	; Bank 0
	BTFSC S1,3
	GOTO LABEL_EE1_SETFD
	BTFSS EE1_FD,0
	GOTO LABEL_EE1_SETFD
	BCF EE1_FD,0


	CALL WRITE_EE1

LABEL_EE1_SETFD

; test input

	BCF STATUS,RP0	; Bank 0
	BCF STATUS,RP1	; Bank 0
	BTFSS S1,3
	GOTO LABEL_EE1_EXIT
	BSF EE1_FD,0

LABEL_EE1_EXIT

;***************************************************
; One Shot -> OS3
;***************************************************


	BTFSS PORTA,0
	GOTO LABEL_OS3_1
	BTFSC OS3_FD,0
	GOTO LABEL_OS3_1
	BSF OS3_FD,0
	BSF S1,3
	GOTO LABEL_OS3_2
LABEL_OS3_1
	BCF STATUS,RP0	; Bank 0
	BCF STATUS,RP1	; Bank 0
	BCF S1,3
	BTFSC PORTA,0
	GOTO LABEL_OS3_2
	BCF OS3_FD,0
LABEL_OS3_2
;****************************************

	GOTO MAIN

	ORG H'2100'

	DE 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
	DE 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
	DE 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
	DE 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
	DE 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
	DE 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
	DE 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
	DE 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0

	END

