CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Wake from sleep every x min proper method

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
jojos



Joined: 30 Apr 2007
Posts: 64

View user's profile Send private message

Wake from sleep every x min proper method
PostPosted: Wed Feb 04, 2009 2:37 am     Reply with quote

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
Ttelmah
Guest







PostPosted: Wed Feb 04, 2009 5:10 am     Reply with quote

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
jojos



Joined: 30 Apr 2007
Posts: 64

View user's profile Send private message

PostPosted: Wed Feb 04, 2009 5:18 am     Reply with quote

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
Ttelmah
Guest







PostPosted: Wed Feb 04, 2009 6:08 am     Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
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


Powered by phpBB © 2001, 2005 phpBB Group