Ice Tube firmware - Drift correction with Auto DST, Dimmer

For RTC breakouts, etc., use the Other Products from Adafruit forum

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
User avatar
wbp
 
Posts: 260
Joined: Mon Mar 07, 2011 1:18 pm

Ice Tube firmware - Drift correction with Auto DST, Dimmer

Post by wbp »

I've taken my latest firmware, which has GPS support, photocell Dimmer, and Auto DST, and ported JSGF's Drift Correction into it, for those of you that don't have a GPS but want Auto DST and Drift Correction.

Enjoy!
William
Attachments
ice tube.111222wm.zip
Firmware for Ice Tube clock with Auto DST, GPS, Dimmer, and Drift Correction
(47.56 KiB) Downloaded 292 times

User avatar
brand
 
Posts: 12
Joined: Sat Dec 31, 2011 4:11 am

Re: Ice Tube firmware - Drift correction with Auto DST, Dimmer

Post by brand »

Nice Christmas present. Thanks.

I'm looking for DST, time calibration, and dimmer, but not GPS, so...

A couple-a-three questions:

If I don't have a GPS chip do I need to dike out part of the code (not a problem), or is there a check for the necessary hardware?

How does JSGF's drift correction code relate to fat16lib's calibration code from this thread: http://forums.adafruit.com/viewtopic.php?f=41&t=12720 ? (Apologies if I'm not linking that right). Is it dependent on the GPS?

How does the autodim relate to daveparker's code from this thread: http://forums.adafruit.com/viewtopic.php?f=41&t=12932 ?

Thanks again.

Take care,
brad

User avatar
wbp
 
Posts: 260
Joined: Mon Mar 07, 2011 1:18 pm

Re: Ice Tube firmware - Drift correction with Auto DST, Dimmer

Post by wbp »

There is a menu item for enabling GPS support. Just set it to "off" if you don't have a GPS (probably the default anyway). Even with it set to "on", if there is no GPS it will just wait for data on the input pin that will never come. It has to see a complete $GPRMC string before it does anything with it.

Drift correction is not dependent on the GPS, if you have a GPS you don't need it. I added it to my code so people could have the other fixes I've done plus drift correction if they don't have a GPS. I had a request to add Autodim to JSGF's firmware and I was having trouble getting it working, something about the interrupt masks maybe? Rather than spend a lot of time trying to figure that out I tried porting the drift correction logic to my firmware, and it worked, so I didn't spend any more time on the JSGF mod. I also fixed a small bug in JSGF's code along the way.

Autodim works. When I tried other firmware that supported Autodim it didn't do what I wanted, so I reworked it. It seems to work well now, try it and see if it suits you. There are 2 menu items for the low and high limits, you can adjust them as you see fit. The limit on the lower setting is currently 30 but I am tempted to make it even lower - the display would be a little uneven but even at 30 it's pretty bright in a completely dark bedroom.

FYI - the way the clock counts seconds is handled is the biggest change in the drift correction logic - the original firmware uses an overflow interrupt; the drift correction logic uses a comparator interrupt instead, which allows making small periodic changes to the comparand to trim the count.

William

User avatar
brand
 
Posts: 12
Joined: Sat Dec 31, 2011 4:11 am

Re: Ice Tube firmware - Drift correction with Auto DST, Dimmer

Post by brand »

Coolness. I have the photo-resistor soldered in and your code installed. Things seem to be working as advertised.

One question:

