This thread has been locked.
If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.
Hi,
This is the first time I've had this issue with C2000Ware - I am calculating an error voltage from two sensors, to be used in a controller code within the DCL Library.
However, with the simple equation (Voltage A - Voltage B) = Error Voltage, if Voltage B exceeds the value of Voltage A, and error voltage becomes negative, the unsigned 16-bit integer tends to show very close to its maximum value as opposed to what I would expect, which is 0. Here is an example:
As soon as the term that is subtracted exceeds the reference voltage, the debugger shows that the unsigned integer becomes extremely high, close to the maximum value for a 16-bit integer, completely messing up my calculations from that point forward. This does make sense because of how unsigned integers work, but I am wondering what is the best way to deal with this. For now I have an "if" condition that assigns the voltage as zero if voltage B is larger than voltage A. This actually can't happen during normal operation but it would be interesting to understand how to deal with this.
I am using uint16_T because I send the error voltage over IPC to CPU2. I suppose I could use float values to calculate the error, and cast to unsigned int before using the IPC and sending to CPU2 but would this be the most efficient way of dealing with this issue? I would just have to convert it back to a float in CPU2 anyway as the errors are then used in the CLA and the DCL Library.
Best regards,
Joel
Hi Joel
However, with the simple equation (Voltage A - Voltage B) = Error Voltage, if Voltage B exceeds the value of Voltage A, and error voltage becomes negative, the unsigned 16-bit integer tends to show very close to its maximum value as opposed to what I would expect, which is 0.
Actually I would expect the value which is being displayed by the debugger. That is because the Expressions view has the type as unsigned int. Hence if you look at the actual contents of memory for the variable, you will see that the Expressions view is correctly displaying the value as an unsigned int.
If you want to see it as a negative number, you can cast the variable in the Expressions view as an int like (int)resError
You should use the proper type (like a long) and then do the conversion as needed
Thanks
ki
Hi KI,
After some research I realised where I had been going wrong, your comment outlines it exactly. In my code for now I kept it as unsigned int and just clamped the value to zero if the Voltage B was larger than Voltage A.
Regarding the float/long idea - I had thought about this, but what if I need to send the value over IPC and HAVE to then cast this as an unsigned 16-bit integer. Surely if I am using floats/longs, and the voltage becomes negative, and I try to cast that to an unsigned 16-bit integer, I will just have the exact same issue. I don't know if the IPC enables float communications but I assume it does not from my experience.
Best regards,
Joel