![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
timer interrupt issues PCM - but really all pics |
Posted: Sat Jun 06, 2009 8:10 am |
|
|
Is there a way to RELIABLY manage MORE than an int8 time keeping variable - with an interrupt based timer?
I have happy been using a timer0 -interrupt based timer that updates a SINGLE byte quantity at a 10 mSec rate - the high level 'user' functions must poll often enough to know how many "ticks' have elapsed.
BUT
I can't think of a way to SAFELY use an int32 counter for the same purpose ,
due to the certainty that timer0 interrupts will occur DURING the high level function calls, trying to READ the multi byte format -and change the bytes of the long integer - as they are being read .
This sort of thing was EZ to solve in the 808x environment I was used to in a former life - is the same sort of code needed here ?
ie disabling the timer0 int - copying my timer int32 counter to a temp var - and then re enabling the timer ints ( what I did in PC assembler)
Is there an elegant - simple solution I should know about ? |
|
![](templates/subSilver/images/spacer.gif) |
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Sat Jun 06, 2009 9:07 am |
|
|
There is a pretty easy means, that is generally used in all similar cases, where consistency of data structures across interrupt calls is an issue: Disabling interrupts temporarily, either globally or the respective conflicting one. |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah Guest
|
|
Posted: Sat Jun 06, 2009 10:10 am |
|
|
Yes. the 'normal' method, is to have a variable used only in the interrupt handler, and a 'copy', used by the external routines.
Code: |
disable_interrupts(global);
local_tick=timer_tick;
enable_interrupts(global);
//work using 'local_tick' here.
|
I also, if using a 'fast' tick, will still only update an 8bit value in the interrupt, then instead of the above code, use:
Code: |
disable_interrupts(global);
if (local_tick > timer_tick)
++local_msb;
local_tick=timer_tick;
enable_interrupts(global);
|
This way there is still only an 8bit counter in the timer, while the main code has a 'tick', and an extended counter. Often I'll create a union, so that I have (say), an 8bit 'tick', and a 16bit extended counter, stored in a 32bit value, to give 24bits useable, without using 32bit arithmetic.
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
|
|
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
|
Powered by phpBB © 2001, 2005 phpBB Group
|