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.

ds1307 with 89c51

Status
Not open for further replies.

baftab

New Member
am making a digital clock...lcd display
it has worked out quite well...
the only problem that i am facing is..when i set the time....it doesnt saves it...
but then..sometimes it does...i hav to try a few times...and it retains the set time
but whn it does...the lcd display on day n seconds get a bit messed up

what do you guys thnk seems to be the problem?
ds1307 is working on 12c protocol
 
Make absolutely certain that the back-up battery supply voltage is 3v...

Easiest way to do it on a prototype/ plug board type set-up is with a simple resistor divider from the 5v rail...on an actual target board use a good quality lithium battery and holder along with a resistor to limit the discharge current in the case of a short on it's terminals.

If the 1307 doesn't see pretty much exactly 3v on it's back-up battery pin it gets all confused about what's going on due to the battery changeover and reset circuitry inside it.

Also, I don't know what you are driving the i2c lines with, but make sure your pull up resistors are high enough in value that the bus can pull down the lines, but not so high that it takes an eon to pull the line up when the i2c lines release the bus...

hope this helps ya out..
 
Last edited:
Hi,
i have worked with DS1307 and i had your problem but i found that the battery pin shouldn't be float. you should connect it to battery or ground.
don't forget to connect a 10k or 4.7k pull up resistor to SDA and SCL.
you should write on DS1307 like this:
SDA var PORTC.0
SCL var PORTC.1
' 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

and you should read from DS1307 like this:
DB0 var byte[8]
' Read time Secs,Mins,Hours,Day,Date,Month,Year,Control
I2CREAD SDA,SCL,$D1,$00,[STR DB0\8] ' Read 8 bytes from DS1307

and also you should show the time on lcd like this:
lcdout $fe,1,"Time=",hex2 DB0[2],":",hex2 DB0[1]

so you should write and read and show the time on lcd in hex.
if you write or read or show the time and date in decimal you will see the wrong numbers for example ,the minute go to 57 and then jump to 64 and it reset on 89. as you know 89 is 59h.
 
that looks a little confusing, wouldn't it have just been easier to tell him to remember that the datasheet states that the data is stored as BCD format ?
 
Hi,
yes in datasheet is written that it is full BCD but when i used BCD i had problem. and then i found that i should read and write and show the numbers in hex.as i said befor when i used BCD the mintures and seconds goto 57 and then jump to 64 and reset at 89. so 89 is 59h and when i read and wrote and showed the numbers in hex ,my problem is solved
 
Last edited:
I use BCD all the time with these devices...I haven't ever had a problem like that with them...
The numbers are definately stored in BCD format. For the purposes of displaying them on an lcd, you must first strip off the unused bits, because some of the registers have higher bits set for other functions, like 12/24hr mode etc, those need to be stripped off leaving you only with the actual data without any control bits. Then you must unpack the BCD and add 30hex to each nibble in order to convert the raw number into ascii for the lcd.

something like this...

read all data into a table
each byte in the table represents one set of digits (BCD)
strip control bits

And Secs with H'EF'
And Mins with H'EF'
And Hours with H'1F'
And Date with H'2F'
And Month with H'1F'
Year doesn't need stripped

send to the display something like this

get secs into temp
swap nibbles in temp
And temp with H'0F'
Add H'30' to temp
send character to lcd

get secs into temp
And temp with H'0F'
Add H'30' to temp
send character to lcd

and so on until you have done all the characters...

if you check to see if the character you are sending is greater 9 in the middle of that then you can add an extra H'07' to it so that you can use the same routine to display alpha characters too

something like this

get secs into temp
swap nibbles in temp
And temp with H'0F'
test for greater than 9 in temp
if it is Add H'07' to temp
Add H'30' to temp
send character to lcd

get secs into temp
And temp with H'0F'
test for greater than 9 in temp
if it is Add H'07' to temp
Add H'30' to temp
send character to lcd

I'll leave the fine art of how you actually code that up, to you...
 
thanks a lot
yeh i knw its bcd and i hav converted the data into hex
i prefer using asm rather thn c

