Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

multiple inputs simultaneously

Status
Not open for further replies.

varunme

Advanced Member level 3
Joined
Aug 10, 2011
Messages
741
Helped
17
Reputation
36
Reaction score
17
Trophy points
1,318
Activity points
5,764
How can I rewrite the below code so that , i can make two input bits high at the same time ?

Code:
void main() {



trisd=0;
trisc=0xff;


while(1)

{

if(portc.f0==1)
               portd.f0=1;
else if(portc.f1==1)
               portd.f1=1;
else if(portc.f2==1)
               portd.f2=1;
else if(portc.f3==1)
               portd.f3=1;
else if(portc.f4==1)
               portd.f4=1;
else if(portc.f5==1)
               portd.f5=1;
else if(portc.f6==1)
               portd.f6=1;
}
}

or this code will work when we press two switches simeltanously ?
 

This code will not detect multiple key presses, because you are using else-if construction, which means that the latter statement/comparison executes only if the former is false. If one is true because the key is pressed then others are not examined and therefore second keypress is not detected.

I generally tend to do keypress detection quite different. I some pseudo-code this would look like.:

-prepare 2 variables: PreviousBtnState and NewBtnState.
-every once in a while (may be triggered with timer) do following
-Read all buttons and write them to NewBtnState (as bit flags for example)
-compare NewBtnState to PreviousBtnState and determine changes
-act accordingly
-copy NewBtnState in place of PreviousBtnState
-repeat

This way not only can you detect changes in many buttons at one, but you may also detect button combinations as well as differentiate button press from button release. This gets even easier if you pic (from syntax i assume that's what you are using) has input change notification functionality.


O, and to achieve real simultaneous detection (not really needed when thing is about keypress detection) then you can only detect logic states which are confined to single port. U need to copy port state to some variable and then check each bit individually or compare against some bit pattern
 
  • Like
Reactions: varunme

    varunme

    Points: 2
    Helpful Answer Positive Rating
can u point one example for me ?
 

Simple like that :

Code:
void main() {

trisd=0;
trisc=0xff;


while(1)

{
portd = portc ;

}
}

Another option is you remove else statement of above code you posted.

+++
 
  • Like
Reactions: varunme

    varunme

    Points: 2
    Helpful Answer Positive Rating
means , using " if " instead of "else if" ?
 

i want to use that method , since i want to use PWM instead of just switch on and off , if a bit high in portc then corresponding bit in portd is 100% , otherwise its 50%
 

using if only, the code is not working
 

varunme,


It is not too clear what you intend.
Could post some schematic diagram of the circuit ?


+++
 

example1:

if((portc.f0==1) && (portc.f1==1))
portd.f0=1;
....


example2:

if ((portc & 0b00000011) == 0b00000011)
portd.f0=1;
 

It is not too clear what you intend.
I want to switch on an led array according to the input from the sensors

like if sensor one is high then the corresponding LED is high, if two sensors are high then the corresponding two LEDs are high and so on
 
Last edited:

I guess that concept provided by turbina solves your problem.

+++
 

But the inputs are not known , it may be at random inputs from the sensors , we cant assign first or second bit, in roads the sensors are activated randomly
 

In assembly, I read the input port (of the switches) then I simply write it (or its complement) to the output port (of the LEDs).
I am not familiar how to write these simple two instructions (or 3 if CPL is used) using C ;)
 

is there any way to give unknown value like "x" to a bit
like

porta.fx ?
so that we can access inside a loop
 

can this be solved by switch statement ?
 

Do the leds and sensors have a one to one relation, I mean sensor1 -> led 1 , sensor2 -> led 2, sensor3 -> led 3, sensor4 -> led 4 or the leds are just an indication of how many inputs are on?
For example is sensor 1 and 5 are on which leds should be on , led 1 and 5 or led 1 and 2 ?

If you want the latter then you can use a temporary variable, set it to 0 and for each sensor that is on do ((temp_var<<1) | 1), this will set the variable to 0b00000000, 0b00000001, 0b00000011...
Then at the end assign this variable to the output port.

Alex

P.S. You need to do an assignment for temp_var to store the new value, like temp_var = ((temp_var<<1) | 1)
 
Last edited:
  • Like
Reactions: varunme

    varunme

    Points: 2
    Helpful Answer Positive Rating
yes,
1-1
2-2
3-3
relation
not just indication , it has 100% in pwm when sensor is activated
if sensor1 is activated -> led1 goes 100% otherwise its 50%
 

I'm still not sure what you are trying to achieve, if an input is 1 then the output becomes 1 and if it is 0 then the output becomes 0?
If this is the case then you only need to copy one port to the other but I assume that you want something different.
Can you explain and give a couple of input/output values?
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top