let P1 is a 8 bit
for masking we can use following instruction in PIC controller
P1 = P1 & 0xf0;
In XMOS (xc) how to write it?
jags
How to mask a port?
-
- Experienced Member
- Posts: 117
- Joined: Tue Oct 18, 2011 3:28 pm
-
- XCore Expert
- Posts: 754
- Joined: Thu Dec 10, 2009 6:56 pm
You will need a variable to buffer the port state.
i.e.
For better performance you want to skip the reads from the port and just always write the changes to p_state and then write p_state to the port.
i.e.
Code: Select all
unsigned p_state; // port state buffer
// set bit 0
p :> p_state;
p_state |= 0x01;
p <: p_state;
// clear bit 0
p :> p_state;
p_state &= ~0x01;
p <: p_state;
-
- XCore Legend
- Posts: 1274
- Joined: Thu Dec 10, 2009 10:20 pm
Also if you are waiting for a specific bit pattern inputs (or just single bit of a multibit port) in XC you can use a select with a pins equal guard:
Just remember you can't put the same port (p8a in this case) in any of the other case's of the select. If you need to detect inputs on multiple pins then just do a select in any port change (using pins not equal to guard) with a switch to do the masking.
regards
Al
Code: Select all
select
{
case p8a when pinseq(Ox01) :> pval :
//Detected LS bit high, do stuff
break;
default:
// What todo otherwise
}
Code: Select all
select
{
case when pinsneq(last) :> current :
// Whenever port value changes (any bit change)
switch(current ^ last)
{
case Ox01 :
// do bit 0 stuff
break;
case Ox02 :
// do bit 1 stuff
break;
......
default : //Otherwise stuff
}
last = current;
break;
}
Al
-
- Member++
- Posts: 22
- Joined: Sun Nov 07, 2010 6:33 pm
Unfortunately this isn't quite right - an input on a bidirectional port will reverse the direction of the port, tristate the pins and potentially input the wrong value. What you need in this situation is a PEEK:Bianco wrote:Code: Select all
unsigned p_state; // port state buffer // set bit 0 p :> p_state; p_state |= 0x01; p <: p_state; // clear bit 0 p :> p_state; p_state &= ~0x01; p <: p_state;
Code: Select all
// set bit 0
p <: peek(p) | 0x01;
// clear bit 0
p <: peek(p) & (~0x01);
-
- XCore Expert
- Posts: 754
- Joined: Thu Dec 10, 2009 6:56 pm
You are right sir!waluigi wrote:Unfortunately this isn't quite right - an input on a bidirectional port will reverse the direction of the port, tristate the pins and potentially input the wrong value. What you need in this situation is a PEEK:Bianco wrote:Code: Select all
unsigned p_state; // port state buffer // set bit 0 p :> p_state; p_state |= 0x01; p <: p_state; // clear bit 0 p :> p_state; p_state &= ~0x01; p <: p_state;
Code: Select all
// set bit 0 p <: peek(p) | 0x01; // clear bit 0 p <: peek(p) & (~0x01);
monday mornings...
-
- Experienced Member
- Posts: 117
- Joined: Tue Oct 18, 2011 3:28 pm
Is peek(p) is valid of a out port?
jags
jags
-
Verified
- XCore Legend
- Posts: 1163
- Joined: Thu Dec 10, 2009 9:20 pm
- Location: Bristol, UK
Yes, any portjagspaul wrote:Is peek(p) is valid of a out port?
jags
Technical Director @ XMOS. Opinions expressed are my own