

  LIST P=16F877A

;*******************RAM register definitions**********************
datai       equ     20h         ; Data input byte buffer
datao       equ     21h         ; Data output byte buffer
bytecount   equ     22h         ; Counter for byte loops
pollcnt     equ     23h         ; Counter for polling loops
loops       equ     24h         ; Delay loop counter
loops2      equ     25h         ; Delay loop counter
T1			equ     26h
T2			equ     27h
;*******************Macro definitions*****************************
WRITE_ADDR  equ     b'10100000' ; Control byte for write operations
READ_ADDR   equ     b'10100001' ; Control byte for read operations
;*******************Include file**********************************
    include "p16f877a.inc"      ; Include file for a PIC16F877A
    errorlevel  -302            ; Suppress message 302 from list file
;*****************************************************************
    org     0x000               ; Set the reset vector
    goto    start               ; Go to the beginning of main
;*******************Begin Main Program****************************
start
    call    Init                ; Initialize device
    call    ByteWrite           ; Byte Write operation 
	call	Kesleltetes
    call    ByteRead            ; Byte Read operation
	call	Kiertekeles
    nop
    goto    $-1                 ; Loop here forever

;*******************Initialization subroutine*********************
;           This routine initializes the MSSP module
;           for I2C Master mode, with a 100 kHz clock.
;*****************************************************************
Init
    bcf     STATUS,RP1          ; Select Bank 01
    bsf     STATUS,RP0

	MOVLW	B'00000000'			;PORTD MIND KIMENET
	MOVWF	TRISD				
    
	movlw   b'11111111'
    movwf   TRISC               ; Set PORTC to all inputs
    clrf    SSPSTAT             ; Disable SMBus inputs
    bsf     SSPSTAT,SMP         ; Disable slew rate control
    movlw   0x9                ; Load 0x18 into WREG /10 MHz-es kvarcnál, 4 MHz-esnél = 9
    movwf   SSPADD              ; Setup 100 kHz I2C clock
    clrf    SSPCON2             ; Clear control bits
    bcf     STATUS,RP0          ; Select Bank 00
    clrf	PORTD
	movlw   b'00101000'
    movwf   SSPCON              ; Enable SSP, select I2C Master mode
    bcf     PIR1,SSPIF          ; Clear SSP interrupt flag
    bcf     PIR2,BCLIF          ; Clear Bit Collision flag
    retlw   0

;*******************Byte write test subroutine********************
;           This routine tests the byte write feature
;           of the serial EEPROM device.  It will write
;           1 byte of data to the device at address 0x0000.
;*****************************************************************
ByteWrite
    call    BSTART              ; Generate Start condition

                                ; Send control byte
    bcf     STATUS,RP0          ; Select Bank 00
    movlw   WRITE_ADDR          ; Load control byte for write
    movwf   datao               ; Copy to datao for output
    call    TX                  ; Send control byte to device

                                ; Send word address high byte
    bcf     STATUS,RP0          ; Select Bank 00
    movlw   0x00                ; Load 0x00 for word address
    movwf   datao               ; Copy to datao for output
    call    TX

                                ; Send word address low byte
    bcf     STATUS,RP0          ; Select Bank 00
    movlw   0x00                ; Load 0x00 for word address
    movwf   datao               ; Copy to datao for output
    call    TX                  ; Send word address to device

                                ; Send data byte
    bcf     STATUS,RP0          ; Select Bank 00
    movlw   0xAA                ; Load 0xAA for data byte AA=B'10101010'
    movwf   datao               ; Copy to datao for output
    call    TX                  ; Send data byte to device

    call    BSTOP               ; Generate Stop condition

;    call    Poll                ; Poll for write completion

    retlw   0