and i got the fault...my VBAT was left floating :(
i thght it wasnt necessary to connect it
i'll fix it and hope it solves my problems :)

thnx amindzo,tunedwolf
 
project is working fine...
just one minor prb ... the display of "seconds" is a but umm...blurred..i dont knw how to put it..its like the numbers are incomplete

plus when i set the alarm, the lcd panel goes wacko...
for a second it shows time and for the next it shows some garbage value...and keeps on blinking in the same fashion until i switch off the alrm
thn everything becomes normal

any clue as to what is wrong?
 
Hi,
which crystal do you use for the microcontroller?
if it is 4MHZ you will have some error in time . i suggest use 20Mhz crystal because you should read the time and calender all the time with i2c instructions.
 
I assume that you do have enough supply decoupling etc in your circuit, and that the power supply is good ? if the power supply to your circuit is solid the display is probably not going wacko as such, you are probably just writing junk to it during your alarm routine by accident....check that you are not changing any of the port lines going to the lcd module unintentionally.

The seconds being blurry is most likely a timing issue...
I don't know exactly how or when you are reading the rtc chip then displaying the data, but you don't want to be in the middle of reading it as it changes,
or worse, reading it in the background using an interrupt, while trying to display the result in your main code body.

If you can, use a timer with a period set for 1 sec as an interrupt source, by the time you have done your context saving etc, the rtc chip will be updated, and you can just read the rtc chip and display the result from there.

Check your power supply is good first, then move on to other possible problems...
 
Last edited:
amindzo said:
Hi,
which crystal do you use for the microcontroller?
if it is 4MHZ you will have some error in time . i suggest use 20Mhz crystal because you should read the time and calender all the time with i2c instructions.


Interesting idea...but incorrect...

The rtc chip running on it's 32Khz xtal is what is keeping time, not the Microcontroller. The Microcontroller is merely reading and displaying the results at regular intervals in time. :)
 
Hi,
i wanted to say that when you use 4Mhz the time of reading time and calender and showing on lcd ,will increase and after working for 4 or 5 hours we have error in minututes about 2 or 3 seconds but when i use 20Mhz crystal it works good.
 
amindzo said:
Hi,
i wanted to say that when you use 4Mhz the time of reading time and calender and showing on lcd ,will increase and after working for 4 or 5 hours we have error in minututes about 2 or 3 seconds but when i use 20Mhz crystal it works good.

You're doing something seriously wrong then - the DS1307 has it's own completely seperate crystal and runs independently of the processor, so the processor clock can't have any effect on it.
 
Hi,
i know that. i mean the time of executing the instruction of the reading and showing time and calender on lcd with microcontroller.
 
amindzo said:
Hi,
i know that. i mean the time of executing the instruction of the reading and showing time and calender on lcd with microcontroller.

I still fail to see what you mean?, to display the time and date you first read it from the DS1307, you do this every time you want to display it - so it's always right (assuming the crystal is accurately trimmed), and not affected in any way by processor speed.
 
i am using an adapter to 4.5 volts
and 2 AA batteries for VBAT

the thing is the minutes hrs are displayed perfectly..its just the seconds tht matter

am using a 32.768ksumthin crystal with ds1307
and 11.0592mhz crystal for 89c51

recv: ;Recieving data from I2C bus
mov r7,#08
back2:
setb sda
setb scl
hold
mov c,sda
rlc a
clr scl
hold
djnz r7,back2
setb sda
clr scl
hold
clr sda
setb scl
hold
clr scl
hold
mov @r0,a
inc r0
ret

this is the code i use to recieve data from ds1307
 
4.5V is the absolute minimum that the DS1307 will run on, however should your supply dip by a smidge for any reason, the rtc chip will get upset...

I see from your code that you are reading it one byte at a time, I have to assume that you handle the Not-ack, start/stop etc properly before and after calling this code to get as many data bytes as you require.

However, like I said earlier, make sure you read the rtc chip, then display the results in that order, from the same routine. For example, don't try to update the lcd in your main code, and read the rtc in an interrupt service routine.
Do it all in one place, something like this...

*initialisation*

set timer for 1 sec period
clr timer
clr old timer flag
enable interrupt for timer

*main code*

blah blah blah


*interrupt service routine*

disable timer interrupt
clr timer interrupt flag
context saving
read rtc chip
convert results to ascii
set lcd position
send ascii to lcd
context restore
re-enable timer interrupt
return from service routine


or if you are polling the code...

*Main code*

blah blah blah
jmp update_time



*update_time*

read rtc chip
convert results to ascii
set lcd position
send ascii to lcd
jmp main code



keep going, we'll get it sorted out in the end :)
 
amindzo said:
Hi,
i wanted to say that when you use 4Mhz the time of reading time and calender and showing on lcd ,will increase and after working for 4 or 5 hours we have error in minututes about 2 or 3 seconds but when i use 20Mhz crystal it works good.


A nice try...but still incorrect...

If you are reading the rtc chip, and merely displaying the results, there is no accumulated error, unless your 32Khz crystal is drifting all over the place.
From the perspective of the Microcontroller, it doesn't matter when you read the rtc chip and display the result, because the data is not held in the Microcontroller.
 
:(
i found out the problem...and it was not what it seemed
the 9th pin of my led was not soldered properly..and the vbat was loose
as soon as i fixed them both
its working as good as ever

thank you people
you guys have been great :D
thnx thnx
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top