Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

Unable to set time on USER clock
Goto page 1, 2  Next
 
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR
View previous topic :: View next topic  
Author Message
CBailey

Bascom Member



Joined: 07 May 2015
Posts: 109

blank.gif
PostPosted: Mon Apr 25, 2016 4:25 pm    Post subject: Unable to set time on USER clock Reply with quote

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
View user's profile
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 2262

blank.gif
PostPosted: Mon Apr 25, 2016 7:29 pm    Post subject: Re: Unable to set time on USER clock Reply with quote

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
View user's profile
CBailey

Bascom Member



Joined: 07 May 2015
Posts: 109

blank.gif
PostPosted: Mon Apr 25, 2016 9:17 pm    Post subject: Reply with quote

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
View user's profile
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 2262

blank.gif
PostPosted: Mon Apr 25, 2016 9:56 pm    Post subject: Reply with quote

How about SYSSEC()...
Back to top
View user's profile
CBailey

Bascom Member



Joined: 07 May 2015
Posts: 109

blank.gif
PostPosted: Mon Apr 25, 2016 10:32 pm    Post subject: Reply with quote

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
View user's profile
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 2262

blank.gif
PostPosted: Mon Apr 25, 2016 10:59 pm    Post subject: Reply with quote

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
View user's profile
CBailey

Bascom Member



Joined: 07 May 2015
Posts: 109

blank.gif
PostPosted: Tue Apr 26, 2016 12:40 am    Post subject: Reply with quote

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
View user's profile
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 2262

blank.gif
PostPosted: Tue Apr 26, 2016 10:52 am    Post subject: Reply with quote

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
View user's profile
O-Family

Bascom Expert



Joined: 23 May 2010
Posts: 320
Location: Japan

japan.gif
PostPosted: Tue Apr 26, 2016 4:23 pm    Post subject: Reply with quote

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
View user's profile Visit poster's website
CBailey

Bascom Member



Joined: 07 May 2015
Posts: 109

blank.gif
PostPosted: Tue Apr 26, 2016 8:13 pm    Post subject: Reply with quote

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
View user's profile
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 5923
Location: Holland

blank.gif
PostPosted: Tue Apr 26, 2016 8:22 pm    Post subject: Reply with quote

what about Incr _bday
_________________
Mark
Back to top
View user's profile Visit poster's website
CBailey

Bascom Member



Joined: 07 May 2015
Posts: 109

blank.gif
PostPosted: Tue Apr 26, 2016 10:45 pm    Post subject: Reply with quote

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
View user's profile
O-Family

Bascom Expert



Joined: 23 May 2010
Posts: 320
Location: Japan

japan.gif
PostPosted: Tue Apr 26, 2016 11:39 pm    Post subject: Reply with quote

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
View user's profile Visit poster's website
CBailey

Bascom Member



Joined: 07 May 2015
Posts: 109

blank.gif
PostPosted: Tue Apr 26, 2016 11:48 pm    Post subject: Reply with quote

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
View user's profile
O-Family

Bascom Expert



Joined: 23 May 2010
Posts: 320
Location: Japan

japan.gif
PostPosted: Wed Apr 27, 2016 12:00 am    Post subject: Reply with quote

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
View user's profile Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR All times are GMT + 1 Hour
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum