PDA

View Full Version : Interface of DS1307 with PIC16F876A



Eng4444
- 28th May 2006, 10:14
Hello everybody,

Is anyone ready to teach me how to program DS1307 to be used with PIC16F876 while programming with PicBasic Pro?

i have never used I2C...

I'm working working on electric energy calculation: Energy = Power . Time

so i need to get the time and multiply it by the power i already calculated.Ne need to see time on LCD but just to manipulate with it internally.

I am even ready to pay for the one who will give a whole program... in fact, i am so squeezed by time and i need a quick solution.

Thank you.

Eng4444
- 28th May 2006, 11:38
Come one please... i need even small clues!

Melanie
- 28th May 2006, 12:39
Like ONE HOUR has elapsed since your initial post and you're bugging people for an answer... on a SUNDAY when almost all the professionals are home lazing by the pool and very few folks are actually looking at the forum.



I2CAddress var Byte
RTCRegister var Byte
RegisterContents var Byte

I2CAddress=$D0
I2CRead SDA,SCL,I2CAddress,RTCRegister,[RegisterContents],I2CError

Look in the PBP manual to see what I've written. Look in the RTC's Datasheet to discover what the Registers are and what they contain. Go to the MELabs website, download and study an RTC example that's posted there...

Eng4444
- 28th May 2006, 13:26
You cannot understand what people have... please don't think that i am just sitting here waiting to study that stuff. I am a biomedical Engineer - from where my nickname- and i will graduate in 2 weeks.

the last time i asked a help about "div32", i felt i was treated in a bad way.

i want you to understand that working with PIC is not my specialization at all; not with PicBasic Pro, nor with DS1307.

i just design medical instruments and machines and train doctors to use them.

But it just happened that in my senior project, i am adding an extra part and using PIC. Then, i will never see PIC in my whole life.

So in case you are able to help me, do it completely please; even if you ask for money.

But i need a whole program... between DS1307 and PIC16F876A.

By the way, can i use a program written in C and write it in the PicBasic Pro program? because i just found an interface written in C language.

Eng4444
- 28th May 2006, 13:30
i think you didn't understand me well because you provided me a program to read from DS1307 and i am thankful.

But still, i dunno how to prgram it ( i mean how to set hours, minutes, seconds, month, day ,year for the first time).

So as i said, i just bought a DS1307 and i have a PIC16F876A. what are the next steps?....


I'll appear an idiot i know but really... this is not my field at all.. i got stuck with it and not way to step back because of lack of time...

thank you all for your understanding.

Eng4444
- 28th May 2006, 14:36
Would you be able to help me?

Charles Linquis
- 28th May 2006, 14:38
The following may prove useful.

Remember that the DS1307 stores all data in BCD format, so you will have
to convert your ASCII input into binary BCD before writing, and will have to
convert back from BCD when reading.


ClockAddr CON %11010000 ' RTC device address (byte addressing)
Offset CON 0
Cntrl CON %00010000 ' sets the SQW/OUT to 1Hz pulse, logic level low

I2CRead SDA_PIN, SCL_PIN,ClockAddr,Offset,[sec,mins,hr,day,date,mon,yr],ErrorTrap


I2CWrite SDA_PIN,SCL_PIN,ClockAddr,Offset,[sec,mins,hr,day,date,mon,yr,cntrl],ErrorTrap

Travin77
- 28th May 2006, 14:54
If you can buy a ds1302 RTC I can provide you with a working program and a schematic. I know it isn't a 1307, but those are the options I can provide.

Travin

Eng4444
- 28th May 2006, 15:45
If you can buy a ds1302 RTC I can provide you with a working program and a schematic. I know it isn't a 1307, but those are the options I can provide.

Travin

++++++++++++++++++++++++++++++++++++++++++++++++++ +++++++

I do not care about the IC, i can buy DS1302 as long as it gives the same result.
i just need to deal with time in a certain way (multiply, divide...etc) so the IC will not cause a prob at all... do you have a program for PIC16F876A? i'll be more than thankful!!!

Melanie
- 28th May 2006, 16:56
Eng4444 please understand that folks don't sit next to their terminals all day long waiting to answer your questions. I might glance at it once a day if I'm at my desk - sometimes more, or I might not for several days at a time... Time may be of the essence for you but I also have other priorities in my life...

If you are calculating Energy = Power . Time, then you are about to open up a whole heap of time calculation problems if you are going to read the time and date, because as you move through midnight into the next day, or increment through into the next month, you have to take into account a whole new set of figures...

