![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
jojos
Joined: 30 Apr 2007 Posts: 64
|
Wake from sleep every x min proper method |
Posted: Wed Feb 04, 2009 2:37 am |
|
|
Hello .I use a Pic 18F2520 chip and compiler version PCH 4.073
I want to make a calculation every 1 or x minutes and then go again to sleep.As a wake factor i am using Timer 1 oscillator .Because of the external crystal 32.768 Khz in pins T1OSO and T1OSI the maximum time between interrupts of timer 1 is 16 sec.So which is the proper way of make my calculation lets say every 1 min and then go to sleep?A first thought is to wake from Timer1's interrupt then increase a counter and when this counter reaches the point that i will have 1 minute time i will make my calculation.Is this a good solution or there will be errors?Thank you in advance |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah Guest
|
|
Posted: Wed Feb 04, 2009 5:10 am |
|
|
First, you are using a compiler, let it do it's job....
setup_timer1(T1_EXTERNAL|T1_CLK_OUT|T1_DIV_BY_2);
Now, this says 'external input', 'generate output to drive a crystal', /2 prescaler (which is what your current pattern selects).
With these settings, the timer will 'tick' every 1/16384th second. 61uSec. So, 50mSec, will need 819 counts. So you would need to preset to a count of 64717.
Again let the compiler do it's work:
set_timer1(64717);
There is a problem for your code, in that the timer by default wakes up in 16bit read/write mode. As such, your writes, won't give the behaviour you expect. However your value appears wrong anyway.
Timer1, is only a 16bit timer. It doesn't have an '8bit' counting mode. Obviously though, if you preload the timer with 65280, it'll have just 256 counts to the next reset. If you want an 8bit timer, just use timer0, which is switchable to 8bit operation. This would save the need for 16bit reads/writes when preloading the registers etc..
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
jojos
Joined: 30 Apr 2007 Posts: 64
|
|
Posted: Wed Feb 04, 2009 5:18 am |
|
|
Thank you Ttelmah for your response.I edited my message cause i found that the problem was i used wrong time calculator to setup my timers.So this problem is solved .If you can help me with my new issue i would be grateful |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah Guest
|
|
Posted: Wed Feb 04, 2009 6:08 am |
|
|
Yes, the counter is perfectly fair/normal.
I'd probably suggest 'KISS', and use a time that is a nice subdivision of the time you want. Remember you don't actually have to call an interrupt handler. So, something like:
Code: |
int8 ctr;
setup_timer1(T1_EXTERNAL|T1_CLK_OUT|T1_DIV_BY_2);
set_timer1(0);
disable_interrupts(GLOBAL);
for (ctr=0;ctr<15;ctr++) {
clear_interrupts(INT_TIMER1);
sleep();
delay_cycles(1); //This instruction is prefetched on sleep - ensure NOP
}
|
Ths will wake for just a half dozen machine cycles every four seconds, giving a total of 60 second, and drawing peanuts. :-)
Because the interrupts are disabled, no handler is called, and the chip just wakes and loops.
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
|