- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
Solved! Go to Solution.
- Labels:
-
PSoC 1
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
looks like you reply when i was typing my reply
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
whoops sorry
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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 */
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Is your software works now? If not, have you try the LONG routine I proposed?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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 🙂
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
is it along the lines of:
if(((PRT0DR & 0x01) == 0x01) || ((PRT0DR & 0x02) == 0x02)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
you can do that too.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Ill try those changes and report back, basically ill have:
if(((PRT0DR & 0x01) == 0x01) || ((PRT0DR & 0x02) == 0x02)
{
//OUTPUT HIGH
}
else
{
// OUTPUT LOW
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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).......
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Just one step a time.
Does your code works after the modfication suggested?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Good to hear that,
Needs to go to bed now. you can post your requirment here and shall check that tomorrow morning. 🙂
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Forgot to add, in my case LED blinks when the two inputs not the same.
Regards, Danma.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.