Cypress Perform

Home > Design Support > Cypress Developer CommunityTM > Cypress Forums > PSoC® 1 > comparing inputs

Bookmark and Share
Cypress Developer CommunityTM
Forums | Videos | Blogs | Training | Rewards Program | Community Components



comparing inputs
Moderator:
ARVI

Post Reply
Follow this topic



comparing inputs

Cf_corp posted on 20 Aug 2012 3:20 AM PST
Top Contributor
74 Forum Posts

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




Re: comparing inputs

danaaknight posted on 20 Aug 2012 03:38 AM PST
Top Contributor
1773 Forum Posts

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.

 



Re: comparing inputs

Cf_corp posted on 20 Aug 2012 04:07 AM PST
Top Contributor
74 Forum Posts

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



Re: comparing inputs

H L posted on 20 Aug 2012 04:15 AM PST
Top Contributor
679 Forum Posts

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

}

 



Re: comparing inputs

danaaknight posted on 20 Aug 2012 04:16 AM PST
Top Contributor
1773 Forum Posts

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.



Re: comparing inputs

Cf_corp posted on 20 Aug 2012 04:26 AM PST
Top Contributor
74 Forum Posts

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

 

 

 



Re: comparing inputs

H L posted on 20 Aug 2012 04:28 AM PST
Top Contributor
679 Forum Posts

looks like you reply when i was typing my reply



Re: comparing inputs

Cf_corp posted on 20 Aug 2012 04:29 AM PST
Top Contributor
74 Forum Posts

whoops sorry

 



Re: comparing inputs

H L posted on 20 Aug 2012 04:35 AM PST
Top Contributor
679 Forum Posts

 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 */

 



Re: comparing inputs

Cf_corp posted on 20 Aug 2012 04:45 AM PST
Top Contributor
74 Forum Posts

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

 



Re: comparing inputs

danaaknight posted on 20 Aug 2012 05:15 AM PST
Top Contributor
1773 Forum Posts

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.



Re: comparing inputs

danaaknight posted on 20 Aug 2012 05:29 AM PST
Top Contributor
1773 Forum Posts

Ap note that may be useful -

 

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

 

Regards, Dana.

 

 



Re: comparing inputs

Cf_corp posted on 20 Aug 2012 06:19 AM PST
Top Contributor
74 Forum Posts

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



Re: comparing inputs

H L posted on 20 Aug 2012 06:49 PM PST
Top Contributor
679 Forum Posts

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



Re: comparing inputs

Bob Marlowe posted on 21 Aug 2012 01:41 AM PST
Top Contributor
1768 Forum Posts

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



Re: comparing inputs

Cf_corp posted on 21 Aug 2012 03:03 AM PST
Top Contributor
74 Forum Posts

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



Re: comparing inputs

H L posted on 21 Aug 2012 05:25 AM PST
Top Contributor
679 Forum Posts

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 :-)

 



Re: comparing inputs

H L posted on 21 Aug 2012 05:28 AM PST
Top Contributor
679 Forum Posts

 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;



Re: comparing inputs

Cf_corp posted on 21 Aug 2012 06:15 AM PST
Top Contributor
74 Forum Posts

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

 



Re: comparing inputs

Cf_corp posted on 21 Aug 2012 06:16 AM PST
Top Contributor
74 Forum Posts

is it along the lines of:

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



Re: comparing inputs

H L posted on 21 Aug 2012 06:22 AM PST
Top Contributor
679 Forum Posts

 you can do that too.



Re: comparing inputs

Cf_corp posted on 21 Aug 2012 06:27 AM PST
Top Contributor
74 Forum Posts

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

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

{

//OUTPUT HIGH

}

else

{

// OUTPUT LOW

}



Re: comparing inputs

H L posted on 21 Aug 2012 06:28 AM PST
Top Contributor
679 Forum Posts

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).......

 



Re: comparing inputs

H L posted on 21 Aug 2012 06:36 AM PST
Top Contributor
679 Forum Posts

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



Re: comparing inputs

H L posted on 21 Aug 2012 06:38 AM PST
Top Contributor
679 Forum Posts

Here you can find some information about bit masking 

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



Re: comparing inputs

H L posted on 21 Aug 2012 06:39 AM PST
Top Contributor
679 Forum Posts

 another web page for bitmasking

http://kineticsandelectronics.com/node/104



