Page 1 of 1

How to mask a port?

Posted: Mon Nov 21, 2011 9:19 am
by jagspaul
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

Re: How to mask a port?

Posted: Mon Nov 21, 2011 1:15 pm
by Bianco
You will need a variable to buffer the port state.

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;

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.

Re: How to mask a port?

Posted: Mon Nov 21, 2011 2:45 pm
by Folknology
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:

Code: Select all

select 
{
  case p8a when pinseq(Ox01) :> pval :
    //Detected LS bit high, do stuff
    break;
   default:
     // What todo otherwise
}
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.

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;
}
regards
Al

Re: How to mask a port?

Posted: Mon Nov 21, 2011 3:45 pm
by waluigi
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;

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:

Code: Select all

// set bit 0
p <: peek(p) | 0x01;

// clear bit 0
p <: peek(p) & (~0x01);

Re: How to mask a port?

Posted: Mon Nov 21, 2011 3:51 pm
by Bianco
waluigi wrote:
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;

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:

Code: Select all

// set bit 0
p <: peek(p) | 0x01;

// clear bit 0
p <: peek(p) & (~0x01);
You are right sir!
monday mornings...

Re: How to mask a port?

Posted: Tue Nov 22, 2011 8:34 am
by jagspaul
Is peek(p) is valid of a out port?

jags

Re: How to mask a port?

Posted: Tue Nov 22, 2011 10:38 am
by Ross
jagspaul wrote:Is peek(p) is valid of a out port?

jags
Yes, any port