Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

need RTC help with pic16f877a

Status
Not open for further replies.

micropad

Member
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

This is not run in play mode in Proteus but run in step mode please help
please see coff file for simulate
 
Last edited:
None the less.. When writing to EEprom (unless its a Ramtron device) You'll need a delay before accessing again of -> 10 mS.

I know that the Dallas RTC is battery backed Sram and has a bus speed of only 100khz....

I'll stick it in ISIS and take a peek....

Your code runs fine using a 10mhz Xtal.... To see everything working attach the I2C debugger...

You do know that in ISIS you cannot use actual resistors for simulation... You must use a digital "pullup " resistor model.
 
Last edited:
Thanks Ian
I got the point, now it is working for me
now I need to put result to seven segment display
I think I need use BCD to seven segment converter, am I correct
Please advice
 
Dear Ian
Thanks for reply and help, I did it as you said, now LCD display only 00 it does not update the time
can you please advice on this . please see image
Thanks In advance

View attachment 62696
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top