Displaying negative numbers
J^3
Posts: 121
Hello,
I am working on developing an object for the MS5534 absolute pressure sensor from Intersema. At this point I am trying to develop a demo program demonstrating the interface to the object. When trying to display a negative temperature using the 'dec' method in 'FullDuplexSerialPlus' I am having some trouble.
I have tried complementing the result, and then adding one to it to get the two's comp of the result. This has not worked, so I am missing something here. Attached is the archive of my work. If anyone has the time to look it over, and explain what is wrong with what I am attempting, I would appreciate it.
Thank you,
J^3
I am working on developing an object for the MS5534 absolute pressure sensor from Intersema. At this point I am trying to develop a demo program demonstrating the interface to the object. When trying to display a negative temperature using the 'dec' method in 'FullDuplexSerialPlus' I am having some trouble.
I have tried complementing the result, and then adding one to it to get the two's comp of the result. This has not worked, so I am missing something here. Attached is the archive of my work. If anyone has the time to look it over, and explain what is wrong with what I am attempting, I would appreciate it.
Thank you,
J^3
Comments
temp := -temp
?
To short? To easy? ;o)
Thanks for the suggestion, but I just tried that, and it still don't work. I get -4_XXX_XXX?
Thanks,
J^3
if (temp & $8000)
temp := -temp
?
temp := -(temp & $7fff)
any where it shows values declared as double.
You are going to need floating point arithmetic or learn how to scale with integers
I tried this code on my PPDB
and the output to PST.EXE seems to be fine
the sign gets inverted and the method dec sends it the right way.
Did you try it yourself on pins 31,30 mode 0?
I guess it is a problem in the PASM-driver
maybe you can try to analyse the difference when using the bin-method of FDX+ that shows the binary representation of the value?
best regards
Stefan
Oh ... ok ... I see.
I think your whole calculation is wrong. You have to extend the temperature from 16 bit to 32 bit BEFORE you do all the calculations. Then temp will already be negative for a negative temperature.
And I found the ~~ operator, as prefix it extends a 16bit signed number to a 32bit signed number ... I guess.
I think your right. After reviewing the algorithm in the data sheet for calculating temperature, the variable 'dt' is where a negative temperature would cause an effect. I think this is where my problem is. Thanks for your help.
Thank you for your help. I found my problem. I was using the shift right operator (>>) to do division by powers of two, when I should have been using the arithmetic shift right operator (~>) to preserve the sign. After updating my methods with this operator everything appears to be working well.
Thanks,
J^3