Inout/output with variable mask

For C and ASSEMBLY users to post questions and code snippets for programming in C and ASSEMBLY. And for any other C or ASM course related questions.

Moderators: Benj, Mods

User avatar
Jan Lichtenbelt
Posts: 785
Joined: Tue Feb 17, 2009 8:35 pm
Location: Haren GN, the Netherlands
Has thanked: 122 times
Been thanked: 259 times
Contact:

Inout/output with variable mask

Postby Jan Lichtenbelt » Thu Nov 12, 2015 1:37 pm

I want to get the input (or set the output) with a variable mask instead of a fixed mask. This last one is possible in Flowcode. But, as far as I know, the variable mask is not possible with Flowcode. The solution in C-code seems simple. For input use the FCP_SET() and for output use FCP_GET(), with the third variable the mask byte.

But where can I find the meaning of the other paramters of these functions?

With kind regards

Jan Lichtenbelt

User avatar
Benj
Matrix Staff
Posts: 14150
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4330 times
Been thanked: 4078 times
Contact:

Re: Inout/output with variable mask

Postby Benj » Thu Nov 12, 2015 3:56 pm

Hello Jan,

You can find the definitions of those functions in the FAMILY_CAL_IO.h file in the CAL directory.

This is the prototype.

FCP_GET(Type, Port, Mask, Shift)
FCP_SET(Type, Port, Mask, Shift, Source)

Type:

B - Byte - Port Byte - Mainly on multi byte devices 16-bit PICs, ARM etc
M - Masked
F - Full Port

A simple way of allowing for data and mask is like this.

Mask = 0x18
Data = 0x08

Reg = (Reg & ~Mask) | (Mask & Data)

A more robust way to do the same thing but using absolute values might be to use something like this.

Shift = 3
Mask = 0x3
Data = 0x1

Reg = (Reg & ~(Mask << Shift)) | ((Mask & Data) << Shift)
These users thanked the author Benj for the post (total 2):
Jan Lichtenbelt (Thu Nov 12, 2015 4:21 pm) • medelec35 (Thu Nov 12, 2015 4:43 pm)
Rating: 11.11%
 

User avatar
Jan Lichtenbelt
Posts: 785
Joined: Tue Feb 17, 2009 8:35 pm
Location: Haren GN, the Netherlands
Has thanked: 122 times
Been thanked: 259 times
Contact:

Re: Inout/output with variable mask

Postby Jan Lichtenbelt » Thu Nov 12, 2015 8:52 pm

Dear Ben

I tried to set the Ports B0, B1 etc to 1, using the variable shift=0,1.2..7

Output_1_to_B_Ports.jpg
Output_1_to_B_Ports.jpg (7.9 KiB) Viewed 17387 times


But it does not works. What to do?

With kind regards

Jan

User avatar
LeighM
Matrix Staff
Posts: 1844
Joined: Tue Jan 17, 2012 10:07 am
Has thanked: 429 times
Been thanked: 612 times
Contact:

Re: Inout/output with variable mask

Postby LeighM » Fri Nov 13, 2015 9:44 am

Hi Jan,
You are nearly there, just need to adjust the mask as well ...

Code: Select all

FCP_SET(M,B,(1<<FCV_SHIFT),FCV_SHIFT,(0xFF));
These users thanked the author LeighM for the post:
Jan Lichtenbelt (Fri Nov 13, 2015 10:57 am)
Rating: 5.56%
 

User avatar
Jan Lichtenbelt
Posts: 785
Joined: Tue Feb 17, 2009 8:35 pm
Location: Haren GN, the Netherlands
Has thanked: 122 times
Been thanked: 259 times
Contact:

Re: Inout/output with variable mask

Postby Jan Lichtenbelt » Fri Nov 13, 2015 10:58 am

Hi ...

It works. Please find an example with running light. I want to use it for an other application.

With kind regrads

Jan Lichtenbelt

Test_variable_Port_index.fcfx
(4.59 KiB) Downloaded 879 times