Jump to content

What is Signed Hex - Arduino help!


blinky

Recommended Posts

Folks,

 

I am building a Moonlight compatible focuser and so far it works perfectly!  I am now trying to add a temperature reading but am having no luck!  According to the spec I found online the reading needs to be a Signed Hexadecimal number, the code below gives a reading about twice what it should be, anybody able to shed some light on this?

 

  int temperature = sensors.getTempCByIndex(0);
     Serial.print(temperature, HEX);
     Serial.println("#");

 

From the Spec , halfway down this page: http://www.indilib.org/media/kunena/attachments/1/HighResSteppermotor107.pdf 

Command      Response              Description

GT                XXXX                     Get the Current Temperature, Signed Hexadecimal

Link to comment
Share on other sites

On the last entry of the Generic Serial Commands section of your pdf file the temperature is specified in half degree increments so it's probable all the temperatures referred to are in half degrees which explains why the number you get is twice what you thought. If the temp is 10 degrees it'll return 20. It enables 0.5 degree resolution without using floating point.

To report negative temperatures signed hex is used. It returns an 8 bit number where bit 7 (most significant bit), if set, represents -128. The other 7 bits are treated as positive bits as normal. Subtracting x01 from x00 you get xFF. Bit 7 is set giving -128. Bits 0-6 are set giving 127. Adding them together gives -1.

An unsigned 8 bit integer x00 to xFF represents 0 to 255.

A signed 8 bit integer x80 to x7F represents -128 to +127.

Remember you'll have to divide by 2 to get the actual temperature so may have to use floating point numbers to display it.

Return values from x80 to x7F enables temperatures from -64.0 to +63.5 degrees in 0.5 degree increments to be represented.

Hope that helps. :smile:

Alan

 

Link to comment
Share on other sites

I've never used an arduino but the function

int temperature = sensors.getTempCByIndex(0);

seems to refer to the getting temperatures from a temp sensor attached to a one-wire IO port. I'm not sure how a serial command to the moonlite to get the temperature fits in with that function unless you have an '#include' file which effectively substitutes moonlite commands for the 'one wire' commands.

Arduino uses C/C++ so a variable declared as int is a signed integer. Internally all number types are stored as a sequence of hexadecimal numbers, even floating point numbers. 

An 'int' will use the most significant bit as a signed bit as I mentioned above. For an 8 bit number this will be bit 7. For a 16 bit number this will be bit 15.

An 'unsigned int' will treat all bits as positive.

It's not a case of having to convert a hex number to signed as being declared as 'int' it already is signed. Having to write that signed number to your moonlite using a serial command is another matter as it seems to want the sequence of bytes written, split into 4 bit 'nibbles' sent as bytes to avoid using the 'char' signed bit. It all gets so complicated.

Alan 

Alan

 

Link to comment
Share on other sites

1 hour ago, blinky said:

Somebody on the Arduino forum seems to be suggestign the number is already a signed hex number.... So maybe all I have to do is divide it by 2

Yes it's already signed as your 'int' temperature declaration is signed. It has to be to handle negative temperatures. Dividing by two you'll lose the half degree information. If you declare temperature as 'float' or 'double' which is double precision floating point you won't lose the half degree information when you divide by 2.

All number representations in C/C++ are signed unless specifically declared as unsigned by putting 'unsigned' in the declaration.

Alan

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue. By using this site, you agree to our Terms of Use.