How to cast two uint8 into int16 in Simulink?

48 次查看(过去 30 天)
I have two uint8 as input of my system. Together, they represent an int16. How can I cast these two values into an int16? In MATLAB, I would convert them to uint16, shift the bits of the first one 8 to the left and the use bitwise OR to get the bit representation and finally apply a typecast to reinterpret the bits (with respect to two's complement). In MATLAB it works:
raw = uint8([255,255])
raw = uint16(raw)
myint16 = typecast(bitor(bitshift(raw(1),8), raw(2)), 'int16') %myint16 = -1
How do I implement this in Simulink? I am restricted to the slcilib (for Code Inspector Compliance). That's why I cannot use the typecast function. Finally, I found a solution, but it's a really complicated if-then-else stuff, with around four casts. Is there another easy way?

回答(1 个)

Fangjun Jiang
Fangjun Jiang 2015-8-31
1. Use the ShiftArithmetic block in Simulink->Logic and Bit Operations.
2. Multiply the first number with 256 and then add to the second number.
  5 个评论
Walter Roberson
Walter Roberson 2015-8-31
I am not sure of the purpose of the bitwise OR ?
Markus
Markus 2015-9-1
编辑:Markus 2015-9-1
@Fangjun Jian: This Simulink implementation was my first approach, too. However, it throws an overflow warning although the results are right. With more strict diagnostics, it doesn't simulate.
@Walter Roberson: The problem ist, that implemented in Simulink with the slcilib, this becomes a more complicated model. You need enabled subsystems to avoid calculation of branches where the overflow could occur. In the end, I get a code looking like that:
Subsystem_B->RelationalOperator = (Subsystem_B->BitwiseOperator > ((uint16_T)
56 32767U));
57
61 if (Subsystem_B->RelationalOperator) {
65 Subsystem_B->Subtract = (uint16_T)(((uint32_T)Subsystem_B->BitwiseOperator)
66 - ((uint32_T)((uint16_T)1U)));
69 Subsystem_B->BitwiseOperator1 = (uint16_T)(~Subsystem_B->Subtract);
70
72 Subsystem_B->DataTypeConversion = (int16_T)Subsystem_B->BitwiseOperator1;
73 }
74
78 Subsystem_B->LogicalOperator = !Subsystem_B->RelationalOperator;
79
83 if (Subsystem_B->LogicalOperator) {
*/
85 Subsystem_B->DataTypeConversion3 = (int16_T)Subsystem_B->BitwiseOperator;
86 }
87
91 if (Subsystem_B->BitwiseOperator > ((uint16_T)32767U)) {
93 Subsystem_Y->Out1 = Subsystem_B->DataTypeConversion;
94 } else {
96 Subsystem_Y->Out1 = Subsystem_B->DataTypeConversion3;
97 }
98
100 }
Actually in manua code, it should just be sth. like myint16value = *(signed short *)&myuint16value;

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Simulink Functions 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by