Example... What is the elapsed time between 14:37:31 on 25/02/06 and 17:11:09 on 26/05/06? That's a whole heap of code to do that. You have to work out how much time elapsed until midnight on one day, and how much time elapsed since midnight on the next. Then a month goes by... and you have to take account how many days have elaspsed. And was this year a Leap Year? If you get it wrong you're a day out... So unless you convert everything to Linear Time (there was a long thread on Julian Date calculation some time ago) you have a headache with a PIC full of lots of code doing no more than calculating an elapsed period.

The DS1307 however provides you with an easier solution that saves you having to do these Time and Date calculations... just count Seconds. Set up the DS1307 to output a Second Pulse to your PIC, and your program sits there doing whatever you want it to do and just simply incrementing a counter every time a Second ticks by.

Melanie

Archilochus
- 28th May 2006, 17:35
Isn't there a different chip in the DSxx series that just records the launch date and then counts elapsed seconds? - can't recall the part # off-hand.

Arch

Melanie
- 28th May 2006, 17:44
http://www.picbasic.co.uk/forum/showthread.php?t=250&highlight=DS1994

Eng4444
- 28th May 2006, 18:04
Maybe you can come up with a new solution for my problem?

i really do not need about making it work for a year or month... not even a week or a full day!

i just want to do a demonstration for 30 minutes and i need to show people who a watching the final number.

so what do you say if i take a second small PIC (12F675 OR 16F84) and run a loop inside it? and then, i take the final number as if it is number of seconds and multiply by it by the Power in PIC16F876... what do you say or suggest?

would it be an accurate number of seconds?

but what to write in that loop? what delay to put.... ?

I hope You or anyone helps me in this affaire...

Eng4444
- 28th May 2006, 19:42
Maybe this is the best solution to work in peak times? i mean the usage of the second PIC...

any advice is appreciable.

Eng4444
- 28th May 2006, 20:28
DS1307: I2C Serial Interface; how to do it?

Charles Linquis
- 28th May 2006, 20:51
The code that I posted earlier works fine for a DS1307!

Travin77
- 28th May 2006, 20:52
Eng,

Tell me how you are determining the power. Are you using sensors, or just extrapolation? How many pins are you using? Need more data. I don't mind helping (what help I can provide as I am new to this stuff). Give more data, and you might just get more results.

Travin

Eng4444
- 28th May 2006, 22:20
sorry to say it but, what are: SDA_PIN, SCL_PIN,ClockAddr,Offset ?

i know nothing about them.....

what are their settings? should i write them as they are? or replace them with other things?

Moreover, how to set the first date and time? you provided me with the way i read or write but how to set the initial time?

thank you in advance :-)

Eng4444
- 28th May 2006, 22:23
Let's say i will use port RC0 from my PIC16F876A.

i am getting power by using sensors. Things are so fine and i am being able to calculate this power. I am even seeing it on LCD. but now i need to go and get the energer... E = POWER . TIME

SO i still need the time... any detailed code suitable with PIC16F876A would be appreciated!

i need to know how to set the ports, how to enter the first date and time, if i need to write something related to frequency or not, how to read time from DS1307 into a variable, how to display it on LCD.

Thank you in advance!

Travin77
- 28th May 2006, 23:54
I just wanted to let you know that there is pre-written code for this chip. Go here:

http://www.melabs.com/resources/samples/submitted/MN1307.txt

This might help.

Travin

Eng4444
- 29th May 2006, 05:52
I just wanted to let you know that there is pre-written code for this chip. Go here:

http://www.melabs.com/resources/samples/submitted/MN1307.txt

This might help.

Travin
__________________________________________________ ____________

I have already found it but i really do not know how to make it work for PIC16F876A! things might be different a bit...

Many people are telling me that i just to need to write 2 to 3 lines only to fix the actual time, after defining the ports used. And then, i'll be able to read from DS1307. so when i saw that program.... i was chocked because of so many many lines....i don't want the user to do anything with time, i do not want to put buttons.. i just want this IC to have internal real time clock working fine so that whenever i want i can take the difference between 2 time intervals in order to get seconds and then, / 3600 to ge tthe hours...

As you see, i just want an extremely simple thing but i am not able to do it...
this is why, i'll keep on asking if someone can help me to write those 7 to 8 lines as i think..

bbarney
- 29th May 2006, 15:07
did you look at this thread
http://www.picbasic.co.uk/forum/showthread.php?t=190