; Source created with Parsic Version 3.x

; at              : 16.01.2000 15:50:41


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

; Project Version : 1.0.17

; Created         : 15.06.1999 01:36:00

; Last change     : 16.01.2000 15:50:36

               PROCESSOR 16C711

               INCLUDE P16C711.INC

               RADIX DEC


               __IDLOCS H'0100'

               __CONFIG _CP_OFF & _PWRTE_ON & _WDT_OFF & _BODEN_ON & _XT_OSC

; Diverses

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


; Variable

ZAEHLER_EE               = 12               ; located in Bank 0, 1
EE1_FD               = 13               ; located in Bank 0, 1
SYS_TMP1               = 14               ; located in Bank 0, 1
SYS_TMP2               = 15               ; located in Bank 0, 1
SYS_TMP3               = 16               ; located in Bank 0, 1
SYS_TMP4               = 17               ; located in Bank 0, 1
SYS_TMP5               = 18               ; located in Bank 0, 1
SYS_TMP6               = 19               ; located in Bank 0, 1
SYS_TMP7               = 20               ; located in Bank 0, 1
MF1               = 21               ; located in Bank 0, 1
MF1_HI               = 22               ; located in Bank 0, 1
SYS_WSAV               = 23               ; located in Bank 0, 1
SYS_SSAV               = 24               ; located in Bank 0, 1
S0               = 25               ; located in Bank 0, 1
ZAEHLER_EE_FD               = 26               ; located in Bank 0, 1
RAM               = 27               ; 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 MF1,F
               GOTO SYS_INT_LABEL_0
               INCF MF1_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


;****************************************
; Subroutines
;****************************************

;*************************

EEEXT_WRITE

               MOVFW INDF
               MOVWF SYS_TMP5
               MOVLW B'00000000'
               GOTO EEEXT0

;*************************

EEEXT_READ

               MOVLW B'00010011'
               GOTO EEEXT0


EEEXT0
               MOVWF SYS_TMP3               ; offset

               MOVLW B'10100000'               ; write control
               IORWF SYS_TMP6,W               ; slave adress

;************************** Byte out *****************************

EEEXT_OUTBYTE
               MOVWF SYS_TMP2
               MOVLW 8
               MOVWF SYS_TMP1

;************************** Start *****************************

               ; SDA low

               BCF STATUS,RP0               ; Bank 0
               BCF PORTA,4
               BSF STATUS,RP0               ; Bank 1
               BCF TRISA,4
; Delay 4µ
               GOTO $+1
               GOTO $+1
EEEXT_OB1
               ; SCL low

               BCF STATUS,RP0               ; Bank 0
               BCF PORTA,3
               BSF STATUS,RP0               ; Bank 1
               BCF TRISA,3

               RLF SYS_TMP2,F

               ; SDA low

               BCF STATUS,RP0               ; Bank 0
               BCF PORTA,4
               BSF STATUS,RP0               ; Bank 1
               BCF TRISA,4
               BTFSC STATUS,CARRY
               ; SDA high

               BSF TRISA,4
; Delay 2µ
               GOTO $+1

               ; SCL high

               BSF TRISA,3
; Delay 1µ
               NOP
               DECFSZ SYS_TMP1,F
               GOTO EEEXT_OB1

;*********  Acknowledge Check ************

               ; SCL low

               BCF STATUS,RP0               ; Bank 0
               BCF PORTA,3
               BSF STATUS,RP0               ; Bank 1
               BCF TRISA,3
; Delay 1µ
               NOP
               ; SDA high

               BSF TRISA,4
; Delay 3µ
               GOTO $+1
               NOP
               ; SCL high

               BSF TRISA,3
; Delay 3µ
               GOTO $+1
               NOP
               BCF STATUS,RP0               ; Bank 0
               BTFSS PORTA,4               ; SDA
               GOTO EEEXTACK
               BSF SYS_TMP3,7
