Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

Using a Timer for Soft clock instead of 32768 kHz xtal
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
KenHorse

Bascom Member



Joined: 16 Jul 2004
Posts: 526

blank.gif
PostPosted: Wed Feb 19, 2020 1:52 am    Post subject: Using a Timer for Soft clock instead of 32768 kHz xtal Reply with quote

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

Bascom Member



Joined: 22 Aug 2009
Posts: 2335

blank.gif
PostPosted: Wed Feb 19, 2020 7:36 am    Post subject: Reply with quote

As clock is configured for 'user' the softclock time keeping routines are not required, thus not compiled.
Back to top
View user's profile
KenHorse

Bascom Member



Joined: 16 Jul 2004
Posts: 526

blank.gif
PostPosted: Wed Feb 19, 2020 7:56 am    Post subject: Reply with quote

MWS wrote:
As clock is configured for 'user' the softclock time keeping routines are not required, thus not compiled.


4th post down

https://www.mcselec.com/index2.php?option=com_forum&Itemid=59&page=viewtopic&t=6127&highlight=softclock
Back to top
View user's profile
EDC

Bascom Expert



Joined: 26 Mar 2014
Posts: 1133

poland.gif
PostPosted: Wed Feb 19, 2020 9:25 am    Post subject: Reply with quote

@KenHorse Read carefully - in that code you have "Config clock = Soft" where in your code it is "User"

There is another solution for clock. Look. It works even in the simulator.


Code:
$regfile = "m2561def.dat"
$crystal = 16000000
$hwstack = 600
$swstack = 450
$framesize = 245
$Baud = 57600
$sim                                  'remark this line for real device, this is for simulator

Dim Tick As Long , Prev_tick As Long
Dim Strtime As String * 8
Dim Strdate As String * 8

Config Clock = User
Config Date = Mdy , Separator = Dot

Strtime = "23:59:45"
Strdate = "02.03.20"

Tick = Syssec(strtime , Strdate)

Config Timer3 = Timer , Prescale = 256 , Clear_timer = 1
Compare3a = 62499                                            '1s @16MHz/256 [EDITED]6249->62499

On Oc3a Sectic
Enable OC3A

Enable Interrupts


Do
 If Prev_tick <> Tick Then
  Prev_tick = Tick

  Strtime = Time(tick)
  Strdate = Date(tick)
  Print Strdate ; "   " ; Strtime
 End If
Loop

Sectic:
 Incr Tick
Return

Settime:
Return

Getdatetime:
Return

Setdate:
Return


Last edited by EDC on Thu Feb 20, 2020 12:09 pm; edited 1 time in total
Back to top
View user's profile Visit poster's website
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 2335

blank.gif
PostPosted: Wed Feb 19, 2020 9:33 am    Post subject: Reply with quote

KenHorse wrote:
4th post down

https://www.mcselec.com/index2.php?option=com_forum&Itemid=59&page=viewtopic&t=6127&highlight=softclock


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

Bascom Member



Joined: 16 Jul 2004
Posts: 526

blank.gif
PostPosted: Wed Feb 19, 2020 9:49 pm    Post subject: Reply with quote

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

Bascom Expert



Joined: 26 Mar 2014
Posts: 1133

poland.gif
PostPosted: Wed Feb 19, 2020 9:57 pm    Post subject: Reply with quote

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

Bascom Member



Joined: 16 Jul 2004
Posts: 526

blank.gif
PostPosted: Wed Feb 19, 2020 10:32 pm    Post subject: Reply with quote

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

Bascom Expert



Joined: 16 Jan 2006
Posts: 2483
Location: Queensland

australia.gif
PostPosted: Thu Feb 20, 2020 12:06 am    Post subject: Reply with quote

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

Bascom Member



Joined: 16 Jul 2004
Posts: 526

blank.gif
PostPosted: Thu Feb 20, 2020 12:23 am    Post subject: Reply with quote

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! Very Happy
Back to top
View user's profile
AdrianJ

Bascom Expert



Joined: 16 Jan 2006
Posts: 2483
Location: Queensland

australia.gif
PostPosted: Thu Feb 20, 2020 3:40 am    Post subject: Reply with quote

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

Bascom Expert



Joined: 26 Mar 2014
Posts: 1133

poland.gif
PostPosted: Thu Feb 20, 2020 12:07 pm    Post subject: Reply with quote

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

Bascom Member



Joined: 16 Jul 2004
Posts: 526

blank.gif
PostPosted: Thu Feb 20, 2020 4:39 pm    Post subject: Reply with quote

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

Bascom Expert



Joined: 26 Mar 2014
Posts: 1133

poland.gif
PostPosted: Thu Feb 20, 2020 4:45 pm    Post subject: Reply with quote

You forget to substract one Wink Even Windows Calculator can do that math Very Happy
Value 24999 with Prescaler 64 should work too Wink
Back to top
View user's profile Visit poster's website
KenHorse

Bascom Member



Joined: 16 Jul 2004
Posts: 526

blank.gif
PostPosted: Thu Feb 20, 2020 4:58 pm    Post subject: Reply with quote

https://eleccelerator.com/avr-timer-calculator/
Back to top
View user's profile
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