Why C codes generated by Matlab Coder contain strange dataor variables like 0U?

greetings community
I am working on the C codes generated by Simulink, and when I generate the codes unknown variables appear. For example in the ".c" code, it appears in the 0U code, and in the libraries and other codes, I only find the following statement where that value appears.
On the other hand, in the file ".c" 0U appears as a value to compare (all the comparisons [<,> =, etc] that I make between the variables is with respect to zero). Is it normal for these situations to arise?
Below I show part of the function of matlab.
Next, we have part of the generated code
I appreciate any information, see if I can trust the generated code and take the code to TI C2000 MCU

 Accepted Answer

In the two subtractions that you do, at least one of the variables is uint16 datatype. Operations on uint16 are defined as converting the numbers to double, then doing the operations, then converting to uint16. Conversion to uint16 is defined as 0 for negative values, and 65535 for values larger than that, and otherwise the actual value.
If you follow the code sequence you can see it testing against 0 and 65536 and imposing those as limits.
0U is not an identifier name: it is zero with suffix U which indicates that it is 0 with datatype unsigned int

5 Comments

Thanks Walter
So, as mentioned, I can say that when reference is made to 0U they are negative numbers (values less than zero).....
tmp_2 has been created as an unsigned 16 bit integer in the code. The test
if (tmp2 <= 0U)
will only be true if tmp2 is exactly 0.
To me the MATLAB code appears to be written without paying attention to the fact that uint16 values are involved and so that the operations are returning uint16. I suspect that there should be some double() in a place or three so that real values are returned.
Yes Walter,
the doubles values are effectively defined. I tell you, the control algorithm must perform mathematical operations, for example, dP and dV can be positive or negative values. I must also save the input values to later compare current values with previous values. That is, the flow chart of the control algorithm is shown in the figure, the initial values are 0 for Vp and Pp respectively.
(attached ".slx" file) In this file is the algorithm control function. When I click the button to generate the code C, an error appears while writing this variable. TO SOLVE THIS ERROR, MODIFY THE CODE AND DECLARE THE Vp and Pp VARIABLES as doubles, like this
This assignment writes a 'uint16' value into a 'double' type. Code generation does not support changing types through assignment. Check preceding assignments or input type specifications for type mismatches. State 'Save_Data'"Pp"
I do not know how to solve this problem and that the generated code is optimal, I am not so good with programming. I appreciate any help or information with this problem
Change
function duty = MPPT_algorithm(Vpv,Ipa)
to
function duty = MPPT_algorithm(Vpv_u16,Ipa_u16)
Vpv = double(Vpv_u16);
Ipa = double(Ipa_u16);

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!