필터 지우기
필터 지우기

How to read and write to an upper half or a specific bit from a decimal number in Simulink?

조회 수: 8 (최근 30일)
Hi.
Can you please tell me how to read and write integer number to the upper part of the register and read and write only one bit?
I can read only the whole register as an integer, but I need only part of it (TBPHS value). Similarly, how to write integer number only to an upper-half?
For the second case, how to read and write value for PHSDIR (one bit) if I am only able to read the whole TBCTL register?
Thank you in advance.
Edit:
For reading, I can use Extract Bits block.
What to use to write to upper-half or specific bit?

채택된 답변

Andy Bartlett
Andy Bartlett 2022년 11월 22일
편집: Andy Bartlett 2022년 11월 22일
Bitwise AND, OR, XOR, NOT (aka complement), shift, ...
are the ways commonly used to set, clear, or otherwise manipulate bits within a larger container.
For example, this code is forcing the first and last bit of a variable to be set to 1, using a mask and bitwise OR.
nbits = 16;
mask = uint16( sum( 2.^[0,nbits-1] ) );
n = 5;
u = uint16( randi([0,2^nbits-1],1,n) );
y = bitor(u,mask);
for i=1:n
fprintf('Input %s\n',dec2bin(u(i),nbits))
fprintf('Mask %s\n',dec2bin(mask,nbits));
fprintf('bitor output %s\n',dec2bin(y(i),nbits))
fprintf('\n')
end
Input 0001011010111100
Mask 1000000000000001
bitor output 1001011010111101
Input 1111011100110011
Mask 1000000000000001
bitor output 1111011100110011
Input 1101101010001100
Mask 1000000000000001
bitor output 1101101010001101
Input 1011111110010110
Mask 1000000000000001
bitor output 1011111110010111
Input 0010011111001001
Mask 1000000000000001
bitor output 1010011111001001
So read the whole register. Perform the bitwise operations to achieve the desired change. Then write whole integer back to the whole register.
Please note the MATLAB code above is only for example of the concept. Please use Simulink Bitwise blocks, Bitset block, etc.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Simulink Functions에 대해 자세히 알아보기

제품


릴리스

R2018a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by