Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Rtc connected to pic16f877a

Status
Not open for further replies.

PA3040

Advanced Member level 3
Advanced Member level 3
Joined
Aug 1, 2011
Messages
883
Helped
43
Reputation
88
Reaction score
43
Trophy points
1,308
Activity points
6,936
Dear All,
This is I2C communication between RTC DS1307 and PIC 16f877a

Please advice. This is not working properly. Please see attachment for files

Code:
#include <p16f877a.inc>
__config 3f39
		
;RTC DS1307
cmd_byte    RES 1
Main
    BANKSEL TRISC                   ; initialize MSSP module
    bsf     TRISC,3                 ; configure PORTC<3> as input
    bsf     TRISC,4                 ; configure PORTC<4> as input
    clrf    TRISD                   ; make PORTB an output
	banksel	PORTD
	CLRF	PORTD

;################################################################################
;# STEP 1: Set the slew rate and baud rate for 100 MHZ operation
;################################################################################
    BANKSEL SSPSTAT                 ; ### point to correct BANK for SSPSTAT
    bsf     SSPSTAT,SMP             ; ### Set for Standard speed slew rate

    movlw   b'1101'
    movwf   SSPADD                  ; set the address of the slave device

;################################################################################
;# STEP 2: Configure MSSP as I2C master with Internal (Fosc/4) Clock source and
;#         enable SDA and SCL pins to operate in I2C mode (SSPCON)
;################################################################################
    BANKSEL SSPCON
    bsf     SSPCON,SSPM3            ; ### set to I2C master mode with Fosc/4
    bsf     SSPCON,SSPEN            ; ### Enable SDA and SCL for I2C

    BANKSEL SSPCON2                 ; ensure SSPCON2 is cleared
    clrf    SSPCON2

;----------------------------------- 
;   Put '0000001' into variable cmd_byte.
;   This is the read commnd that will be sent to the RTC

    BANKSEL cmd_byte
    movlw   0x05                    ; config register command byte
    movwf   cmd_byte


;-----------------------------------
; MAIN PROGRAM LOOP
;-----------------------------------    

Loop

;################################################################################
;# STEP 3: Initiate an I2C "START condition" and test for when it completes
;################################################################################
    BANKSEL SSPCON2                 ; switch to bank containing SSPCON2
    bsf     SSPCON2,SEN             ; ### Initiate a START condition
    btfsc   SSPCON2,SEN             ; ### Test if it is finished?
    goto    $-1	                    ; If not, re-execute previous instruction

;-----------------------------------

    movlw   B'11010000'             ; send address of RTC w/0 in the LSB
    BANKSEL SSPBUF                  ; send RTC ADDRESS (write)
    movwf   SSPBUF
    btfsc   SSPSTAT,R_W             ; wait for write to complete
    goto    $-1

;-----------------------------------

    BANKSEL SSPCON2                 ; Wait for ACK from RTC
    btfsc   SSPCON2,ACKSTAT         ; ack?
    goto    $-1

;-----------------------------------

    BANKSEL cmd_byte                ; send the READ command
    movf    cmd_byte,W              ; send COMMAND byte (config)
    
    BANKSEL SSPBUF
    movwf   SSPBUF
    movlw   0x00
    
    BANKSEL SSPCON2
    sublw   0x00
    btfss   STATUS,Z
    goto    $-4
    btfsc   SSPSTAT,R_W
    goto    $-1

;-----------------------------------

    BANKSEL SSPCON2                 ; get ACK from RTC
    btfsc   SSPCON2,ACKSTAT         ; ack?
    goto    $-1

    bsf     SSPCON2,RSEN            ; resend START condition   
    btfsc   SSPCON2,RSEN            ; send repeated start
    goto    $-1

    movlw   B'11010000'             ; send RTC ADDRESS
    BANKSEL SSPBUF                  ; with READ bit set
    movwf   SSPBUF
    movlw   0x00
    
    BANKSEL SSPCON2
    andwf   SSPCON2,w
    sublw   0x00
    btfss   STATUS,Z
    goto    $-4
    btfsc   SSPSTAT,R_W
    goto    $-1

