Home
JAQForum Ver 20.06
Log In or Join  
Active Topics
Local Time 10:31 19 May 2024 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : Maximite and RTC ds1307

     Page 2 of 2    
Author Message
BobD

Guru

Joined: 07/12/2011
Location: Australia
Posts: 935
Posted: 02:05pm 17 Jan 2014
Copy link to clipboard 
Print this post

  Grogster said  
  Juri74 said  (i was lost on dayoftheweek=.. line)


You and me both!!!!

That is some serious mathematics going on there...

I put a timer on this function of TZA's for interest's sake, and it crunches all that maths in 3ms - wow - impressive. It will take me 3 DAYS just to get my head around what the hell it is all doing!!!

@ TZA - You description post above is very helpful in understanding the maths a bit more.

You guys don't need to worry about the actual detail of the calculations. Just read the Wikipedia link I referred to back here. and it describes the theory behind the various algorithms. How they accommodate the varying years, leap years and months. Just gloss over their equations unless you are going to develop your own code from them.
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9082
Posted: 02:07pm 17 Jan 2014
Copy link to clipboard 
Print this post

Oh I know what you are saying, Bob, and I have read the link a couple of times, but still don't really follow it. It's as simple as wanting to understand how it works, is all.

You're right though - it does not really matter in the larger scheme of things, so long as it works! Edited by Grogster 2014-01-19
Smoke makes things work. When the smoke gets out, it stops!
 
paceman
Guru

Joined: 07/10/2011
Location: Australia
Posts: 1329
Posted: 08:12pm 17 Jan 2014
Copy link to clipboard 
Print this post

Graeme/TZ,
How about adding it to the MMBasic library. There are quite a few of these great little bits of code that are so useful to many people but not that easy to find otherwise.

Greg
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9082
Posted: 08:28pm 17 Jan 2014
Copy link to clipboard 
Print this post

TZA wrote the hard part, but I believe Bob also has a similar routine.

I can't steal that and put it in the library, as I did not write it!

If TZA is happy enough to have it entered, I am more then happy to have my bit too, but I don't want to step on anyone's toes, as I did not actually write the routine, if you see what I mean.(other then the bit with the array and calling the function to show the day of the week by name, but that is an addition to TZA's source more then anything...)

How do you submit something for the library anyway?


Smoke makes things work. When the smoke gets out, it stops!
 
BobD

Guru

Joined: 07/12/2011
Location: Australia
Posts: 935
Posted: 08:38pm 17 Jan 2014
Copy link to clipboard 
Print this post

  Grogster said   TZA wrote the hard part, but I believe Bob also has a similar routine.

I can't steal that and put it in the library, as I did not write it!

If TZA is happy enough to have it entered, I am more then happy to have my bit too, but I don't want to step on anyone's toes, as I did not actually write the routine, if you see what I mean.(other then the bit with the array and calling the function to show the day of the week by name, but that is an addition to TZA's source more then anything...)

How do you submit something for the library anyway?


Grogster
Here is how to do it at this link http://geoffg.net/maximite.html . It's public now so why not submit it. I would be surprised if TZA would not agree.
Bob

MMBasic Library
The MMBasic library is a collection of programs specifically written for MMBasic.
These include games like Space Invaders and Pacman, demonstration snippets of code
and programs to do interesting things like generate morse code. Because the
programs are so small they are all bundled together into a single downloadable file
available in the download section below.

The library is managed by Hugh Buckle and is a great resource for beginners and
experts alike. If you have written a program for MMBasic and you believe that it is
worth sharing, please send it to Hugh at mmlib@geoffg.net and he will add it in.
You should also include a description of what the program does for the library index.

 
MicroBlocks

Guru

Joined: 12/05/2012
Location: Thailand
Posts: 2209
Posted: 01:51am 18 Jan 2014
Copy link to clipboard 
Print this post

Once it is in a post it is in the public domain. :)

I thought about changing it a little.
It now accepts year, month and day.
It would probably be nicer in the MMBasic context to just have one parameter Date$.
What do you think?

Microblocks. Build with logic.
 
paceman
Guru

Joined: 07/10/2011
Location: Australia
Posts: 1329
Posted: 02:41am 18 Jan 2014
Copy link to clipboard 
Print this post

You're a good programmer TZ, do it as you see best.

Greg
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 3678
Posted: 03:04am 18 Jan 2014
Copy link to clipboard 
Print this post

  TZAdvantage said   Once it is in a post it is in the public domain. :)


