- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Labels:
-
PSoC 1
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Yes, I've got one. What is your problem?
Bob
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
The lesser pillar of knowledge also has one.
Still larning, Dana.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- 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
And btw: Did you intend to write &&(Power < 0)
Bob
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Please re-create the .zip, still archive is broken
Bob
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Actually no. The line 46 works without the "ogical and"
Chris
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- 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
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- 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
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
@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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content