comparing inputs

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
Anonymous
Not applicable

Hi Guys,

   

Ive been playing around with my PSoC eval1 board and  I was trying to write a code that would look at two inputs and if one was high but the other wasnt then an led would come on but if both were on or none or any other condition other than outlined then the led would come on,

   

I was thinking i might work something like this:

   

if (PRT1DR |= 0X01 && PRT1DR !=0X02)  // port 1.0 High, port 1.1 low

   

{

   

///LED on

   

}

   

Else

   

{

   

// No led on

   

}

   

I am not 100% on how to do this but ill look it up if i can find some useful info on the subject.

   

Thanks guys

0 Likes
1 Solution
Anonymous
Not applicable

Also check that you are setting port 0.4 because you wrote to port1 not port0

   

 Port_0_Data_SHADE |= 0X10;  // PORT 0.4 SET HIGH Output

   

/* wrong port?*/     

   

//PRT1DR = Port_0_Data_SHADE

View solution in original post

0 Likes
87 Replies
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

Basically -

   

 

   

if (  ( PRT1DR == 0x01 ) || ( PRT1DR == 0x02 ) ) {

   

////LED on

   

} else {

   

////LED off

   

}

   

 

   

If upper 6 bits in port used mask off with -

   

unsigned char portstate = PRT1DR & 0x03;

   

if ( ( portstate == 0X01 ) || ( portstate==0X02 ) ) {

   

 

   

Regards, Dana.

0 Likes
Anonymous
Not applicable

Hi Dana,

   

Thanks so much for your help,

   

Basically I have:

   

if ((PRT0DR == 0x01) || ( PRT0DR == 0x02)){  //inputs are port 0.0 and 0.1

   

//LED ON

   

}

   

else {

   

//LED OFF

   

}

   

I tried this and made both p0.0 and p0.1 high and the LED on p1.0 remains illuminated. I must be missing something

   

Similarly can i do this if (PRT0DR == 0X01) || (PRT0DR != 0X02)) \\ If i want the LED on when P0.0 is high and P0.1 is low?

   

Thanks

0 Likes
Anonymous
Not applicable

If you want the LED on only when ONE and ONLY ONE of the inputs is high

   

   

if ((PRT1DR == 0X01) || (PRT1DR == 0X02))  //only when one of the two bits is high

   

{

   

        //LED on

   

}

   

Else

   

{

   

       // No led on

   

}

0 Likes
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

What are you using for LED on code, and how is the led connected ?

   

 

   

You might consider posting project, or an extraction of it.

   

 

   

Regards, Dana.

0 Likes
Anonymous
Not applicable

Hi guys,

   

The LED is put on using the following (I am using shadow registers on port 0 and port 1):

   

if(condition etc.)

   

{

   

Port_1_Data_SHADE &= ~0x01;  //led on p1.0

   

PRT1DR = Port_1_Data_SHADE;

   

}

   

else

   

{

   

Port_1_Data_SHADE |= 0x01;

   

PRT1DR = Port_1_Data_SHADE;

   

}

   

this code was working fine for me if i was only looking at the value of 1 input, but doesnt seem to be working when Im comparing 1 or more inputs.

   

Thanks

0 Likes
Anonymous
Not applicable

looks like you reply when i was typing my reply

0 Likes
Anonymous
Not applicable

whoops sorry

0 Likes
Anonymous
Not applicable

 How is your hardware setup? 

   

The following is a bit long, but this should give you the correct result. If this doesn't work, there must be some other issues.

   

   

if (PRT0DR == 0) 

   

{

   

  shardow register = 0;

   

}

   

else if  (PRT0DR == 1) 

   

{

   

   shardow register = 1;

   

 

   

}

   

   

else if  (PRT0DR == 2) 

   

{

   

   shardow register = 1;

   

 

   

}

   

   

else if  (PRT0DR == 3) 

   

{

   

   shardow register = 0;

   

 

   

}

   

   

   

   

/* output the shadow register here */

0 Likes
Anonymous
Not applicable

Hi lleung,

   

The port pins are set up like so,

   

Output Led is stdCPU and drive mode is strong,

   

I have a port pin set high all the time to enable me to make the input pin High or Low using a wire, the port pin is driven strong,

   

I have the input pin set up as stdCPU with the drive mode being pull down

0 Likes
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

If LED is tied  pin > LED > R > Vdd, then LED turns on with a logic 0. This is preferred

   

method for driving an LED, eg. available drive current is greater.

   

 

   

Your code looks correct, is LED wired as above or pin > LED > R > Vss ?

   

In this case it runs on with logic 1.

   

 

   

