View previous topic :: View next topic |
Author |
Message |
qaxobi27b
Joined: 30 Apr 2011 Posts: 2
|
time trigger |
Posted: Sat Apr 30, 2011 4:15 am |
|
|
I would like to make a time delayed trigger..
something like a delay but the problem with the delay is that it stops all the program for the specified amount of time, i just want a to count during the program is running and after like 3 seconds i do that function. is there a way to do this?
note: I tried the get_timer() command but it counts up to 64K and resets more than once in a second. |
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Sat Apr 30, 2011 4:33 am |
|
|
This is usually done with timer interrupts. 3 seconds is a long time, so you will have to set a timer to interrupt say every 30ms and count 100 interrupts. _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
|
qaxobi27b
Joined: 30 Apr 2011 Posts: 2
|
|
Posted: Sat Apr 30, 2011 4:45 am |
|
|
SherpaDoug wrote: | This is usually done with timer interrupts. 3 seconds is a long time, so you will have to set a timer to interrupt say every 30ms and count 100 interrupts. |
Thanks for the quick reply, but can you tell me a bit more on how to set the timer interrupts to every 30ms. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9221 Location: Greensville,Ontario
|
|
Posted: Sat Apr 30, 2011 5:30 am |
|
|
In the Code Library there's a Subject '...software based Real Time Clock..'(sorry , haven't figured how to paste the link...)
Works like a charm,currently I have it setup to trigger the Dallas DS18b20P temp sensors which need 750ms to 'do their thing' in the 'background'.
Having the ISR do the 'dirty work' allows 'Main' to do a LOT more than just sit in a 3/4 second delay loop,twiddling bits ! |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun May 01, 2011 4:09 pm |
|
|
Here's an example of how to make a timer interrupt every 10 ms.
Code: |
#include <16F877.H>
#fuses HS, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock = 20000000)
// This gives an RTCC interrupt rate of approx. 100 Hz
// (every 10 ms) with a 20 MHz crystal.
#define RTCC_PRELOAD (256 - 195)
// #define RTCC_PRELOAD (256 - 39) // For 4 MHz crystal
#int_rtcc
void rtcc_isr(void)
{
set_rtcc(RTCC_PRELOAD); // Reload the Timer.
output_toggle(PIN_B0); // Toggle a pin
}
//================================
void main()
{
setup_counters(RTCC_INTERNAL, RTCC_DIV_256);
set_rtcc(RTCC_PRELOAD);
clear_interrupt(INT_RTCC);
enable_interrupts(INT_RTCC);
enable_interrupts(GLOBAL);
while(1);
} |
|
|
|
pic_micro
Joined: 07 Feb 2011 Posts: 26
|
timer0 and timer1 formula |
Posted: Mon May 02, 2011 8:02 am |
|
|
Can you explain how the 256-195 relate to 10ms?
Can you show the formulas |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon May 02, 2011 1:32 pm |
|
|
The Timer is clocked at the instruction cycle rate, which is 1/4 of the PIC
oscillator frequency. So with a 20 MHz oscillator, the timer is clocked at 5 MHz.
We're using a pre-scaler of 256. So the instruction clock is divided by
256 before it gets to the Timer. The actual Timer clock is 5 MHz / 256 =
19531 Hz.
If we want the Timer to run at approximately 100 Hz, then we can see by
inspection that we need to divide 19531 by 195, which gives approximately
100 Hz. |
|
|
5440
Joined: 08 Feb 2009 Posts: 11
|
|
Posted: Sun May 08, 2011 11:27 am |
|
|
I recently made a similar system where I used a 1ms interupt, where it just increments a 16 bit counter. I wrote my own ISR to make it tight, took some time but got it done. I had an issue with the fast stack that threw me for a loop as I am using priority ints and had the retfie 1 and retfie 0 in wrong spots.
I then made two functions, one that is called when a new time value is required or must be called again say in blinking a LED. It looks at the current counter value (buffered) then adds the appropriate time while checking for counter overflow. The other function is called every scan which checks all the timers (configured as a timer array of type struct). I'm using about 10 of these timers for any time up to about 65 sec on an PIC18F @ 20MHZ. I use them to blink LEDs, debounce switches etc. The timer struct has control bits configured much like in an AB PLC 5 that are checked elsewhere to see if timer is done, still timing etc.
Working fine so far, just have to watch using it for very short time values less around the 1-5ms ranges as my current scan time is in the .8ms range. |
|
|
|