EEEXTACK
               ; SCL low

               BCF PORTA,3
               BSF STATUS,RP0               ; Bank 1
               BCF TRISA,3

               BTFSC SYS_TMP3,7               ; stop bit if error
               GOTO EEEXT_STOP

;*********** Jump table ********************

               MOVFW SYS_TMP3
               ANDLW B'00001111'
               ADDWF PCL,F

               GOTO EEEXT_WRADR               ; 0 = SEND_ADRESS
               GOTO EEEXT_WRDATA               ; 1 = SEND_DATA
               GOTO EEEXT_STOP               ; 2 = SEND_STOP

               GOTO EEEXT_WRADR               ; 3 = SEND_ADRESS
               GOTO EEEXT_INITREAD               ; 4 = INIT_READ
               GOTO EEEXT_RDDATA               ; 5 = READ_DATA

;******************************************

; Send adress

EEEXT_WRADR
               INCF SYS_TMP3,F               ; increment offset
               MOVFW SYS_TMP4               ; contains adress
               GOTO EEEXT_OUTBYTE

;**************************

; Send data

EEEXT_WRDATA
               INCF SYS_TMP3,F               ; increment offset
               MOVFW SYS_TMP5               ; contains data
               GOTO EEEXT_OUTBYTE

;**************************

; Init Read

EEEXT_INITREAD
               ; SDA high

               BSF TRISA,4
               INCF SYS_TMP3,F               ; increment offset
               ; SCL high

               BSF TRISA,3
               MOVLW B'10100001'               ; init read
               GOTO EEEXT_OUTBYTE

;**************************

; Read Data

EEEXT_RDDATA
               ; SDA high

               BSF TRISA,4
               MOVLW 8               ; set counter
               MOVWF SYS_TMP1

EEEXT_RD0
               ; SCL high

               BSF TRISA,3

               BCF STATUS,CARRY
               BCF STATUS,RP0               ; Bank 0
               BTFSC PORTA,4               ; SDA
               BSF STATUS,CARRY
               RLF SYS_TMP5,F
               ; SCL low

               BCF PORTA,3
               BSF STATUS,RP0               ; Bank 1
               BCF TRISA,3
               ; SDA high

               BSF TRISA,4
               DECFSZ SYS_TMP1,F
               GOTO EEEXT_RD0

               ; SDA high

               BSF TRISA,4
               ; SCL high

               BSF TRISA,3
; Delay 3µ
               GOTO $+1
               NOP
               ; SCL low

               BCF STATUS,RP0               ; Bank 0
               BCF PORTA,3
               BSF STATUS,RP0               ; Bank 1
               BCF TRISA,3

;*************  Generate a STOP bit and RETURN  ***********

EEEXT_STOP
               ; SDA low

               BCF STATUS,RP0               ; Bank 0
               BCF PORTA,4
               BSF STATUS,RP0               ; Bank 1
               BCF TRISA,4
; Delay 4µ
               GOTO $+1
               GOTO $+1
               ; SCL high

               BSF TRISA,3
; Delay 4µ
               GOTO $+1
               GOTO $+1
               ; SDA high

               BSF TRISA,4

               BTFSS SYS_TMP3,7
               GOTO EEEXT_NEXT
               SWAPF SYS_TMP3,W               ; again
               ANDLW B'00000011'
               ADDWF PCL,F
               GOTO EEEXT_WRITE
               GOTO EEEXT_READ

EEEXT_NEXT
               RETURN

;***************************************************
; EE-Modul -> EE1
;***************************************************

WRITE_EE1
               CLRF SYS_TMP6               ; set Busadress

               CLRF SYS_TMP4

               BCF STATUS,IRP               ; vars - located in Bank 0
               MOVLW 1
               MOVWF SYS_TMP7
               MOVLW LOW ZAEHLER_EE
               MOVWF FSR