if(condition etc.)

   

{

   

Port_1_Data_SHADE &= ~0x01; //led on p1.0

   

PRT1DR = Port_1_Data_SHADE;

   

}

   

else

   

{

   

Port_1_Data_SHADE |= 0x01;

   

PRT1DR = Port_1_Data_SHADE;

   

}

   

 

   

Regards, Dana.

0 Likes
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

Ap note that may be useful -

   

 

   

      http://www.cypress.com/?docID=38040

   

 

   

Regards, Dana.

0 Likes
Anonymous
Not applicable

Hi Dana,

   

The LED im using is one of the 4 available on the Eval1 prototyping board, so its Port pin Vcc > LED > R > GND by the looks of things

0 Likes
Anonymous
Not applicable

Is your software works now? If not, have you try the LONG routine I proposed?

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

In PSoC 1 there is -just to make things easier- an LED-component which may be adapted for active high (as on your board) and active low leds. Just tell the component to which port and pin your LED is connected and that's it. That easy!

   

There are some APIs for turning the LED on or off and as I recently saw the component itself is using a shadow-register internally, so that you are not aware of and do not have to care for. Simply turn your LED on and off at the right places within your program.

   

 

   

Happy lighting

   

Bob

0 Likes
lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable

Hi guys,

   

lleung: I havent yet tried the LONG method you posted.

   

ive posted the project in a .zip folder for anyone wanting to have a quick look and see if you guys can tell whats wrong

   

thanks

0 Likes
Anonymous
Not applicable

I would make a few changes as the followings

   

 

   

Port_0_Data_SHADE |= 0X30// PORT 0.4 SET HIGH Output     

   

/* wrong port?*/     

   

//PRT1DR = Port_0_Data_SHADE     

   

PRT0DR = Port_0_Data_SHADE;     

   

Port_0_Data_SHADE |= 0X20// PORT 0.5 SET HIGH Output     

   

PRT0DR = Port_0_Data_SHADE;     

   

 

   

*******     

   

For checking of input you need to mask the 2 input bits for checking 

   

 

   

//if (( PRT0DR == 0x01 ) || ( PRT0DR == 0x02 )) //conditions at Port 0.0 and Port 0.1     

   

/* Need to mask the 2 input bits for checking */     

   

if (( (PRT0DR & 0x3) == 0x01 ) || ( (PRT0DR & 0x3)  == 0x02 )) //conditions at Port 0.0 and Port 0.1

   

  

   

happy debugging 🙂

0 Likes
Anonymous
Not applicable

 Sorry a little mistake

   

You can set 0.4 and 0.5 in one step as in the following

   

 

   

   

Port_0_Data_SHADE |= 0X30;  // PORT 0.4, 0.5 SET HIGH Output

   

PRT0DR = Port_0_Data_SHADE;

0 Likes
Anonymous
Not applicable

Hi Lleung,

   

Thanks for your replies,

   

I didnt think of optimising the code by making both ports high in one go using the command you gave:

   

Port_0_Data_SHADE |= 0X30; // PORT 0.4, 0.5 SET HIGH Output

   

PRT0DR = Port_0_Data_SHADE;

   

When you say mask the inputs what excalty do you mean

   

thanks

0 Likes
Anonymous
Not applicable

is it along the lines of:

   