I used a 1K ohm resistor, rather than the original spec 10K ohm (following the thread here: http://forums.adafruit.com/viewtopic.php?f=41&t=12932 to the second or third page). I see where you're munging the brightness in the interrupt handler for SIG_ADC and that you have the values for PHOTOCELL_DARK and PHOTOCELL_LIGHT set to 1010 and 100 respectively.

My question is, are these values calibrated for the 1K ohm resistor or the 10K ohm? I can go back through the various source code packages that have been posted and compare values, but I thought I'd ask first.

Have you done anything with ifdef statements around the GPS code? It's great stuff, but I'd like to dike it out of the menu at least. The clock is a gift, and I'd like to simplify the interface.

Thanks again for the code.

Take care,
brad

User avatar
wbp
 
Posts: 260
Joined: Mon Mar 07, 2011 1:18 pm

Re: Ice Tube firmware - Drift correction with Auto DST, Dimmer

Post by wbp »

The values for PHOTOCELL_DARK and PHOTOCELL_LIGHT in my code are set for a 20K ohm resistor. It's been a while but if memory serves I was not happy with the sensitivity using a 1K resistor. I wanted the clock to get really dark in low light levels. If you use a 1K resistor you'll need to measure the values that the ADC produces and set the values accordingly.

I had a menu item that displayed the ADC result, I could probably drop that back in the code. The "TESTMODE" block also does this but I don't know if it still compiles or not.

I can probably add IFDEF for the GPS support - I'll take a look and see if I can get it to compile OK. I don't have time to test it in the clock right now so you'll have to do that.

William

User avatar
wbp
 
Posts: 260
Joined: Mon Mar 07, 2011 1:18 pm

Re: Ice Tube firmware - Drift correction with Auto DST, Dimmer

Post by wbp »

Brand, try this version and see if it does what you want. To disable the GPS feature edit the line in the iv.h file that defines it.

William
Attachments
ice tube.120112wm.zip
Ice Tube firmware with conditional GPS, display of ADC level
(47.97 KiB) Downloaded 234 times

User avatar
brand
 
Posts: 12
Joined: Sat Dec 31, 2011 4:11 am

Re: Ice Tube firmware - Drift correction with Auto DST, Dimmer

Post by brand »

William,

Again, working as advertised. Many more thanks.

The ADC level code was very helpful in setting PHOTOCELL_LIGHT and PHOTOCELL_DARK. With the 1K resistor, it typically fell between 870 and 1010. I set the levels at 900 and 1000, and it's working for me.

I didn't test the TESTMODE block...

Take care,
brad

User avatar
brand
 
Posts: 12
Joined: Sat Dec 31, 2011 4:11 am

Re: Ice Tube firmware - Drift correction with Auto DST, Dimmer

Post by brand »

Sigh.

Sorry to bother you, William, but I'm a little baffled with the drift correction. I've got the #define uncommented (and I've got the FEATURE_9 and FEATURE_WmDST the way I want them, so I'm pretty sure I've got that right -- I do have a pretty deep general computer background).

However, no matter what I set the drift correction to -- I've tried +67, +32 (in case of overflow), +48, and even -67, and no matter what I set it to, it loses about four seconds a day.

One possible glitch is that ASSR is set = to _BV(AS2), rather than or'd (|=) as in the original code, but that shouldn't make any difference in selecting the crystal.

There's an unconditional OCR2A = DRIFT_BASELINE + drift_corr; line that's commented out (apparently for testing). I'm going to uncomment it and see what that does for me.

Any other suggestions?

Thanks in advance.

Take care,
brad

User avatar
wbp
 
Posts: 260
Joined: Mon Mar 07, 2011 1:18 pm

Re: Ice Tube firmware - Drift correction with Auto DST, Dimmer

Post by wbp »

Brad,
The code in that area looks like this:

Code: Select all

#ifdef FEATURE_DRIFTCORR
  /* Apply drift correction on the first second of each hour */
//	OCR2A = DRIFT_BASELINE + drift_corr;  // always apply it (testing) - !!!
  if (time_m == 0) {
    if (time_s == 0)
			OCR2A = DRIFT_BASELINE + drift_corr;
    else if (time_s == 1)
			OCR2A = DRIFT_BASELINE;
    if (time_s <= 1) {
			/* wait for update to take effect (should only be a couple of CPU cycles) */
			while (ASSR & _BV(OCR2AUB))
				;
    }
  }
#endif  

  fix_time();  // fix up time values that overflow
Note that there is a line with "OCR2A = DRIFT_BASELINE + drift_corr;" that is NOT commented out. That's the one that is supposed to adjust the clock interval. I added the line that IS commented out to test the drift correction process. If you uncomment that line then the correction will be applied once a second, and you should see the time speed up or slow down very noticeably.

Normally the correction factor is only "applied" once per hour. If for some reason your oscillator is way off, I suppose that may not be enough. I have not done the math on this.

I think I know why it's not working though - I just realized that the test for the minute and second both being zero probably does not work where it is. Try moving the next line, the call to "fix_time()", up to above the drift correction block. Until "fix_time" is run, the seconds will be 60, not zero.

Code: Select all

  fix_time();  // fix up time values that overflow

#ifdef FEATURE_DRIFTCORR
  /* Apply drift correction on the first second of each hour */
//	OCR2A = DRIFT_BASELINE + drift_corr;  // always apply it (testing) - !!!
  if (time_m == 0) {
    if (time_s == 0)
			OCR2A = DRIFT_BASELINE + drift_corr;
    else if (time_s == 1)
			OCR2A = DRIFT_BASELINE;
    if (time_s <= 1) {
			/* wait for update to take effect (should only be a couple of CPU cycles) */
			while (ASSR & _BV(OCR2AUB))
				;
    }
  }
#endif  
William

User avatar
brand
 
Posts: 12
Joined: Sat Dec 31, 2011 4:11 am

Re: Ice Tube firmware - Drift correction with Auto DST, Dimmer

Post by brand »

Move fix_time(); right below time_s++; Check. I knew there was something about that function that bothered me (other than the fact that we might have to go back in time. : - )

I'll give it a shot.

Take care,
brad

User avatar
wbp
 
Posts: 260
Joined: Mon Mar 07, 2011 1:18 pm

Re: Ice Tube firmware - Drift correction with Auto DST, Dimmer

Post by wbp »

OK, please let me know. FWIW, I think you'll get better sensitivity on the dimmer with a 20k resistor.
Enjoying "Radioactive" from Amazon, by the way - Thanks!
William

User avatar
brand
 
Posts: 12
Joined: Sat Dec 31, 2011 4:11 am

Re: Ice Tube firmware - Drift correction with Auto DST, Dimmer

Post by brand »

Well, I've got it losing about 10 seconds a day now, so that's progress... of a sort. I just reset the drift correction in the other direction, so we'll see what it looks like tomorrow.

The dimmer is working fine for me. You're right about a broader range with a higher value resistor, but 1K ohm and 900 to 1000 for the range is fine for my purposes.

Fingers crossed.

Take care,
brad

User avatar
wbp
 
Posts: 260
Joined: Mon Mar 07, 2011 1:18 pm

Re: Ice Tube firmware - Drift correction with Auto DST, Dimmer

Post by wbp »

Brad,
if even with drift correction you are still seeing 10 seconds a day I think you probably have a bad crystal. I am going to turn my GPS off and run without it for a few days so I can see what kind of drift I get, but it really sounds like what you have is excessive.
William

User avatar
brand
 
Posts: 12
Joined: Sat Dec 31, 2011 4:11 am

Re: Ice Tube firmware - Drift correction with Auto DST, Dimmer

Post by brand »

Okay, I have it running fast for the first time. You've cracked it.

Now to calibrate.

Let me now how you like "Radioactive." I'm always looking for good adds to my reading list.

Take care,
brad

User avatar
wbp
 
Posts: 260
Joined: Mon Mar 07, 2011 1:18 pm

Re: Ice Tube firmware - Drift correction with Auto DST, Dimmer

Post by wbp »

Great news - glad to hear it's actually working. Does the sign of the correction factor make sense? I guess one could argue it either way. If the correction is positive, should that speed up or slow down the clock?

William

Locked
Please be positive and constructive with your questions and comments.

Return to “Clock Kits (discontinued)”