|
|
View previous topic :: View next topic |
Author |
Message |
wmeade
Joined: 08 Sep 2003 Posts: 16
|
How do I detect when external interrupts stop. |
Posted: Tue Jan 13, 2004 11:59 am |
|
|
The code below is working code. I need to tell when the external interrupts have stopped so I can display 0.00 FPM. I think that some sort of a timmer would do the trick but everything I have tried has failed. It seems that if the external pulse inputs stop suddinly that all the variables retain there values and the output stays the same after the pulses stop. Any suggestions would be greatly appreciated.
Code: |
#include <16f873.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP,PUT
#use delay(clock=20000000)
#use rs232(baud=19200, xmit=PIN_C6, rcv=PIN_C7)
byte i,j,k,flag,state,bcd_in,no_targets,roll_dia;
int16 counter,pulse_ms,counter2;
int16 ptics[5];
float current_speed,pi_value,roll_circ,fpm,pps;
short First_Edge = FALSE; // first edge flag
// ********** INTERRUPT ROUTINES *********************************************************************
#INT_EXT
void edge_detect_isr(void)
{
if(First_Edge) // Falling edge
{
ext_int_edge(H_to_L);
SET_TIMER2(0);
Counter = 0;
First_Edge = FALSE;
}
else // Rising edge
{
First_Edge = TRUE;
ext_int_edge(H_to_L);
pulse_ms = counter;
//next 5 lines average the pulses 5 times
ptics[j++] = pulse_ms; // store new value
if ( j==5 ) j = 0; // reset
current_speed = 0; // init for average value calculation
for ( k=0; k<5; ) current_speed += ptics[k++]; // sum the 5 values
current_speed /= 5.0; // devide by 5 for pulse average
}
}
#INT_TIMER2
void timer2_isr(void)
{
counter++; // increment variable
counter2++; // increment variable
}
// ********** START OF MAIN PROGRAM ******************************************************************
main() {
setup_adc_ports(NO_ANALOGS); // diable analog
setup_timer_2 ( T2_DIV_BY_4,250,5);
enable_interrupts(INT_TIMER2); // enable interrupt for timer 2
enable_interrupts(INT_EXT); // enable external interrupt (B0)
ext_int_edge(H_to_L); // interrupt on high to low pulse edge
enable_interrupts(GLOBAL); // enable global interrupts
roll_dia = 5;
no_targets = 2;
pi_value = 3.14159;
roll_circ = ((roll_dia * pi_value)/no_targets);
for (i=0; i<5; ) ptics[i++] = pulse_ms; // fill rolling average values
i=0; // set i to 0
counter2=0; // initialize 232 output delay to 0
do // start main loop
{
If (counter2 >= 1000){ // delay used to output 232 every 1 second
pps = (1000/current_speed); // calculate pulses per second
fpm = ((pps * roll_circ / 12) * 60); // calculate feet per minute
printf("%7.2f\n\r", fpm); // send data out 232
counter2=0;} // reset RS-232 timmer
} while (true); // end of main loop
}
|
|
|
|
Neutone
Joined: 08 Sep 2003 Posts: 839 Location: Houston
|
|
Posted: Tue Jan 13, 2004 12:56 pm |
|
|
Base the FPM on pulses per second. No pulses per second means no FPM. This as opposed to a count of time per pulse. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Jan 13, 2004 1:22 pm |
|
|
The way that I look for "no input" in my programs is with a software timer.
I use a hardware Timer, which is setup to interrupt every 10 ms.
This Timer is usually the RTCC (Timer 0). Inside the interrupt service
routine for Timer 0, I check several software timer variables. If
they are non-zero, I decrement them.
One of those software timer variables is called the "no_input_timer".
Let's say I want declare that a "no input" condition has occurred,
if there is no input continuously for one second. Here's how I do it:
Initialize the "no_input_timer" variable to be 100. (100 x 10 ms =
1 second). Then, in the Timer0 isr, that variable is checked to
see if it's non-zero. If it is, then it's decremented. After 100 ticks
go by, it will become 0.
In the program, there is another routine, which checks to see if an input
occurred. If you're looking for an External Interrupt, this routine would
be the INT_EXT isr.
So, if you get an input (an INT_EXT interrupt), then inside that isr,
you should reload the "no_input_timer" variable with the preset value
(which is 100). In other words, the "no_input_timer" is only permitted
to count down to 0, if there have been no External Interrupts continuously
for 1 second. Once it becomes 0, it's only permitted to stay at 0
if you continue to have no inputs. Any input that occurs will cause
it to be reloaded to 100.
Then in your main loop, you poll the "no_input_timer" variable.
If it's zero, then you have had no input, for at least 1 second.
Then you can display some appropriate warning to the user.
This post contains more information about the use of software timers,
as described above.
http://www.ccsinfo.com/forum/viewtopic.php?t=17189 |
|
|
|
|
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
|