PDA

View Full Version : Propeller Real Time Clock



william chan
11-10-2006, 07:17 PM
Can I use one cog to keep track of real time and calendar?
How accurate will the clock be using the 5Mhz crystal and running at 80Mhz?
Will the accuracy be comparable to using a 32Khz clock crystal?

Has anybody created a real time clock and calendar driver yet?

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
www.fd.com.my
www.mercedes.com.my

Cobalt
11-10-2006, 10:33 PM
Interesting that you should mention that, I've been working on a RTC system. However, the only thing that may be an issue is that I use an external 8-pin DIP chip to keep track of the time/date. It is the DS1302 ( www.parallax.com/detail.asp?product_id=604-00005 (http://www.parallax.com/detail.asp?product_id=604-00005) ) chip, and it only requires 3 pins to communicate, two of which could be multiplexed if I/O is an issue.

I know that using a cog to keep track of time would be a money/space saver, but having one that would keep track of time and calendar would be a bit tricky, hence why I looked into the DS1302. It does seconds, minutes, hours (12 or 24 hour modes), date, month, year and the day (Monday, Tuesday, Wednesday...)

Attached is the horribly ugly code that I am known for... it may be a bit difficult to understand at first, but its actually really simple.
It is really plain, it displays the current time on a VGA monitor... it shows HH:MM:SS in 24-hour mode, then under it is the time in binary(it is in BCD though, because that is how the chip is designed). Two more lines down it shows the date in DD/MM/YYYY configuration, then under that I attempted to have it display the date again, only it would spell out the day (Friday for example) and then month. It also shows the date again, only with a "st", "nd", "rd" or "th" at the end of the number... but I realised that with my code, it only reads the 1's posistion, and therefore would add an "rd" to 13 for example.... which would be 13rd, haha.
Have fun.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
while alive = 1
wakeup
program(propeller)
eat(3)
sleep(7)

Loopy Byteloose
11-11-2006, 12:02 AM
Jon Williams was working on a 2 cog RTC before he left. I am not sure if that is a real limit and a 1 cog RTC is not possible. On the other hand, there is a great deal of appeal to having an independent RTC share the I2C bus and have its own BU power so that the Propeller could call upon it only when a time stamp or time calculation is needed.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
"If you want more fiber, eat the package.· Not enough?· Eat the manual."········


···················· Tropical regards,····· G. Herzog [·黃鶴 ]·in Taiwan

Beau Schwabe
11-11-2006, 12:41 AM
William chan,

There is a DS1307 (I2C) version in the propeller object exchange library.

http://ww1.parallax.com/Default.aspx?tabid=65


Accuracy of course is going to depend on the quality of your crystal. The lower the ppm (pulses per million) value the better
your accuracy is going to be. Suppose your 5MHz crystal was rated at ±50ppm. After dividing down the 5MHz crystal to 32.768kHz,
your effective ppm would be about ±.33 in this case. ((32.768kHz/5MHz)* ±50ppm) = ±.33ppm A typical 32.768kHz crystal would
have a ppm rating of ±20ppm.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Beau Schwabe (mailto:bschwabe@parallax.com)

IC Layout Engineer
Parallax, Inc.

Paul Baker
11-11-2006, 03:11 AM
Using the Propeller as a basis for a RTC, there are two sources of error. The first is the accuracy of the crystal used as Beau stated, and the accuracy of your generated heartbeat (time basis signal). If you configure a counter in Duty Cycle mode with a FRQ(A/B) of $D1B7 will generate a tick once every 1.0000016987 milliseconds or an error of +1.6987 ppm. This error is combined the error of the crystal though not nessesarily in the same direction. Another counter could be used to compensate for the heartbeat error by shifting the FRQ(A/B) to $D1B6 for 17 milliseconds every 10,000,000 seconds, compensating for the crystal accuracy requires another clock reference even more accurate.