WRITE_EE1_LABEL0
               CALL EEEXT_WRITE

               INCF FSR,F               ; next adress
               INCF SYS_TMP4,F
               DECFSZ SYS_TMP7,F
               GOTO WRITE_EE1_LABEL0

               RETURN


;***************************************************
; EE-Modul -> EE1
;***************************************************

READ_EE1
               MOVLW 0               ; set Busadress
               MOVWF SYS_TMP6

               MOVLW 0
               MOVWF SYS_TMP4

               BCF STATUS,IRP               ; vars - located in Bank 0
               MOVLW 1
               MOVWF SYS_TMP7
               MOVLW LOW ZAEHLER_EE
               MOVWF FSR

READ_EE1_LABEL1
               CALL EEEXT_READ
               MOVFW SYS_TMP5
               MOVWF INDF

               INCF FSR,F               ; next adress
               INCF SYS_TMP4,F
               DECFSZ SYS_TMP7,F
               GOTO READ_EE1_LABEL1


               RETURN


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

; init var

               CLRF ZAEHLER_EE
               CLRF EE1_FD
               CLRF SYS_TMP1
               CLRF SYS_TMP2
               CLRF SYS_TMP3
               CLRF SYS_TMP4
               CLRF SYS_TMP5
               CLRF SYS_TMP6
               CLRF SYS_TMP7
               CLRF MF1
               CLRF MF1_HI
               CLRF SYS_WSAV
               CLRF SYS_SSAV
               CLRF S0
               CLRF ZAEHLER_EE_FD


               CALL READ_EE1               ; read vars from external EE

               BCF STATUS,RP0               ; Bank 0
               CLRF TMR0
               BCF INTCON,T0IF
               BSF INTCON,GIE               ; Interrupt freigeben

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

MAIN

;***************************************************
; Anzug-Verzögerung -> MF1
;***************************************************

               BCF STATUS,RP0               ; Bank 0
               BTFSC PORTA,0               ; Eingang auf High ?
               GOTO LABEL_MF1_CHKTIME               ; Ja

; RESET

               CLRF MF1
               CLRF MF1_HI
               BCF S0,0
               GOTO LABEL_MF1_EXIT               ; raus

LABEL_MF1_CHKTIME

               BTFSC S0,0               ; Ausgang schon auf High ?
               GOTO LABEL_MF1_EXIT               ; Ja

               MOVLW 0
               SUBWF MF1_HI,W
               BTFSS STATUS,CARRY
               GOTO LABEL_MF1_EXIT

               MOVLW 195
               SUBWF MF1,W
               BTFSS STATUS,CARRY
               GOTO LABEL_MF1_EXIT

               BSF S0,0
LABEL_MF1_EXIT

;***************************************************
; Zähler -> ZAEHLER_EE
;***************************************************

               BTFSS S0,1               ; Reset ?
               GOTO LABEL_ZAEHLER_EE_COUNT
               CLRF ZAEHLER_EE
               GOTO LABEL_ZAEHLER_EE_SETOUT

LABEL_ZAEHLER_EE_COUNT

; Test auf positive Flanke

               BTFSS S0,0
               GOTO LABEL_ZAEHLER_EE_SETFD
               BTFSC ZAEHLER_EE_FD,0
               GOTO LABEL_ZAEHLER_EE_SETFD

               BSF ZAEHLER_EE_FD,0


               BTFSC S0,1               ; Vorwärts oder Rückwärts ?
               GOTO LABEL_ZAEHLER_EE_BACKWARD

               INCF ZAEHLER_EE,F
               GOTO LABEL_ZAEHLER_EE_SETOUT

LABEL_ZAEHLER_EE_BACKWARD
               DECF ZAEHLER_EE,F

LABEL_ZAEHLER_EE_SETOUT

; Output Q0

               BTFSS ZAEHLER_EE,0
               GOTO LABEL_ZAEHLER_EE_SETOUT0
               BCF STATUS,RP0               ; Bank 0
               BSF PORTB,0
               GOTO LABEL_ZAEHLER_EE_SETOUT0_END
