Topics Topics Edit Profile Profile Help/Instructions Help Member List Member List  
Search Last 1|3|7 Days Search Search Tree View Tree View  

Help. PIC 16F886 to Real time clock m...

:: EPE Chat Zone ­:: ­Radio Bygones Message Board :: » EPE Forum Archives 2010 - » Archive through 01 September, 2010 » Help. PIC 16F886 to Real time clock module. « Previous Next »

  Thread Last Poster Posts Pages Last Post
  ClosedClosed: New threads not accepted on this page        

Author Message
Top of pagePrevious messageNext messageBottom of page Link to this message

bob9999
Frequent Contributor
Username: bob9999

Post Number: 65
Registered: 08-2007

Rating: N/A
Votes: 0 (Vote!)

Posted on Wednesday, 11 August, 2010 - 07:05 pm:   Edit Post Delete Post Print Post    Move Post (Moderator/Admin Only)

Me again! Im up to the point where I've got the temp module, Barom module and LCD all running nicely.
I've programmed an accurate clock which works fine.
Trouble is I have yet to add more items and the program is getting longer.
I've bought a DS1307 RTC module from Maxim to use instead of the programed clock. I thought I'd take a pulse every minute and feed that into the program.
Thing is, I've read the Data sheet and understand it fairly well. What I can't figure out is how to synchronise the required 100Khz clock to the data stream using the I2C protocol. I've searched Microchip but don't realy know what I'm looking for!
Would someone point me in the right direction please.
An example program I've found shows 7-pages of code, most of which is foreign to me.
In short, I want to interface a 1307 RTC to a LCD. There must be a library somewhere. It's the synchronising I'm not sure about.
Most grateful for any assistance.
Kind Regards,
RGR
Top of pagePrevious messageNext messageBottom of page Link to this message

istedman
Frequent Contributor
Username: istedman

Post Number: 177
Registered: 05-2005


Rating: N/A
Votes: 0 (Vote!)

Posted on Wednesday, 11 August, 2010 - 11:31 pm:   Edit Post Delete Post Print Post    Move Post (Moderator/Admin Only)

Are you trying to read the real time clock from the DS1307?

All that takes is 7 reads from I2C slave address D1, starting at word 0. This will return the current time and date in BCD format.

Are you writing your software in assembler of a high level language?

For assembly language, you can use the Microchip Application Maestro to generate an I2C library function, it also has LCD driver routines in assembler.

You don't need to synchronise the DS1307 to the LCD.You could set a timer to expire every second and generate an interrupt, the interrupt routine reads the RTC and outputs the updated data.

HTH

Ian
Top of pagePrevious messageNext messageBottom of page Link to this message

microguy
Frequent Contributor
Username: microguy

Post Number: 149
Registered: 01-2010

Rating: N/A
Votes: 0 (Vote!)

Posted on Thursday, 12 August, 2010 - 03:27 am:   Edit Post Delete Post Print Post    Move Post (Moderator/Admin Only)

I agree, I think you're thinking about it in far lower level terms than it needs to be.

I think you should be thinking of it in terms of like how a PC talks to a modem or something. There's no "sync'ing" to it.

It's a device you just request data from. And as long as you set up proper address, and reads etc... you should get your data in memory.

It's more like say RS-232 than something you need to synch two clocks to be able to talk to each other.

Doesn't EPE have this covered in one of their tutorials?
Top of pagePrevious messageNext messageBottom of page Link to this message

joe
:::: Super User ::::
Username: joe

Post Number: 864
Registered: 05-2005

Rating: N/A
Votes: 0 (Vote!)

Posted on Thursday, 12 August, 2010 - 07:31 am:   Edit Post Delete Post Print Post    Move Post (Moderator/Admin Only)

"I've bought a DS1307 RTC module from Maxim to use instead of the programed clock. I thought I'd take a pulse every minute and feed that into the program.
"

So, it sounds like you want to display the time from the RTC on your display ?