;*******************Byte read test subroutine*********************
;           This routine tests the byte read feature
;           of the serial EEPROM device.  It will read
;           1 byte of data at address 0x0000 from the device.
;*****************************************************************
ByteRead
    call    BSTART              ; Generate Start condition

                                ; Send control byte
    bcf     STATUS,RP0          ; Select Bank 00
    movlw   WRITE_ADDR          ; Load control byte for write
    movwf   datao               ; Copy to datao for output
    call    TX                  ; Send control byte to device

                                ; Send word address high byte
    bcf     STATUS,RP0          ; Select Bank 00
    movlw   0x00                ; Load 0x00 for word address
    movwf   datao               ; Copy to datao for output
    call    TX                  ; Send high byte to device

                                ; Send word address low byte
    bcf     STATUS,RP0          ; Select Bank 00
    movlw   0x00                ; Load 0x00 for word address
    movwf   datao               ; Copy to datao for output
    call    TX                  ; Send word address to device

    call    BRESTART            ; Generate Restart condition

                                ; Send control byte
    bcf     STATUS,RP0          ; Select Bank 00
    movlw   READ_ADDR           ; Load control byte for read
    movwf   datao               ; Copy to datao for output
    call    TX                  ; Send control byte to device

                                ; Read data byte
    bsf     STATUS,RP0          ; Select Bank 01
    bsf     SSPCON2,ACKDT       ; Select to send NO ACK bit
    call    RX                  ; Read data byte from device

    call    BSTOP               ; Generate Stop condition

    retlw   0

;*****************************************************************
;
;    A kiküldött és a vett adat összehasonlítása majd kijelzése  
;            PORTD 0. bit = 1 az adatok különbözőek
;            PORTD 1. bit = 1 az adatok megegyeznek
;
;*****************************************************************
Kiertekeles
	bcf		STATUS,Z
	movf	datai,W
	sublw	B'10101010'
	btfss	STATUS,Z
	goto	Kijelzes0
	goto	Kijelzes1
Kijelzes0
	bsf		PORTD,0
	goto	Kilep
Kijelzes1
	bsf		PORTD,1
Kilep
	return


