; Source created with Parsic Version 3.x

; at              : 2009.02.07. 8:47:34


; Project File    : C:\PROGRA~1\Parsic\HE\Sz_e_h.PIC

; Project Version : 1.0.3

; Created         : 2009.02.07. 8:34:44

; Last change     : 2009.02.07. 8:47:32

	PROCESSOR 16F628

	INCLUDE P16F628.INC

	RADIX DEC


	__IDLOCS H'0100'

	__CONFIG _CP_OFF & _PWRTE_ON & _WDT_OFF & _BODEN_ON & _XT_OSC & _LVP_OFF & _MCLRE_OFF

; Diverses

ZERO	EQU Z	; ZERO-Flag
CARRY	 EQU C	; CARRY-Flag


; Variable

ZV1	= 32	; located in Bank 0
ZV1_FD	= 33	; located in Bank 0
S0	= 34	; located in Bank 0
ZV2	= 35	; located in Bank 0
ZV2_FD	= 36	; located in Bank 0
LT1_FD	= 37	; located in Bank 0
LT2_FD	= 38	; located in Bank 0
TR1	= 39	; located in Bank 0
TR1_HI	= 40	; located in Bank 0
RAM	= 41	; located in Bank 0
SYS_TMP1	= 112	; located in Bank 0, 1, 2, 3
SYS_TMP2	= 113	; located in Bank 0, 1, 2, 3
SYS_TMP3	= 114	; located in Bank 0, 1, 2, 3
SYS_TMP4	= 115	; located in Bank 0, 1, 2, 3
SYS_TMP5	= 116	; located in Bank 0, 1, 2, 3
SYS_TMP6	= 117	; located in Bank 0, 1, 2, 3
SYS_TMP9	= 118	; located in Bank 0, 1, 2, 3
SYS_TMP10	= 119	; located in Bank 0, 1, 2, 3
SYS_WSAV	= 120	; located in Bank 0, 1, 2, 3
SYS_SSAV	= 121	; located in Bank 0, 1, 2, 3
SYS_FSAV	= 122	; 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
;****************************************

;***************************************************
; Subroutines for LB1
;***************************************************

LCD_LB1_INIT
	BCF STATUS,RP0	; Bank 0
	BCF STATUS,RP1	; Bank 0
	BCF PORTB,4	; ir-register
LCD_LB1_L1
	CALL LCD_LB1_OUT4
	DECFSZ SYS_TMP1,F
	GOTO LCD_LB1_L1

	MOVFW PORTB
	ANDLW H'F0'
	IORLW B'0011'	; Function Set
	MOVWF PORTB

	BSF PORTB,5	; write
	GOTO $+1
	BCF PORTB,5

	BSF SYS_TMP1,5	; wait
LCD_LB1_L2
	CALL LCD_LB1_OUT4
	DECFSZ SYS_TMP1,F
	GOTO LCD_LB1_L2

	BSF PORTB,5	; write
	GOTO $+1
	BCF PORTB,5

	CALL LCD_LB1_OUT4

	BSF PORTB,5	; write
	GOTO $+1
	BCF PORTB,5

	CALL LCD_LB1_OUT4

	BCF PORTB,0	; 4-Bit Interface

	BSF PORTB,5	; write
	GOTO $+1
	BCF PORTB,5

	CALL LCD_LB1_OUT4

	MOVLW B'00101000'	; Function Set 4-Bit,2-Lines
	CALL LCD_LB1_OUT

	MOVLW B'00001100'	; Display on, Cursor off, Blink off
	CALL LCD_LB1_OUT

	MOVLW B'00000110'	; Entrymode set
	CALL LCD_LB1_OUT

LCD_LB1_CLR
	BCF STATUS,RP0	; Bank 0
	BCF STATUS,RP1	; Bank 0
	BCF PORTB,4	; ir-register
	MOVLW B'00000001'	; Display clear
	CALL LCD_LB1_OUT

	BSF SYS_TMP1,5	; wait
LCD_LB1_L3
	CALL LCD_LB1_OUT4
	DECFSZ SYS_TMP1,F
	GOTO LCD_LB1_L3
	GOTO LCD_LB1_OUT4