;-----------------------------------
						
    BANKSEL SSPCON2                 ; get ACK from RTC
    btfsc   SSPCON2,ACKSTAT         ; ack?
    goto    $-1

    bsf     SSPCON2,RCEN            ; receive RTC
    btfsc   SSPCON2,RCEN            ; enable receive mode
    goto    $-1
    BANKSEL SSPBUF                  ; retrieve RTC reg
    movf    SSPBUF,w                ; put measured TIME into WREG

;-----------------------------------

NACK	
    BANKSEL SSPCON2                 ; send NOT-ACK condition
    bsf     SSPCON2,ACKDT
    bsf     SSPCON2,ACKEN
    btfsc   SSPCON2,ACKEN
    goto    $-1

STOP			
    bsf     SSPCON2,PEN             ; send stop condition
    btfsc   SSPCON2,PEN
    goto    $-1
    
;-----------------------------------

    BANKSEL PORTD                   ; send LSB of temp data to PORTB
    movwf   PORTD
    nop
    goto    Loop

;-----------------------------------

	END                         ; directive telling MPASM that code is done
 

Attachments

  • att.rar
    12.5 KB · Views: 70

I'm not assembly user, but I have DS1307 code in C language. I don't know you need that or not..
 

Dear Engshahrul,
Thanks for reply. I need to do it myself with assembly.can please advice any wrong with above assembly codes
Thanks
 
Last edited:

Hi,

Have not used that chip, done my own RTC using Timer1 instead.

Have a look at Maxims Pic example **broken link removed**

There are lots of tips in the forum, just search on 'ds1307'

Some other sites using assembler -
**broken link removed**
**broken link removed**
 
  • Like
Reactions: PA3040

    PA3040

    Points: 2
    Helpful Answer Positive Rating
Thanks wp100
really helpful the links
I attached the coff file to my tread, then anyone can simulate it using Proteus. please advice any wrong with my program
Thanks in advance
 

Dear All I changed my program please see

Code:
dely		equ		0x20
dely1		equ		0x21
#include <p16f877a.inc>
__config 3f39
errorlevel -302		
;RTC DS1307

			org		0x00
start		goto	main
main		clrwdt
			call	sys_init
			call	i2c
			goto	main
i2c
;################################################################################
;# STEP 1: Set the slew rate and baud rate for 100 MHZ operation
;################################################################################
    		BANKSEL SSPSTAT                 ; ### point to correct BANK for SSPSTAT
    		bsf     SSPSTAT,SMP             ; ### Set for Standard speed slew rate

   
;################################################################################
;# STEP 2: Configure MSSP as I2C master with Internal (Fosc/4) Clock source and
;#         enable SDA and SCL pins to operate in I2C mode (SSPCON)
;################################################################################
    		BANKSEL SSPCON
    		bsf     SSPCON,SSPM3            ; ### set to I2C master mode with Fosc/4
    		bsf     SSPCON,SSPEN            ; ### Enable SDA and SCL for I2C

    		BANKSEL SSPCON2                 ; ensure SSPCON2 is cleared
    		clrf    SSPCON2