Your PIC program will have a list of tasks to do; read a value from this sensor, do some calculations, output a result on the LCD, do other "stuff", and when you've done all that, go back to the beginning and do it all again in an endless loop.

Now you want to slip in some additional commands to read the DS1307 to get the time and display that.

If this is the case you could, for example, do something like:

Read Time from RTC
Does mins = Stored mins value
Yes - Exit routine (The time hasnt moved on more than a min at this point)
No - Store mins value from clock.
Do stuff like sending new time to display
Exit routine

It dosn't matter how often you call this routine as long as you have the CPU cycles to spare.
The comparing of the mins with a previous value means that you detect as soon as the mins value changes, so you don't keep refreshing the LCD display (assuming thats what you want to do with this value)... that prevents annoying display flicker.

What you may need to be carfull about is "how" you connect the RTC to the PIC.

The datasheet for the RTC says that the clock speed maximun is 100KHz, and a PIC, bit banging will easily exceed this maximum speed.

Some PIC's have special I2C interfaces built in that can handle the timing for you.

Maxim have a datasheet that covers interfacing this RTC with a PIC that may help:
http://www.maxim-ic.com/app-notes/index.mvp/id/3921
Read my ramblings - www.techbites.com/joe-farr
Top of pagePrevious messageNext messageBottom of page Link to this message

bob9999
Frequent Contributor
Username: bob9999

Post Number: 66
Registered: 08-2007

Rating: N/A
Votes: 0 (Vote!)

Posted on Thursday, 12 August, 2010 - 09:59 am:   Edit Post Delete Post Print Post    Move Post (Moderator/Admin Only)

Thanks guys for your helpful Info. In reply:
I'm programing in Assembler. I understand the mechanics of read/write to the 1307, I don't understand how to synchronise the clock line (SCL) to the data line (SDA).
Do I need a 100Khz clock?
If I generate 100Khz on say RA0, and use RA1 as the data line, how are these two synchronised?
The data sheet timing diagram looks complicated. I understand the actual commands to use, just implementing them at the signal-level.
Mebe I'm looking too deep I don't know. Never done it before.
I'll try Maestro, sounds like a good plan.
I don't need to sync anything to the LCD. Thats a stand-alone module in my design.
The required calculations / conversions are also OK with me. I have the routines from earlier projects.
Sorry to appear dense, I am, though, still learning here.
If you would assist with the detail of how clock and data are synchronised I would be most grateful.
Kind Regards,
RGR
Top of pagePrevious messageNext messageBottom of page Link to this message

joe
:::: Super User ::::
Username: joe

Post Number: 866
Registered: 05-2005

Rating: N/A
Votes: 0 (Vote!)

Posted on Thursday, 12 August, 2010 - 11:18 am:   Edit Post Delete Post Print Post    Move Post (Moderator/Admin Only)

First, the 100Khz is the maximum clocking speed; you can work at any speed you like upto that and it can even be variable.
If you exceed the maximum there's a possibility that the RTC will start dropping bits in the data stream and you will get garbled (or no) results back.

The process of using a data line and clock line for serial communications is quite simple once you get your head around it.

For example... lets assume that you want to send binary 101 from the PIC to the RTC.

As a starting point, set the clock line to a known position - low.

