|
|
View previous topic :: View next topic |
Author |
Message |
global
Joined: 01 Feb 2005 Posts: 21 Location: Paris
|
Count 5 minutes if nobody logged in ... |
Posted: Tue May 10, 2005 4:36 am |
|
|
Hello,
I use a PIC 18F458 with PCW Compiler 3.15.
I wondered how i could do if i wanted to wait for 5 minutes and then switch off a button.
If during these 5 minutes someone log in ( we'll say, there is a variable that indicates if it is true or not) just don't execute the switch off.
I have a RTC which has an alarm that i can configurate when i take time and then add 5 minutes to do that, but i wondered if i had better to count with a timer ...
Could the group tell me whats better or not or sould I do it in a different angle or approach it a different way.
My clock = 4000000
If you have examples as well, they are welcome.
Many thanks. |
|
|
valemike Guest
|
|
Posted: Tue May 10, 2005 6:49 am |
|
|
you can do it with the PIC's internal timers, e.g. TIMER1
Code: |
#INT_TIMER1 // This function is called every time
void clock_isr() // timer 1 overflows (65535->0), which is
{ // approximately 19 times per second for
if(--int_count==0) // this program.
{
++seconds;
int_count = INTS_PER_SECOND;
}
}
void reset_one_second_counter(void)
{
int_count = INTS_PER_SECOND;
seconds = 0;
}
|
i based the above snippets on example ex_stwt1.c
When you want to start the timer, call the reset_one_second_counter() function.
You will want to poll when 5 minutes has elapsed. In this case, 5 minutes = 5 x 60 seconds = 300 seconds.
In your main loop, you want to check the following:
if (seconds >= 300) // make sure that seconds is an unsigned long!
{
// your timer has exceeded 5 minutes. Do what you want to do now.
}
The code is missing other things, such as initializing timer1, and enabling the interrupt for it. Look at ex_stwt1.c |
|
|
global
Joined: 01 Feb 2005 Posts: 21 Location: Paris
|
|
Posted: Tue May 10, 2005 7:07 am |
|
|
Ok thank you very much,
i found ex_stwt.c, and i wondered what was really important to make sure that initialization was correct ?
Is that important if i count for 5 min or 15 minutes ?
Because the programm may be "stopped" because of counting isn't it ?
Iy should be a problem ...
How can i calculate INTS_PER_SECOND ?
Code: | #include <18F458.h>
#fuses HS,NOPROTECT
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7) // Jumpers: 8 to 11, 7 to 12
#endif
#define INTS_PER_SECOND 76 // (20000000/(4*256*256))
byte seconds; // A running seconds counter
byte int_count; // Number of interrupts left before a second has elapsed
#int_rtcc // This function is called every time
clock_isr() { // the RTCC (timer0) overflows (255->0).
// For this program this is apx 76 times
if(--int_count==0) { // per second.
++seconds;
int_count=INTS_PER_SECOND;
}
}
main() {
int_count=INTS_PER_SECOND;
set_timer0(0);
setup_counters( RTCC_INTERNAL, RTCC_DIV_256);
enable_interrupts(INT_RTCC);
enable_interrupts(GLOBAL);
if (seconds >= 300) // make sure that seconds is an unsigned long!
{
// your timer has exceeded 5 minutes. Do what you want to do now.
}
} |
Many thanks |
|
|
valemike Guest
|
|
Posted: Tue May 10, 2005 9:22 am |
|
|
The example i went from is ex_stwt1.c. Your example that you pulled up uses timer0. All depends on which timers you have available.
The calculation is in the comments in the example .c files. For example, 20000000 is the crystal's clock, in this case 20MHz. One of the /4 terms in the dividend is because the raw crystal speed is divided by 4. The other /4 i think is because they choose a divide/4, and the 256 is the number of ticks in the timer. (256 for an 8-bit timer rtcc; or 16656 for a 16-bit timer).
You can count as many seconds as your unsigned long will hold, which is 65535.
What do you mean by the program "getting stopped" by counting? Yes it interrupts when the timer overflows, but the program continues execution after the isr completes. |
|
|
global
Joined: 01 Feb 2005 Posts: 21 Location: Paris
|
|
Posted: Tue May 10, 2005 9:27 am |
|
|
Where could if find ex_stwt1.c ?
It is just taht i have to change TIMER0 by TIMER1 ?
Yes : Quote: | What do you mean by the program "getting stopped" by counting? Yes it interrupts when the timer overflows, but the program continues execution after the isr completes. |
Does it mean that the program execution is waiting for the count to be done so the isr to be completed for continuing ?
It waits for the isr to be completed so it can't do something else ?
That was my question... |
|
|
valemike Guest
|
|
Posted: Tue May 10, 2005 11:04 am |
|
|
It's in the /PICC/examples/ directory, same as ex_stwt.c
You do have the up-to-date version of the compiler, right? The example can be found there.
To answer your other question...
The program doesn't "wait". What happens is that the timer overflows, setting an interrupt flag bit, then the isr gets executed; after that's done, the program continues. |
|
|
|
|
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
|