;***************************************************

LCD_LB1_GOTOXY
	BCF STATUS,RP0	; Bank 0
	BCF STATUS,RP1	; Bank 0
	BCF PORTB,4	; ir-register
	MOVLW H'40'
	BTFSC SYS_TMP2,0	; Ypos
	ADDWF SYS_TMP1,F
	MOVLW D'20'
	BTFSC SYS_TMP2,1	; Ypos
	ADDWF SYS_TMP1,F
	BSF SYS_TMP1,7	; select dd-ram
	GOTO LCD_LB1_OUT1

;***************************************************

LCD_LB1_WRITESTR
	BCF STATUS,RP0	; Bank 0
	BCF STATUS,RP1	; Bank 0
	BSF PORTB,4	; dd-ram
LCD_LB1_WRITESTR1
	CALL LCD_LB1_CHAROUT
	BSF INTCON,GIE
	ANDLW H'FF'	; last is zero
	BTFSC STATUS,ZERO
	RETURN

	MOVWF SYS_TMP1
	CALL LCD_LB1_OUT1
	INCFSZ SYS_TMP4,F	; next char
	GOTO LCD_LB1_WRITESTR1
	INCF SYS_TMP5,F	; next page
	GOTO LCD_LB1_WRITESTR1

LCD_LB1_CHAROUT
	BCF INTCON,GIE
	MOVFW SYS_TMP5
	MOVWF PCLATH
	MOVFW SYS_TMP4
	MOVWF PCL

;***************************************************

LCD_LB1_DEZO10000

	MOVLW LOW 10000
	MOVWF SYS_TMP1
	MOVLW HIGH 10000
	MOVWF SYS_TMP2

	CALL LCD_LB1_CONVERTDEZ
	MOVF SYS_TMP9,F
	BTFSS STATUS,ZERO
	GOTO LCD_LB1_DEZO1000
	CALL LCD_LB1_CHECKNULL

LCD_LB1_DEZO1000

	MOVLW LOW 1000
	MOVWF SYS_TMP1
	MOVLW HIGH 1000
	MOVWF SYS_TMP2

	CALL LCD_LB1_CONVERTDEZ
	MOVF SYS_TMP9,F
	BTFSS STATUS,ZERO
	GOTO LCD_LB1_DEZO100
	CALL LCD_LB1_CHECKNULL

LCD_LB1_DEZO100

	MOVLW LOW 100
	MOVWF SYS_TMP1
	CLRF SYS_TMP2

	CALL LCD_LB1_CONVERTDEZ
	MOVF SYS_TMP9,F
	BTFSS STATUS,ZERO
	GOTO LCD_LB1_DEZO10
	CALL LCD_LB1_CHECKNULL

LCD_LB1_DEZO10

	MOVLW LOW 10
	MOVWF SYS_TMP1
	CLRF SYS_TMP2

	CALL LCD_LB1_CONVERTDEZ
	MOVF SYS_TMP9,F
	BTFSS STATUS,ZERO
	GOTO LCD_LB1_DEZO1
	CALL LCD_LB1_CHECKNULL

LCD_LB1_DEZO1
	MOVLW 48
	ADDWF SYS_TMP4,W
	GOTO LCD_LB1_WRITECHAR

;***************************************************

LCD_LB1_CONVERTDEZ
	MOVF SYS_TMP9,F
	BTFSS STATUS,ZERO
	DECF SYS_TMP9,F
	CLRF SYS_TMP6
LCD_LB1_CONVERTDEZ2
	INCF SYS_TMP6,F

	MOVFW SYS_TMP1
	SUBWF SYS_TMP4,F

	MOVFW SYS_TMP2
	BTFSS STATUS,CARRY
	INCFSZ SYS_TMP2,W
	SUBWF SYS_TMP5,F
	BTFSC STATUS,CARRY
	GOTO LCD_LB1_CONVERTDEZ2

	MOVFW SYS_TMP2
	ADDWF SYS_TMP5,F
	MOVFW SYS_TMP1
	ADDWF SYS_TMP4,F
	BTFSC STATUS,CARRY
	INCF SYS_TMP5,F

	DECF SYS_TMP6,W
	BTFSS STATUS,ZERO
	BSF SYS_TMP10,0
	RETURN

