View previous topic :: View next topic |
Author |
Message |
bhyatyab
Joined: 26 Jan 2005 Posts: 13
|
Timers |
Posted: Mon Mar 07, 2005 1:32 am |
|
|
Hi
I need some help using timers. Say I want some code to run after 10 minutes - will this work?
int secs;
int mins;
int hrs;
#int_RTCC
RTCC_isr() {
set_timer1(0*8000);
}
#int_TIMER1
TIMER1_isr() {
set_timer1(0*8000);
secs=secs+1;
if (secs==60){mins=mins+1;secs=0;}
if (mins==60){hrs=hrs+1;mins=0;}
if (hrs==24){hrs=0;}
}
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_psp(PSP_DISABLED);
setup_spi(FALSE);
setup_wdt(WDT_OFF);
setup_timer_0(RTCC_INTERNAL);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
enable_interrupts(INT_EXT);
enable_interrupts(INT_EXT1);
enable_interrupts(INT_EXT2);
enable_interrupts(GLOBAL); |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Mon Mar 07, 2005 3:21 am |
|
|
First a remark: When posting code, please use the Code button, this will ensure your identation is preserved and makes the code easier to read.
Second remark: When posting code on this forum, please post a COMPLETE working example and tell us which processor you are using, sometimes it is also helpfull to post your compiler version.
What clock frequency are you running?
Your example has several problems:
1) You have disabled timer1, so the timer1_interrupt will never fire.
2) '0*8000' equals 0, what do you want to achieve here?
3) Using the timer0 interrupt to change the value of timer1?????????? Don't do this.
4) You haven't adjusted the timer rates for your clock frequency, this requires setting the pre- and/or postscaler division ratios. Read the documentation on the setup_timer_x functions.
Basically I advise you to study the examples you will find when searching this forum with the keywords 'real time clock' or 'rtc'. |
|
|
bhyatyab
Joined: 26 Jan 2005 Posts: 13
|
Timers |
Posted: Mon Mar 07, 2005 3:40 am |
|
|
Hi
Sorry, Im using a 18F452. I want to implement a timer - something must happen after 24hrs - can you guide me.
Thanks |
|
|
Ttelmah Guest
|
|
Posted: Mon Mar 07, 2005 5:10 am |
|
|
And you still have not told us the clock rate?...
Best Wishes |
|
|
bhyatyab
Joined: 26 Jan 2005 Posts: 13
|
Timers |
Posted: Mon Mar 07, 2005 5:34 am |
|
|
Clock rate is 20Mhz |
|
|
Ttelmah Guest
|
|
Posted: Mon Mar 07, 2005 5:57 am |
|
|
Code: |
#include <18F452.h>
#device adc=8
#use delay(clock=20000000)
#fuses HS,PUT,BROWNOUT,NOWDT,WDT1
int16 tick;
int16 seconds;
#define TICKS_PER_SECOND (1000)
//With a 20MHz clock/16, with the timer counting 0...249, and interrupting on
//every tenth reset, this gives:
//((20000000/16)/250)/10 times per second = 1000
#define TIME_I_WANT_TO_WAIT (600)
//For ten minutes
#int_timer2
void tick_handler(void) {
if(tick) {
tick--;
}
else {
tick=(TICKS_PER_SECOND-1)
if (seconds) --seconds;
}
}
void main() {
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_psp(PSP_DISABLED);
setup_spi(FALSE);
setup_wdt(WDT_OFF);
setup_timer_0(RTCC_INTERNAL);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DIV_BY_16,249,10);
setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
while(1) {
seconds=TIME_I_WANT_TO_WAIT;
while (seconds) ;
//You get here when 'seconds' reaches zero
}
}
|
Shows one approach |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Mon Mar 07, 2005 6:20 am |
|
|
In main() the code for enabling the timer2 interrupts is missing: Code: | enable_interrupts(INT_TIMER2);
enable_interrupts(GLOBAL); |
And one small suggestion, change Code: | setup_timer_0(RTCC_INTERNAL); | to Code: | setup_timer_0(RTCC_OFF); |
|
|
|
Ttelmah Guest
|
|
Posted: Mon Mar 07, 2005 8:53 am |
|
|
I agree.
I used the 'wizard' to generate the framework, and hadn't bothered to check. :-)
Best Wishes |
|
|
|