CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Time Delay with condition loop.

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Eleengnrng



Joined: 21 Apr 2006
Posts: 6

View user's profile Send private message

Time Delay with condition loop.
PostPosted: Tue Aug 29, 2006 9:54 am     Reply with quote

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







PostPosted: Tue Aug 29, 2006 10:13 am     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Aug 29, 2006 10:39 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Aug 30, 2006 1:03 am     Reply with quote

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.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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