;***************************************************

LCD_LB1_CONVERTHEX
	ANDLW 15
	MOVWF SYS_TMP1
	MOVLW 48
	ADDWF SYS_TMP1,F
	MOVLW 58
	SUBWF SYS_TMP1,W
	BTFSS STATUS,CARRY
	GOTO $+3
	MOVLW 7
	ADDWF SYS_TMP1,F
	MOVFW SYS_TMP1

;***************************************************

LCD_LB1_WRITECHAR
	BCF STATUS,RP0	; Bank 0
	BCF STATUS,RP1	; Bank 0
	BSF PORTB,4	; dd-ram

;***************************************************

LCD_LB1_OUT
	MOVWF SYS_TMP1	; store
LCD_LB1_OUT1
	CLRF SYS_TMP3	; LCDWriteStr (first nibble)
LCD_LB1_OUT2
	SWAPF SYS_TMP1,W	;high nibble
LCD_LB1_OUT3
	ANDLW H'0F'	; mask
	MOVWF SYS_TMP2	; store
	BCF STATUS,RP0	; Bank 0
	BCF STATUS,RP1	; Bank 0
	MOVFW PORTB	; load port
	ANDLW H'F0'	; mask
	IORWF SYS_TMP2,W	; add cmd
	MOVWF PORTB	; out

	BSF PORTB,5	; E high
	GOTO $+1
	BCF PORTB,5	; E low

	BTFSC SYS_TMP3,3	; ready ?
	GOTO LCD_LB1_OUT4	; yes
	BSF SYS_TMP3,3	; second nibble
	MOVFW SYS_TMP1
	GOTO LCD_LB1_OUT3	; next nibble

LCD_LB1_OUT4
LCD_LB1_OUTV
	DECFSZ SYS_TMP3,F
	GOTO LCD_LB1_OUTV
	RETURN

;***************************************************

LCD_LB1_CHECKNULL
	BTFSS STATUS,ZERO
	DECF SYS_TMP9,F
	MOVWF SYS_TMP1
	BTFSS STATUS,ZERO
	BSF SYS_TMP10,0
	MOVLW 48
	BTFSC SYS_TMP10,0
	ADDWF SYS_TMP1,f
	MOVLW 32
	BTFSS SYS_TMP10,0
	ADDWF SYS_TMP1,F
	MOVFW SYS_TMP1
	GOTO LCD_LB1_WRITECHAR
; 00D2

;****************************************
;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 143
	BSF STATUS,RP0	; Bank 1
	MOVWF OPTION_REG


; Richtung setzen

	MOVLW B'11111111'
	MOVWF TRISA

	MOVLW B'11000000'
	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


;***************************************************
; Init LCD -> LB1
;***************************************************

	CALL LCD_LB1_INIT

	BCF STATUS,RP0	; Bank 0
	BCF STATUS,RP1	; Bank 0
	CLRF TMR0
	BCF INTCON,T0IF
	BSF INTCON,GIE	; Interrupt freigeben

;**********************************
;******* Haupt-Programm ***********
;**********************************

MAIN

;***************************************************
; Zähler -> ZV1
;***************************************************

	BCF STATUS,RP0	; Bank 0
	BCF STATUS,RP1	; Bank 0
	BTFSS S0,0	; 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,2
	GOTO LABEL_ZV1_SETFD
	BTFSC ZV1_FD,0
	GOTO LABEL_ZV1_SETFD

	BSF ZV1_FD,0


	BTFSC S0,1	; 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,2
	GOTO LABEL_ZV1_EXIT
	BCF ZV1_FD,0

LABEL_ZV1_EXIT

;***************************************************
; Vergleich -> IF1
;***************************************************

	MOVLW 100
	BCF STATUS,RP0	; Bank 0
	BCF STATUS,RP1	; Bank 0
	SUBWF ZV2,W	; F - W -> W

LABEL_IF1_CHECK
	BCF STATUS,RP0	; Bank 0
	BCF STATUS,RP1	; Bank 0
	BTFSS STATUS,CARRY	; Test auf -> >=
	GOTO LABEL_IF1_FALSE

