Shop OBEX P1 Docs P2 Docs Learn Events
PropBasic Help!!! — Parallax Forums

PropBasic Help!!!

$WMc%$WMc% Posts: 1,884
edited 2016-09-25 04:13 in Propeller 1
I'm haven problems with the code below. All I get from the terminal window is crazy numbers.
This chip works great with the BS2.
The spec sheet for the MAX31855 calls for at least 2.5 volts....I'm supply 3.3volts
I think my debug window may be the problem?


Code starts here
' --- WMc_MAX31855K type "K" T/C reader---9_21_16
'
DEVICE P8X32A, XTAL1, PLL16X
XIN 5_000_000
MAX_cs PIN 0 high '
MAX_so PIN 1 '
MAX_clk PIN 2 '
MAX31855 VAR LONG
F_Temp VAR LONG
ascii HUB STRING(10)
PROGRAM Start
Start:
DO
LOW MAX_CS ' Stop read of T/C...Set to write mode MAX31855...Chip select PIN 0
PAUSE 100
SHIFTIN MAX_so, MAX_clk, MSBPOST, MAX31855\16 ' Read MAX31855
HIGH MAX_cs ' set to read T/C mode Stop write mode MAX31855...Chip select
MAX31855 = ABS F_Temp
F_Temp = F_Temp *9 'Start Celus
F_Temp = F_Temp /5 'to
F_Temp = F_Temp +32 'F conversion
'LOW MAX_clk
'ascii = STR MAX31855, 4
ascii = STR F_Temp, 4
ascii = ascii + 13 ' Add a carrage return
SEROUT 30, T115200, ascii
PAUSE 100
LOOP
END

