Shop OBEX P1 Docs P2 Docs Learn Events
PropBasic and One-wire — Parallax Forums

PropBasic and One-wire

Hi Bean,
I am struggling with the load library instructions as they pertain to One-wire Read and Write.

I have serial numbers of the one-wire devices I will use so I don't need code for reading the serial numbers.

Could you supply code for writing PropBasic instructions that set the four digital outputs of the DS2450, read the four analog inputs of the DS2450, and read the temperature of the DS1820?

Sincerely,

Discovery
«1

Comments

  • Does anyone have PropBasic code that will do the job?

    Discovery
  • I use the following BS2 code to read DS18S20 temperature devices. What equivalent PropBasic code is needed to read DS18S20 devices on my Propeller?

    '{$STAMP BS2p}
    'One-Wire serial communications to the DS18S20
    'I/O Definitions
    DQ PIN 0

    'Constants
    RdROM CON $33
    SkipROM CON $CC
    Cvrt Tmp CON $44
    RdSP CON $BE

    'Variables
    romData VAR Byte(8)
    tempIn VAR Word
    sign VAR tempIn.BIT8
    tLo VAR tempIn.LOWBYTE
    tHi VAR tempIn.HIGHBYTE
    tSign VAR Bit
    tempC VAR Word
    tempF VAR Word

    Main:
    DO
    OWOUT DQ, 1, [rdROM]
    OWIN DQ, 2, [STR romData\8]
    GOSUB Get_Temperature
    DEBUG HOME,
    "SD18S20",CR,
    "_________",CR,
    SDEC tempC, " C ",CR,
    SDEC tempF, " F "
    PAUSE 500
    LOOP

    END

    Get_Temperature:
    OWOUT DQ, 1, [SkipROM, CvrtTmp] 'Start conversion
    PAUSE 500
    OWOUT DQ, 1, [SkipROM, RdSP]
    OWIN DQ, 2, [tLo, tHi] 'Read temperature

    tSign = sign 'Save the sign bit
    tempIn = tempIn/2 'Round to whole bit
    IF tSign = 0 THEN NoNeg1
    tempIn = tempIn | $FF00 'Extend sign bits for negs

    NoNeg1:
    tempC = tempIn 'Save Celcius value
    tempIn = tempIn */ $01CC 'Multiply bu 1.8
    IF tSign = 0 THEN NoNeg2 'If negative, extend sign bit
    tempIn = tempIn | $FF00

    NoNeg2:
    tempF = tempIn + 32 'Finish C to F conversion
    RETURN

    Sincerely,

    Discovery


  • Does anyone have working one-wire Propeller Basic code for the DS18S20 temperature sensor and the DS2450 quad digital output / quad analog converter?

    Or, is there anyone with the where-with-all to write the one-wire Propeller Basic code?

    Sincerely,

    Discovery

    PS This is really important!
  • Discovery wrote: »
    Does anyone have working one-wire Propeller Basic code for the DS18S20 temperature sensor and the DS2450 quad digital output / quad analog converter?

    I don't know if there's any existing PropBasic code for that. Have you looked in the Obex for Spin objects that access that hardware? If there are some, then I can see 3 solutions for you:

    (1) Port the Spin code to PropBasic. This will require a bit of work, but would have the big advantage that (if you share the code) other PropBasic users will be able to use the libraries.

    (2) Port your code to Spin. How easy/hard this will be depends on your code, of course, but Spin isn't too hard to learn. The advantage here is that Spin is widely used and supported on the Propeller.

    (3) Port your code to Fastspin/BASIC, and use the Spin objects directly from Basic. Fastspin/BASIC is a bit different in syntax from PropBasic, so this would be work, but if your code isn't too large it might be easier than option (1).

  • tritoniumtritonium Posts: 543
    edited 2019-01-03 21:27
    Hi

    My PropBasic directory from Bean has a directory called "demos"
    in there is a file DS18B20.pbas

    at the top of this document it says

    ' Reads the temperature from a Dallas DS18B20
    ' Converts it to ascii and sends it to the PC
    ' Connect DS18B20 DQ pin to PIN 1 on the propeller
    ' Don't forget the 4.7K pullup resistor too...
    '
    DEVICE P8X32A, XTAL1, PLL16X
    etc....

    Dave
  • tritonium,

    How do I access your PropBasic directory?

    Discovery
  • PublisonPublison Posts: 12,366
    edited 2019-01-03 22:00
    This is from Dave from the third post about this topic that was deleted.
    Hi

    lookup this forum post

    http://forums.parallax.com/discussion/118611/download-propbasic-here-00-01-14-last-version-for-bst/p1


    At the bottom of first post there are zip files

    There are four files- the leftmost one is called Demos.zip



    http://forums.parallax.com/discussion/download/71912/Demos.zip

    Dave
  • Hi

    This is from Dave from the third post about this topic that was deleted.


    How did that happen?
    Glad you caught it.

    Dave
  • tritonium wrote: »
    Hi

    This is from Dave from the third post about this topic that was deleted.


    How did that happen?
    Glad you caught it.

    Dave

    A third post was started about the same topic which is against forum rules. It was deleted.
  • Sir or Madam,
    I am aware that you blocked my attempts to reach Bean by invoking the redundancy rule. In the beginning, Bean said that he would help as I learn PropBasic since I wanted to convert my industrial control system from BS2 controllers to Propeller controllers using PropBasic exclusively. The first of two control boards was successfully converted to a Propeller using Bean's help. He supplied the Real-Time clock PropBasic code that ran on a COG but the clock ran too fast. It was the brilliance of ersmith who realized that the WAIT addr instruction could be used to adjust the rate. Smith was absolutely correct and I was able to increase the 80_000_000 value to 80_000_700 and trim the clock to be highly accurate. The combination of Bean and ersmith solved the problem that I had no clue how to solve. I was hoping to contact Bean for help in using the Propeller one-wire commands since my system uses one of two one-wire coax lines to read my system sensors and a second one-wire coax line to control devices in the factory which include motors, heaters, etc. I tried to use the information in the PropBasic one-wire library to read temperature of a single DS18S20 using my Propeller Activity Board but everything I tried produced an error "Insufficient Number of Parameters" but as far as I can tell no additional parameters are needed for the OW-READ instruction. But things get more difficult when I need to communicate with several DS2450 Quad A/D Converter/Quad Digital Output devices that are used extensively in my system. The BS2 code is used for communication with the DS2450s and sends instructions to the chip for configuring the device to be a quad ADC or a quad DO. This code was supplied by a forum member and it works perfectly with the exception that DS2450 can only convert 8 bits and no more. The specification states that 16 bits are possible. I apologize for trying to open another forum discussion with Bean but it appears that Bean is too busy and just maybe he could provide a spark that ersmith or someone else could follow to generate the PropBasic code that I need to make the conversion.

    Discovery
  • PublisonPublison Posts: 12,366
    edited 2019-01-04 14:34
  • Okay...thank you Publison,

    Tritonium,
    I downloaded the demo code and installed it in my program. It compiled and executed but I have no way of knowing if it reads the DS18S20 since the temperature message is output using SEROUT TX, Baud, Message and I do not have the TX Pin connected to my PC. I don't know how to do that.
    Secondly, my system uses six DS18S20 temperature sensors...how do I individually address each sensor?
    Finally, can the variable "value" be used in an IF statement to determine if the value is between 10 and 30 if in Celsius units?

    Would you have the time to look into devising the PropBasic code for reading and writing data to the DS2450 quad ADC/DO?

    Sincerely,
    Discovery
  • I inserted an IF-ENDIF statement to determine what the value of "value" was as returned by the one-wire program code. The value was between 194 and 205 and changed with finger temperature. The actual room temperature was 70F or 21C. I don't know what number base is returned by the DS18S20.

    Discovery
  • Tritonium,
    The demo program appears to read the DS18S20 as long as I divide the numerical in "value" by 10.
    So a value of 205 is actually reading a temperature of 20.5C and this value changes as I increase or decrease the temperature of the sensor. This is good.

    Discovery

  • Discovery, I can't help but wonder if you ever got around to looking at the 18S20 data sheet.
  • Yes,
    Way back when I was working with the initial development of the BS2 software shown above. Sending control data to the DS18S20 using the PropBasic commands: OWWRITE, OWREAD, OWRESET, with the additional parameters attached appears to be far simpler than the BS2 commands.

    Now I will configure a DS2450S and attach it to the Propeller One-wire and try to send and receive data from its registers.
    Discovery
  • The following PropBasic code works for reading one device on the one-wire. It uses the SKIP instruction $CC.

    OWRESET DQPin
    PAUSE 750
    OWWRITE DQPin, $44CC\16 'SKIP & CONVERT
    OWRESET DQPin
    PAUSE 750
    OWWRITE DQPin, $BECC\16 'SKIP & READ SCRATCH PAD
    OWREAD DQPin, value\16 'READ THE TEMPERATURE

    I want to read multiple devices on the one-wire using the Match instruction $55.

    I tried the following code but got an error "INVALID INTEGER" at compile time.

    OWRESET DQPin
    PAUSE 750
    OWWRITE DQPin, $4410848293020800D555\80 'MATCH 64 bit serial number & CONVERT
    OWRESET DQPin
    PAUSE 750
    OWWRITE DQPin, $BE10848293020800D555\80 'MATCH 64 bit serial number & READ SCRATCH PAD
    OWREAD DQPin, value\16 'READ THE TEMPERATURE

    $55 is the Match Instruction
    $10848293020800D55 IS THE 64 BIT serial number
    $BE is the Read Scratch Pad Instruction

    What is the correct way to match the serial number of one-wire devices?

    Sincerely,

    Discovery


  • I could really use some help.

    Discovery
  • Not trying to be rude but the answers to all your questions are right here: https://www.mouser.com/datasheet/2/256/DS18S20-370056.pdf
  • Mr. pmrobert,

    Show me the PropBasic code you wrote and used to match the serial number of a DS18S20 temperature sensor.

    Discovery
  • Based on the answers you've received, it doesn't appear that anyone on the forum has written PropBasic code to access multiple one-wire devices. I haven't either, but I took a quick look at the datasheet and found "DS18S20 operation example 1" on page 18, that gives the sequence of events that you need to do.

    You also have a fundamental misunderstanding about PropBasic: the error message is referring to that 80-bit number that you're trying to send. The largest integer PropBasic can handle is 32 bits, thus the "invalid integer" error. Break that huge number into smaller parts and send them separately, adjusting the "number of bits" in the OWWRITE command as needed. For clarity, I'd break them up logically, i.e. send the command byte, then the serial number in smaller pieces, etc. If you follow the sequence in the datasheet, and don't try to send something larger than PropBasic can deal with, you should have better luck. You should also read the entire section of the datasheet (1-wire bus system) that preceeds the example.

    And please don't ask me to offer code, because I haven't written any for what you are trying to do. I'm just trying to point you in the right direction.
  • I've done it in Spin, but you only seem interested in PropBASIC. FWIW, here's the method that will read the temperature from a specific sensor.
    pub read_tca(p_sn) | tmpC, idx
    
    '' Reads temperature from DS18b20 at specific address
    '' -- returns degrees C in 0.0001 degree units
    '' -- p_sn is pointer to byte array with serial #
    
      ow.reset               
      ow.write(MATCH_ROM)                                           ' use serial #
      repeat idx from 0 to 7                                        ' send serial #
        ow.write(byte[p_sn][idx])          
      ow.write(CVRT_TEMP)                                           ' start conversion          
      repeat                                                        ' let conversion finish                 
        tmpC := ow.rdbit     
      until (tmpC == 1)    
      ow.reset               
      ow.write(MATCH_ROM)
      repeat idx from 0 to 7
        ow.write(byte[p_sn][idx])            
      ow.write(RD_SPAD)                                             ' read scratchpad     
      tmpC := ow.read                                               ' lsb of temp      
      tmpC |= ow.read << 8                                          ' msb of temp
    
      tmpC := ~~tmpC * 625                                          ' extend sign, 0.0001° units
    
      return tmpC
    

    If you spend a bit of time with the suggested documentation it should make sense.

  • 32bits maximum. I will try sending the information again in smaller chunks. Thanks Jones.

    Discovery
  • The 80-bit string for conversion was recomposed into two 24-bit strings and one 32-bit string as shown below.

    OWWRITE DQPin, $0800D555\32
    OWWRITE DQPin, $829302\24
    OWWRITE DQPin, $441084\24

    The 80-bit string for reading the scratch pad was recomposed into two 24-bit strings and one 32-bit string as shown below.

    OWWRITE DQPin, $0800D555\32
    OWWRITE DQPin, $829302\24
    OWWRITE DQPin, $BE1084\24

    This code compiled without errors; however the program downloaded into the Propeller did not read the temperature...most likely it did not find a match.

    Could the sequence of bits in the serial number be in error? I will try reversing the bits.

    Discovery
  • It's very likely that PropBasic only works well with 8, 16, and 32 bit integers. 24 bits is not a natural integer size of the Propeller.
  • The 80-bit string for conversion was recomposed into two 24-bit strings and one 32-bit string as shown below.

    OWWRITE DQPin, $0800D555\32
    OWWRITE DQPin, $829302\24
    OWWRITE DQPin, $441084\24

    The 80-bit string for reading the scratch pad was recomposed into two 24-bit strings and one 32-bit string as shown below.

    OWWRITE DQPin, $0800D555\32
    OWWRITE DQPin, $829302\24
    OWWRITE DQPin, $BE1084\24

    This code compiled without errors; however the program downloaded into the Propeller did not read the temperature.

  • I will recompose the instructions in 8, 16, and 32-bit chunks and try again.

    Discovery
  • I recomposed the instructions into 16-bit segments and sent them using OWWRITE DQPin,.

    The program compiled without errors but the Propeller could not match the ID.

    I reversed the ID bits in the 16-bit segments and the Propeller could not make a match.

    To verify the program I used the skip command and the Propeller converted the temperature reading just fine.

    I don't know what to try next.

    Discovery
  • 1-Wire devices are byte-oriented; stick with bytes. I would suggest creating subroutines for 1W functions so that you can save space in your compiled code. In my 1W object (for Spin) I have code for these functions

    -- OW_RESET
    -- OW_WR_BYTE
    -- OW_RD_BYTE
    -- OW_RD_BIT
  • My expertise is not in writing low-level code so your suggestion, even though a good one, would be very difficult for me to implement. I want to stick with PropBasic since it is so simple.

    The PropBasic code used in the above program that reads the DS18S20 using the skip ($CC) instruction and the convert ($44) work perfectly and they are sent as two byte pairs \16 equivalent to a word.

    OWWRITE DQPin, $44CC\16
    OWWRITE DQPin, $BECC\16

    Some time ago I asked for a PropBasic program to read the DS18S20 and one was supplied by a forum member. It did not work, so I added a PAUSE 750 instruction after the OWRESET DQPin instruction and that did the trick. I wonder if some other step needs to be inserted to make the MATCH instruction work.

    Any ideas?

    Sincerely,

    Discovery
Sign In or Register to comment.