PDA

View Full Version : Programming RTC DS1302 on PPDM



StephenMoore
11-13-2011, 10:33 PM
I am having a hard time getting a response out of the clock chip on my PPDM. I use the following with the SPI_Asm and Full DuplexSerial objects:
All I get are zeros for data:




{{
************************************************
* Propeller SPI Assembly Demo v1.0 *
* Author: Beau Schwabe *
* Copyright (c) 2009 Parallax *
* See end of file for terms of use. *
************************************************
}}


CON
_clkmode = xtal1 + pll16x
_xinfreq = 5_000_000


OBJ
SPI : "SPI_Asm" ''The Standalone SPI Assembly engine
Ser : "FullDuplexSerial" ''Used in this DEMO for Debug


CON
' DS1302 command definitions

readSec = $81
readMin = $83
readHr = $85
readDate = $87
readMonth = $89
readDay = $8B
readYear = $8D
readWP = $8F
readTCS = $91
readRAM = $C1 ' RAM from $C1 to $FD

writeSec = $80
writeMin = $82
writeHr = $84
writeDate = $86
writeMonth= $88
writeDay = $8A
writeYear = $8C
writeWP = $8F
writeTCS = $91
writeRAM = $C1 ' RAM from $C0 to $FC

readClockBurst = $BF
readRAMBurst = $FF
writeClockBurst = $BE
writeRAMBurst = $FE






PUB SPI_DEMO|DQ,CLK,RESET,ClockDelay,ClockState,Sec,Mi nute,Hr
{{
Once the 'SPI.start' command is called from Spin, it will remain running in its own COG.
If the SHIFTIN or SHIFTOUT command are called with 'Bits' set to Zero, then the COG will shut
down. Another way to shut the COG down is to call the 'SPI.stop' command from Spin.

--------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------


DS1302 comm program

--------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------


}}


'' -----[ Initialization ]--------------------------------------------------


''Serial communication Setup
Ser.start(31, 30, 0, 9600) '' Initialize serial communication to the PC through the USB connector
'' To view Serial data on the PC use the Parallax Serial Terminal (PST) program.


''SPI Setup
''SPI.start(ClockDelay, ClockState)
SPI.start(15, 1) '' Initialize SPI Engine with Clock Delay of 15 and Clock State of 1




''DS1302 Setup


DQ := 0 '' Set DS1302 Data Pin
CLK := 1 '' Set DS1302 Clock Pin
Reset := 2 '' Set DS1302 Reset Pin


waitcnt(clkfreq * 3 +cnt)


Ser.str(string("DS1302 clock"))
Ser.tx(13)
Ser.tx(13)

' -----[ Program Code ]----------------------------------------------------
repeat
HIGH(Reset) '' alert the DS1302
SPI.SHIFTOUT(DQ, CLK, SPI#LSBPRE , 8, readSec) '' Request to read the time
Sec := SPI.SHIFTIN(DQ, CLK, SPI#LSBPOST, 8) '' read the time
LOW(Reset) '' release the DS1302
Sec := Sec & $0F '' mask seconds value
Ser.str(string("Seconds = "))
Ser.dec(Sec)
Ser.tx(13)
waitcnt(clkfreq + cnt)



PUB HIGH(Pin)
dira[Pin]~~
outa[Pin]~~

PUB LOW(Pin)
dira[Pin]~~
outa[Pin]~
{{
┌───────────────────────────────────────────────── ────────────────────────────────────────────────── ───────────────────────────┐
│ TERMS OF USE: MIT License │
├───────────────────────────────────────────────── ────────────────────────────────────────────────── ───────────────────────────┤
│Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation │
│files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, │
│modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software│
│is furnished to do so, subject to the following conditions: │
│ │
│The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.│
│ │
│THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE │
│WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR │
│COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, │
│ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. │
└───────────────────────────────────────────────── ────────────────────────────────────────────────── ───────────────────────────┘
}}



Can anyone help?

BR
11-13-2011, 11:46 PM
You might try this object (http://obex.parallax.com/objects/519/) to see if you have more luck with it...I've tried to make it fairly bullet-proof so will hopefully work with minimal effort. And obviously before anything else, make sure that your wire connections are correct.

Good luck.

StephenMoore
11-14-2011, 01:58 AM
Works great thank you very much. Is it necessary to initialize the DS1302 with clear halt bit in order to get a reading?

BR
11-15-2011, 01:30 AM
I don't think so. Now that you've got a working example, you can play around with it a bit to see what works. Note that there are two "setup" functions included, one that initializes the RTC and another that initializes the prop to drive the RTC hardware. If you've got a battery backup on your RTC, only the prop setup function need be called on power-up. Of course, you have to implement some kind of scheme to detect whether the RTC has already been initialized, though...for example, storing a known value (255, let's say) in one of the RAM registers. Then, on prop power-up, you can read this register as part of your boot-up routine...if it contains a 255, you know the RTC time is valid. If not, prompt for user input to set clock.

Hope this helps.

StephenMoore
11-16-2011, 04:50 AM
This makes sense. I need to rig up a battery holder with some sort of 5V battery.

Thank you for taking the time to help.

SM