<note> these are back of the page calculations, I may be off on some numbers </note>

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Paul Baker (mailto:pbaker@parallax.com)
Propeller Applications Engineer
[/url][url=http://www.parallax.com] (http://www.parallax.com)
Parallax, Inc. (http://www.parallax.com)

Newzed
11-11-2006, 03:36 AM
Cobalt, I tried your program and all I get for the first line is:

715:715:715

I get the same thing with the 1302 disconnected.

Sid

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Sid Weaver
Don't have VGA?

Newzed@aol.com (mailto:Module?Newzed@aol.com)
·

Cobalt
11-11-2006, 02:55 PM
Sid,
It is most likely a code error somewhere... I'll try to figure out what happened.
What do the other lines look like?
Are you using the Demo board?
Are you using pull-up resistors on the 1302?
Since it does the same thing without the 1302 in place, some registers may just be messing up somewhere... however, I do find it interesting that you are getting 3 charaters between the semicolons... the code is only supposed to write 2.

I like feedback, positive or negative!

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
while alive = 1
wakeup
program(propeller)
eat(3)
sleep(7)

Newzed
11-11-2006, 08:57 PM
Cobalt, here is exactly what my screen displays:

715:715:715
11_1111 111_1111 111_1111

715/715/20175
,· 715th, 20715

The 1302 is OK - it runs fine with my Stamp.· Since the 1302 operates at 5VDC and the Prop at 3.3VDC, I have a 1.2K resistor in series with the three clock leads going to the Prop.

Sid

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Sid Weaver
Don't have VGA?

Newzed@aol.com (mailto:Module?Newzed@aol.com)
·

Cobalt
11-11-2006, 09:34 PM
Judging from the info you told me, it seems that the I/O pin is constantly being read as high, the 11_111 111_1111 111_1111 is the BCD of the HH:MM:SS. Since it is all 1's, the input pin to the prop seems to be floating due to the resistors (I'm guessing).
The 1302 can be powered by as low as 2.0 volts, I just power mine at 3.3v - straight from the Vdd on the demo board.

Try pulling those series resistors out, and just power the 1302 at 3.3v. Let me know what happens!

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
while alive = 1
wakeup
program(propeller)
eat(3)
sleep(7)

Cobalt
11-11-2006, 10:00 PM
Sid,
I just hooked my 1302 up with series resistors and ran it at 5v on my demo board, and it ran fine. I pulled the chip out of the bread board, and my monitor did the 715 thing. So, I disconnected the /Rst, IO and Sclk pins one at a time, and when I disconnected the IO pin, it did the 715 thing again. With either of the other two pins disconnected, it displayed 0's instead. I even tried running it without the xtal across pins 2 and 3, and it just displayed the last time that was in it's registers and didn't continue to count.

So I'm quite certain that the IO pin is floating. I'm also using the same, unmodified program that I uploaded and I'm getting no errors. The only other thing that I can think of is possibly a wiring error? Check at the top of the spin file, it gives details on how to hook the 1302 up to the propeller.

DS1302 pin 5 (/Rst) -> Propeller P2
DS1302 pin 6 (I/O) -> Propeller P1
DS1302 pin 7 (Sclk) -> Propeller P0

Note that the "P0" through "P2" does not mean the pin, it is the actual IO port number.

You can hook the 1302 up to a different set of pins, however you must change the pin numbers in the CON block.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
while alive = 1
wakeup
program(propeller)
eat(3)
sleep(7)

Newzed
11-11-2006, 10:03 PM
Cobalt, I don't think tht would make any difference.· Remember, I get the same readings with the 1302 completely disconnected.

Sid



▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Sid Weaver
Don't have VGA?

Newzed@aol.com (mailto:Module?Newzed@aol.com)
·

Newzed
11-11-2006, 10:20 PM
Cobalt,· I switched to Prop Pins 19, 20 and 21 and now I'm getting all 0's.· What size resistor are you using?

Sid

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Sid Weaver
Don't have VGA?

Newzed@aol.com (mailto:Module?Newzed@aol.com)
·

Newzed
11-11-2006, 11:57 PM
Cobalt, I hooked up my scope to the three lines from the clock to the Prop.· I'm getting a good 3V pulsetrain on each line but the Prop displays mostly 0's.· The top line displays:

00:00:NNNN where N may be any number from 0 to 5.

Sid

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Sid Weaver
Don't have VGA?

Newzed@aol.com (mailto:Module?Newzed@aol.com)
·

Newzed
11-12-2006, 05:34 AM
Cobalt, I finally got the program working.· After I had modified it for my personal use, I incorporated the entire program into one of my Propeller programs running on Mike Green's OS.· I adjusted the "repeat" in Main to "repeat 100".· I incorporated a "Get time" option into my Menu, and when I press "G", the clock program runs and displays the time and date for about 5 seconds, then goes back to the main Menu.· Very neat.

Thank you for the program.· Running it in OS makes a tremendous difference.

Sid

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Sid Weaver
Don't have VGA?

Newzed@aol.com (mailto:Module?Newzed@aol.com)
·

Newzed
11-12-2006, 05:40 AM
One more thing - I saw no way to set the clock time.· Looks like I will have to patch it to a Stamp when I need to reset it.

Sid

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Sid Weaver
Don't have VGA?

Newzed@aol.com (mailto:Module?Newzed@aol.com)
·

Cobalt
11-12-2006, 10:13 PM
Sid,
I'm really glad to hear you got it working - although it is still strange that it would not run stand-alone. http://forums.parallax.com/images/smilies/confused.gif

Anyhow, there is a way to set the time through the program... like I said, the code is kinda dirty... didn't really clean it up a lot or make it really user-friendly before posting it.
Under the "init" routine (at the bottom) there should be 7 lines of syntax that are commented out:

'· WriteCmd($80, %0000_0000) 'Seconds
'· WriteCmd($82, %0000_0000) 'Minutes
'· WriteCmd($84, %0000_0000) 'Hours
'· WriteCmd($86, %0000_0000) 'Date
'· WriteCmd($88, %0000_0000) 'Month
'· WriteCmd($8A, %0000_0000) 'Day
'· WriteCmd($8C, %0000_0000) 'Year

You use the code by deleting the ' mark so that the tool will program it to the Prop.· The values are in BCD, so the upper/lower nibbles are the 10's and the 1's posistion for the numbers, respectively.
For example:
If you wanted to set the hour to 9 o'clock, the minutes to 32, and seconds to 46, you would do this:

WriteCmd($80, %0100_0110) 'Seconds
WriteCmd($82, %0011_0010) 'Minutes
WriteCmd($84, %0000_1001) 'Hours

Like I said, it is in BCD (Binary Encoded Decimal) so 32 dec = 0011_0010, NOT 0010_0000.
Also, be careful with the Seconds and Hours registers:· bit 7 in Seconds is the Clock Halt bit - setting this bit to a 1 will make the chip stop keeping time!· Also, the 7 bit in the Hours register will set it to 12/24 hour mode: Clearing the bit makes it 24-hour mode (which is what the program is designed to use), and setting it to 1 puts it in 12 hour mode.

Also, I noticed that you said you moved the chip to Prop pins 19-21.· I take it that you aren't using the Demo board?· I built the clock on my demo board... maybe that has something to do with it.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
while alive = 1
wakeup
program(propeller)
eat(3)
sleep(7)

Newzed
11-12-2006, 11:08 PM
Cobalt, that seems to be a hard way to set the time, espcially if you are setting the time while watching a time reference.· The 1302 will accpet HEX2 input instead of BCD - I never use BCD.· I'm going to see if I can figure out a way to set the clock by selecting an option on my Menu, then putting the "WriteCmd" lines in a separate method.· If I can work it out, this would let you reset the time "on the fly" without going in and actually changing the program.

I'll let you know how it works out.

Sid

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Sid Weaver
Don't have VGA?

Newzed@aol.com (mailto:Module?Newzed@aol.com)
·

Cobalt
11-13-2006, 08:53 AM
Sid,
If you get that to work, that would be great. I used BCD because the spec sheet that I refrenced said everything was BCD.

I wrote those lines into the init routine because I wanted a quick-and-dirty way to do it while I was writing the code in the Main routine... and the code that you downloaded wasn't really inteded to be given out so soon. It just so happened that this topic came up when it did.

I am going to eventually alter the program such that you can use a PS/2 keyboard to type in the time, or just use an array of buttons to change the time (such as a normal clock!). This would have been done over a week ago, but school and work takes priority! (well, most of the time anyways)

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
while alive = 1
wakeup
program(propeller)
eat(3)
sleep(7)