Jump to content
Sign in to follow this  
Sparky1039

Dual Clock Frequencies?

Recommended Posts

Question, does Boost C support dual clock frequencies for the final compiled code? I looked in the help file and found nothing, but wanted to ask here.

My project uses two primary clock frequencies, one at 2 MHZ (INTOSC), and another at 12MHZ (external crystal). It's a ultra low power sensing device that uses the 2MHz clock for data gathering from sensors (SPI, internal ADC ect...) and periodically will switch over to 12MHz for high speed serial coms (230.4Kbaud). I use a bunch of delays in code for both clock frequencies and I wish to preserve the values as they are. But obviously if I compile for 12MHz delays the 2MHz delays will be fast and vice versa. So the question is can I compile using PP directives for both clock frequencies and keep the delays correct?

Of course I can always scale the requisite delays to match the fast/slow clock frequency but this seems messy and prone to mistakes because each are hand tweaked and I could easily forget one.

Any suggestions?

 

thx

Share this post


Link to post
Share on other sites

Hi

 

AFAIK there is no such feature on BoostC, neither in other compilers.

 

The built in "delay" tools are ususally macros that produce loops timed by the number of machine cycles in them, hence the "#pragma CLOCK_FREQ" that defines the value from where the number of instructions in the loop is computed.

Given so, after compilation the delays will be hardcoded in something like a "decfsz" loop and a handfull of "nop"

 

I would suggest you set up a interrupt based timing system.

With interrupt driven delays, the counters, timers and pre/postscallers can easiiy be loaded with values adjusted for the clock frequency in use.

The interrupt driven timing system has an extra advantage of releasing the mcu time for some usefull work instead of beeing locked in an active delay.

 

BTW.

Maybe you should consider using the NOVO RTOS has the base for your project.

All the tools for interrupt driven timing and freeing the mcu to take care of some other jobs while one task is on delay are tehre and easy to use.

 

 

Best regards

Jorge

Edited by JorgeF

Share this post


Link to post
Share on other sites

I suspected as much. I just want to be sure I wasn't missing a PP directive somewhere that allowed for this capability. In my case it's not a big deal to scale the delays appropriately (in this case 6x).

With most PIC's hosting multiple clock sources nowadays I thought maybe industry compiler developers would have offered this kind of flexibility to support them.

Thanks

Share this post


Link to post
Share on other sites

Hi

 

I wouldn't expect that.

Adjusting to changes in clock frequency means changing things at runtime and knowing when to change them.

I don't imagine a compiler doing such thing. Compilers don't write program neither create algorithms, they simply translate high level languages to machine code. ;)

 

Some things simply can be done without the super-computer we ususally call "brains". :D :D

 

 

 

Best regards

Jorge

Share this post


Link to post
Share on other sites
I use a bunch of delays in code for both clock frequencies and I wish to preserve the values as they are. But obviously if I compile for 12MHz delays the 2MHz delays will be fast and vice versa. So the question is can I compile using PP directives for both clock frequencies and keep the delays correct?

Of course I can always scale the requisite delays to match the fast/slow clock frequency but this seems messy and prone to mistakes because each are hand tweaked and I could easily forget one.

Any suggestions?

 

There are various ways to solve this, depending on how much extra code you want to write.

 

One way to do it using the built-in delay function would be to compile 2 copies of the code (one at each clock speed) into separate obj files (with different names), and then link both of them in using the linker.

 

Another way would be to write your own delay function or macro that uses a parameter to select the clock speed. This isn't too hard; I've done it a few times.

 

Yet another way would be to use a Timer. If you have a spare one, this technique is actually the most flexible because you can use the same delay loop code, but just preset the Timer to a different value depending on the clock speed you want.

 

don

Share this post


Link to post
Share on other sites

One way to do it using the built-in delay function would be to compile 2 copies of the code (one at each clock speed) into separate obj files (with different names), and then link both of them in using the linker.

 

This particular approach won't work. The delay functions are so special that it's the linker, not compiler who generates code for them. Things are done this way to make sure that bank and code page switches (the compiler doesn't know anything about) won't affect the timing of these functions.

 

Regards,

Pavel

Share this post


Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...