+ Post New Thread
Results 1 to 6 of 6
  1. #1
    Newbie level 3
    Points: 317, Level: 3

    Join Date
    Apr 2011
    Posts
    3
    Helped
    0 / 0
    Points
    317
    Level
    3

    Code ASM for i2c of pic16f877a and ds1307 rtc

    i'm working on i2c comunication between pic 16f877a and ds1307. I'm done but i have a problem:





    As you can see, when the i2c debugger is employed, my circuit work so well! But when i remove the i2c debugger, it don't work. Anny body help? please.Sorry for my bad english

    Code:
    		#DEFINE	ADDR_RTC	0xD0
    		#DEFINE	ADDRESS		0x07
    ;*******************************************		
    		LIST		p=PIC16F877A
    		INCLUDE 	<P16F877A.INC>
    		__CONFIG _CP_OFF & _WDT_OFF & _BODEN_OFF & _PWRTE_ON & _XT_OSC & _WRT_OFF & _LVP_OFF & _CPD_OFF
    ;*******************************************
    ;	CODE GIAO TIEP I2C VOI DS1307 CHE DO MASTER
    ;	TINH TOAN HIEN THI RA LCD
    ;	GIAO TIEP LCD 4BIT
    ;	RB7-RB4 = DB7-DB4  ; RB3=E ; RB2=RW ; RB1=RS
    ;	SU DUNG KHOI I2C CO SAN CUA 16F87X
    ;*******************************************		
    
    ;	PHAN KHAI BAO
    BIT_E	EQU		3
    BIT_RW	EQU		2
    BIT_RS	EQU		1
    
    
    CBLOCK		0x20
    		SECOND				;CAC BIEN LUU THOI GIAN
    		MINUTE
    		HOUR
    		DAY
    		DATE
    		MONTH
    		YEAR
    		L					;BIEN L VA H DUNG TRONG CHUONG TRINH CON_V
    		H
    		TEMP				;LUU TRU TAM THOI CHO LCD
    		counta				;CAC BIEN DUNG CHO HAM DELAY
    		countb
    		count1
    		COUNT
    ENDC
    		ORG 	0x0000 
    		GOTO	START
    		
    		ORG		0x0004
    		GOTO	INTR
    		ORG		0x0005
    START
    		CALL	INITPIC			;KHOI TAO PIC
    		CALL	INITLCD			;KHOI TAO CHO LCD
    		CALL	INIT_I2C		;KHOI TAO GIAO TIEP I2C
    		GOTO	$
    
    INTR
    		CALL	READ_TIME
    		CALL	DISPLAY_TIME
    		BCF		INTCON,1
    		RETFIE
    		
    	;***KHOI TAO CAC CHAN CHO PIC***************************************
    INITPIC
    		CLRF	PORTB
    		BANKSEL	TRISB
    		MOVLW	B'00000001'
    		MOVWF	TRISB
    
    		MOVLW	B'11111111'
    		MOVWF	TRISC
    		
    		BANKSEL	INTCON
    		CLRF	INTCON
    		BSF		INTCON,GIE
    		BSF		INTCON,INTE
    		RETLW	0
    
    
    
    	;***CHUONG TRINH KHOI TAO CHO I2C BUS HOAT DONG*********************
    INIT_I2C 
           
            BANKSEL SSPCON          	; chon MSSP che do MASTER MODE 
            MOVLW   B'00101000'     	; Thiet lap MSSP cho chan PORTC o che do I2C 
            MOVWF   SSPCON           	; SSPEN, SSPM BITS 
    
            BANKSEL SSPSTAT          	;Thiet lap che do chuan cua toc do baud ( SMP= 1 )  
            MOVLW   B'10000000'      	; Toc do chuan la 100Kb 
            MOVWF   SSPSTAT     
    
            BANKSEL SSPADD            	; Thiet lap toc do duong truyen 
            MOVLW   0x28             	; Tinh toan gia tri cho thiet lap toc do 
            MOVWF   SSPADD
    
    		CALL    STARTBit 
    		;***GUI TIN HIEU START***************
            
    		MOVLW   ADDR_RTC
            CALL    WRITE_I2C 
    		;***GUI DIA CHI SLAVE****************
            
    		MOVLW   ADDRESS
            CALL    WRITE_I2C 
    		;***GUI DIA CHI BYTE CONTROL***********
    		
    		MOVLW   0x90
            CALL    WRITE_I2C
    		;*************GUI BYTE DIEU KHIEN CHI BYTE CONTROL***********
            
    		CALL    STOPBit 
    		;***GUI TIN HIEU DUNG GIAO TIEP***********       
            
    		RETLW   0x00
    
    	;******CHUONG TRINH DOC VAO THOI GIAN TU SD1307***************
    READ_TIME 
            CALL    STARTBit          ; Gui bit Start 
    ;************GUI BIT START***********
            MOVLW   ADDR_RTC          ; Load CONTROL BYTE  ( cho viec xuat du lieu ) 
            CALL    WRITE_I2C 
    
    ;************GUI CONTROL IN**************
            MOVLW   0x00         ; Load ADDRESS Byte vao W 
            CALL    WRITE_I2C
    		 
    ;************GUI DIA CHI BYTE DAU**************     
            CALL	STOPBit
    		CALL 	STARTBit 
    ;************QUA TRINH RESTART**************       
            MOVLW   0xD1
            CALL    WRITE_I2C
    
    ;************GUI CONTROL OUT*****************   
            BSF     STATUS,RP0 
            BTFSC   SSPSTAT,2         ; kiem tra bit R/W 
            GOTO    $-1 
    
            BANKSEL SSPCON2 
            BSF     SSPCON2,RCEN      ; Cho phep RECEIVE Mode (I2C )          ; loi o cho nay day 
            CALL    WaitMSSP
     	
    
            BCF     STATUS,RP0 
            BCF     PIR1,SSPIF
    		CALL	ACKBit
    
    		BANKSEL SSPBUF
            MOVF    SSPBUF,W
    		MOVWF	SECOND
    ;************DOC BYTE SECOND*****************
            BSF     STATUS,RP0 
            BTFSC   SSPSTAT,2         ; kiem tra bit R/W 
            GOTO    $-1 
    
            BANKSEL SSPCON2 
            BSF     SSPCON2,RCEN      ; Cho phep RECEIVE Mode (I2C )          ; loi o cho nay day 
            CALL    WaitMSSP
     	
    
            BCF     STATUS,RP0 
            BCF     PIR1,SSPIF
    		CALL	ACKBit
    
    		BANKSEL SSPBUF
            MOVF    SSPBUF,W
            MOVWF	MINUTE
    
    ;************DOC BYTE PHUT*****************
            BSF     STATUS,RP0 
            BTFSC   SSPSTAT,2         ; kiem tra bit R/W 
            GOTO    $-1 
    
            BANKSEL SSPCON2 
            BSF     SSPCON2,RCEN      ; Cho phep RECEIVE Mode (I2C )          ; loi o cho nay day 
            CALL    WaitMSSP
     	
    
            BCF     STATUS,RP0 
            BCF     PIR1,SSPIF
    		CALL	ACKBit
    
    		BANKSEL SSPBUF
            MOVF    SSPBUF,W
    		MOVWF	HOUR
    ;************DOC BYTE GIO*****************
            BSF     STATUS,RP0 
            BTFSC   SSPSTAT,2         ; kiem tra bit R/W 
            GOTO    $-1 
    
            BANKSEL SSPCON2 
            BSF     SSPCON2,RCEN      ; Cho phep RECEIVE Mode (I2C )          ; loi o cho nay day 
            CALL    WaitMSSP
     	
    
            BCF     STATUS,RP0 
            BCF     PIR1,SSPIF
    		CALL	ACKBit
    
    		BANKSEL SSPBUF
            MOVF    SSPBUF,W
    		MOVWF	DAY
    ;************DOC BYTE THU*****************
            BSF     STATUS,RP0 
            BTFSC   SSPSTAT,2         ; kiem tra bit R/W 
            GOTO    $-1 
    
            BANKSEL SSPCON2 
            BSF     SSPCON2,RCEN      ; Cho phep RECEIVE Mode (I2C )          ; loi o cho nay day 
            CALL    WaitMSSP
     	
    
            BCF     STATUS,RP0 
            BCF     PIR1,SSPIF
    		CALL	ACKBit
    
    		BANKSEL SSPBUF
            MOVF    SSPBUF,W
    		MOVWF	DATE
    ;************DOC BYTE NGAY*****************
            BSF     STATUS,RP0 
            BTFSC   SSPSTAT,2         ; kiem tra bit R/W 
            GOTO    $-1 
    
            BANKSEL SSPCON2 
            BSF     SSPCON2,RCEN      ; Cho phep RECEIVE Mode (I2C )          ; loi o cho nay day 
            CALL    WaitMSSP
     	
    
            BCF     STATUS,RP0 
            BCF     PIR1,SSPIF
    		CALL	ACKBit
    
    		BANKSEL SSPBUF
            MOVF    SSPBUF,W
    		MOVWF	MONTH
    ;************DOC BYTE THANG*****************
            BSF     STATUS,RP0 
            BTFSC   SSPSTAT,2         ; kiem tra bit R/W 
            GOTO    $-1 
    
            BANKSEL SSPCON2 
            BSF     SSPCON2,RCEN      ; Cho phep RECEIVE Mode (I2C )          ; loi o cho nay day 
            CALL    WaitMSSP
     	
    
            BCF     STATUS,RP0 
            BCF     PIR1,SSPIF
    		CALL	NACKBit
    
    		BANKSEL SSPBUF
            MOVF    SSPBUF,W
    		MOVWF	YEAR
    ;************DOC NAM*****************		
    		CALL	STOPBit
    		
    		RETURN
    ;********KET THUC HAM DOC THOI GIAN TU DS1307***************
    		
    		;****CHUONG TRINH GUI 1 BYTE TA I2C pdlong*****************
    WRITE_I2C
            BANKSEL SSPBUF
            MOVWF   SSPBUF
            CALL    WaitMSSP 
            RETLW   0x00  
    
    		;****CHUONG TRINH CON GUI TIN HIEU START************
    STARTBit 
    		BANKSEL SSPCON2 
            BSF     SSPCON2,SEN       ; Gui bit start 
    		CALL    WaitMSSP    
            RETLW   0x00 
    		
    		;***CHUONG TRINH CON GUI BIT RESTART****************
    RESTARTBit 
            BANKSEL SSPCON2 
            BSF     SSPCON2,RSEN      ; Gui bit restart  
            CALL    WaitMSSP    
            RETLW   0x00  
    		
    		;***CHUONG TRINH CON GUI TIN HIEU STOP**************
    STOPBit 
            BANKSEL SSPCON2           ; Gui va kiem tra bit Stop, doi cho den khi hoan thanh xong 
            BSF     SSPCON2,PEN       ; gui bit Stop 
            CALL    WaitMSSP 
            RETLW   0x00 
    		
    		;***CHUONG TRINH CON GUI TIN HIEU ACK***************
    ACKBit 
            BANKSEL SSPCON2 
            BCF     SSPCON2,ACKDT     ; Chon gui ACK 
            BSF     SSPCON2,ACKEN     ; Gui 
            CALL    WaitMSSP 
            RETLW   0x00  
    		
    		;***CHUONG TRINH CON GUI TIN HIEU NACK**************
    NACKBit 
            BANKSEL SSPCON2 
            BSF     SSPCON2,ACKDT     ; Chon gui NACK 
            BSF     SSPCON2,ACKEN     ; Gui di 
            CALL    WaitMSSP 
            RETLW   0x00  
    		
    		;***CHUONG TRINH KIEM TRA TRANG THAI HOAN THANH VIEC TRUYEN NHAN*********************
    WaitMSSP 
            BANKSEL PIR1 
            BTFSS   PIR1,SSPIF        ;KIEM TRA TRANG THAI CO
            GOTO    $-1               ;CHUA XONG
            BCF     PIR1,SSPIF        ;DA XONG, KET THUC VONG LAP
            RETLW   0
    		
    		
    ;************************************************************
    INITLCD							;KHOI TAO CHO LCD
    		CALL	Del100			;CHO LCD
    		
    			
    		MOVLW	B'00111000'		;CAC HAM KHOI TAO
    		MOVWF	PORTB
    		BCF		PORTB,BIT_E	
    		
    		CALL	Del5 
    		
    
    		MOVLW	B'00111000'
    		MOVWF	PORTB
    		BCF		PORTB,BIT_E
    
    		CALL	Del5 
    		
    
    		MOVLW	B'00111000'
    		MOVWF	PORTB
    		BCF		PORTB,BIT_E
    					
    		CALL	LCDBUSY			;KIEM TRA CO FLAG
    
    		MOVLW	B'00101000'		;CHE DO GIAO TIEP 4BIT
    		MOVWF	PORTB
    		BCF		PORTB,BIT_E		;CAC LENH VE SAU SE DUOC GUI LAM 2 LAN
    							
    		CALL	LCDBUSY	
    		MOVLW	B'00101000'
    		MOVWF	PORTB
    		BCF		PORTB,BIT_E		;LAN 1
    		MOVLW	B'10001000'
    		MOVWF	PORTB
    		BCF		PORTB,BIT_E		;LAN 2
    		
    		CALL	LCDBUSY			;DIEU KHIEN VIEC HIEN THI
    		MOVLW	B'00001000'		
    		MOVWF	PORTB
    		BCF		PORTB,BIT_E		;LAN 1
    		MOVLW	B'11001000'		;DISP=ON,CURSOR=OFF,BLINK=OFF, BIT_E=1,BIT_RW=0,BIT_RS=0
    		MOVWF	PORTB
    		BCF		PORTB,BIT_E		;LAN 2
    		
    		CALL	LCDBUSY			;XOA MAN HINH
    		MOVLW	B'00001000'
    		MOVWF	PORTB
    		BCF		PORTB,BIT_E
    		MOVLW	B'00011000'
    		MOVWF	PORTB
    		BCF		PORTB,BIT_E
    		
    		CALL	LCDBUSY			;SET CHE DE ENTRY
    		MOVLW	B'00001000'		; BIT_E=1,BIT_RW=0,BIT_RS=0
    		MOVWF	PORTB
    		BCF		PORTB,BIT_E		;LAN 1
    		MOVLW	B'01101000'		;ENTRY SET - TANG CON TRO, KHONG DICH,BIT_E=1,BIT_RW=0,BIT_RS=0
    		MOVWF	PORTB
    		BCF		PORTB,BIT_E		;LAN 2
    		
    		RETURN
    ;********************************************************
    LCDBUSY							;HAM KIEM TRA CO BAN CUA LCD BUSY FLAG
    		BSF		STATUS,RP0		;CHON BANK1
    		MOVLW	B'11110001'
    		MOVWF	TRISB			;CHUYEN CAC CHAN DATA THANH INPUT
    		BCF		STATUS,RP0		;CHON BANK0
    		
    		BSF		PORTB,BIT_RW	;CHE DO DC DU LIEU
    		BCF		PORTB,BIT_RS	;CHE DO LENH
    		
    		BSF		PORTB,BIT_E
    		MOVF	PORTB,W			;DOC CHAN B VAO W
    		BCF		PORTB,BIT_E		;DOC 4 BIT CAO
    		BSF		PORTB,BIT_RW
    		BCF		PORTB,BIT_RS
    		BSF		PORTB,BIT_E
    		NOP						
    		BCF		PORTB,BIT_E		;DOC 4 BIT THAP
    		
    		ANDLW	0x80
    		BTFSS	STATUS,Z		;KIEM TRA CO BAN
    		GOTO	LCDBUSY			;NEU BAN THI LAP LAI
    								;CON KHONG THI TIEP TUC
    		BCF		PORTB,BIT_RW
    		BSF		STATUS,RP0		;DUA CHAN B TRO LAI THANH OUTPUT
    		MOVLW	B'00000001'
    		MOVWF	TRISB
    		BCF		STATUS,RP0
    		
    		RETURN
    ;**********************************************************
    LINE2							;HAM XUONG DONG VA DUA CON TRO VE DAU DONG
    		CALL	LCDBUSY
    		MOVLW	B'11001000'
    		MOVWF	PORTB
    		BCF		PORTB,BIT_E
    		MOVLW	B'00001000'
    		MOVWF	PORTB
    		BCF		PORTB,BIT_E
    		RETURN
    		
    ;**********************************************************
    LINE1							;HAM DUA CON TRO VE DAU DONG 1 VA XOA MAN HINH
    		CALL	LCDBUSY
    		MOVLW	B'00001000'
    		MOVWF	PORTB
    		BCF		PORTB,BIT_E
    		MOVLW	B'00011000'
    		MOVWF	PORTB
    		BCF		PORTB,BIT_E
    		RETURN
    
    ;**********************************************************
    
    LCDWRITE						;VIET KI TU TRONG W RA LCD
    		MOVWF	TEMP
    		CALL	LCDBUSY
    		MOVF	TEMP,W
    		ANDLW	B'11110000'
    		IORLW	B'00001010'		;BIT_E=1,BIT_RW=0,BIT_RS=1
    		MOVWF	PORTB
    		BCF		PORTB,BIT_E		;GUI 4 BIT CAO
    		SWAPF	TEMP,W			;SWAP NIBBLES AND STORE IN W REGISTER
    		ANDLW	B'11110000'
    		IORLW	B'00001010'		;BIT_E=1,BIT_RW=0,BIT_RS=1
    		MOVWF	PORTB
    		BCF		PORTB,BIT_E		;GUI 4BIT THAP
    		RETURN
    ;**********************************************************
    CONV							;TACH CAC GIA TRI THOI GIAN TRONG THANH GI W
    		MOVWF	TEMP
    		SWAPF	TEMP,W
    		ANDLW	B'00001111'
    		ADDLW	0x30
    		MOVWF	H
    		MOVF	TEMP,W
    		ANDLW	B'00001111'
    		ADDLW	0x30
    		MOVWF	L
    		RETURN
    		
    	;***HAM HIEN THI CAC GIA TRI THOI GIAN LEN LCD
    DISPLAY_TIME
    		
    		CALL 	LINE1
    		MOVF	HOUR,W
    		CALL	CONV
    		
    		MOVF	H,W
    		CALL	LCDWRITE
    		
    		MOVF	L,W
    		CALL	LCDWRITE
    		;***KET THUC HIEN THI GIO********
    		
    		MOVLW	0X3A			; DAU HAI CHAM
    		CALL	LCDWRITE
    		
    		MOVF	MINUTE,W
    		CALL	CONV
    		
    		MOVF	H,W
    		CALL	LCDWRITE
    		
    		MOVF	L,W
    		CALL	LCDWRITE
    		;***KET THUC HIEN THI PHUT********
    		
    		MOVLW	0X3A			; DAU HAI CHAM
    		CALL	LCDWRITE
    		
    
    		MOVF	SECOND,W
    		CALL	CONV
    		
    		MOVF	H,W
    		CALL	LCDWRITE
    		
    		MOVF	L,W
    		CALL	LCDWRITE
    		;***KET THUC HIEN THI GIAY********
    		
    		CALL	LINE2		;XUONG DONG
    		
    		MOVF	DATE,W
    		CALL	CONV
    		
    		MOVF	H,W
    		CALL	LCDWRITE
    		
    		MOVF	L,W
    		CALL	LCDWRITE
    		;***KET THUC HIEN THI NGAY********
    		
    		MOVLW	0X3A			; DAU HAI CHAM
    		CALL	LCDWRITE
    		
    		MOVF	MONTH,W
    		CALL	CONV
    		
    		MOVF	H,W
    		CALL	LCDWRITE
    		
    		MOVF	L,W
    		CALL	LCDWRITE
    		;***KET THUC HIEN THI THANG********
    		
    		MOVLW	0X3A			; DAU HAI CHAM
    		CALL	LCDWRITE
    		
    		MOVF	YEAR,W
    		CALL	CONV
    		
    		MOVF	H,W
    		CALL	LCDWRITE
    		
    		MOVF	L,W
    		CALL	LCDWRITE
    		;***KET THUC HIEN THI NAM********
    		
    		RETLW	0
    		
    	;***KET THUC HAM HIEN THI THOI GIAN RA LCD
    	;*****************************************************
    ;CAC HAM DELAY pdlong DUOC SU DUNG-THONG SO TINH O TAN SO 4MHz
    Del0	retlw	0x00			;delay 0mS - TRO VE NGAY LAP TUC
    Del1	movlw	d'1'			;delay 1mS
    	goto	Delay
    Del5	movlw	d'5'			;delay 5mS
    	goto	Delay
    Del10	movlw	d'10'			;delay 10mS
    	goto	Delay
    Del20	movlw	d'20'			;delay 20mS
    	goto	Delay
    Del50	movlw	d'50'			;delay 50mS
    	goto	Delay
    Del100	movlw	d'100'			;delay 100mS
    	goto	Delay
    Del250	movlw	d'250'			;delay 250 ms
    Delay	movwf	count1
    d1	movlw	0xC7			;delay 1mS
    	movwf	counta
    	movlw	0x01
    	movwf	countb
    Delay_0
    	decfsz	counta, f
    	goto	$+2
    	decfsz	countb, f
    	goto	Delay_0
    
    	decfsz	count1	,f
    	goto	d1
    	retlw	0x00
    	END

    •   Alt3rd April 2011, 15:40

      advertising

        
       

  2. #2
    Advanced Member level 5
    Points: 24,198, Level: 37

    Join Date
    May 2009
    Location
    Prime Meridian
    Posts
    2,909
    Helped
    822 / 822
    Points
    24,198
    Level
    37

    Re: Code ASM for i2c of pic16f877a and ds1307 rtc

    Hi,

    Pin 1, Mclre, should be tied up to +5v with a 10k resistor; essential in the actual hardware, though does not explain why one of your sims works and the other not, but try it anyway.



    •   Alt3rd April 2011, 21:06

      advertising

        
       

  3. #3
    Newbie level 3
    Points: 317, Level: 3

    Join Date
    Apr 2011
    Posts
    3
    Helped
    0 / 0
    Points
    317
    Level
    3

    Re: Code ASM for i2c of pic16f877a and ds1307 rtc

    Thank for your coment, wp100. I did that but nothing change :(



    •   Alt4th April 2011, 12:21

      advertising

        
       

  4. #4
    Advanced Member level 5
    Points: 24,198, Level: 37

    Join Date
    May 2009
    Location
    Prime Meridian
    Posts
    2,909
    Helped
    822 / 822
    Points
    24,198
    Level
    37

    Re: Code ASM for i2c of pic16f877a and ds1307 rtc

    Hi,

    Try pointing into the LM016 and right click, in that menu goto Edit Properties, at the bottom, change the Clock to 500kHz instead of 250kHz - sometimes can make the difference.



  5. #5
    Newbie level 3
    Points: 317, Level: 3

    Join Date
    Apr 2011
    Posts
    3
    Helped
    0 / 0
    Points
    317
    Level
    3

    Re: Code ASM for i2c of pic16f877a and ds1307 rtc

    I solved, but i'm not really know what is my problem. I just copy an other circuit from a project on internet Which have a 16f877a and ds1307 and it worked. I think there may be something wrong with my proteus. Whatever, thank you so much!. :)



  6. #6
    Full Member level 6
    Points: 9,381, Level: 23

    Join Date
    Jul 2004
    Location
    Hungary
    Posts
    391
    Helped
    150 / 150
    Points
    9,381
    Level
    23

    Re: Code ASM for i2c of pic16f877a and ds1307 rtc

    An idea:
    just try to change the two 4k7 resistors (r4 and r5) with a resistor called PULLUP (digital only)



+ Post New Thread
Please login