No it isn't. Anything anyone writes is their copyright in vast areas of the world. (USA, Europe, etc, at least.) No need to assert copyright, it is automatic. If someone wants it to be PD then they need to say so.

Of course when it's a copy of something then the original retains whatever status it had and the copy may in itself be a breach of copyright.

I don't know what laws Thailand has but I sure know these issues for USA and Europe. I believe Australia & NZ will be similar.

JohnEdited by JohnS 2014-01-19
 
BobD

Guru

Joined: 07/12/2011
Location: Australia
Posts: 935
Posted: 12:20pm 18 Jan 2014
Copy link to clipboard 
Print this post

Given the talk about copyright, with some of the code where does it start? The piece currently being discussed is the conversion of a date into a Day of the Week (aka DoW). Like many others I subscribe to not re-inventing the wheel if at all possible. I started fiddling with RTCs on the Maximite back in late 2011 and a guy named DuinoMiteMegaAndy was active in this forum then and he was into RTCs. He put his code out in the open so I copied it and modified it for my use. Andy's work included a bit of code to convert a date to a DoW. Here is Andy's code way back then and it was probably not original. Other forum members also did a lot of work on RTCs back in 2011.

  Quote  
10 '--------ASRTC_R1.bas----------------------------
20 '-------- Auto Set RTC From PIC32 to DS3231/DS1307 Time/DOW/Date-------
30 'Author: Hacker - DuinoMiteMegaAndy
40 'Date: 17/12/2011
50 'Version: Rev. 1.0b
60 'MMBasic Firmware: 2.7 DuinoMite Mega
70 'Platform: Win XP/SP3
80 'Credits: CrackerJack
90 '
100 ' --------------Code Begin--------------------
110 'Get time from PIC32 time$ and date$ and save to DS3231 RTC
120 tempdec = VAL(LEFT$(TIME$, 2))
130 GOSUB 640
140 hours = hex
150 tempdec = VAL(MID$(TIME$, 4, 2))
160 GOSUB 640
170 minutes = hex
180 tempdec = VAL(RIGHT$(TIME$, 2))
190 GOSUB 640
200 seconds = hex
210 tempdec = VAL(LEFT$(DATE$, 2))
220 DOW_DAY = tempdec
230 GOSUB 640
240 day = hex
250 tempdec = VAL(MID$(DATE$, 4, 2))
260 DOW_MONTH = tempdec
270 GOSUB 640
280 month = hex
290 tempdec = (VAL(RIGHT$(DATE$, 4)) - 2000)
300 DOW_YEAR = tempdec
310 GOSUB 640
320 year = hex
330 rtcctrl = &h10
340 a = (14 - dow_month) / 12
350 a = FIX(a)
360 y = dow_year - a
370 y = FIX(y)
380 m = dow_month + (12 * a) - 2
390 m = FIX(m)
400 d = ((dow_day + y) + FIX(y / 4) - FIX(y / 100) + FIX(y / 400) + FIX((31 * m) / 12))
410 DOW = d MOD 7
420 DOW = DOW + 1 ' offset for DS3231/DS1307 RTC --> Sun(1)Mon(2)Tue(3)Wed(4)Thu(5)Fri(6)Sat(7)
430 tempdec = DOW ' DOW dec
440 GOSUB 640 ' convert to hex BCD
450 rtcwday = hex
460 ' Write Time to RTC
470 i2caddr = &h68 ' DS3231 I2C address
480 I2CEN 100,1000 ' Enable I2C
490 I2CSEND i2caddr, 0, 8, &h0, seconds, minutes, hours, rtcwday, day, month, year
500 I2CDIS
510 IF DOW = 1 THEN dow$ = "Sunday "
520 IF DOW = 2 THEN dow$ = "Monday "
530 IF DOW = 3 THEN dow$ = "Tuesday "
540 IF DOW = 4 THEN dow$ = "Wednesday "
550 IF DOW = 5 THEN dow$ = "Thursday "
560 IF DOW = 6 THEN dow$ = "Friday "
570 IF DOW = 7 THEN dow$ = "Saturday "
580 ? "0=ok 1=nack 2=timeout"; MM.I2C
590 ? "DS3231 RTC has been set to ";TIME$;" ";DATE$;" ";DOW$;DOW
600 INPUT "Hit CR To Continue "; Query$
610 CLS
620 RUN "b:TD_R1.bas"
630 END
640 '------Convert to Hex--------------
650 hex = FIX(tempdec / 10) * 16
660 hex = hex OR ((tempdec / 10) - (FIX(tempdec / 10))) * 10
670 RETURN
680 '