LABEL_ZAEHLER_EE_SETOUT0
               BCF STATUS,RP0               ; Bank 0
               BCF PORTB,0
LABEL_ZAEHLER_EE_SETOUT0_END

; Output Q1

               BTFSS ZAEHLER_EE,1
               GOTO LABEL_ZAEHLER_EE_SETOUT1
               BSF PORTB,1
               GOTO LABEL_ZAEHLER_EE_SETOUT1_END
LABEL_ZAEHLER_EE_SETOUT1
               BCF PORTB,1
LABEL_ZAEHLER_EE_SETOUT1_END

; Output Q2

               BTFSS ZAEHLER_EE,2
               GOTO LABEL_ZAEHLER_EE_SETOUT2
               BSF PORTB,2
               GOTO LABEL_ZAEHLER_EE_SETOUT2_END
LABEL_ZAEHLER_EE_SETOUT2
               BCF PORTB,2
LABEL_ZAEHLER_EE_SETOUT2_END

; Output Q3

               BTFSS ZAEHLER_EE,3
               GOTO LABEL_ZAEHLER_EE_SETOUT3
               BSF PORTB,3
               GOTO LABEL_ZAEHLER_EE_SETOUT3_END
LABEL_ZAEHLER_EE_SETOUT3
               BCF PORTB,3
LABEL_ZAEHLER_EE_SETOUT3_END

; Output Q4

               BTFSS ZAEHLER_EE,4
               GOTO LABEL_ZAEHLER_EE_SETOUT4
               BSF PORTB,4
               GOTO LABEL_ZAEHLER_EE_SETOUT4_END
LABEL_ZAEHLER_EE_SETOUT4
               BCF PORTB,4
LABEL_ZAEHLER_EE_SETOUT4_END

; Output Q5

               BTFSS ZAEHLER_EE,5
               GOTO LABEL_ZAEHLER_EE_SETOUT5
               BSF PORTB,5
               GOTO LABEL_ZAEHLER_EE_SETOUT5_END
LABEL_ZAEHLER_EE_SETOUT5
               BCF PORTB,5
LABEL_ZAEHLER_EE_SETOUT5_END

; Output Q6

               BTFSS ZAEHLER_EE,6
               GOTO LABEL_ZAEHLER_EE_SETOUT6
               BSF PORTB,6
               GOTO LABEL_ZAEHLER_EE_SETOUT6_END
LABEL_ZAEHLER_EE_SETOUT6
               BCF PORTB,6
LABEL_ZAEHLER_EE_SETOUT6_END

; Output Q7

               BTFSS ZAEHLER_EE,7
               GOTO LABEL_ZAEHLER_EE_SETOUT7
               BSF PORTB,7
               GOTO LABEL_ZAEHLER_EE_SETOUT7_END
LABEL_ZAEHLER_EE_SETOUT7
               BCF PORTB,7
LABEL_ZAEHLER_EE_SETOUT7_END


LABEL_ZAEHLER_EE_SETFD

; Test auf negative Flanke

               BTFSC S0,0
               GOTO LABEL_ZAEHLER_EE_EXIT
               BCF ZAEHLER_EE_FD,0

LABEL_ZAEHLER_EE_EXIT

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

               BCF S0,1               ; immer Low

;***************************************************
; EE-Modul -> EE1
;***************************************************


; Test of falling edge

               BTFSC S0,0
               GOTO LABEL_EE1_SETFD
               BTFSS EE1_FD,0
               GOTO LABEL_EE1_SETFD
               BCF EE1_FD,0


               CALL WRITE_EE1

LABEL_EE1_SETFD

; test input

               BTFSS S0,0
               GOTO LABEL_EE1_EXIT
               BSF EE1_FD,0

LABEL_EE1_EXIT

;****************************************


               GOTO MAIN


               END

