Shop OBEX P1 Docs P2 Docs Learn Events
Optimized Clock Engine — Parallax Forums

Optimized Clock Engine

KyeKye Posts: 2,200
edited 2009-11-05 00:34 in Propeller 1
Hey guys,

I just finished a spin·driver for the DS1307.

It features public methods for:

Getting/Setting the seconds on the DS1307,
Getting/Setting the minutes on the DS1307,
Getting/Setting the hours on the DS1307,
Getting/Setting the day on the DS1307,
Getting/Setting the date on the DS1307,
Getting/Setting the·month on the DS1307,
Getting/Setting the·year on the DS1307,

Turning the square wave output on (to a constant value which can be changed),
Turning the square wave output off (to a constant value which can be changed),

Getting/Setting the NVRAM on the DS1307,

The driver·has BCD to decimal conversion built in so that it returns decimal numbers for the time and can be·configured with decimal numbers for the time.

The driver has been completely optimized and features no bloat, the driver also includes idiot protection·which limits all method input parameters into their acceptable range.

Interface schematics for the I2C bus are included with driver as well as the ability to change the pins used for communication.

Enjoy!


▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Nyamekye,
«1

Comments

  • T ChapT Chap Posts: 4,223
    edited 2009-02-26 02:55
    Thanks for posting, I am looking to test the 1307 soon.
  • mctriviamctrivia Posts: 3,772
    edited 2009-02-26 04:26
    i need to figure out if this is compatible with my 1340. If so this will be a lot of help.
  • mctriviamctrivia Posts: 3,772
    edited 2009-02-26 04:59
    nope unfortunately not compatible.
  • KyeKye Posts: 2,200
    edited 2009-02-26 05:28
    Would you like me make another one for you?

    Just pass me the spec sheet, and I'll post the modified code here.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Nyamekye,
  • mctriviamctrivia Posts: 3,772
    edited 2009-02-26 05:38
    http://www.datasheetcatalog.org/datasheet/maxim/DS1340.pdf

    pretty close but not the same exactly.
  • KyeKye Posts: 2,200
    edited 2009-02-26 06:08
    Okay, Give me a while (I'm very fast actually) and I'll have done for you in no time.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Nyamekye,
  • KyeKye Posts: 2,200
    edited 2009-02-26 14:33
    Okay, here it is, a clock driver for the DS1340

    I looked over the data sheet and it should have worked somewhat already with the DS1307 driver. Both have the same I2C setup.

    Good luck,



    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Nyamekye,
  • mctriviamctrivia Posts: 3,772
    edited 2009-02-26 15:35
    thanks. I will try it out tonight. the time feature would be compatible but the memory location to some other feature was different.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Need to make your prop design easier or secure? Get a PropMod $50CAN has crystal, eeprom, and programing header in a 40 pin dip 0.7" pitch module.
  • mctriviamctrivia Posts: 3,772
    edited 2009-02-26 23:42
    tryed the following code

    obj
    oClock: "clockEngine"

    pub main|hhh,mmm,sss
    hhh:=oClock.getHours
    mmm:=oClock.getMinutes
    sss:=oClock.getSeconds
    oD.PrintNum(0,7,0,hhh,2)
    oD.PrintNum(20,7,0,mmm,2)
    oD.PrintNum(40,7,0,sss,2)



    i get a value of 165,165,165 printed to my screen any idea why?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Need to make your prop design easier or secure? Get a PropMod $50CAN has crystal, eeprom, and programing header in a 40 pin dip 0.7" pitch module.
  • KyeKye Posts: 2,200
    edited 2009-02-27 04:12
    I think you get 65 when the driver can't get the device to respond... as in it gets all ones from the clock. Remember the BCD conversion.

    Question, are you using pull ups on both I2C pins? Its required here for the driver, it does not drive the I/O pins.

    And, I can give you pointers if you would like on what parts of the driver do what but its not really complex. Fairly simply infact. If you want to reprogram it a little.

    I already checked to make sure it runs the same way for acessing and such as the DS1307, so... I can't really say why it doesn't work for the DS1340.

    Try setting the date and such first to make sure the clock is started and look at how you need to setup the real time clock as described in the manual. The DS1340 is not as simple as the DS1307.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Nyamekye,
  • mctriviamctrivia Posts: 3,772
    edited 2009-02-27 04:49
    no i only have a pull up on sda. the prop manual schematic does not show one on scl.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Need to make your prop design easier or secure? Get a PropMod $50CAN has crystal, eeprom, and programing header in a 40 pin dip 0.7" pitch module.
  • Mike HuseltonMike Huselton Posts: 746
    edited 2009-02-27 06:07
    Groovy.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    JMH
  • KyeKye Posts: 2,200
    edited 2009-02-27 14:52
    Well, theres your problem.

    Also, remember to configure the clock before reading from it first.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Nyamekye,
  • mctriviamctrivia Posts: 3,772
    edited 2009-03-01 03:10
    1340 code works though not quiet true to spec. But i understand the pull up resister makes coding faster.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Need to make your prop design easier or secure? Get a PropMod $50CAN has crystal, eeprom, and programing header in a 40 pin dip 0.7" pitch module.
  • KyeKye Posts: 2,200
    edited 2009-03-01 03:14
    That's good to hear, I coded it on the protoboard so I had pullups. Also the I2C bus is open drain so its more true to spec that actually driving the line which can give you problems - and its faster and more compacct as you say.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Nyamekye,
  • mctriviamctrivia Posts: 3,772
    edited 2009-03-01 03:23
    the sda is open drain. scl i am pretty sure is not. adding the pull up resister is no problem. I love how you layed out the i2c drivers. i am now converting your code to pasm and adding the eeprom driver also. goal is to have 1 cog work as eeprom loader and clock.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Need to make your prop design easier or secure? Get a PropMod $50CAN has crystal, eeprom, and programing header in a 40 pin dip 0.7" pitch module.
  • mctriviamctrivia Posts: 3,772
    edited 2009-03-01 03:42
    however it is probably a good idea to give your constants a name and just use that like in this file

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Need to make your prop design easier or secure? Get a PropMod $50CAN has crystal, eeprom, and programing header in a 40 pin dip 0.7" pitch module.
  • KyeKye Posts: 2,200
    edited 2009-03-01 18:18
    Yeah, thats alot more readable.

    Note that I programmed the driver to send not aknowledges and aknowledges automatically. So, you will need to modify the receiving and transmiting rountines or they will allow for only one byte at a time.

    And, I would reconmend just making a spin processor and using the code as it is. I first tried to do this driver in asm and I found that it was a complete wates of one processor, and very flaky...

    Better to just have one processor running the code in the background in spin.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Nyamekye,
  • mctriviamctrivia Posts: 3,772
    edited 2009-03-01 18:30
    I was planning to interleve several tasks together. still not sure if it is worth it though.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Need to make your prop design easier or secure? Get a PropMod has crystal, eeprom, and programing header in a 40 pin dip 0.7" pitch module with uSD reader, and RTC options.
  • KyeKye Posts: 2,200
    edited 2009-03-02 00:59
    If you want a headache go for it. I posted my asm engine a while back. Look arround for it in the forums.

    The name was something akin to asm I2C driver, do with it as you will or something. Note, that it was buggy....

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Nyamekye,
  • tdtd Posts: 8
    edited 2009-03-03 22:34
    Kye:

    I am using the 1307 chip and find that every once in awhile I will get back 165 for all time/date values.· If I read it again the values come back 165 again then 2-3 reads later it is all fine again.· I am not sure if it is external noise (capacitative or not).· Do you have any insights into this.· I am basically troubleshooting a prototype and am dumping the values on the screen using tvout as I read them from the chip.

    I am going to give your driver a try instead of the one I am currently using which was a general i2c driver that I modded to work with the 1307....

    Tony
  • mctriviamctrivia Posts: 3,772
    edited 2009-03-03 23:31
    I had that with my 1340 when I had no pull up on sda maybe it is loose

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Need to make your prop design easier or secure? Get a PropMod has crystal, eeprom, and programing header in a 40 pin dip 0.7" pitch module with uSD reader, and RTC options.
  • KyeKye Posts: 2,200
    edited 2009-03-04 00:40
    Mine should work if you have a pull ups on both lines. Its perfected.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Nyamekye,
  • mctriviamctrivia Posts: 3,772
    edited 2009-03-04 01:55
    ya. the only problem is if you download the time at 5:59:59 and you do it in order h,m,s but it rolls over to 6:00:00 before reading minutes the clock will say 6:59:59 momentarily. is there any way to download all time and date data at once? my temporary fix is to download twice back to back and see if time jumped more then 30 sec.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Need to make your prop design easier or secure? Get a PropMod has crystal, eeprom, and programing header in a 40 pin dip 0.7" pitch module with uSD reader, and RTC options.
  • KyeKye Posts: 2,200
    edited 2009-03-04 02:15
    Well, yes, but well, I disabled that feature. If you want to hack the driver go ahead...but seriously, why would that ever be a concern?

    (The feature for bulk transfers is listed in the data sheet)

    You just set the clock and let it run...

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Nyamekye,
  • mctriviamctrivia Posts: 3,772
    edited 2009-03-04 02:47
    set clock and let it run is great. it is a rare problem that did happen to me one time. I just happened to be reading the time from the clock got the hours then it rolled over and got the minutes and seconds. my system was off for an hour until I read the clock again.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Need to make your prop design easier or secure? Get a PropMod has crystal, eeprom, and programing header in a 40 pin dip 0.7" pitch module with uSD reader, and RTC options.
  • propwellpropwell Posts: 87
    edited 2009-10-22 08:20
    Hi Kye,

    thank you for your driver! Just one question:
    Somebody said...
    Interface schematics for the I2C bus are included with driver as well as the ability to change the pins used for communication.

    is it made by just changing the to variables at the beginning? I unfortunately connected the clock on pins 8 and 9, and don't wan't to produce a new board!

    Thank you,
    Max
  • propwellpropwell Posts: 87
    edited 2009-10-22 08:23
    ah and another question: as the DS1307 qorks with 5V, don't i have to translate the signals sent by / to the DS1307 to 3,3V (what the propeller uses?)
  • photomankcphotomankc Posts: 943
    edited 2009-10-22 13:47
    There are specs in the Phillips I2C datasheets that explain how to properly interface a 5V and 3.3V I2C bus. I imagine many people cheat and just tie the clock and data to 3.3V and use the fact that it will work well enough. The 3.3V high signal is enough to register as a logic 1.

    I found this for Phillips suggestion on a level-shifter but I have seen people suggest just series resistors between the levels and pullups on either side.

    http://www.nxp.com/news/backgrounders/bg_esc9727/
  • photomankcphotomankc Posts: 943
    edited 2009-10-22 14:00
    @Kye,

    I'm writing a driver for the NXP PCF8583 and I came across your code for the 1307. Although they are fairly different animals for getting and setting I can make good use of your I2C code (fast for spin, I measured it at around 30KHz) and BCD conversions. Is there any particular way you would like me to credit that in the code? I plan to put the driver up on the exchange once complete. I have it mostly running right now, just need to get the year taken care of. It only does a 4 year count not the full 0-99 count of most clocks.
Sign In or Register to comment.