Re: comparing inputs

Cf_corp posted on 21 Aug 2012 06:40 AM PST
Top Contributor
74 Forum Posts

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

 

 



Re: comparing inputs

H L posted on 21 Aug 2012 06:46 AM PST
Top Contributor
679 Forum Posts

 Just one step a time.

Does your code works after the modfication suggested?



Re: comparing inputs

Cf_corp posted on 21 Aug 2012 06:54 AM PST
Top Contributor
74 Forum Posts

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

 



Re: comparing inputs

H L posted on 21 Aug 2012 07:03 AM PST
Top Contributor
679 Forum Posts

 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



Re: comparing inputs

Cf_corp posted on 21 Aug 2012 07:09 AM PST
Top Contributor
74 Forum Posts

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



Re: comparing inputs

H L posted on 21 Aug 2012 07:11 AM PST
Top Contributor
679 Forum Posts

 Good to hear that,

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



Re: comparing inputs

Cf_corp posted on 21 Aug 2012 07:33 AM PST
Top Contributor
74 Forum Posts

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

}



Re: comparing inputs

danaaknight posted on 21 Aug 2012 08:27 AM PST
Top Contributor
1773 Forum Posts

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.



Re: comparing inputs

danaaknight posted on 21 Aug 2012 08:28 AM PST
Top Contributor
1773 Forum Posts

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

 

Regards, Danma.



Re: comparing inputs

Cf_corp posted on 21 Aug 2012 08:32 AM PST
Top Contributor
74 Forum Posts

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,



Re: comparing inputs

H L posted on 21 Aug 2012 05:32 PM PST
Top Contributor
679 Forum Posts

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



Re: comparing inputs

Cf_corp posted on 22 Aug 2012 02:56 AM PST
Top Contributor
74 Forum Posts

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.

 

 



Re: comparing inputs

danaaknight posted on 22 Aug 2012 03:35 AM PST
Top Contributor
1773 Forum Posts

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.



Re: comparing inputs

H L posted on 22 Aug 2012 04:03 AM PST
Top Contributor
679 Forum Posts

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.

 



Re: comparing inputs

Cf_corp posted on 22 Aug 2012 04:16 AM PST
Top Contributor
74 Forum Posts

Id have something like, the output is only ever high when only one of the inputs is high but not any more at the same time.

INPUT OUPUT

0000    LOW

0001   HIGH

0010   HIGH

0011   LOW

0100   HIGH

0101   LOW

0110   LOW

0111   LOW

1000  HIGH

1001  LOW

1010  LOW

1011  LOW

1110  LOW

1111  LOW

 



Re: comparing inputs

H L posted on 22 Aug 2012 04:25 AM PST
Top Contributor
679 Forum Posts

 Excellent!

I think the table is what you want the output to be,.

Did you compare this with your if(...) statement? Does it match what you want?



Re: comparing inputs

Cf_corp posted on 24 Aug 2012 02:08 AM PST
Top Contributor
74 Forum Posts

Hi,

Ive been busy this week but I will try,

Im not 100% on how to implement that truth table in my code, I was thinking of doing it in 4 steps as i am using 4 inputs,

i was thinking :

//input on p0.0

// enable output connected to p0.0 only

if(p0.0 is high and p0.1, p0.2, p0.3 are not high) {

// led on

}

else {

//led off

}

//disable output for p0.0

// make the code do the same 3 more times but for p0.1, p0.2, p0.3

im thinking of making like a security device and i was thinking cyling through each individiual input and checking that only the predesignated input is high and the rest are low.

 

 

 



Re: comparing inputs

danaaknight posted on 24 Aug 2012 03:50 AM PST
Top Contributor
1773 Forum Posts

You have 4 inputs, each unique, so that suggests a table of 16, 1 bit deep.

So assemble the 4 bits into a nibble. or just read the port with the inputs, and

mask off upper 4 bits if they are lower 4 bits, or mask off lower 4 bits and shift

right by 4. This produces an address into our table, 0 - 15.

 

struct {

unsigned int outval : 1;

} ledrive[16] = { 0, 1, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0 };

 

So your address becomes - ledrive[your address] to access the output value for that input.

 

Regards, Dana.



Re: comparing inputs

danaaknight posted on 24 Aug 2012 04:40 AM PST
Top Contributor
1773 Forum Posts

