View previous topic :: View next topic |
Author |
Message |
KenHorse
Joined: 16 Jul 2004 Posts: 523
|
Posted: Wed Feb 19, 2020 1:52 am Post subject: Using a Timer for Soft clock instead of 32768 kHz xtal |
|
|
So I've found several examples using SEARCH but Bascom is throwing a Label Not Found (Error : 61 Line : 23 Label not found [_SOFT_CLOCK] , in File
Code: |
$regfile = "m2561def.dat"
$crystal = 16000000
$hwstack = 600
$swstack = 450
$framesize = 245
$Baud = 57600
Dim Tick As Byte
Config Clock = User
Config Date = MDY , Separator = .
Date$ = "02.03.20"
Time$ = "23:59:45"
Config Timer3 = Timer , Prescale = 256 , Clear Timer = 1
Compare3a = 6250
Timer3 = 0
On OC3A SECTICTENTHS
Enable OC3A
Start Timer3
Enable interrupts
SECTICTENTHS:
Incr Tick
If Tick >= 10 Then
Tick = 0
!JMP _SOFT_CLOCK
End If
Return
Settime:
Return
Getdatetime:
Return
Setdate:
Return |
I've even tried "!CALL _SOFT_CLOCK" with the same error
What am I missing?
(BASCOM-AVR version : 2.0.8.2 ) |
|
Back to top |
|
|
MWS
Joined: 22 Aug 2009 Posts: 2262
|
Posted: Wed Feb 19, 2020 7:36 am Post subject: |
|
|
As clock is configured for 'user' the softclock time keeping routines are not required, thus not compiled. |
|
Back to top |
|
|
KenHorse
Joined: 16 Jul 2004 Posts: 523
|
|
Back to top |
|
|
EDC
Joined: 26 Mar 2014 Posts: 971
|
|
Back to top |
|
|
MWS
Joined: 22 Aug 2009 Posts: 2262
|
Posted: Wed Feb 19, 2020 9:33 am Post subject: |
|
|
If you found a sample, what is the reason you don't follow it?
My sentence answered your question, looks you did not understand what it said, here's the long version:
I know how to to it, thus I answered to your question why you can not call _SOFT_CLOCK with: MWS wrote: | clock is configured for 'user' |
In your code clock is not configured for 'soft' as in the sample, it is configured for 'user'.
With 'user' the _SOFT_CLOCK-routines are not required, thus not compiled in, as time-keeping is done by the routines
Code: | Settime:
Return
Getdatetime:
Return
Setdate:
Return |
which are preferably used for external RTC purposes.
In your linked sample you will notice:
Code: | Config Clock = Soft |
and also reconfiguring the timer to disconnect from external 32kHz xtal.
Does this give you some insight? |
|
Back to top |
|
|
KenHorse
Joined: 16 Jul 2004 Posts: 523
|
Posted: Wed Feb 19, 2020 9:49 pm Post subject: |
|
|
Indeed, I did miss that he was defining it as Soft and not User
My bad (and it makes perfect sense)
Last question.
Is it possible to change the value of Compare3a (currently set to 6250) "on the fly" to compensate for a crystal that isn't exactly on frequency? Perhaps by stopping the timer, setting the new value and restarting it? |
|
Back to top |
|
|
EDC
Joined: 26 Mar 2014 Posts: 971
|
Posted: Wed Feb 19, 2020 9:57 pm Post subject: |
|
|
Yes. You can change COMPARE3A anytime. One thing can happen if Timer will be, for example at 6400, and you set COMPARE3A to 6000 then compare will not fire once and timer will simply overflow at 65535 but this is not big deal in this case. Stopping and Restarting the Timer is not needed.
Some Timers modes have dual buffering and value is updated when Timer reach TOP or BOTTOM but this is not valid for Clear_timer I think.
You can always update OCR3A in the interrupt routine but this will increase you interrupt code. |
|
Back to top |
|
|
KenHorse
Joined: 16 Jul 2004 Posts: 523
|
Posted: Wed Feb 19, 2020 10:32 pm Post subject: |
|
|
EDC wrote: | Yes. You can change COMPARE3A anytime. One thing can happen if Timer will be, for example at 6400, and you set COMPARE3A to 6000 then compare will not fire once and timer will simply overflow at 65535 but this is not big deal in this case. Stopping and Restarting the Timer is not needed.
Some Timers modes have dual buffering and value is updated when Timer reach TOP or BOTTOM but this is not valid for Clear_timer I think.
You can always update OCR3A in the interrupt routine but this will increase you interrupt code. |
I was thinking about that as any change would really be part of a calibration procedure and afterwards be rarely used. But having the possibility to make that change may be invaluable
Thanks |
|
Back to top |
|
|
AdrianJ
Joined: 16 Jan 2006 Posts: 2483 Location: Queensland
|
Posted: Thu Feb 20, 2020 12:06 am Post subject: |
|
|
The value of Compare3 can only be set in integer amounts. If its around 6000, then the smallest increment is about 1:6000, thats about 14 secs per day. So its not a very fine adjustment. Thats huge in terms of 'normal' crystal accuracy. _________________ Adrian Jansen
Computer language is a framework for creativity |
|
Back to top |
|
|
KenHorse
Joined: 16 Jul 2004 Posts: 523
|
Posted: Thu Feb 20, 2020 12:23 am Post subject: |
|
|
AdrianJ wrote: | The value of Compare3 can only be set in integer amounts. If its around 6000, then the smallest increment is about 1:6000, thats about 14 secs per day. So its not a very fine adjustment. Thats huge in terms of 'normal' crystal accuracy. |
Current value is 6250, which gives a (nominal) tic of .1 second.
Bascom doesn't seem to object of any value (for example, 6251 or 6249). I'm testing the difference that makes but you're saying it will make a bigger one than I'm looking for. I'll let you know in 12 hours whether it makes a significant difference! |
|
Back to top |
|
|
AdrianJ
Joined: 16 Jan 2006 Posts: 2483 Location: Queensland
|
Posted: Thu Feb 20, 2020 3:40 am Post subject: |
|
|
A few other thoughts, reaching back into ancient history:
You might be able to use a lower base frequancy, like near 1Hz, rather than 0.1Hz, by setting the timer3 compare up near 62000 ( dont go over 65535 or you will overflow ! ). That will get you 1 digit better resolution, so smaller adjustment steps, on the compare setting.
Typical computer clock crystals up in the MHz range have significant temp coeffs. So you might find that the temp drifts will upset the accuracy. Also the drive power can affect the frequency. There are usually two settings in the fuses to select which is better for the application. In general lower drive power is better for stability, if it can be used.
The 32KHz crystals used for eg watches are a different cut, with about as low a TC as possible. Thats why they are used for timekeeping, and Atmel offer a second crystal connection to be able to use them on many chips. _________________ Adrian Jansen
Computer language is a framework for creativity |
|
Back to top |
|
|
EDC
Joined: 26 Mar 2014 Posts: 971
|
Posted: Thu Feb 20, 2020 12:07 pm Post subject: |
|
|
The formula for OCx is (F_CPU/PRESCALER/FREQ) - 1 because uC will execute interrupt minimum after one tick.
Till now I dont see typo in my example. COMPARE3A should have value 62499 not 6249. Because it is not my first time typo I always describe in the comment what I want to achieve (1s @16MHz/256)
|
|
Back to top |
|
|
KenHorse
Joined: 16 Jul 2004 Posts: 523
|
Posted: Thu Feb 20, 2020 4:39 pm Post subject: |
|
|
EDC wrote: | The formula for OCx is (F_CPU/PRESCALER/FREQ) - 1 because uC will execute interrupt minimum after one tick.
Till now I dont see typo in my example. COMPARE3A should have value 62499 not 6249. Because it is not my first time typo I always describe in the comment what I want to achieve (1s @16MHz/256)
|
As my target period is .1 sec and not 1 sec, 6250 is correct I believe |
|
Back to top |
|
|
EDC
Joined: 26 Mar 2014 Posts: 971
|
Posted: Thu Feb 20, 2020 4:45 pm Post subject: |
|
|
You forget to substract one Even Windows Calculator can do that math
Value 24999 with Prescaler 64 should work too
|
|
Back to top |
|
|
KenHorse
Joined: 16 Jul 2004 Posts: 523
|
|
Back to top |
|
|
|