if(((PRT0DR & 0x01) == 0x01) || ((PRT0DR & 0x02) == 0x02)

0 Likes
Anonymous
Not applicable

 you can do that too.

0 Likes
Anonymous
Not applicable

Ill try those changes and report back, basically ill have:

   

if(((PRT0DR & 0x01) == 0x01) || ((PRT0DR & 0x02) == 0x02)

   

{

   

//OUTPUT HIGH

   

}

   

else

   

{

   

// OUTPUT LOW

   

}

0 Likes
Anonymous
Not applicable

Since port 0.4 and 0.5 are high

   

when you read the data back, it always be binary 001100XX 

   

So  if (PRT0DR == 0x01) would alwasy fail

   

I prefer to do the following

   

 

   

usigned char ucTempPort0Inputs;

   

ucTempPort0Inputs = PRT0DR & 0x3;

   

if ((ucTempPort0Inputs == 0x1) || ucTempPort0Inputs).......

0 Likes
Anonymous
Not applicable

Also check that you are setting port 0.4 because you wrote to port1 not port0

   

 Port_0_Data_SHADE |= 0X10;  // PORT 0.4 SET HIGH Output

   

/* wrong port?*/     

   

//PRT1DR = Port_0_Data_SHADE

0 Likes
Anonymous
Not applicable

Here you can find some information about bit masking 

   

http://en.wikipedia.org/wiki/Mask_(computing)

0 Likes
Anonymous
Not applicable

 another web page for bitmasking

   

      http://kineticsandelectronics.com/node/104

0 Likes
Anonymous
Not applicable

Hi lleung,

   

I was thinking of making the code give an output if port 0.0 was high and port 0.1 was low, and vice versa kind of like this:

   

while(1)

   

{

   

   

Port_0_Data_SHADE |= 0x10; //make p0.4 high

   

   

PRT1DR = Port_0_Data_SHADE;

   

 

if (port 0.0 high) and (port 0.1 low)

{

// Make Output high

}

else

{

// Make Output Low

}

// make port 0.4 low again

 

0 Likes
Anonymous
Not applicable

 Just one step a time.

   

Does your code works after the modfication suggested?

0 Likes
Anonymous
Not applicable

Hi Lleung,

   

Sorry was getting a little ahead of myself,

   

yes I just noticed that i was writing to the wrong port,

   

Ok so with those changes the led is illuminated when port 0.0 and port 0.1 are both low,  the led is off when either port 0.0 or 0.1 are high but still off when both p0.0 and p0.1 are high

0 Likes
Anonymous
Not applicable

 Because you use

   

     

            

if(((PRT0DR & 0x01) == 0x01) || ((PRT0DR & 0x02) == 0x02))    

   

     

            

in this case when P0.1 and P0.2 are high, the above check is still ture     

   

     

            

   

you should use     

   

 if(((PRT0DR & 0x03) == 0x01) || ((PRT0DR & 0x03) == 0x02))    

   

 in that case if p0.1 and p0.0 are high, the above test would be false     

0 Likes
Anonymous
Not applicable

oh cool, that worked,

   

the led is off if either port 0.0 or port 0.1 are high but the LED is on when both are high or both are low

   

thanks lleung

0 Likes
Anonymous
Not applicable

 Good to hear that,

   

Needs to go to bed now. you can post your requirment here and shall check that tomorrow morning. 🙂

0 Likes
Anonymous
Not applicable

Thanks very much Lleung, that works well,

   

At most I was thinking of having for outputs and four inputs,

   

I was thinking of having the LED on P1.0 on only when Port 0.0 was High but Port 0.1, 0.2, 0.3 were LOW or not HIGH

   

would it be something like:

   

if (((PRT0DR & 0X0F) == 0X01) || ((PRT0DR & 0X0F) != 0X02) || ((PRT0DR & 0X0F) != 0X04) || ((PRT0DR & 0X0F) != 0X08))

   

{

   

//LED off

   

}

   

else

   

{

   

led on

   

}

0 Likes
lock attach
Attachments are accessible only for community members.
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

Looks like most of your issues resolved. I basically did a project,

   

Port 0 LED drive, PSOCEval1 board, Port 1 the two inputs

   

that you wanted LED lit when one input high, the other low,

   

and off when both at same state. I did not use shadow regs, but

   

should have.

   

 

   

Anyways I was not keeping track of the state of the thread, I will post project

   

anyways. I did find out LED is tied to ground on PSOCEval1, so output wants

   

to be high for turn on.

   

 

   

Regards, Dana.

0 Likes
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

Forgot to add, in my case LED blinks when the two inputs not the same.

   

 

   

Regards, Danma.

0 Likes
Anonymous
Not applicable

Hi Dana,

   

Thanks for your reply, I see you used the LED module, it seems pretty easy to use,

   

I will have a good look through your project, thanks for submitting it,

0 Likes
Anonymous
Not applicable

I would use a table as following to check the logic,  compare what you want with outcome of the if statement ( assuming you are the CPU)

   

INPUT           Expected              OP of if statement

   

0000

   

0001

   

0010

   

0011

   

0100

   

....

   

1111

0 Likes
Anonymous
Not applicable

All i really need is four conditions, p0.0 p0.1 p0.2 p0.3

   

 

   

INPUT OUTPUT

   

1000     led off

   

if p0.0 is high and all other ports are low then the led is off, otherwise make led or output high

   

similarily I was thinking of making changes fown the line or fo experimenting with,

   

such as

   

INPUT OUTPUT

   

0010     led off

   

0100     led off etc.

0 Likes
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

If you have 4 inputs, make a table 16 bits deep, with expected

   

response for each bit, and the address is the 4 bit input

   

representation to be tested.

   

 

   

Regards, Dana.

0 Likes
Anonymous
Not applicable

By making the table and do the evaluation yourself. you'll figure out the mistake and make the correction.

   

This way you'll not make the error next time.

0 Likes