Andy's code to convert to DoW is in lines 340 to 410 with an adjustment in line 420 because some (all?) RTCs use weekday numbers from 1 to 7 rather than zero to 6 as the algorithm produces. I also found the Wikipedia article on the subject. Good enough for him and so for me and it was also very much out in public. I used Andy's code and simplified it. In particular I condensed lines 340 to 390 down to 2 lines of code and 400 to 420 into one line of code.

Note the use of FIX in the code instead of INT. In MMBasic, FIX is truncation of of the number to an integer where INT is truncation with rounding.
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9082
Posted: 02:29pm 18 Jan 2014
Copy link to clipboard 
Print this post

  JohnS said  No it isn't. Anything anyone writes is their copyright in vast areas of the world. (USA, Europe, etc, at least.) No need to assert copyright, it is automatic. If someone wants it to be PD then they need to say so.[/quote]

Yes, that is certainly true here in NZ - you don't need to go through any kind of registration or filing for copyright, it just has to be yours(not plagurised, in other words!), original, and marked with the copyright symbol, often (C) or (c), and year.

  JohnS said  Of course when it's a copy of something then the original retains whatever status it had and the copy may in itself be a breach of copyright.[/quote]

Exactly - that was why I was not wanting to "Step on anyone's toes" so to speak.

Smoke makes things work. When the smoke gets out, it stops!
 
MicroBlocks

Guru

Joined: 12/05/2012
Location: Thailand
Posts: 2209
Posted: 06:12pm 18 Jan 2014
Copy link to clipboard 
Print this post

That day of week code is already pretty old, probably as old as the first microcomputer. I don't know its origin.
Many used to share code without hanging copyrights to everything.
The one i posted and shared back then too i have used in a old dos qbasic program from around 1991-1992. I was a member of a computer club and that was what we did.
I like to rewrite code i find, like more sensible variable names, more structure if possible, that kind of things.
I already said i got it from somewhere, would not be nice if i would claim copyright.

INT in MMBasic gets the whole number less or equal. The difference is that FIX will just return the whole part. The difference is with negative numbers only.

Adding it to the library would be fine, contributions is not the reason why i post code. I suggest to contribute it to "As found on the Backshed Forum", maybe even with a link so that future users can find this forum and read the discussion about it.



Microblocks. Build with logic.
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9082
Posted: 06:55pm 18 Jan 2014
Copy link to clipboard 
Print this post

Right-o, I will submitt and comment the code, so that TZA is credited for the function that does all the number crunching. References to the forums and members here will also be included. Weather it is accepted, is another matter altogether....

Hope you all don't think I am being to pedantic about all this, but God knows there is enough ID theft and taking credit for someone else's ideas in the world, without me adding to it - credit where credit is due - I have ethics and morals...
Smoke makes things work. When the smoke gets out, it stops!
 
     Page 2 of 2    
Print this page


To reply to this topic, you need to log in.

© JAQ Software 2024