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.

Connecting DS1307 to PIC16F877A

Status
Not open for further replies.

amindzo

Member
Hi,
I have connected DS1307 to PIC16F877A.sometimes it works.
sometimes i can see the correct time and calender on lcd and sometimes
not . when i reset the microcontroller for many times , eventually it works in one of resets.
could you help me with this problem?

i use micro code studio picbasic pro2.41

this is my program:

define OSC 4
include"modedefs.bas"
DEFINE LCD_DREG PORTD
DEFINE LCD_DBIT 4
DEFINE LCD_RSREG PORTD
DEFINE LCD_RSBIT 1
DEFINE LCD_EREG PORTD
DEFINE LCD_EBIT 0
DEFINE LCD_BITS 4
DEFINE LCD_LINES 2
pause 100
TRISC= %11111111
SDA var PORTC.0
SCL var PORTC.1
DB0 var byte[8]
CMCON = %00000111 ' Comparators = off
pause 1000
gosub write_1307
read_1307:
' Read time Secs,Mins,Hours,Day,Date,Month,Year,Control
I2CREAD SDA,SCL,$D1,$00,[STR DB0\8] ' Read 8 bytes from DS1307


lcdout $fe,1,"Time=",hex2 DB0[2],":",hex2 DB0[1]
pause 2000
goto read_1307
end

Write_1307:
' Set time & date to 21:58:00 Tuesday 6th of July 2004
I2CWRITE SDA,SCL,$D0,$00,[$00,$58,$21,$2,$6,$7,$4,$90] ' Write to DS1307
pause 10
RETURN ' Sec Min Hr Day D M Y Control
 
I think the problem is you need somone familiar with the dallas 1307 clock calandar chip , who also is familiar with your dialect of picbasic.
and who is also fluent in the 16F877A
 
there are a lot of picbasic examples for the 1307, it is a popular chip

the quirky bit is how the date and time are stored... if I recall, they are BCD encoded.

the 877a has dedicated i2c hardware, I strongly recommend you use the onboard hardware i2c transciever opposed to the software emulation - it makes things a lot smoother. also, use discrete variables instead of a string array, it helps make things easier to read.

another thing, I notice your read statement addresses device $D1 and your write statement addresses device $D0 ... the i2cread command is automaticly going to set the read/write bit for you, so I would use $D0 for both reading and writing. I think the only time the bit is not set automaticly is when you're using low-level handshaking.
 
Hi
I have used from the mentioned program in the first post for the same work but with this difference that i have connected SDA and SCL to PORTA.0 and PORTA.1 respectively. i want to ask you if i should do any conversion again?
Please Help me because i have seen the results on the LCD as follows:
Time=00:00:00
date=00:00:00
 
Hi,
this is the correct program:

define OSC 4
include"modedefs.bas"
' Set LCD Data port
DEFINE LCD_DREG PORTD
' Set starting Data bit (0 or 4) if 4-bit bus
DEFINE LCD_DBIT 4
' Set LCD Register Select port
DEFINE LCD_RSREG PORTD
' Set LCD Register Select bit
DEFINE LCD_RSBIT 1
' Set LCD Enable port
DEFINE LCD_EREG PORTD
' Set LCD Enable bit
DEFINE LCD_EBIT 0
' Set LCD bus size (4 or 8 bits)
DEFINE LCD_BITS 4
' Set number of lines on LCD
DEFINE LCD_LINES 2
ADCON1=7
pause 100
TRISC= %11111111
SDA var PORTC.0
SCL var PORTC.1
DB0 var byte[8]

CMCON = %00000111 ' Comparators = off

pause 1000

gosub write_1307

read_1307:
' Read time Secs,Mins,Hours,Day,Date,Month,Year,Control
I2CREAD SDA,SCL,$D1,$00,[STR DB0\8] ' Read 8 bytes from DS1307


lcdout $fe,1,"Time=",hex2 DB0[2],":",hex2 DB0[1]
pause 2000
goto read_1307
end

Write_1307:
' Set time & date to 15:55:00 Tuesday 6th of July 2004
I2CWRITE SDA,SCL,$D0,$00,[$00,$55,$15,$2,$6,$7,$4,$90] ' Write to DS1307
pause 10
RETURN ' Sec Min Hr Day D M Y Control


if you use PORTA , you should define like this:

SDA var PORTA.0
SCL var PORTA.1

if you have problem,ask me again , maby i have some problem with this program because i haven't work with micro controllers for 6 month and i have forgatten the correct code so if you have problem ask me again to study more and answer your question.
 
i have used your program with deleting CMCON for avoiding from the error of PICbasic compiler and [define OSC 4] because i have used the external XT and i have deleted [include modedefs.bas].
Do you know why i have faced to this results:
Time=00:00:00
date=00:00:00
 
Hi, which crystal do you use? if you use 4mhz you don't need to define.but if you use another crystal for example 20Mhz you should define at the start of the program.
 
I have used from 4MHz XT but i should ask you some questions . Why we have used $D1 and $D0 and $00 in I2CREAD? should i change them when i have used from PORTA.0 & PORTA.1 for SDA and SCL? can i delete the ADCON1=7 in the program?
 
Hi,
you should use $D0 and $D1 because of the DS1307(you can see the DS1307 datasheet for more information) so you don't have to change them and also you don't have to delete the ADCON1=7 because PORTA is an analog port so when you want to use that as digital you should intialize the ADCON1=7 to convert the analog port to digital. what about the crystal you use for DS1307?
maby it has some problem.
for reding time:
lcdout "Time=",hex2 DB0[2],":",hex2 DB0[1]

and for reading date:
lcdout "Date:",hex2 DB0[4],"-",hex2 DB0[5],"-",hex2 DB0[6]
 
Last edited:
I have used 32.768 khz XT and connect it to Pins 1 and 2 without any capacitor. give me time for test it with ADCON1=7 .
Thank's alot for your helping. I hope to hear more from you and your activities in IRAN if you like because i am living in IRAN like you. note that we can talk each other in Farsi.
I will tell you the results from the program as soon as possible.
 
You can see my final program that doesn't work yet.


DEFINE lcd_bits 8
DEFINE lcd_dreg portc
DEFINE lcd_dbit 0
DEFINE lcd_rsreg portd
DEFINE lcd_rsbit 5
DEFINE lcd_ereg portd
DEFINE lcd_ebit 4
DEFINE lcd_lines 4
ADCON1=7
PAUSE 100
trisc=0
trisd=0
TRISA= %11111111
SDA var PORTA.0
SCL var PORTA.1
DB0 var byte[8]
gosub write_1307
read_1307:
I2CREAD SDA,SCL,$D1,$00,[STR DB0\8]
lcdout $fe,1,"Time=",hex2 DB0[2],":",hex2 DB0[1],":",hex2 db0[0]
lcdout $fe,$c0,"date=",hex2 DB0[4],":",hex2 DB0[5],":",hex2 db0[6]
pause 1000
goto read_1307
end
Write_1307:


' Set time & date to 21:58:00 Tuesday 6th of July 2004
I2CWRITE SDA,SCL,$D0,$00,[$00,$58,$21,$00,$6,$7,$4,$90]
pause 10
RETURN


I have used two pull up resistor (10k) for SDA and SCL to 5 volt and a 3.6V Bat In PIN(3). unfortunately i can't see any pulse in pin(7) and the results are:
Time=00:00:00
date=00:00:00
I don't know what's the problem!!!
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top