Note you can always make the bit table 4 wide to handle all 4 outputs.

 

Regards, Dana.



Re: comparing inputs

Cf_corp posted on 24 Aug 2012 05:59 AM PST
Top Contributor
74 Forum Posts

Hiya Dana,

Do you know of any sample projects that use this procedure? sorry to bother you about it but i am easily confused,

thank you very much



Re: comparing inputs

danaaknight posted on 24 Aug 2012 08:48 AM PST
Top Contributor
1773 Forum Posts

Here is a simple sample project to try.

 

Its not optimal, I would have not used the LED modules, rather used direct writes to

pins to drive LEDs. That would eliminate the case statements in subroutine, possibly

a simple loop would have sufficed. I will leave that up to you.

 

If you do read modify writes on any ports then implement shadow registers.

 

Project attached.

 

Regards, Dana.



Re: comparing inputs

danaaknight posted on 24 Aug 2012 11:21 AM PST
Top Contributor
1773 Forum Posts

The attached are some ap notes on State Machine design in PSOC, very

much a table driven solution set.

 

Regards, Dana.



Re: comparing inputs

danaaknight posted on 25 Aug 2012 05:09 AM PST
Top Contributor
1773 Forum Posts

Last post, here is a project not using LED module, using shadow registers

and writing to the port instead of using LED_On() etc....

 

Saved code space, cleaner.

 

Regards, dana.



Re: comparing inputs

EricS posted on 25 Aug 2012 09:52 AM PST
Top Contributor
46 Forum Posts

 Here is the code Dana posted but optimized a little more, only 4 lines are necessary with the use of LED UM.

 



Re: comparing inputs

danaaknight posted on 25 Aug 2012 02:31 PM PST
Top Contributor
1773 Forum Posts


Hitech w/ 4 LED modules used Eric
754 FLASH
18  RAM

Hitech w no LED modules used Dana
495 FLASH
18  RAM

 

Imagcraft w/ 4 LED modules used Eric
912 FLASH
20 RAM

Hitech w no LED modules used Dana
707 FLASH
20  RAM

 

Fascinating, because looking at Erics correct pick of API's to use I would have guessed his coding much better.

 

Looks are deceiving.

 

Regards, Dana.



Re: comparing inputs

danaaknight posted on 25 Aug 2012 02:39 PM PST
Top Contributor
1773 Forum Posts

Challenge to the crew, can anyone beat the code density on this,

use the project earliest LEDdrive1.zip in thread as basis. Bob

should be able to blow our socks off. Reward is one "eatme"

sticker to be placed in a suitable personal location :)

 

Regards, Dana.



Re: comparing inputs

Bob Marlowe posted on 26 Aug 2012 04:34 AM PST
Top Contributor
1768 Forum Posts

Well, anyone interestred in 733 Bytes Rom and 4 Bytes Ram with with a 6-character change in the program?

But probably I can even do better... Stay tuned....

 

Bob



Re: comparing inputs

Bob Marlowe posted on 26 Aug 2012 05:05 AM PST
Top Contributor
1768 Forum Posts

Final version with shadow-register:

ROM 2% full. 551 out of 32768 bytes used (does not include absolute areas).

RAM 1% full. 4 bytes used (does not include stack usage).

Built with ICCM8C STD V7.05.00

 

LEDDrive - 0 error(s) 0 warning(s) 13:57:57

 

 



Re: comparing inputs

Bob Marlowe posted on 26 Aug 2012 06:37 AM PST
Top Contributor
1768 Forum Posts

...and with some better optimization settings I finally get

ROM 2% full. 534 out of 32768 bytes used (does not include absolute areas).

RAM 2% full. 4 bytes used (does not include stack usage).

Built with ICCM8C STD V7.05.00

ImageCraft, no LED-Modules, with shadow-register.



Re: comparing inputs

H L posted on 26 Aug 2012 06:44 AM PST
Top Contributor
679 Forum Posts

I didn't have the board on hand to test. But this should work.

With the port_ shadow.

 

void main(void)

