View previous topic :: View next topic |
Author |
Message |
Eleengnrng
Joined: 21 Apr 2006 Posts: 6
|
Time Delay with condition loop. |
Posted: Tue Aug 29, 2006 9:54 am |
|
|
Hello I am trying to get a loop to execute for 8s then exit or exit if the external interrupt is triggered the interrupt is active low. Here is what I have tried and other combinations. I am using a PIC16F877 at 20Mhz Clock; all the proper tris registers are set and interrupts. I have been able to have the loop terminate after the interrupt is triggered but cant seem to get the delay to work.
Code: |
while((input(PIN_B0)!=0)&&(delay_ms(8000))){
output_high(PIN_A4);// Turn Buzzer on
}
output_low(PIN_A4); // turn buzzer off |
Any help is appreciated. |
|
|
Ttelmah Guest
|
|
Posted: Tue Aug 29, 2006 10:13 am |
|
|
The delay function, does not return a value, so cannot be used in a conditional test. You could do what you show with either:
[code]
do {
output_high(PIN_A4);
} while (delay_ms(8000),input(PIN_B0));
[code]
or
[code]
while (input(PIN_B0)) {
output_high(PIN_A4);
delay_ms(8000);
}
[/code]
The first will raise the pin, wait eight seconds, then test the input pin, while the latter tests the pin before starting.
Best Wishes |
|
|
Humberto
Joined: 08 Sep 2003 Posts: 1215 Location: Buenos Aires, La Reina del Plata
|
|
Posted: Tue Aug 29, 2006 10:39 am |
|
|
If you want to exit the loop if the EXTernal INTerrupt is triggered, you would do
something like this:
Code: |
int8 counter;
#INT_EXT
void EXT_inte()
{
counter = 1; // At worst case will add 40ms of latency
} // but is safest than counter = 0
in main....
ext_int_edge(H_to_L);
enable_interrupts(INT_EXT);
enable_interrupts(GLOBAL);
counter = 200;
do
{
output_high(PIN_A4); // Turn Buzzer on
delay_ms(40); // 200*40=8000
counter--;
}while(counter);
output_low(PIN_A4); // Turn Buzzer off
|
Humberto |
|
|
MikeW
Joined: 15 Sep 2003 Posts: 184 Location: Warrington UK
|
|
Posted: Wed Aug 30, 2006 1:03 am |
|
|
The way i do these sort of things is by having a timer isr which sets flags to signify a timing period is over. Then in the main loop, just check the timeout flag for being set. That way, you can check for your event change, be it a pin change, or interrupt change, and act accordingly.
hope this has helped. |
|
|
|