Shop OBEX P1 Docs P2 Docs Learn Events
Displaying negative numbers — Parallax Forums

Displaying negative numbers

J^3J^3 Posts: 121
edited 2011-03-13 21:48 in Propeller 1
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

Comments

  • MagIO2MagIO2 Posts: 2,243
    edited 2011-03-13 14:53
    What's wrong with:

    temp := -temp

    ?

    To short? To easy? ;o)
  • MagIO2MagIO2 Posts: 2,243
    edited 2011-03-13 14:59
    PS: I did not have a closer look to your driver code for the MS5534, but a quick glance into the datasheet. As it's 16bit value you get from it, maybe your if statement is wrong?
  • J^3J^3 Posts: 121
    edited 2011-03-13 15:01
    MagIO2,

    Thanks for the suggestion, but I just tried that, and it still don't work. I get -4_XXX_XXX?

    Thanks,

    J^3
  • J^3J^3 Posts: 121
    edited 2011-03-13 15:04
    Ok, I will try that. Since the returned value is 16 bit, I shoud be saying something like

    if (temp & $8000)
    temp := -temp
    ?
  • MagIO2MagIO2 Posts: 2,243
    edited 2011-03-13 15:10
    You have to remove the previous sign bit before:

    temp := -(temp & $7fff)
  • PerryPerry Posts: 253
    edited 2011-03-13 15:12
    You are following Ap note 502?

    any where it shows values declared as double.

    You are going to need floating point arithmetic or learn how to scale with integers
  • J^3J^3 Posts: 121
    edited 2011-03-13 15:16
    I don't believe the 15th bit of 'temp' is the sign bit. I have tested this by using 'UART.bin(temp, 32)' in order to see what bit is being used for the sign bit, and it appears that bit 22 of temp is being used when the temp is negative. The variable 'temp' is the result of some other calibration functions though, so maybe I should be checking earlier on for the temperature being negative, like at the raw data point from the sensor (D1)?
  • StefanL38StefanL38 Posts: 2,292
    edited 2011-03-13 15:17
    Hi Mag,

    I tried this code on my PPDB
    CON
      _xinfreq = 5_000_000
      _clkmode = xtal1 + pll16x
      
      RX   = 26
      TX   = 27 
      MODE = 4      'I was using the MAX3232 on board the PPDB.
      BAUD = 9600   'If you are using pins 31 and 30, mode should be 0.
    
      DIN  = 0
      DOUT = 1
      MCLK = 2
      SCLK = 3
      
    OBJ
      UART    : "FullDuplexSerialPlus"
      MS5534  : "MS5534_v.01"
      
    VAR
     byte idx, cog
     long calWords[4], coeffs[6], d1, d2, temp, press
    
     
    PUB Demo
      UART.start(31, 30, 0, 115200)
    
      repeat
        'temp := -10
        'repeat 
          Waitcnt(ClkFreq + cnt)
          temp := -10
          temp := -temp
          UART.dec(temp)
          UART.tx(13)
    
          temp := 15
          temp := -temp
          UART.dec(temp)
          UART.tx(13)
          UART.tx(13)
    
    and the output to PST.EXE seems to be fine
    10
    -15
    
    10
    -15
    
    10
    -15
    
    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
  • MagIO2MagIO2 Posts: 2,243
    edited 2011-03-13 15:36
    @ P^3:
    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.
  • J^3J^3 Posts: 121
    edited 2011-03-13 15:43
    MagIO2,

    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.
  • J^3J^3 Posts: 121
    edited 2011-03-13 21:48
    All,

    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
Sign In or Register to comment.