{

unsigned char portdata = 0;

unsigned char ucLEDdata = 0;

for (;;) 

{

Port_1_Data_SHADE = 1;

portdata = PRT0DR & 0x0F; // Read the 4 input pins, mask off the uppper 4 bits, they are irrelevant

if (~portdata || (portdata && (portdata -1)))

               {

Port_1_Data_SHADE = 0;

}

PRT1DR = Port_1_Data_SHADE;

}

 

 

 



Re: comparing inputs

H L posted on 26 Aug 2012 06:46 AM PST
Top Contributor
679 Forum Posts

 Sorry mistake.

 

void main(void)

{

unsigned char portdata = 0;

unsigned char ucLEDdata = 0;

for (;;) 

{

Port_1_Data_SHADE = 1;

portdata = PRT0DR & 0x0F; // Read the 4 input pins, mask off the uppper 4 bits, they are irrelevant

if (~portdata || (portdata & (portdata -1)))

{

Port_1_Data_SHADE = 0;

}

PRT1DR = Port_1_Data_SHADE;

}

}



Re: comparing inputs

H L posted on 26 Aug 2012 08:01 PM PST
Top Contributor
679 Forum Posts

MY bad, it was late last night, another mistake.

if (~portdata || (portdata & (portdata -1)))

should be

if ((!portdata) || (portdata & (portdata -1)))

I do not have a PSOC1 board here to test. :-) hope this works.



Re: comparing inputs

danaaknight posted on 27 Aug 2012 04:03 AM PST
Top Contributor
1773 Forum Posts

@lleung, maybe I am not following your approach, but this is what

the description is of the functionality needed -

 

1) There are 4 input signals.

2) 4 LED outputs.

3) Each LED, its state, is determined by the 4 inputs via a bit truth table

that is 16 entries deep, 1 bit wide, basically an array of bits. Each LED

has a different truth table associated with it. There are 16 possible input

combinations, simple binary. So one truth table is used, 16 entries, and

effectively 4 bits wide via a bit structure.

 

Goal is to opitimize FLASH size of code.

 

Regards, Dana.



Re: comparing inputs

danaaknight posted on 27 Aug 2012 04:03 AM PST
Top Contributor
1773 Forum Posts

@lleung, maybe I am not following your approach, but this is what

the description is of the functionality needed -

 

1) There are 4 input signals.

2) 4 LED outputs.

3) Each LED, its state, is determined by the 4 inputs via a bit truth table

that is 16 entries deep, 1 bit wide, basically an array of bits. Each LED

has a different truth table associated with it. There are 16 possible input

combinations, simple binary. So one truth table is used, 16 entries, and

effectively 4 bits wide via a bit structure.

 

Goal is to opitimize FLASH size of code.

 

Regards, Dana.



Re: comparing inputs

H L posted on 27 Aug 2012 04:10 AM PST
Top Contributor
679 Forum Posts

 It seems that only 1 LED is being used for indication. All other LEDs are 0. Did I miss something here?

 



Re: comparing inputs

danaaknight posted on 27 Aug 2012 04:35 AM PST
Top Contributor
1773 Forum Posts

The originator of the post has to fill in the truth table for the other LEDs,

I had left a note earlier for him to do that.

 

Regards, Dana.



Re: comparing inputs

H L posted on 27 Aug 2012 04:44 AM PST
Top Contributor
679 Forum Posts

 I think this is his/her requirment.

****

 

Id have something like, the output is only ever high when only one of the inputs is high but not any more at the same time.

INPUT OUPUT

0000    LOW

0001   HIGH

0010   HIGH

0011   LOW

0100   HIGH

0101   LOW

0110   LOW

0111   LOW

1000  HIGH

1001  LOW

1010  LOW

1011  LOW

1110  LOW

1111  LOW

 

 **********

 

He only need 1 ouput



Re: comparing inputs

danaaknight posted on 27 Aug 2012 06:02 AM PST
Top Contributor
1773 Forum Posts

 

Here is where I think he was asking it to be 4 outputs, he misspeled "for", should have

been "four".

 

 

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

}

 

 

Maybe I mis-interpreted.

 

Regards, Dana.



Re: comparing inputs

H L posted on 27 Aug 2012 06:17 AM PST
Top Contributor
679 Forum Posts

 OK.

Hope this work.

  ROM 2% full. 528 out of 32768 bytes used (does not include absolute areas).

  RAM 2% full. 3 bytes used (does not include stack usage).

 

 



Re: comparing inputs

danaaknight posted on 27 Aug 2012 01:54 PM PST
Top Contributor
1773 Forum Posts

That takes care of one led, not four.

 

