View previous topic :: View next topic |
Author |
Message |
CBailey
Joined: 07 May 2015 Posts: 109
|
Posted: Mon Apr 25, 2016 4:25 pm Post subject: Unable to set time on USER clock |
|
|
Based on code found here: http://www.mcselec.com/index2.php?option=com_forum&Itemid=59&page=viewtopic&t=11291
I made the below code. It works great, except I can't figure out how to set it. The clock starts at zero, and goes back to what it was every time I try to set it.
What am I missing?
Thanks!
Quote: | '
'*******************************************************************************
$regfile = "usb1286.dat"
$crystal = 16000000
$baud = 19200
$framesize = 100
$hwstack = 100
$swstack = 100
Config CLOCKDIV = 1
'********************************************************************************
'Configure Clock
Dim local_time As Long ' local_time is the number of seconds in the day
Dim New_second As Byte
Config Date = Dmy , Separator = .
Config Clock = User
'we want to set up counter 1 to toggle 1x per sec
Config Timer1 = Counter , Prescale = 1024 'counts 16,000,000/1024 = 15625 counts per sec
On Timer1 Timer1Overflow
Enable Timer1
Enable Interrupts
Dim Seconds as Byte
Dim PreviousSeconds as Byte ' This is just a temporary variable to determine a second has passed
Dim TimerPreset as Long
print "START START START START START START START"
'***************************************************************************
'***************************************************************************
TimerPreset = 65535 - 15625 ' Timer counts up to 65535. This makes timer count only 15625
' TimerActive = 1 ' This would be set by the program. We set it here to test.
Timer1 = TimerPreset
'*************************************************************
time$ = "23:58:00"
Date$ = "03/23/16"
do
If PreviousSeconds <> Seconds then
PreviousSeconds = Seconds
' Here we service the RTC
Time$ = Time(local_time)
Date$ = Date(local_time)
endif
' This is just to test setting time after it's been running
if local_time = 11 then
time$ = "23:58:00"
Date$ = "01/23/17"
end if
Print time$ ; " " ; Date$ ; " " ; local_time ' Just for testing
loop
'***************************
getdatetime:
Return
Setdate:
Return
Settime:
Return
'****************************************************************************
'********************************************************************************
Timer1Overflow:
'Print "We triggered an Interrupt"
Incr Seconds
Incr Local_time
Set New_second
Timer1 = TimerPreset ' This is 65536 - 15625
Return |
(BASCOM-AVR version : 2.0.7.8 ) |
|
Back to top |
|
|
MWS
Joined: 22 Aug 2009 Posts: 2262
|
Posted: Mon Apr 25, 2016 7:29 pm Post subject: Re: Unable to set time on USER clock |
|
|
CBailey wrote: | What am I missing? |
Every time another second passed by, time and date is reset to local_time:
Code: | If PreviousSeconds <> Seconds then
' ...
Time$ = Time(local_time)
Date$ = Date(local_time)
endif |
And local_time, besides being increase by one, stays the same and is not influenced by time and date. |
|
Back to top |
|
|
CBailey
Joined: 07 May 2015 Posts: 109
|
Posted: Mon Apr 25, 2016 9:17 pm Post subject: |
|
|
Thanks for the help MWS!
It's pretty obvious I don't understand the time functions. However, with your help I did figure out this way to get the time to work:
Quote: | Settime:
local_time = secofday()
Return |
When I do this, I can set the time, and it appears to be correct. However, I can't figure out how to do the same thing with the date. Thoughts? |
|
Back to top |
|
|
MWS
Joined: 22 Aug 2009 Posts: 2262
|
Posted: Mon Apr 25, 2016 9:56 pm Post subject: |
|
|
How about SYSSEC()... |
|
Back to top |
|
|
CBailey
Joined: 07 May 2015 Posts: 109
|
Posted: Mon Apr 25, 2016 10:32 pm Post subject: |
|
|
That seems to work. Am I correct in assuming I need to update the date manually? When the clock rolls over, the date is staying the same. |
|
Back to top |
|
|
MWS
Joined: 22 Aug 2009 Posts: 2262
|
Posted: Mon Apr 25, 2016 10:59 pm Post subject: |
|
|
CBailey wrote: | Am I correct in assuming I need to update the date manually? |
What means "manually"? You've updated TIME$ via time() and DATE$ via date(), if you omit the one or the other, then it is not updated - as one would epect. |
|
Back to top |
|
|
CBailey
Joined: 07 May 2015 Posts: 109
|
Posted: Tue Apr 26, 2016 12:40 am Post subject: |
|
|
What I meant was that I update the seconds manually, and didn't know if I had to update the days manually as well. As it is, they don't update when the clock rolls past midnight. |
|
Back to top |
|
|
MWS
Joined: 22 Aug 2009 Posts: 2262
|
Posted: Tue Apr 26, 2016 10:52 am Post subject: |
|
|
CBailey wrote: | they don't update when the clock rolls past midnight. |
Time() and Date() are conversion routines, their business is not to look up the previous assigned variable and decide upon a difference whether to alter the result.
Any additional logic must be implemented by the user, which is also quite understandable, as they have to be universal. |
|
Back to top |
|
|
O-Family
Joined: 23 May 2010 Posts: 320 Location: Japan
|
Posted: Tue Apr 26, 2016 4:23 pm Post subject: |
|
|
Hi CBailey
Month and day are reversed.
Config Date = Dmy , Separator = .
Config Date = Mdy , Separator = .
Code: |
'********************************************************************************
'Configure Clock
Dim Local_time As Long ' local_time is the number of seconds in the day
Dim New_second As Byte
Config Date = Dmy , Separator = .
Config Clock = User
Time$ = "23:59:59"
Date$ = "23.03.16"
Print Time$ ; " " ; Date$ ; " " ; Local_time
Local_time = Syssec()
Local_time = Local_time + 1
Time$ = Time(local_time)
Date$ = Date(local_time)
Print Time$ ; " " ; Date$ ; " " ; Local_time ' Just for testing
Stop
|
|
|
Back to top |
|
|
CBailey
Joined: 07 May 2015 Posts: 109
|
Posted: Tue Apr 26, 2016 8:13 pm Post subject: |
|
|
Thanks, I noticed that I had the wrong date format after I posted my code.
I'm having problems finding a way to increment the day. I've found a number of read-only system variables, but no easy way to add a day, something like 'Day = Day + 1'. Does anyone know how to increment a day? |
|
Back to top |
|
|
albertsm
Joined: 09 Apr 2004 Posts: 5923 Location: Holland
|
Posted: Tue Apr 26, 2016 8:22 pm Post subject: |
|
|
what about Incr _bday _________________ Mark |
|
Back to top |
|
|
CBailey
Joined: 07 May 2015 Posts: 109
|
Posted: Tue Apr 26, 2016 10:45 pm Post subject: |
|
|
Thanks for the reply Mark!
I think you might have misunderstood. Instead of figuring out how to increment, I'm trying to find a way to increment a day. For instance, if Date$ = "03.23.16", I would like an easy way to increment the day to "03.24.16" (Assuming CONFIG CLOCK = MDY) |
|
Back to top |
|
|
O-Family
Joined: 23 May 2010 Posts: 320 Location: Japan
|
Posted: Tue Apr 26, 2016 11:39 pm Post subject: |
|
|
I do not understand a reason why you want to add 1 day.
I think that it is not necessary for a clock.
However, one day is 86,400 seconds. (60sec * 60min * 24hour)
Code: |
'********************************************************************************
'Configure Clock
Dim Local_time As Long ' local_time is the number of seconds in the day
Dim New_second As Byte
Config Date = Mdy , Separator = .
Config Clock = User
Time$ = "23:59:59"
Date$ = "03.23.16"
Print Time$ ; " " ; Date$ ; " " ; Local_time
Local_time = Syssec() 'To convert the date and time in seconds.
Local_time = Local_time + 86400 'Adds one day.
Time$ = Time(local_time) 'To convert the seconds on the date and time.
Date$ = Date(local_time)
Print Time$ ; " " ; Date$ ; " " ; Local_time ' Just for testing
Stop
|
Last edited by O-Family on Tue Apr 26, 2016 11:54 pm; edited 1 time in total |
|
Back to top |
|
|
CBailey
Joined: 07 May 2015 Posts: 109
|
Posted: Tue Apr 26, 2016 11:48 pm Post subject: |
|
|
Ahh, that's a good way to do it. I also figured out what Mark might have been saying. While I couldn't find anything about _bday, I did find _day, and solved it this way:
Quote: |
If time$ = "23:59:59" then
Incr _day
End If
|
|
|
Back to top |
|
|
O-Family
Joined: 23 May 2010 Posts: 320 Location: Japan
|
Posted: Wed Apr 27, 2016 12:00 am Post subject: |
|
|
You're mistaken.
Even increments the _day, month and year are unchanged.
By adding one second to convert the date and time in seconds, date is also added automatically.
Local_time = Local_time + 1
Did you try my attached program ? |
|
Back to top |
|
|
|