Next, set the data line to the desired logic level, high in our case, then set the clock line high. (Some devices LATCH in the data as the clock line goes from low to high (rising edge), some as it goes high to low (falling edge). The trick here is that you have to leave this clock line high for a minimum time (see datasheet). So you now wait. After the minimum time has elepsed, you then set the clock line low. You are now free to set the data line to any level required and the RTC won't yet care; in our example we set the data line low, then set the clock line high, wait minimum time, then low.. and so on to the end of the data being sent.

So, setup the data value (data line set high or low as needed), clock in the bit (clock line from low to high... hold... clock line back to low), then setup the next data value bit and repeat.

It's important that whilst the clock line is high, you don't fiddle with the data line as this will confuse the RTC.

When you've sent the data required, you should end up, in our exmaple, with the data line high, but the clock line always low.

I can't supply you any sample code as I do all my PIC developments in a high-level language and you can send a complete I2C command in one line of code :-(

Hope that helps,
Joe
Read my ramblings - www.techbites.com/joe-farr
Top of pagePrevious messageNext messageBottom of page Link to this message

epithumia
Frequent Contributor
Username: epithumia

Post Number: 766
Registered: 06-2006

Rating: N/A
Votes: 0 (Vote!)

Posted on Thursday, 12 August, 2010 - 12:00 pm:   Edit Post Delete Post Print Post    Move Post (Moderator/Admin Only)

I see the 16F886 has a built-in I2C module. Can you use that? Reading the above it sounds as if you are bit-bashing the ports, which is far more difficult (but do-able).

The I2C module takes care of generating the clock and keeping the data synchronised.

Also, remember that you need a couple of (say) 4K7 pulling resistors on the clock and data lines, because I2C uses open-drain signals.

Epi
If you need me, Neil and me will be hanging out with the Dream King. - Tori Amos
Top of pagePrevious messageNext messageBottom of page Link to this message

epithumia
Frequent Contributor
Username: epithumia

Post Number: 767
Registered: 06-2006

Rating: N/A
Votes: 0 (Vote!)

Posted on Thursday, 12 August, 2010 - 12:06 pm:   Edit Post Delete Post Print Post    Move Post (Moderator/Admin Only)

Also:

http://www.i2cbus.com/

and if you want the official spec, try googling "philips i2c specification"

Epi
If you need me, Neil and me will be hanging out with the Dream King. - Tori Amos
Top of pagePrevious messageNext messageBottom of page Link to this message

bob9999
Frequent Contributor
Username: bob9999

Post Number: 67
Registered: 08-2007

Rating: N/A
Votes: 0 (Vote!)

Posted on Thursday, 12 August, 2010 - 08:21 pm:   Edit Post Delete Post Print Post    Move Post (Moderator/Admin Only)

Many thanks again:

Joe,

Your explanation has cleared the "Mists." I now see that the clock is not a continuously running chain of pulses. I'd read the data sheet wrongly!
I have accumulated a few program examples, and am also studying JAL ... a higher level language. Being competant in Basic steers me away from C somehow.

epithumia,

I've looked at the 16F886 data sheet, MSSP. The text runs some 30 pages. One day I'll get round to it! Thanks for the info on the pull-ups.
Your link to I2Cbus.com was Ideal. I now, hopefully, understand the process at last.
Kind Regards,
RGR
Top of pagePrevious messageNext messageBottom of page Link to this message

joe
:::: Super User ::::
Username: joe

Post Number: 867
Registered: 05-2005

Rating: N/A
Votes: 0 (Vote!)

Posted on Friday, 13 August, 2010 - 06:12 am:   Edit Post Delete Post Print Post    Move Post (Moderator/Admin Only)

"I now see that the clock is not a continuously running chain of pulses. I'd read the data sheet wrongly!"

Correct. It's a continious stream when your reading/writing to the device, but the rest of the time it's idle. It wouldnt supprise me if that's not actually stated in the datasheet; one of those things that's just assumed everbody would know.

My prefered PIC language is BASIC and there are several available if that's where your skills and interests are.

For example, to access the RTC in BASIC, you could do something like this:
rst = 1 'ready for tx
SHOUT io, sclk, LSBFIRST, [$bf] 'Read all 8 RTC registers in burst mode
SHIN io, sclk, LSBPRE, [ nRTC[5], nRTC[4], nRTC[3], nRTC[2], nRTC[1], nRTC[6], nRTC[0], nRTC[7] ]
rst = 0 ' Reset RTC

Joe
Read my ramblings - www.techbites.com/joe-farr

Administration Administration Log Out Log Out   Previous Page Previous Page Next Page Next Page