View previous topic :: View next topic |
Author |
Message |
SlimG
Joined: 03 Dec 2004 Posts: 6
|
Detecting a 0 to 1 transistion(rising edge) on a pic i/p |
Posted: Fri Feb 04, 2005 4:46 am |
|
|
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 |
|
![](templates/subSilver/images/spacer.gif) |
valemike Guest
|
|
Posted: Fri Feb 04, 2005 6:58 am |
|
|
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();
} |
|
![](templates/subSilver/images/spacer.gif) |
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Fri Feb 04, 2005 7:10 am |
|
|
If you want a second opinion, mine is the same as valemike's ![Very Happy](images/smiles/icon_biggrin.gif) |
|
![](templates/subSilver/images/spacer.gif) |
Guest
|
Re: Detecting a 0 to 1 transistion(rising edge) on a pic i/p |
Posted: Fri Feb 04, 2005 8:46 am |
|
|
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. |
|
![](templates/subSilver/images/spacer.gif) |
|