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

Detecting a 0 to 1 transistion(rising edge) on a pic i/p

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



Joined: 03 Dec 2004
Posts: 6

View user's profile Send private message

Detecting a 0 to 1 transistion(rising edge) on a pic i/p
PostPosted: Fri Feb 04, 2005 4:46 am     Reply with quote

Im trying to detect a 0 to 1 transistion on a pic i/p pin using a while loop.

Here is the code I have writen.

int sample_1, sample_2;

sample_1 = input(pin_a0);
sample_2 = input(pin_a0);


while (sample_1==1 && sample_2==0) // the condtn does work, it fails both ways
;

bla;

bla;

What i want is for the loop to continue when sample_1 is 0 and sample_2 is 1, but for the loop to fail when sample_1 is 1 and sample_2 is 0

can anyone help?

thanx people.

Regards
valemike
Guest







PostPosted: Fri Feb 04, 2005 6:58 am     Reply with quote

What you are doing is sampling sample_1 and sample_2 back-to-back. 99.999% of the time you will always get the same value on both. You should be sampling inside the while-loop. In fact you don't need two variables, just one.

Code:

// if the pin is '1', wait for it to be '0'; if the pin is already '0' you'll zoom right past this first while-loop
while (input(PIN_A0));

// ok, the pin is now '0'; wait for the 0 to 1 transition...
while (!input(PIN_A0));



Of course, if you have your watchdog timer enabled, then you'll have to refresh the watchdog timer inside the while loops

while (input(PIN_A0))
{
restart_wdt();
}
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Fri Feb 04, 2005 7:10 am     Reply with quote

If you want a second opinion, mine is the same as valemike's Very Happy
Guest








Re: Detecting a 0 to 1 transistion(rising edge) on a pic i/p
PostPosted: Fri Feb 04, 2005 8:46 am     Reply with quote

SlimG wrote:
Im trying to detect a 0 to 1 transistion on a pic i/p pin using a while loop.

Here is the code I have writen.

int sample_1, sample_2;

sample_1 = input(pin_a0);
sample_2 = input(pin_a0);


while (sample_1==1 && sample_2==0) // the condtn does work, it fails both ways
;

bla;

bla;

What i want is for the loop to continue when sample_1 is 0 and sample_2 is 1, but for the loop to fail when sample_1 is 1 and sample_2 is 0

can anyone help?

thanx people.

Regards

There are other ways. You did not say which PIC you are using.
If the PIC has interrupt ability, then why not let the hardware do the work.
Using an Interrupt pin set to deetct the rising edge it is possible to detect a trasistion from 0 to 1 and never miss a transition.
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