;*******************Start bit subroutine**************************
;           This routine generates a Start condition
;           (high-to-low transition of SDA while SCL
;           is still high.
;*****************************************************************
BSTART
    bcf     STATUS,RP1
    bcf     STATUS,RP0          ; Select Bank 00
    bcf     PIR1,SSPIF          ; Clear SSP interrupt flag
    bsf     STATUS,RP0          ; Select Bank 01
    bsf     SSPCON2,SEN         ; Generate Start condition
    bcf     STATUS,RP0          ; Select Bank 00
bstart_wait
    btfss   PIR1,SSPIF          ; Check if operation completed
    goto    bstart_wait         ; If not, keep checking

    retlw   0

;*******************Restart bit subroutine**************************
;           This routine generates a Repeated Start
;           condition (high-to-low transition of SDA
;           while SCL is still high.
;*****************************************************************
BRESTART
    bcf     STATUS,RP1
    bcf     STATUS,RP0          ; Select Bank 00
    bcf     PIR1,SSPIF          ; Clear SSP interrupt flag
    bsf     STATUS,RP0          ; Select Bank 01
    bsf     SSPCON2,RSEN        ; Generate Restart condition
    bcf     STATUS,RP0          ; Select Bank 00
brestart_wait
    btfss   PIR1,SSPIF          ; Check if operation completed
    goto    brestart_wait       ; If not, keep checking

    retlw   0

;*******************Stop bit subroutine***************************
;           This routine generates a Stop condition
;           (low-to-high transition of SDA while SCL
;           is still high.
;*****************************************************************
BSTOP
    bcf     STATUS,RP1
    bcf     STATUS,RP0          ; Select Bank 00
    bcf     PIR1,SSPIF          ; Clear SSP interrupt flag
    bsf     STATUS,RP0          ; Select Bank 01
    bsf     SSPCON2,PEN         ; Generate Stop condition
    bcf     STATUS,RP0          ; Select Bank 00
bstop_wait
    btfss   PIR1,SSPIF          ; Check if operation completed
    goto    bstop_wait          ; If not, keep checking

    retlw   0

;*******************Data transmit subroutine**********************
;           This routine transmits the byte of data
;           stored in 'datao' to the serial EEPROM
;           device. Instructions are also in place
;           to check for an ACK bit, if desired.
;           Just replace the 'goto' instruction,
;           or create an 'ackfailed' label, to provide
;           the functionality.
;*****************************************************************
TX
    bcf     STATUS,RP1
    bcf     STATUS,RP0          ; Select Bank 00
    bcf     PIR1,SSPIF          ; Clear SSP interrupt flag
    movf    datao,W             ; Copy datao to WREG
    movwf   SSPBUF              ; Write byte out to device
tx_wait
    btfss   PIR1,SSPIF          ; Check if operation completed
    goto    tx_wait             ; If not, keep checking
;   bsf     STATUS,RP0          ; Select Bank 01
;   btfsc   SSPCON2,ACKSTAT     ; Check if ACK bit was received
;   goto    ackfailed           ; This executes if no ACK received    

    retlw   0

;*******************Data receive subroutine***********************
;           This routine reads in one byte of data from
;           the serial EEPROM device, and stores it in
;           'datai'.  It then responds with either an
;           ACK or a NO ACK bit, depending on the value
;           of 'ACKDT' in 'SSPCON2'.
;*****************************************************************
RX
    bcf     STATUS,RP1
    bcf     STATUS,RP0          ; Select Bank 00
    bcf     PIR1,SSPIF          ; Clear SSP interrupt flag
    bsf     STATUS,RP0          ; Select Bank 01
    bsf     SSPCON2,RCEN        ; Initiate reception of byte
    bcf     STATUS,RP0          ; Select Bank 00
rx_wait
    btfss   PIR1,SSPIF          ; Check if operation completed
    goto    rx_wait             ; If not, keep checking
    movf    SSPBUF,W            ; Copy byte to WREG
    movwf   datai               ; Copy WREG to datai
    bcf     PIR1,SSPIF          ; Clear SSP interrupt flag
    bsf     STATUS,RP0          ; Select Bank 01
    bsf     SSPCON2,ACKEN       ; Generate ACK/NO ACK bit    
    bcf     STATUS,RP0          ; Select Bank 00
rx_wait2
    btfss   PIR1,SSPIF          ; Check if operation completed
    goto    rx_wait2            ; If not, keep checking

    retlw   0



;*******************Acknowledge Polling subroutine****************
;           This subroutine polls the EEPROM device
;           for an ACK bit, which indicates that the
;           internal write cycle has completed. Code
;           is in place for a timeout routine, just
;           uncomment the 'goto TimedOut' line, and
;           provide a 'TimedOut' label.
;*****************************************************************
Poll
    bcf     STATUS,RP0          ; Select Bank 00
    movlw   .40
    movwf   pollcnt             ; Set max polling times to 40
polling
    call    BRESTART            ; Generate start bit
    bcf     STATUS,RP0          ; Select Bank 00
    movlw   WRITE_ADDR          ; Now send the control byte
    movwf   datao               ; Copy control byte to buffer
    call    TX                  ; Output control byte to device
    bsf     STATUS,RP0          ; Select Bank 01
    btfss   SSPCON2,ACKSTAT     ; Was the ACK bit low?
    goto    exitpoll            ; If yes, stop polling
                                ; If no, check if polled 40 times
    bcf     STATUS,RP0          ; Select Bank 00
    decfsz  pollcnt,F           ; Is poll counter down to zero?
    goto    polling             ; If no, poll again
;   goto    TimedOut            ; If yes, part didn't respond
                                ; in time, so take action
exitpoll
    call    BSTOP               ; Generate stop bit
    retlw   0


;*****************************************************************
;
;           6 ms késleltetés az írás és az olvasás között
;
;*****************************************************************
Kesleltetes					
	MOVLW	d'255'			
	MOVWF	T1			
DEL
	MOVLW	d'8'
	MOVWF	T2				

DEL1
	DECFSZ	T2,f		
	GOTO	DEL1
	DECFSZ	T1,f		
	GOTO	DEL
	RETURN		





   END