Comments

  • tritoniumtritonium Posts: 543
    edited 2016-09-25 06:35
    Hi

    Shouldn't

    MAX31855 = ABS F_Temp

    be

    F_Temp = ABS MAX31855

    ?

    Dave
  • $WMc% wrote: »
    LOW MAX_CS ' Stop read of T/C...Set to write mode MAX31855...Chip select PIN 0
    PAUSE 100
    SHIFTIN MAX_so, MAX_clk, MSBPOST, MAX31855\16 ' Read MAX31855
    HIGH MAX_cs ' set to read T/C mode Stop write mode MAX31855...Chip select

    I am using MAX6675 (predecessor to MAX31855) in a project. My working code has two distinct differences: 1) no pause after setting CS low, and 2) MSBPRE instead of MSBPOST. While I have not exhaustively reviewed the MAX31855 datasheet, I would expect that the SPI would be consistent.

    LOW CS_6675 ' MAX6675 chip select is active low
    SHIFTIN MISO, SERCLK, MSBPRE, T_reading\16
    HIGH CS_6675

    What is the minimum conversion time for MAX31855? I recall for MAX6675 it is something like 220 msec.
  • have you considered using a TV for debugging? I love my old TV ;)
  • To summarize:
    1) The MAX31855 data is never assigned to the F_Temp variable (see Dave/tritonium's post above).
    2) You may wish to try MSBPRE instead of MSBPOST.
    3) Only the first 14 bits will be temperature data, but the code reads 16 bits. You can replace "\16" with "\14".
    4) Conversion time for MAX31855 is 100 msec, so PAUSE 100 at the end of the loop is good.
    5) The power-up conversion, however, requires 200 msec. Adding PAUSE 200 before the DO LOOP will ensure the first reading is good.
    6) You can remove PAUSE 100 after LOW MAX_CS. I do not believe that it will create a problem, but it is not necessary.

    Please let us know if this helps!
  • I've only written code in Spin, which is MSBPRE (this was for an HVAC system and did work, though I don't have hardware that I can test PropBASIC with at the moment)
    pub read_raw  
    
    '' Reads 32-bit raw value from device
    '' -- includes tc, internal, and fault data
    
      chip_select(true) 
        
      repeat 32
        lastscan := (lastscan << 1) | ina[miso]                     ' get bits, MSBPRE mode
        outa[sck] := 1                                              ' clock next bit
        outa[sck] := 0 
    
      chip_select(false)
         
      return lastscan
    
    As you can see, the bit is sampled before (PRE) clock, and the order is MSBFIRST

    My code samples all 32 bits so that I can extract the constituent parts from it. Here are the other methods -- I'm not sure if all the operators translate to PropBASIC (e.g., ~>).
    pub read_tc(rescan)
    
    '' Reads thermocouple junction (external temperature)
    '' -- if recan is false (0), value from last scan is used
    '' -- returns signed 12-bit value
    
      if (rescan)
        read_raw
    
      return lastscan ~> (32-12)
    
    
    pub read_cj(rescan)  
    
    '' Reads cold junction (internal temperature)
    '' -- if recan is false (0), value from last scan is used
    '' -- returns signed, 8-bit value
    
      if (rescan)
        read_raw
    
      return (lastscan << 16) ~> (32-8)
    
    
    pub read_faults(rescan)
    
    '' Returns fault bits
    '' -- if recan is false (0), value from last scan is used
    
      if (rescan)
        read_raw
    
      return lastscan & %0111
    
  • $WMc%$WMc% Posts: 1,884
    Thanks for all the help and feed back
    '
    "Hi

    Shouldn't

    MAX31855 = ABS F_Temp

    be

    F_Temp = ABS MAX31855

    ?

    Dave"
    '
    This was a great help. I can't believe I over looked this.
    '
    I tried the MSBPRE but it doesn't read rite. I don't have a calibrator with me now, But I'll bring one home tomorrow to check accuracy of the temp.
  • $WMc%$WMc% Posts: 1,884
    Here is my latest PropBasic code for the MAX31855.....It's working good with the help from my forum friends, I forgot the Fluke 724 Calibrator. I'll bring it home tomorrow and verify the accuracy. The code must be within 0.25% accuracy at full scale or it's unsound code....!!!
    '
    " code start below"

    ' --- WMc_MAX31855K_Ver3.pbas---... type "K" T/C reader---9_21_16
    '
    DEVICE P8X32A, XTAL1, PLL16X
    XIN 5_000_000
    MAX_cs PIN 0 high '
    MAX_so PIN 1 '
    MAX_clk PIN 2 '
    MAX31855 VAR LONG
    F_Temp VAR LONG
    ascii HUB STRING(10)
    PROGRAM Start
    PAUSE 250
    Start:
    DO
    LOW MAX_CS ' Stop read of T/C...Set to write mode MAX31855...Chip select PIN 0
    'PAUSE 100
    SHIFTIN MAX_so, MAX_clk, MSBPOST, MAX31855\11 ' Read MAX31855
    PAUSE 200 '
    HIGH MAX_cs ' set to read T/C mode Stop write mode MAX31855...Chip select
    F_Temp = ABS MAX31855
    F_Temp = F_Temp *9 'Start Celus
    F_Temp = F_Temp /5 'to
    F_Temp = F_Temp +32 'F conversion
    ascii = STR F_Temp, 4
    ascii = ascii + 13 ' Add a carrage return
    SEROUT 30, T115200, ascii
    PAUSE 100
    LOOP
    END

    " end code"
  • $WMc% wrote: »
    SHIFTIN MAX_so, MAX_clk, MSBPOST, MAX31855\11 ' Read MAX31855
    PAUSE 200 '
    HIGH MAX_cs ' set to read T/C mode Stop write mode MAX31855...Chip select

    A few thoughts ...
    PAUSE 200, shown above, does not seem to serve any function. The PAUSE 250 before the loop ensures that the first reading has time to convert. The PAUSE 100 at the end of each loop ensures that the next reading has time to convert. So, I am not aware of any value in waiting to raise CS.

    The MAX31855 data is truncated at 11 bits, which would stop at 2 degrees C resolution. Shifting 14 bits is the only way to get to the full 0.25 degrees C resolution.

    I am no expert, but the Serial Interface portion of the datasheet - shown below - sure sounds like the definition of MSBPRE (Data is MSB-first; sample bits before clock pulse):

    The first bit, D31, is the thermocouple temperature sign bit, and is presented to the SO pin within tDV of the falling edge of CS. Bits D[30:18]
    contain the converted temperature in the order of MSB to LSB, and are presented to the SO pin within tD0 of the falling edge of SCK.



    One last idea for dealing with floating point math, is to check out the "*/" operator. It is nicely described in the PropBASIC 00.01.43 document, but a similar operator was also used in PBASIC.

    F_Temp = F_Temp */ 117965 ' same as multiplying by 1.800003 and probably much better than integer-only math
  • $WMc%$WMc% Posts: 1,884
    @hatallica
    '
    I started out trying the "F_Temp = F_Temp */ 117965" but the compiler kept giving me an error, I know now that it was because I had MAX31855 and F_Temp reversed in another line of code so F_Temp had a value of 0. The */ works now.
    '
    I have the MSBPRE working now in the code. I don't understand how MSBPOST was working but it was. I used MSBPOST from a BS2 code I wrote about a year and a half ago for this chip. Spark Fun had the wrong data sheet posted for the 31855, So I used a "O" scope to see the data for the BS2.
  • $WMc%$WMc% Posts: 1,884
    edited 2016-09-29 01:37
    ' --- WMc_MAX31855K type "K" T/C reader---9_21_16
    '
    DEVICE P8X32A, XTAL1, PLL16X
    XIN 5_000_000
    MAX_cs PIN 0 high
    MAX_so PIN 1
    MAX_clk PIN 2'
    MAX31855 VAR LONG
    F_Temp VAR LONG
    ascii HUB STRING(10)
    PROGRAM Start
    PAUSE 250
    Start:
    DO
    LOW MAX_CS
    SHIFTIN MAX_so, MAX_clk, MSBPRE, MAX31855\12 '
    HIGH MAX_cs
    F_Temp = ABS MAX31855
    F_Temp = F_Temp */117965
    '
    F_Temp = F_Temp +32
    ascii = STR F_Temp, 4
    ascii = ascii + 13 ' CR
    SEROUT 30, T115200, ascii
    PAUSE 100
    LOOP
    END
    '
    '
    This the latest code. I have made some recommended changes . It is pretty tight at the bottom of the scale "0'C - 32'F". But it
    s off a little at the top.
    '
    0C'........... = 1'C
    32'F..........= 33'F
    1379Ç......= 1329'Ç
    2498'F......= 2424'F.....( 2498'F-1370Ç is the highest the 724 calibrator will source)
    '
    The chip uses linear mV instead of linear T...So no look-up chart.
Sign In or Register to comment.