PSoC1 Eval Kit

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

cross mob
ChDe_285186
Level 4
Level 4

 Bob or Dana,

   

Do either of you two wise pillars of knowledge have a PSoC1 Eval kit?

   

I have a strange problem with my current project.

   

Chris 

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

Yes, I've got one. What is your problem?

   

 

   

Bob

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

The lesser pillar of knowledge also has one.

   

 

   

Still larning, Dana.

0 Likes
lock attach
Attachments are accessible only for community members.
ChDe_285186
Level 4
Level 4

 Bob,

   

 I have a strange problem with  an if statement.

   

I have attached the project. It checks P0,0 for a logic '0' and if the value "power " is less than 255 it increments that value by 1.

   

It also checks P0,1 for a logic '0' and if the "Power" is greater than 0 it decrements the value by 1. It displays these changes on the LCD.

   

Look at line 46 for the increment and it works fine.

   

Now look at line 55. That also works.

   

 However If I change line 55 to the same format as line 46   "if ((~PTR0DR & 0x02) & (Power>0))" recompile , rebuild and program, the decrement fails?

   

 

   

Can you see if you can reproduce this anomoly?

   

Chris

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

Archive is broken, please re-post.

   

@Dana: why "lesser pillar"? Just because I am for 5 minutes on place# 1 and you on place# 2?? That's going to change back within a matter of hours...

   

 

   

Bob

0 Likes
lock attach
Attachments are accessible only for community members.
ChDe_285186
Level 4
Level 4
        Bob, Dana try this from IE   
0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

And btw: Did you intend to write &&(Power < 0)

   

 

   

Bob

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

Please re-create the .zip, still archive is broken

   

 

   

Bob

0 Likes
ChDe_285186
Level 4
Level 4

 Actually no. The line 46 works without the "ogical and"

   

Chris

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

PRT0DR & 0x02 may deliver 0x02 when pin is high. (Power < 0) will deliver 0 or 0x01. Binary ANDed will always result in FALSE (0x00)

   

so better do a logical And (&&)

   

 

   

Bob

0 Likes
lock attach
Attachments are accessible only for community members.
ChDe_285186
Level 4
Level 4
        Don't know what happened there. This should be OK 316kB.   
0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

PTR0DR & 0x01 will deliver 0x01 when pin0 is high. Binary anded with 0x01 (which is "TRUE") will result in 0x01 which is still TRUE.

   

Use the logical AND as requred since you use a logical comparision on one side.

   

 

   

Bob

0 Likes
ChDe_285186
Level 4
Level 4

 the expression is (~PTR0DR & 0x01) & (Power<255) for the increase.That checks that the inverse value of the pin state is checked. ie because the pin is held high with a pullup we are looking for a low or false to make the statement active AND we want the Power to be less than 255 in order to increment. If the pin is still hihg or the Power value =255 then we do nothing.

   

Chris

0 Likes
lock attach
Attachments are accessible only for community members.
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

Corrected

   

 

   

Bob

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

There are two choices, accept my hints or don't, but do not argue with a compiler. Handbook is here publications.gbdirect.co.uk/c_book/

   

 

   

Bob

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

If you do Read modify Write operations on a port you need to use

   

shadow register. Discussed here -

   

 

   

    

   

          

   

http://www.cypress.com/?rID=2900     AN2094 - PSoC® 1 - Getting Started with GPIO

   

 

   

Regards, Dana.

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

@Dana: why "lesser pillar"? Just because I am for 5 minutes on place# 1 and you on place# 2?? That's going to change back within a matter of hours...

   

 

   

Bob, I did not know there is a # 1 position. Wow, does one get crowned, robes, jewel encrusted

   

shoes ? I am so looking forward to Medieval that. How does one find out if they are # 1, error rate,

   

knowledge, customer interface.......

   

 

   

Should this discussion really be in a customer request for assistance thread ? Should we be apologizing

   

to Chris for inappropriate thread content ? I think so. My apologies Chris, I will stay on topic.

   

 

   

Dana.

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

Dana, I did not name you "Lesser Pillar of Knowledge" and I was questionig why you did that. And maybe I've forgotten to put a smiley onto that post, forgive me for getting off the point (which is undoubtfully the forgotten "&&")

   

 

   

Bob

0 Likes
ChDe_285186
Level 4
Level 4

 Hmm,.  Did either of you actually try to run the code as written. Because it works. I was not asking for your corrections just that you try to verify the results that I was seeing.

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

No, I did not run the code. For me it is quite clear that the code behaves exactly as you described. the double "if" for the second part does exactly what you wanted to do, it replaces a logical "AND". So what question remains for you?

   

 

   

Bob

0 Likes
ChDe_285186
Level 4
Level 4

 The original question was why does the first if statement with the two parts "&"ed (46) work when exactly the same type of expression later (55) will not work. I must separate the parts into two if statements at (55) for a sucessful step.

   

Chris

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

Chris,

   

I tried to explain the difference between "&" and "&&"

   

The "single" operators are named "bit wise" operators and that is exactly the way they work on bytes, words and so on. They do not work as the "double" operators which are named "logical" operators and they are working on boolean variables which are usually 8 bit values. Apart from the "double" operators there exist two NOT operators: The logical "!" and the bit-wise "~".An Example: !5 is equal to 0x00 (because 5 is true per definition and !TRUE is equal to FALSE, which is 0 per definition) while the expression ~5 is equal to ~0b000 0101 which is 0b1111 1010.

   

So coming back to your original statements

   

The expression (~PRT0DR & 0x02) will be handled bit-wise and will give the result 0x00 or 0x02.

   

The expression (Power > 0) is a logical expression and will deliver 0x00 (false) or 0x01 (true)

   

Now you perform a bit-wise AND of these two values which will ALWAYS result in 0x00 which is false per definition.

   

0x02 bit-wise ANDed with 0x01 will always be 0x00

   

When you use the logical operator "&&" the situation looks different:

   

0x02 is per definition true, anded with another value of "true" will give the result you expect to happen.

   

 

   

Bob

0 Likes
ChDe_285186
Level 4
Level 4

OK. Its the ~ that confused me.  From my reading I determined that the ~ would deliver the conjugate of the expression. If the expression was TRUE then in the conjugate  we would need a FALSE to satisfy the expression. I was not aware that the ~ also forced a bitwisw evaluation of the expression.

   

And i have just run a quick test to satisfy myself that I understand.

   

I could use the ~ and the && or I could use the ! and the &. Both formats work equally as well in the test project.

   

Chris

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

There is another common pitfall I have seen rather often:

   

Since FALSE is defined to be 0 (zero) TRUE is defined to be !FALSE (which both you may #define yourself)

   

So if(b == FALSE) will always run as expected, but

   

if(b == TRUE) will not. Even the equivalent to the first if(!(b == TRUE)) will go amiss.

   

The reason for this is that the compiler has an internal representation of TRUE (which is usually 1) Comparing any value to TRUE will compare it to 1 which usually is not what you want.

   

if(b) questions b !=0 (as the definition for TRUE and FALSE is) and can easily be used without the compareision with "=="

   

 

   

Bob

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

A useful C book attached.

   

 

   

Don't forget to use shadow registers....

   

 

   

Regards, Dana.

0 Likes