Regards, Dana.



Re: comparing inputs

H L posted on 27 Aug 2012 06:19 PM PST
Top Contributor
679 Forum Posts

As you've mentioned, the output can be modified by changing the lowest four bit of the array.



Re: comparing inputs

danaaknight posted on 28 Aug 2012 03:29 AM PST
Top Contributor
1773 Forum Posts

I missed the obvious. Also I got hung up on trying to minimize array size with bit structures,

which in your case, blew away their effectiveness with the simplicity of a simple BYTE array.

 

I will not be applying for coder of the year award anytime soon :)

 

Regards, Dana.



Re: comparing inputs

H L posted on 28 Aug 2012 04:54 AM PST
Top Contributor
679 Forum Posts

 So when can I have my 'eatme" sticker



Re: comparing inputs

danaaknight posted on 28 Aug 2012 11:02 AM PST
Top Contributor
1773 Forum Posts

Eat Me  Vinyl Sticker Another great Colorado sticker!  This one has a picture of a wicked little worm on a hoo

Here it is.

 

Regards, Dana.



Re: comparing inputs

H L posted on 29 Aug 2012 05:29 AM PST
Top Contributor
679 Forum Posts

 Thanks 



Re: comparing inputs

Cf_corp posted on 29 Aug 2012 07:49 AM PST
Top Contributor
74 Forum Posts

Hi Dana, hi Lleung,

Thanks for all your helpful replies, ive been messing about with the ledtwinky sample project you sent,

my apologies with my program requirements, Dana you were quite correct in assuming I said four inputs not instead of the "for" I typed.

Ive also been going through the project that Lleung has submitted, It seems quite interesting, Ive loaded up that project with the PSoC designer software and I went through the code and hardware setup,

Ive just been wondering, in the project youve got port 1.0-1.3 as output "strong" for the LED's and P 0.0 -0.3 as inputs, For that program to work should I use the p0.4-p0.7 as strong outputs to drive an output for the inputs on P0.0-P0.3. to test that code should I hook up P1.0-P1.3 to the 4 LED's on the PSoC EVAL1 board and go through the truth table?

thanks for all your help guys, you guys are the best!



Re: comparing inputs

danaaknight posted on 29 Aug 2012 08:43 AM PST
Top Contributor
1773 Forum Posts

"Ive just been wondering, in the project youve got port 1.0-1.3 as output "strong" for the LED's and P 0.0 -0.3 as inputs, For that program to work should I use the p0.4-p0.7 as strong outputs to drive an output for the inputs on P0.0-P0.3. to test that code should I hook up P1.0-P1.3 to the 4 LED's on the PSoC EVAL1 board and go through the truth table?"

 

Yes to all. If inputs ultimately coming from mechanical switches normall you would debounce them.

But if debounce realtively short, say a few hundred mS,  and input frequency of occurance low, you

may be able to ignore bounce issues.

 

Regards, Dana.



Re: comparing inputs

danaaknight posted on 29 Aug 2012 08:46 AM PST
Top Contributor
1773 Forum Posts

Another approach is a single input, each time it fires, go to the next address on

truth table and drive LEDs. Switch would have to be debounced. Or use a timer

set at 10 sec, and walk thru the table, incing the address in ISR.

 

Regards, Dana.



Re: comparing inputs

Cf_corp posted on 30 Aug 2012 04:45 AM PST
Top Contributor
74 Forum Posts

Hi Dana,

I ran through the truth table and as expected it works beautifully, the led illuminates according to the truth table in the program,

As mentioned before if i want the program to do as i described before, im thinking of having outputs and inputs connected like so.

Its not critical if only one led is used as an output,

Signal Output   Input   LED output

P0.4                   P0.0     P1.0

P0.5                   P0.1      P1.1 or P1.0 // maybe better if four leds are used for testing

P0.6                    P0.2     P1.2 or P1.0 // same reason as above

P0.7                   P0.3     P1.3 or P1.0 //as above

im thinking of having the program step through with the possibility of a very small delay between input checks

so basically the program would be like this:

// enable port 0.4

// check port 0.0

// IF port 0.0 high as it should be led (p1.0) off

// else  led (p1.0) on

//disable Port 0.4

//Small delay, repeat same code for other three inputs

Im thinking of just using the code Lleung kindly provided and just modifying the bits to be masked if thats how its done?