LABEL_IF1_TRUE
	BSF S0,3
	GOTO LABEL_IF1_EXIT

LABEL_IF1_FALSE
	BCF S0,3

LABEL_IF1_EXIT
;***************************************************
; Schmitt-Trigger 8-Bit -> ST1
;***************************************************

	BTFSC S0,0	; 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,0	; if greater or equal
	GOTO LABEL_ST1_ENDE

LABEL_ST1_RESET
	BCF S0,0	; if less
LABEL_ST1_ENDE

;***************************************************
; Zähler -> ZV2
;***************************************************

	BTFSS S0,3	; 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 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

	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 S0,4
	GOTO LABEL_ZV2_EXIT
	BCF ZV2_FD,0

LABEL_ZV2_EXIT

;***************************************************
; LCD -> LT1
;***************************************************

; test of positiv edge

	BCF STATUS,RP0	; Bank 0
	BCF STATUS,RP1	; Bank 0
	BTFSC LT1_FD,0
	GOTO LABEL_LT1_SETFD
	BTFSS S0,6
	GOTO LABEL_LT1_SETFD


	MOVLW 5
	MOVWF SYS_TMP1
	MOVLW 0
	MOVWF SYS_TMP2

	CALL LCD_LB1_GOTOXY

	BCF STATUS,RP0	; Bank 0
	BCF STATUS,RP1	; Bank 0
	MOVFW ZV1
	MOVWF SYS_TMP4
	CLRF SYS_TMP5

	BSF SYS_TMP10,0
	MOVLW 2
	MOVWF SYS_TMP9
	CALL LCD_LB1_DEZO100
LABEL_LT1_SETFD

	BCF STATUS,RP0	; Bank 0
	BCF STATUS,RP1	; Bank 0
	BTFSS S0,6
	GOTO LABEL_LT1_SETFD2
	BSF LT1_FD,0
	GOTO LABEL_LT1_END
LABEL_LT1_SETFD2
	BCF STATUS,RP0	; Bank 0
	BCF STATUS,RP1	; Bank 0
	BCF LT1_FD,0
LABEL_LT1_END

;***************************************************
; LCD -> LT2
;***************************************************

; test of positiv edge

	BTFSC LT2_FD,0
	GOTO LABEL_LT2_SETFD
	BTFSS S0,6
	GOTO LABEL_LT2_SETFD


	MOVLW 5
	MOVWF SYS_TMP1
	MOVLW 2
	MOVWF SYS_TMP2

	CALL LCD_LB1_GOTOXY

	BCF STATUS,RP0	; Bank 0
	BCF STATUS,RP1	; Bank 0
	MOVFW ZV2
	MOVWF SYS_TMP4
	CLRF SYS_TMP5

	BSF SYS_TMP10,0
	MOVLW 2
	MOVWF SYS_TMP9
	CALL LCD_LB1_DEZO100
LABEL_LT2_SETFD

	BCF STATUS,RP0	; Bank 0
	BCF STATUS,RP1	; Bank 0
	BTFSS S0,6
	GOTO LABEL_LT2_SETFD2
	BSF LT2_FD,0
	GOTO LABEL_LT2_END
LABEL_LT2_SETFD2
	BCF STATUS,RP0	; Bank 0
	BCF STATUS,RP1	; Bank 0
	BCF LT2_FD,0
LABEL_LT2_END

;***************************************************
; Timer -> TR1
;***************************************************

	MOVLW 0	; Test auf >=
	SUBWF TR1_HI,W	;F-W -> W
	BTFSS STATUS,ZERO
	GOTO LABEL_TR1_CHECK
	MOVLW 39
	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 39
	SUBWF TR1,F	;F-W -> F
	BTFSS STATUS,CARRY
	DECF TR1_HI,F

LABEL_TR1_SET	; toggel output
	BTFSC S0,6
	GOTO LABEL_TR1_CLR
	BSF S0,6
	GOTO LABEL_TR1_ENDE
LABEL_TR1_CLR
	BCF S0,6
LABEL_TR1_ENDE

;****************************************

	GOTO MAIN






	END

