Contatto
MCP 4 – User's manual
DUEMMEGI
3.2.14- BMASK(x)
The
BMASK(x)
function returns a 16-bit number having, in its binary format, only one bit set to 1 at the
position of (x-1)%16. This notation means (x-1) module 16 and it is equivalent to the remainder of the division
of (x-1) by 16. The
BMASK(x)
function is therefore a mask which can be useful for bit operations.
The script in the following example calls 4 times a subroutine which must set or reset a virtual point if the
value of a register is respectively greater or less than a constant value; since both the virtual point and the
register and the constant value change at each call, then these parameters have to be passed to the
subroutine.
Since the virtual point has to be written
, then this parameter should be passed as reference,
but this is not allowed because it is a bit (see paragraph SUBROUTINEs and FUNCTIONS).
This is a typical case requiring the
BMASK(x)
function. Therefore the calling passes to the subroutine the
address of the Word containing the virtual point (
WORD
(
Vn
)
) and the mask allowing to identify, in the Word,
the position of the bit related to that virtual point (
BMASK
(n)
).
To set the virtual point, the subroutine executes the OR between the Word containing the point and the mask
(which, as said, contains only one bit set to 1 at the position of the bit related to the desired point).
To reset the virtual point, the subroutine executes the AND between the Word containing the point and the
complement of the mask (which therefore will contain only one 0 at the position of the bit related to the
desired point).
script
1
trigger
= 1
call
TEST(
R0
, 50,
WORD
(
V49
),
BMASK
(49))
call
TEST(
R1
, 100,
WORD
(
V50
),
BMASK
(50))
call
TEST(
R2
, 150,
WORD
(
V51
),
BMASK
(51))
call
TEST(
R3
, 200,
WORD
(
V52
),
BMASK
(52))
exit
sub
TEST(REGIN, KAPPA, [WVIRT], MSK)
if
REGIN > KAPPA
then
WVIRT = WVIRT | MSK
// set virtual point
else
WVIRT = WVIRT & !MSK
// reset virtual point
endif
endsub
endscript
The
BMASK(x)
function can be applied to any other bit parameter; the following example is very similar to the
previous one, but on the contrary the subroutine switch ON and OFF real outputs instead of virtual points.
script
1
trigger
= 1
call
TEST(
R0
, 50,
WORD
(
O1.5
),
BMASK
(5))
call
TEST(
R1
, 100,
WORD
(
O1.6
),
BMASK
(6))
call
TEST(
R2
, 150,
WORD
(
O1.7
),
BMASK
(7))
call
TEST(
R3
, 200,
WORD
(
O1.8
),
BMASK
(8))
exit
sub
TEST(REGIN, KAPPA, [WOUT], MSK)
if
REGIN > KAPPA
then
WOUT = WOUT | MSK
// output ON
else
WOUT = WOUT & !MSK
// output OFF
endif
endsub
endscript
Page 44 of 87
Rel.: 1.2 October 2018
DUEMMEGI
s.r.l. - Via Longhena, 4 – 20139 MILANO
Tel. 02/57300377 - Fax 02/55213686 –
www.duemmegi.it