so it would be like;

port_1_data_shade &= 0xf0;

port_1_data_shade |= (ledrive[prt0dr & 0x0E]); // 0xOE for port 0.0 ?

any advice would be greatly appreciated,

thanks guys

 

 



Re: comparing inputs

H L posted on 30 Aug 2012 05:27 AM PST
Top Contributor
679 Forum Posts

 Here is what I can understand

if P0.4 connected to P0.0 and P0.4 is high P1.0 LED should be ON

if P0.5 connected to P0.1 and P0.5 is high P1.1 LED should be ON

if P0.6 connected to P0.2 and P0.6 is high P1.2 LED should be ON

if P0.7 connected to P0.3 and P0.7 is high P1.3 LED should be ON

I this correct?

 



Re: comparing inputs

Cf_corp posted on 30 Aug 2012 05:57 AM PST
Top Contributor
74 Forum Posts

Hi Lleung,

That is 100% correct



Re: comparing inputs

H L posted on 30 Aug 2012 06:10 AM PST
Top Contributor
679 Forum Posts

 The following is not complete solution, but a suggestion for you to write the program (something is still missing, but you don't need to worry about it for now, we'll do that 1 step a time)

unsigned char ucP1Shadow=0;

whiel(1)

{

/* set P0.4*/

....

/* Check P0.0, if is high, ucOutput = ucOutput | 0x1 */

......

/* set P0.5*/

 

/* Check P0.1, if is high, ucOutput = ucOutput | 0x2 */

....

...

...

/* after checking all 4 input and outputs */

port1data = ucP1Shadow & 0x0f

 

}



Re: comparing inputs

Cf_corp posted on 30 Aug 2012 06:13 AM PST
Top Contributor
74 Forum Posts

Hi Lleung,

Would it be better if i just do this for two inputs for now and if successful then just copy the procedure for the remaining two inputs?

thanks

 



Re: comparing inputs

H L posted on 30 Aug 2012 06:25 AM PST
Top Contributor
679 Forum Posts

some type, here is the corrected one

*****

unsigned char ucP1Shadow=0;

 

whiel(1)

{

/* set P0.4*/

....

/* Check P0.0, if is high,  ucP1Shadow  =  ucP1Shadow | 0x1 */

......

 

/* set P0.5*/

 

/* Check P0.1, if is high,  ucP1Shadow  =  ucP1Shadow  | 0x2 */

 

....

...

...

/* after checking all 4 input and outputs */

port1data = ucP1Shadow & 0x0f

 

}

*************

This is the most basic approach and is easy to understand,  there are other approaches that are faster and smaller code size, but do this one first.



Re: comparing inputs

H L posted on 30 Aug 2012 06:26 AM PST
Top Contributor
679 Forum Posts

 Yes, you can do one input first , then two input then all four input...

happy coding

 

 



Re: comparing inputs

Cf_corp posted on 30 Aug 2012 06:58 AM PST
Top Contributor
74 Forum Posts

Hi Lleung,

Thanks for your swift and helpful replies,

right now ive got something like

unsigned char ucP1Shadow=0;

