![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
horde_fuego
Joined: 31 Mar 2009 Posts: 11
|
2 simultaneous low inputs not working |
Posted: Tue May 19, 2009 7:32 am |
|
|
hello good evening to everyone!
from the code below, why is it for example that when both PIN_A0 and PIN_A1 are pulled high simultaneously there is still an output at port B and the output is from one of the "if(input(PIN_A0)" or "if(input(PIN_A1))" conditions?is there something wrong with my code?why is it that the logical AND is not working correctly as it should in this code?also the same case applies to other conditions of simultaneous high inputs.please help!!thanks in advance!
Code: |
#include "C:\Documents and Settings\user\My Documents\picc project\sss.h"
#use fast_io(a)
#use fast_io(b)
void do_it_here();
void main()
{
set_tris_a(0x1f);
set_tris_b(0x00);
for(;;)
{
while(input(PIN_A4))
{
output_b(0x00);
do_it_here();
output_b(0x00);
}
while(!input(PIN_A4))
{
output_b(0x20);
delay_ms(100);
output_b(0x00);
delay_ms(100);
}
}
}
void do_it_here()
{
if(input(PIN_A0) && input(PIN_A1))
{
output_b(0x00);
}
if(input(PIN_A0) && input(PIN_A2))
{
output_b(0x00);
}
if(input(PIN_A1) && input(PIN_A2))
{
output_b(0x00);
}
if(input(PIN_A0) && input(PIN_A1) && input(PIN_A2))
{
output_b(0x00);
}
if(input(PIN_A0))
{
output_high(PIN_B0);
delay_ms(1200);
output_high(PIN_B1);
delay_ms(1200);
output_high(PIN_B2);
delay_ms(1200);
output_high(PIN_B3);
delay_ms(1200);
output_high(PIN_B4);
delay_ms(1200);
}
if(input(PIN_A1))
{
output_high(PIN_B0);
delay_ms(2000);
output_high(PIN_B1);
delay_ms(2000);
output_high(PIN_B3);
delay_ms(2000);
}
if(input(PIN_A2))
{
output_high(PIN_B0);
delay_ms(2000);
output_high(PIN_B1);
delay_ms(2000);
output_high(PIN_B2);
delay_ms(2000);
}
if(input(PIN_A3))
{
output_high(PIN_B0);
delay_ms(3000);
output_high(PIN_B1);
delay_ms(3000);
}
}
| ![Razz](images/smiles/icon_razz.gif) |
|
![](templates/subSilver/images/spacer.gif) |
ECACE
Joined: 24 Jul 2006 Posts: 94
|
|
Posted: Tue May 19, 2009 11:45 am |
|
|
After only a short look over your code:
From your code:
Code: | void do_it_here()
{
if(input(PIN_A0) && input(PIN_A1))
{
output_b(0x00);
}
if(input(PIN_A0) && input(PIN_A2))
{
output_b(0x00);
}
if(input(PIN_A1) && input(PIN_A2))
{
output_b(0x00);
}
if(input(PIN_A0) && input(PIN_A1) && input(PIN_A2))
{
output_b(0x00);
}
if(input(PIN_A0))
{
output_high(PIN_B0);
delay_ms(1200);
output_high(PIN_B1);
delay_ms(1200);
output_high(PIN_B2);
delay_ms(1200);
output_high(PIN_B3);
delay_ms(1200);
output_high(PIN_B4);
delay_ms(1200); |
Say you have A0 & A1 both high.
Your code is saying in the first statement, if those two are high to make the output of B to be low.
Then the rest of the AND statements fail as only A0 AND A1 are high.
The code falls right through the AND statements to the next one that is valid, which in your case is:
Code: | if(input(PIN_A0))
{
output_high(PIN_B0);
delay_ms(1200);
output_high(PIN_B1);
delay_ms(1200);
output_high(PIN_B2);
delay_ms(1200);
output_high(PIN_B3);
delay_ms(1200);
output_high(PIN_B4);
delay_ms(1200);
}
if(input(PIN_A1))
{
output_high(PIN_B0);
delay_ms(2000);
output_high(PIN_B1);
delay_ms(2000);
output_high(PIN_B3);
delay_ms(2000);
} |
So you should see port B go low, for a very short time, then your:
if(input(A0)) statement will make B0 go high for 1200mS, then B1...
I don't know how fast you are running your clock, but the amount of time for it to run through all your initial AND statements is very short.
Much shorter than you would ever be able to see with your eye.
Is this what you are seeing? _________________ A HW Engineer 'trying' to do SW !!! Run!!! |
|
![](templates/subSilver/images/spacer.gif) |
horde_fuego
Joined: 31 Mar 2009 Posts: 11
|
|
Posted: Tue May 19, 2009 9:55 pm |
|
|
I tried changing it to this statement:
Code: |
if(input(PIN_A0))
{
output_high(PIN_B0);
delay_ms(1200);
output_high(PIN_B1);
delay_ms(1200);
output_high(PIN_B2);
delay_ms(1200);
output_high(PIN_B3);
delay_ms(1200);
output_high(PIN_B4);
delay_ms(1200);
}
else if(input(PIN_A1))
{
output_high(PIN_B0);
delay_ms(2000);
output_high(PIN_B1);
delay_ms(2000);
output_high(PIN_B3);
delay_ms(2000);
}
else if(input(PIN_A2))
{
output_high(PIN_B0);
delay_ms(2000);
output_high(PIN_B1);
delay_ms(2000);
output_high(PIN_B2);
delay_ms(2000);
}
else if(input(PIN_A3))
{
output_high(PIN_B0);
delay_ms(3000);
output_high(PIN_B1);
delay_ms(3000);
}
else
{
output_b(0x00);
}
|
Still the same thing happens. Sorry for the inconvenience. Is there any technique so that only one input should be sensed and the others can be ignored by the PIC? |
|
![](templates/subSilver/images/spacer.gif) |
ECACE
Joined: 24 Jul 2006 Posts: 94
|
|
Posted: Wed May 20, 2009 7:31 am |
|
|
If you were to paste the code you just gave in place of the second group of IF statements, then yes, it would do the exact same thing.
I don't understand exactly what you want your program to do.
If you want it to sense the condition of two lines being high and only do one of the responses from your second group of IF statements, then you could do something along the lines of:
Code: | if (input (PIN_A0) && (PIN_A1)
{
Response_HH();
}
if (input(PIN_A0 && ! (PIN_A1)
{
Response_HL();
}
...
...all of your different conditional if statements
...
else
{
Response_Default();
}
void Response_HH()
{
...
... Do what ever you wanted for a High High signal
...
}
void Response_HL()
{
...
... Do what ever you wanted for a High Low signal
...
}
void Response_Default()
{
...
... Do something if none of the conditions were met
...
} |
Not sure if this helps. Again, I don't know exactly what you are trying to do in your program though. _________________ A HW Engineer 'trying' to do SW !!! Run!!! |
|
![](templates/subSilver/images/spacer.gif) |
horde_fuego
Joined: 31 Mar 2009 Posts: 11
|
|
Posted: Wed May 20, 2009 8:53 am |
|
|
Thank you very much for your response! I will try this now. :grin:PIN_A0 and PIN_A1 correspond to sensors output and each pin is connected separately to a 2 2-input AND gate, and also there is a common synchronizing input tied to each available pin in the 2 2-input AND gate. If ever both the sensors go high and the synchronizing input becomes high also, the PIC must output low until one of the sensors outputs is pulled low. Thanks for your help! |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah Guest
|
|
Posted: Wed May 20, 2009 8:58 am |
|
|
As a general comment, a few of things apply:
First, remember there will be a time lag between the pins being sampled. It sounds as if these are possibly driven by something like switches, in which case, the tiny time involved won't matter, but it might in other circumstances.
Second, remember also, that in general, the expression won't even test the second part, if the first has evaluated as 'untrue'.
Finally, if these are switches, remember that they _will_ exhibit 'bounce', possibly making and breaking a dozen times when changed. The logic could well be propagating 'through' the tests on bounces....
Now, there seems to be a lot of 'pointless' code, in what is posted.
You start, if A4, goes high, setting the whole of port B to 0. Then call 'do_it_here'. You then test for four different pairs of conditions, and set the output low again if these are true. Why bother?. The output is _already_ low.... The first sixteen lines of 'do_it_here', are basically pointless.
Then you test A0. In your test with both lines high, it is high, so this code will execute, setting all the lines high. Then the code tests A1, which is also high, so another 6 seconds passed, but the lines are already high, and no changes to the output wll be seen.
We also have no proof that your ciruit actually pulls any line low anywhere. You talk about pulling lines 'high', but what circuitry isthere to pull the lines low?...
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
|
|
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
|