LOOP
			call    I2CStart        ; Take control of the I2C bus
            movlw   b'11010000'		; DS1307|I2C_WR
            call    I2CWrite        ; Send the RTC write command
            movlw   h'00'
            call    I2CWrite        ; And the register index 
            call    I2CRestart      ; Then restart the I2C bus        
            movlw   b'11010001'		;DS1307|I2C_RD
            call    I2CWrite        ; Send the RTC read command
            call    I2CRead         ; Read the store the seconds
            banksel PORTD
            movwf   PORTD
               ; call	delay
                call    I2CAck
                call    I2CRead          ; Read the store the minutes
                banksel PORTD
                movwf   PORTD
               ; call	delay
                call    I2CAck          
                call    I2CRead          ; Read the store the hours
                banksel PORTD
                movwf   PORTD
                call    I2CAck
                call    I2CRead          ; Read the store the day
                banksel PORTD
                movwf   PORTD
                call    I2CAck
                call    I2CRead          ; Read the store the date
                banksel PORTD
                movwf   PORTD
                call    I2CAck
                call    I2CRead         ; Read the store the month
                banksel PORTD
                movwf   PORTD
                call    I2CAck
                call    I2CRead          ; Read the store the year
                banksel PORTD
                movwf   PORTD
                call    I2CAck
                call    I2CRead        
                call    I2CNak          ; Tell RTC that transfer is over
                call    I2CStop         ; And release I2C bus
				GOTO	LOOP
;===============================================================================
; I2C Communication Routines
;-------------------------------------------------------------------------------

; Generates an I2C start condition.

I2CStart
                banksel SSPCON2
                bsf     SSPCON2,SEN     ; Generate START condition
                btfsc   SSPCON2,SEN     ; And wait for it to complete
                goto    $-1
                return

; Generates an I2C restart condition.

I2CRestart
                banksel SSPCON2
                bsf     SSPCON2,RSEN    ; Generate RESTART condition
                btfsc   SSPCON2,RSEN    ; And wait for it to complete
                goto    $-1
                return

; Generates an I2C stop condition.
                
I2CStop
                banksel SSPCON2
                bsf     SSPCON2,PEN     ; Generate STOP condition
                btfsc   SSPCON2,PEN     ; And wait for it to complete
                goto    $-1        
                return

; Transmits the byte in W to the I2C bus.
 
I2CWrite
                banksel SSPBUF
                movwf   SSPBUF          ; Initiate I2C write
                banksel SSPSTAT
                btfsc   SSPSTAT,R_W     ; And wait for completion
                goto    $-1
                return

; Recieves a byte from the I2C and returns its value.

I2CRead
                banksel SSPCON2
                bsf     SSPCON2,RCEN    ; Initiate I2C read
                btfsc   SSPCON2,RCEN    ; And wait for completion
                goto    $-1
                banksel SSPBUF
                movf    SSPBUF,W        ; Then read the data value
                return

; Sends an I2C ACK signal to the slave device.

I2CAck
                banksel SSPCON2
                bcf     SSPCON2,ACKDT   ; Send acknowledgement
                bsf     SSPCON2,ACKEN
                goto    I2CWait

; Sends an I2C NAK signal to the slave device.

I2CNak
                banksel SSPCON2
                bsf     SSPCON2,ACKDT   ; Send negative acknowledgement
                bsf     SSPCON2,ACKEN                

; Waits for the current I2C action to complete.
               
I2CWait
                banksel SSPSTAT
                btfsc   SSPSTAT,R_W     ; Wait for any transmission to finish
                goto    $-1
                banksel SSPCON2
DoI2CWait      	movf    SSPCON2,W       ; Get a copy of the status flags
                andlw   b'00011111'     ; And mask out non-status bits
                skpz
                goto    DoI2CWait       ; And repeat until all I/O is done
                return
                
delay			movlw	.200
				movwf	dely
s2				decfsz	dely
				goto	s1
				return
s1				movlw	.100
				movwf	dely1
				decfsz	dely1
				goto	$-1
				goto	s2
                
                
sys_init 		
				BANKSEL TRISC                   ; initialize MSSP module
		    	bsf     TRISC,3                 ; configure PORTC<3> as input
		   	 	bsf     TRISC,4                 ; configure PORTC<4> as input
		    	clrf    TRISD                   ; make PORTD an output
				banksel	PORTD
				clrf	PORTD
				return

                end

It is working in step mode in Proteus but not in play mode
please help
 

Dear All
My I2C project working well
So I need put to the seven segment display reading from RTC result
I think RTC result in BCD
please help
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top