void main (void){

while(forever){

PRT0DR |= 0X10; //turn on p0.4

ucP1Shadow = ucP1Shadow |= 0x1;

//same code repeated three more times for the other inputs

when you say:

/* after checking all 4 input and outputs */

port1data = ucP1Shadow & 0x0f

 

is  "port1data" equal to PRT1DR in my code?



Re: comparing inputs

H L posted on 30 Aug 2012 07:12 AM PST
Top Contributor
679 Forum Posts

Yes, "port1data" is PRT1DR.

There is something missing

 unsigned char ucP1Shadow=0;

void main (void){

while(forever){

PRT0DR |= 0X10; //turn on p0.4

 

/* check if port1.0 is high , if it is, then ucP1Shadow = ucP1Shadow |= 0x1 */

 

/* write your checking code here */

if ( ...)

{

  ...............

}

 

ucP1Shadow = ucP1Shadow |= 0x1;

 

 

 

 



Re: comparing inputs

Cf_corp posted on 12 Sep 2012 02:27 AM PST
Top Contributor
74 Forum Posts

Hi folks,

Apologies for not replying for a while, but i was busy for a while,

Ive been messing around with the suggested code Lleung gave me but Im still trying to get it to work properly, Im not sure how to get it to do exactly what I want.  would it be possible to see a sample bit of the code written for one of the port pins conditions, then I will try and modify it to work for the other three port pins? Sorry its just taking me a while to understand this correctly



Re: comparing inputs

Bob Marlowe posted on 12 Sep 2012 03:04 AM PST
Top Contributor
1768 Forum Posts

Hi folks,

I have to drop in again to point out a rather commonly used error. Again it is code that uses a read-modify-write sequence.

You wrote

PRT0DR |= 0x10;

Thi IS(!!) a read-modify-write sequence!! When an LED is connected to bit4 of port0 then the read will not always result in a "1" when the LED was turned on before. The read of a PRTxDR will reflect the actual voltage on the pin and not the value written to the port last.

 

So the correct sequence to handle that would be:

 

Initialize Shadow_Register

Output shadow_Register to port

.

.

Shadow_Register |= NewBitsToSet; // This IS read-modify-write, but only of the Shadow-Register

Output Shadow_Register to port

.

.

Shadow_Register &= ~BitsToClear; // similar to the above

Output Shadow_Register to port

 

I am not yet quite sure whether some of your problems are related to understand C-language or to understand PSoC architecture, but we can help you with both.

Again: when you are a newbee: Do not use ANY registers (even PORT0DR) to output a signal and use an LED-component instead which will provide you with some APIs to control the state and encapsulates the use of a shadow-register which you do not need for this any more.

For your input pins: You will have to read the port data-register but keep in mind that schitt-triggering and de-bouncing is a must for interfacing mechanical interfaces.

 

Bob

 



Re: comparing inputs

Cf_corp posted on 12 Sep 2012 03:39 AM PST
Top Contributor
74 Forum Posts

Hi Bob,

Thanks for your reply.

So its not recomended to use the PRT0DR etc, I was thinking of using an LED API instead. I was thinking it would simplify things as there would be no need for shadow registers etc and I remember seeing something like LED on LED OFF to control the output.  Currently I have four led's connected on outputs as you probably already are aware of, I may only have one output instead of four seperate outputs and will probably attach a flasher or buzzer through a transistor power a relay etc, will an LED moodule API still work for that?

Thanks



Re: comparing inputs

Bob Marlowe posted on 12 Sep 2012 04:54 AM PST
Top Contributor
1768 Forum Posts

Yes, of course an LED-module will work. You may specify the output beein active low or active high and when you name it accordingly there is no LED to be seen in your code, instead you'll see a Relay_On() or a Buzzer_On().

 

Bob



Re: comparing inputs

occupy posted on 19 Sep 2012 01:54 PM PST
Top Contributor
61 Forum Posts

happy coding  , and   happy  ending!!!






ALL CONTENT AND MATERIALS ON THIS SITE ARE PROVIDED "AS IS". CYPRESS SEMICONDUCTOR AND ITS RESPECTIVE SUPPLIERS MAKE NO REPRESENTATIONS ABOUT THE SUITABILITY OF THESE MATERIALS FOR ANY PURPOSE AND DISCLAIM ALL WARRANTIES AND CONDITIONS WITH REGARD TO THESE MATERIALS, INCLUDING BUT NOT LIMITED TO, ALL IMPLIED WARRANTIES AND CONDITIONS OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT OF ANY THIRD PARTY INTELLECTUAL PROPERTY RIGHT. NO LICENSE, EITHER EXPRESS OR IMPLIED, BY ESTOPPEL OR OTHERWISE, IS GRANTED BY CYPRESS SEMICONDUCTOR. USE OF THE INFORMATION ON THIS SITE MAY REQUIRE A LICENSE FROM A THIRD PARTY, OR A LICENSE FROM CYPRESS SEMICONDUCTOR.

Content on this site may contain or be subject to specific guidelines or limitations on use. All postings and use of the content on this site are subject to the Terms and Conditions of the site; third parties using this content agree to abide by any limitations or guidelines and to comply with the Terms and Conditions of this site. Cypress Semiconductor and its suppliers reserve the right to make corrections, deletions, modifications, enhancements, improvements and other changes to the content and materials, its products, programs and services at any time or to move or discontinue any content, products, programs, or services without notice.

Spec No: None; Sunset Owner: GRAA; Secondary Owner: RAIK; Sunset Date: 01/01/20