PDA

View Full Version : DS1302 add ons



GrantmcF
11-15-2011, 04:34 PM
I'm looking for routines to add a scheduler and control buttons to the DS1302_full object. I want to be able to use the buttons to set the time and date without a PC interface. I looked through the OBEX and did not find anything that looked like it would work. Anybody have any suggestions?

BR
11-16-2011, 02:42 AM
You could try this object (http://obex.parallax.com/objects/696/) as a starting point. It is pretty easy to add functionality using it as a template...

JLocke
11-16-2011, 05:44 AM
I've used that same object (beau1button) to set the time on a DS1307. Works great, and only 1 button!

Here's how I use the button. This loop is in cog 0, as all the work is being done in other cogs...


repeat ' loop
if ina[BUTTON] ' ├─ if button pressed
waitcnt((clkfreq / 16) + cnt) ' │ ├─ short delay to help debounce
' │ │
if ina[BUTTON] ' │ └─ button still pressed?
settingRTC := TRUE ' │ └─ set the indicator flag
' │
waitcnt((clkfreq / 10) + cnt) ' └─ wait 0.1 seconds


My various cog methods check the flag 'settingRTC' to see if they should alter their behavior. For instance, the 'clock' cog:




PUB cog_Clock | t
''* initializes and reads the RTC
''* updates the global time variables every 1 sec
''
' == cog initialization ==
rtc.RTCEngineStart(RTC_DATA, RTC_CLOCK, -1) ' start the clock object

waitcnt((clkfreq * 2) + cnt) ' wait 2 seconds
t := cnt

' == cog loop ==
repeat ' loop
ifnot settingRTC ' └─ if normal operation
rtc.readTime ' ├─ get current time
' │
hour := rtc.clockHour ' ├─ update the hub time values
minute := rtc.clockMinute ' │
second := rtc.clockSecond ' │
day := rtc.clockDate ' │
month := rtc.clockMonth ' │
year := rtc.clockYear ' │
dow := rtc.clockDay ' │
waitcnt(t += clkfreq) ' ├─ update every second
' │
else ' else we're setting the date/time
setTimeButton ' ├─ so call function to set the date/time
Comms.str(LCD, String("?f")) ' ├─ clear the LCD screen
waitcnt((clkfreq / 20) + cnt) ' ├─ short delay after screen clear
settingRTC := FALSE ' ├─ reset the flag
t := cnt ' ├─ keep the count going
waitcnt(t += clkfreq) ' └─ update 't' for one second delay



other cogs test for the flag similarly. And these two functions tie it up. I think you'll find the object (beau1button) is well documented and easy to understand.




PRI beau1button(buttonPin, strPtr, varSet, loLim, hiLim) | tmp
' 1-button user input routine as suggested by Beau:
' http://forums.parallax.com/showthread.php?p=921531
'
'* press and release to increment digit
'* press and hold to increment field
'buttonPin = input button pin number
'strPtr = pointer to message string output to line 1 of LCD
'varSet = variable to be set (determines initial setting
'loLim = lower limit of variable range
'hi Lim = high limit of variable range
'* set up to work with a single pushbutton switch connected to the buttonPin with a pulldown
'* outputs to LCD
'
' 220Ω
' +3.3v ─[button]──┳──── buttonPin
' │
' 10KΩ
' 
' gnd
'
waitpeq(0, |< buttonPin, 0) 'wait for button release

repeat 'loop
Comms.str(LCD, String("?f")) '├─ clear screen
Comms.str(LCD, strPtr) '├─ display the prompt
Comms.str(LCD, String("?y1")) '├─ move to the first line
Comms.str(LCD, String("?x00")) '├─ move to first column
Comms.str(LCD, SimplNum.decx(varSet, 2)) '├─ display the variable
'│
waitpeq(|< buttonPin, |< buttonPin, 0) '├─ wait for button press
tmp := cnt '├─ copy the counter value
waitcnt((clkfreq / 20) + cnt) '├─ short delay to help debounce
waitpne(|< buttonPin, |< buttonPin, 0) '├─ wait for button release
'│
if (cnt - tmp) > INCR_MENU '├─ button held down > 1 sec?
return varSet '│ └─ yes, return value
else '├─ otherwise
varSet++ '│ └─ no, increment value
'│
if varSet > hiLim '├─ if top of list
varSet := loLim '│ └─ start over at bottom of list
'│
waitcnt((clkfreq / 5) + cnt) '└─ short pause to help debounce

{
================================================== =============================
}
PRI setTimeButton
'routine to set day/time using a single button
'has to be called from cog_Clock (for access to rtc)

repeat until not lockset(lockData) ' lock the access

dow := beau1Button(BUTTON, @dowStr, dow, 1, 7)
day := beau1Button(BUTTON, @dayStr, day, 1, 31)
month := beau1Button(BUTTON, @monthStr, month, 1, 12)
year := beau1Button(BUTTON, @yearStr, year, 11, 26)
hour := beau1Button(BUTTON, @hourStr, hour, 0, 23)
minute := beau1Button(BUTTON, @minuteStr, minute, 0, 59)
second := 0
second := beau1Button(BUTTON, @secondStr, second, 0, 59)
year := year + 2000 ' fill out the year
rtc.writeTime(second, minute, hour, dow, day, month, year)
waitcnt((clkfreq / 4) + cnt)
lockclr(lockData) ' unlock

DAT
FileHeader byte "Date,Time,Upstairs (F),Humidity (%),Downstairs (F),Outside (F),Humidity (%)", 13, 0

dowStr byte "Set Day (Sun=1)", 0 'prompt strings used in setting RTC
dayStr byte "Set Date", 0
monthStr byte "Set Month", 0
yearStr byte "Set Year", 0
hourStr byte "Set Hour", 0
minuteStr byte "Set Minute", 0
secondStr byte "Set Second", 0

GrantmcF
11-17-2011, 03:41 PM
Thanks guys. I will try that when I get a chance. I'm fighting the DS1302_ full right now. I can't seem to get the debug display to come up. I have to get that object to work as advertised first before I start modifying.

Jorge P
11-20-2011, 11:56 AM
I am doing something similar to this project but I am not using buttons, I will be using a 4x4 matrix keypad to change the date and time through a couple of PCA9555 IC's. I need to finish a few things before I continue with the project but it is still moving along in the background. I have a DS1302 connected to pins 3, 4, 5 of the propeller in that project. I am only using the 6 digit 16 segment display on the "Propeller Professional Development Board" to view the date and time so you may have to add some code to see the output on other systems.

If you would like to see a bit more about my project using a PCA9555 bus extender, check out my post in the next few weeks as I will be posting a new update. http://forums.parallax.com/showthread.php?134537-PPDB-Using-the-full-PPDB-Code-Version-1.3 .

GrantmcF
12-04-2011, 04:34 PM
I finally got the clock working with a serial lcd and 3 button configuration. I'm still need help with an on/off scheduler. Are there any objects available to kick start me?