- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Greetings to all,
one switch control the led On at time one press
next press the LED will OFF
please help how can i create it.
switch is push type of switch.
following is the module to be modify.
// input to display is switch press.
void display(BYTE switsh_state)
{
LED( (switch_state & 0x01) );
}
Pramod.
- Labels:
-
PSoC 1
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
You have several different states, so best would be to use a state-machine approach:
State 1 - Idle, Switch is released, LED is Off
State 2 - Active Switch is pressed, LED is On
State 3 - Waitng for second press, switch is released, LED stays On
State 4 - Active, Switch is pressed again, LED Is Off
The next state, when the switch is released is State 1.
... and do not forget to debounce your switch.
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 have attached code for refrence, code successfully making LED On after first press, but then it continusly remain on their is no effect of second press. I try out many changes but unable to make LED OFF at second press.
Switches are capsense switches. Can any one suggest suitable changes.
Pramod.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Just looking at part of code, this -
if (sensor_state & 0x01==1)
be typed as
if ( ( sensor_state & 0x01 ) == 1 )
per precedence order in C
- 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 usually I try to avoid logical comparisions in C, so I would have written
if ( sensor_state & 0x01 ) ....:
since I am not interested whether the result is equal to one, but if it is not equal to zero which is per definition TRUE
As a side-effect when ANDing with a bit-mask and I am interested in any of the masked bits set the syntax would be the same:
if ( sensor_state & BitMask ) ....:
Happy coding
Bob
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Fear not Bob -
if ( ( sensor_state & 0x01 ) == 1 )
& is a C bitwise operator
== is a C Relational and Equality Operator
So no logical test occured !
But then we both used an "if", surely that is a logical ? But C seems to think that
is a statement.
😞 I am so worried now, all that code I have out in the field...........
Regards, Dana.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I would call "==" ,"!=",>" and so on to be logical operators in opposite to a bitwise operators as "^","~" and so on.
Bob
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Forgot to mention that logical operator's results are always "FALSE" or "TRUE" or rather 0x00 and 0x01
Bob
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I used these references -
http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B
http://msdn.microsoft.com/en-us/library/z68fx2f1.aspx
and my C book. http://zanasi.chem.unisa.it/download/C.pdf
Regards, Dana.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thanks to all.
Manage to solve above problem.
Pran.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
You are always welcome, Pran.
@Dana
We are probably discussing about language differences and not C-syntax, My (British) C-book shows: publications.gbdirect.co.uk/c_book/chapter3/logical_expressions.html
Bob