1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

URM37 V3.2 with OSHON.

Discussion in 'Oshonsoft' started by SwingeyP, Apr 5, 2012.

  1. SwingeyP

    SwingeyP Member

    Joined:
    Apr 26, 2010
    Messages:
    292
    Likes:
    2
    Location:
    Birmingham UK
    Not sure if this is in the right place but as everything developed so far has used OSHON here goes.

    Well after a 2 year break (mainly due to illness) I am back on my robot project.

    I never really found any answers to this so here goes again.

    I have a URM 37 V3.2 ultrasonic sensor. I want to be able to read the temperature (and later distance - temp looks easier for the moment) from the sensor and act accordingly. I just can't get my head around how it works.

    Here's the code

    Code (text):

    Define CONF_WORD = 0x3ffc  'Internal Oscillator'

    '- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -
    'URM37 Ultrasonic Test written by paul swingewood
    'July 2011   - PIC 16F648A
    '--------------------------------------------------------------------------

    'Define SIMULATION_WAITMS_VALUE = 2


    AllDigital

    'Define the comms for the LCD display.
    Define LCD_LINES = 2
    Define LCD_CHARS = 16
    Define LCD_BITS = 4
    Define LCD_DREG = PORTB
    Define LCD_DBIT = 4  'Use the high order bits'
    Define LCD_RSREG = PORTA
    Define LCD_RSBIT = 2
    Define LCD_EREG = PORTA
    Define LCD_EBIT = 0
    Define LCD_RWREG = PORTA
    Define LCD_RWBIT = 1
    Define LCD_READ_BUSY_FLAG = 1


    Define LCD_COMMANDUS = 5000  'delay after LCDCMDOUT, default value is 5000
    Define LCD_DATAUS = 100  'delay after LCDOUT, default value is 100
    Define LCD_INITMS = 20


    Define SEROUT_DELAYUS = 5000


    Dim temperature As Byte
    Dim distance As Byte
    Dim datain(4) As Byte  'Array to store raw Data (Remember 0-3) word?

    startup:
    Lcdinit LcdCurBlink
    Lcdcmdout LcdClear
    WaitMs 20
    Gosub welcome
    WaitMs 20
    Gosub setbaudrate

    start:
        Gosub sendcommandtemp
        Gosub readtemp
        Gosub calculatetemp
        Gosub displaytemp
        WaitMs 20
        Gosub sendcommanddistance
        Gosub readdistance
        Gosub calculatedistance
        Gosub displaydistance

    Goto start
    '--- sub routines ---
    stop:
    End                                              

    welcome:
    Lcdcmdout LcdLine1Pos(1)
    Lcdout "Waiting for DATA"
    Return                                            

    setbaudrate:
    Hseropen 9600  'Set the baud rate to 38400
    Hserout 00h
    Hserout 00h
    Hserout 00h
    Hserout 00h
    Hserout 00h
    WaitMs 10  '300
    Hserout 00h
    Hserout 00h
    Hserout 00h
    Hserout 00h
    Hserout 00h
    Return                                            

    sendcommandtemp:
    'send temp command to URM37'
    Hserout 11h  'Send 11 00 00 11 to the URM37
    Hserout 00h  'Tell the device we
    Hserout 00h  'want to read the temperature
    Hserout 11h
    Return                                            

    readtemp:
    'Read the data coming back from the URM37'Hserin datain(0)  'Bit1 -14
    Hserin datain(0)  'Bit1 - 55
    Hserin datain(1)  'Bit2 - 55
    Hserin datain(2)  'Bit3 - 00
    Hserin datain(3)  'Bit4 - 00
    Return                                            

    calculatetemp:
    temperature = datain(2)
    Return                                            

    displaytemp:
    Lcdcmdout LcdLine1Pos(1)
    Lcdout "Temperature = ", #temperature
    Return                                            

    sendcommanddistance:
    'send distance command to URM37'
    Hserout 22h  'Send 22 00 00 22 to the URM37
    Hserout 00h  'Tell the device we
    Hserout 00h  'want to read the distance
    Hserout 22h
    Return                                            

    readdistance:
    'Read the data coming back from the URM37'Hserin datain(0)  'Bit1 -14
    Hserin datain(0)  'Bit1 - 55
    Hserin datain(1)  'Bit2 - 55
    Hserin datain(2)  'Bit3 - 00
    Hserin datain(3)  'Bit4 - 00
    Return                                            

    calculatedistance:
    distance = datain(2)
    Return                                            

    displaydistance:
    Lcdcmdout LcdLine2Pos(1)
    Lcdout "Distance = ", #distance
    Return

     
    I have also attached the details of the URM37 as a PDF.

    Am I send the 'commnd construct' and reading the 'data consruct' correctly?
    It seems to work in simulation but no go in the real world.

    Can anyone help please?

    Regards - Paul
     
  2. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,187
    Likes:
    644
    Location:
    Ex Yorks' Hants UK
    ONLINE
    hi,
    Look at this.

    readtemp: ' your subr
    'Read the data coming back from the URM37'Hserin datain(0) 'Bit1 -14
    ''assume a positive tempr
    Hserin datain(0) '0001,0001
    Hserin datain(1) '0000,tttt
    Hserin datain(2) 'tttt,tttt
    Hserin datain(3) 'SUM
    Return

    LET TEMPR = Low Byte of [(1) *256] + (2) = 12bit Tempr WORD 0000.tttt,tttt,tttt

    Use Oshonsoft #TEMPR instruction to show temperature on LCD

    For minus tempr, MASK off the 1111,tttt in (1)

    OK.?
     
    Last edited: Apr 5, 2012
  3. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,187
    Likes:
    644
    Location:
    Ex Yorks' Hants UK
    ONLINE
    hi,
    Run this in Oshonsoft.
    NOTE: added/amended sections marked with +++++++++++
    I have remmed out the UART read for the Tempr and loaded test values.

    EDIT: Change temperature from a Byte to a WORD

    Code (text):
    Define CONF_WORD = 0x3ffc  'Internal Oscillator'

    '- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -
    'URM37 Ultrasonic Test written by paul swingewood
    'July 2011   - PIC 16F648A
    '--------------------------------------------------------------------------

    'Define SIMULATION_WAITMS_VALUE = 2


    AllDigital

    '++++++++++++++++++++++++++++++++
    'setup temp variables
    Dim ascbfr4 As Byte
    Dim ascbfr3 As Byte
    Dim ascbfr2 As Byte
    Dim ascbfr1 As Byte
    Dim ascbfr0 As Byte
    Dim temp3 As Word
    Dim binval As Word
    '+++++++++++++++++++++++++++++

    'Define the comms for the LCD display.
    Define LCD_LINES = 2
    Define LCD_CHARS = 16
    Define LCD_BITS = 4
    Define LCD_DREG = PORTB
    Define LCD_DBIT = 4  'Use the high order bits'
    Define LCD_RSREG = PORTA
    Define LCD_RSBIT = 2
    Define LCD_EREG = PORTA
    Define LCD_EBIT = 0
    Define LCD_RWREG = PORTA
    Define LCD_RWBIT = 1
    Define LCD_READ_BUSY_FLAG = 1


    Define LCD_COMMANDUS = 5000  'delay after LCDCMDOUT, default value is 5000
    Define LCD_DATAUS = 100  'delay after LCDOUT, default value is 100
    Define LCD_INITMS = 20


    Define SEROUT_DELAYUS = 5000


    Dim temperature As Word
    Dim tempdeci As Word
    Dim distance As Byte
    Dim datain(4) As Byte  'Array to store raw Data (Remember 0-3) word?

    startup:
    Lcdinit LcdCurBlink
    Lcdcmdout LcdClear
    WaitMs 20
    Gosub welcome
    WaitMs 20
    Gosub setbaudrate

    start:
        Gosub sendcommandtemp
        Gosub readtemp
        Gosub calculatetemp
        Gosub displaytemp
        WaitMs 20
        Gosub sendcommanddistance
        Gosub readdistance
        Gosub calculatedistance
        Gosub displaydistance

    Goto start
    '--- sub routines ---
    stop:
    End                                              

    welcome:
    Lcdcmdout LcdLine1Pos(1)
    Lcdout "Waiting for DATA"
    Return                                            

    setbaudrate:
    Hseropen 9600  'Set the baud rate to 38400
    Hserout 00h
    Hserout 00h
    Hserout 00h
    Hserout 00h
    Hserout 00h
    WaitMs 10  '300
    Hserout 00h
    Hserout 00h
    Hserout 00h
    Hserout 00h
    Hserout 00h
    Return                                            

    sendcommandtemp:
    'send temp command to URM37'
    Hserout 11h  'Send 11 00 00 11 to the URM37
    Hserout 00h  'Tell the device we
    Hserout 00h  'want to read the temperature
    Hserout 11h
    Return                                            

    readtemp:
    'Read the data coming back from the URM37'Hserin datain(0)  'Bit1 -14
    'Hserin datain(0)  'Bit1 - 55
    'Hserin datain(1)  'Bit2 - 55
    'Hserin datain(2)  'Bit3 - 00
    'Hserin datain(3)  'Bit4 - 00
    '++++++++++++++++++++++++++++
    datain(0) = 11h
    datain(1) = 04h  '123.4C decimal
    datain(2) = d2h
    datain(3) = ffh
    '++++++++++++++++++++++++++++                                      

    calculatetemp:
    temperature = 256 * datain(1) + datain(2)
    binval = temperature
    Gosub bin2asc

    Return                                            

    '+++++++++++++++++++++++++
    displaytemp:
    Lcdcmdout LcdClear
    Lcdout "Temp = ", ascbfr3, ascbfr2, ascbfr1, ".", ascbfr0, "C"

    Return                                            
    '+++++++++++++++++++++++++++++
    sendcommanddistance:
    'send distance command to URM37'
    Hserout 22h  'Send 22 00 00 22 to the URM37
    Hserout 00h  'Tell the device we
    Hserout 00h  'want to read the distance
    Hserout 22h
    Return                                            

    readdistance:
    'Read the data coming back from the URM37'Hserin datain(0)  'Bit1 -14
    Hserin datain(0)  'Bit1 - 55
    Hserin datain(1)  'Bit2 - 55
    Hserin datain(2)  'Bit3 - 00
    Hserin datain(3)  'Bit4 - 00
    Return                                            

    calculatedistance:
    distance = datain(2)
    Return                                            

    displaydistance:
    Lcdcmdout LcdLine2Pos(1)
    Lcdout "Distance = ", #distance
    Return                                            

    '+++++++++++++++++++++++++++++++++++++++++++++++
    'you can convert any binary value from 0000h to fffFh to 0000 to 65535 decimal
    'just name the binary word as binval and call this subr and the ASCII
    'result will be in ascbfr4,3,2,1,0, ready for your LCD or UART
    'just pop the DP in the output to the LCD [as shown above]

    bin2asc:
    ascbfr4 = binval / 10000
    temp3 = binval Mod 10000

    ascbfr3 = temp3 / 1000
    temp3 = binval Mod 1000

    ascbfr2 = temp3 / 100
    temp3 = temp3 Mod 100

    ascbfr1 = temp3 / 10
    ascbfr0 = temp3 Mod 10
    'results are BCD so
    'convert to ASCII for LCD or UART
    ascbfr4 = ascbfr4 Or 0x30
    ascbfr3 = ascbfr3 Or 0x30
    ascbfr2 = ascbfr2 Or 0x30
    ascbfr1 = ascbfr1 Or 0x30
    ascbfr0 = ascbfr0 Or 0x30
    Return            
     
    Last edited: Apr 5, 2012
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. SwingeyP

    SwingeyP Member

    Joined:
    Apr 26, 2010
    Messages:
    292
    Likes:
    2
    Location:
    Birmingham UK

    Hello again Eric,

    That looks very useful unfortunately I can't get that far. I have the LCD with 'waiting for data' at the moment. I just can't get it to see anything. I'm sure this is something to do with the command and data messages.
    Looking at the datasheet do you think I am addressing the device properly as 4 pulses out and then 4 pulses back in?

    I have had this device working with some PC software that comes with it. linked Via RS232. All perfect. So idecided to make an RS232 to TTL converter and set the device as TTL. I know this is complicating things but I wanted to ensure that TTL was working on the device. So TTL to RS232 and that worked fine too. I know the device is working in both modes.

    So, now I have the device tx wired to rx on the pic and device rx wired to tx on the pic. I am using a PIC16F648A. This thing has driven me mad for ages. I took it to an electronics 'guru' and he couldn't fathom it either. Im sure its something really simple. I can't get any meaningful help from the manufacturer as I don't speak Mandarin.

    Regards - Paul
     
  6. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,187
    Likes:
    644
    Location:
    Ex Yorks' Hants UK
    ONLINE
    hi Paul,
    Is the URM set for Mode 1.? [passive, awaiting a Read command]

    Are the selector jumpers set for TTL.?

    Have you ever connected the URM in TTL to RS232.??? Warning on page 4 of datasheet.?

    I would suggest the following check.

    1. Keep the TX/RX TTL lines connected on the URM and PIC.
    2. Temporally add a RS232 converter [MAX232 etc] to the PIC TX pin and link the output from the MAX to a PC serial port [dont forget the 0V pin is also required.
    3. Run a UART utility program in the PC and monitor the PIC's TX output to the URM
    4 Then monitor the URM's TX output to the PIC.

    Eric

    This should enable you to check the data transfer codes.
     
  7. SwingeyP

    SwingeyP Member

    Joined:
    Apr 26, 2010
    Messages:
    292
    Likes:
    2
    Location:
    Birmingham UK
    Hi Eric. Wired everything up as you suggested and i'm getting some weird result.

    Running the program I get 'Waiting for DATA' on the display.
    So I sent the following command from the PC terminal in OSHON 11 00 00 11 as hex byte.
    This then makes the display show Temp = 000.0C
    The return on the PC Terminal reports 11 01 OA 1C returned.

    I'm so close I can tatse it .... lol

    Any ideas as to what's going on? - Timing maybe?

    Interesting that the diaplay won't read until the command is sent from the PC terminal ....

    Regards - Paul
     
  8. SwingeyP

    SwingeyP Member

    Joined:
    Apr 26, 2010
    Messages:
    292
    Likes:
    2
    Location:
    Birmingham UK
    Ahhh. The weird data seems to be a sync problem. It's difficult to determine when its reading and writing ..... So the 000 temp makes sense as its seeing 11 00 00 11 at the wrong time.
    This doesn't explain why it won't run without the signal from the PC though.
     
  9. SwingeyP

    SwingeyP Member

    Joined:
    Apr 26, 2010
    Messages:
    292
    Likes:
    2
    Location:
    Birmingham UK
    THINK ALOUD ......

    Ok so next logical thing is to disconect the URM 37. Sending data from the terminal now display temeperature and distance readings on the LCD.
    I would have expected to see the returned data to the terminal from the pic. Nothing displays. So maybe this IS a timing issue.

    I'll add a few delays ...
     
  10. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,187
    Likes:
    644
    Location:
    Ex Yorks' Hants UK
    ONLINE
    Hi,
    Is this how you have it wired on the hardware.?

    Do you see the PIC to URM strong OK on the PC

    also the URM to PIC reply.?

    I assume that you have the PIC and URM 0V's connected.??
     
  11. SwingeyP

    SwingeyP Member

    Joined:
    Apr 26, 2010
    Messages:
    292
    Likes:
    2
    Location:
    Birmingham UK
    Yes thats exactly how I have it. With the URM in place I see the data returned from it. With it removed I see nothing returned. I would have expected to see the 'enable' commands coming from the pic. I'm sure this is where the problem lies now. I have just added 500ms delays inbetween each enable so the pic send 11 waitsms 500 then 00 waitms 500 etc etc as the enable.

    I still don't see this on the PC terminal. TX/RX must be the rigt way round or the code wouldn't move on when it sees data from the PC terminal.

    Still no joy at present but closer than I have ever been ......

    Regards - Paul
     
  12. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,187
    Likes:
    644
    Location:
    Ex Yorks' Hants UK
    ONLINE
    hi Paul,
    Have you removed these calls
    WaitMs 20
    Gosub sendcommanddistance
    Gosub readdistance
    Gosub calculatedistance
    Gosub displaydistance

    from this sequence while you are testing the Tempr.??
    start:
    Gosub sendcommandtemp
    Gosub readtemp
    Gosub calculatetemp
    Gosub displaytemp
    ' WaitMs 20
    ' Gosub sendcommanddistance
    ' Gosub readdistance
    ' Gosub calculatedistance
    ' Gosub displaydistance

    Goto start
     
  13. SwingeyP

    SwingeyP Member

    Joined:
    Apr 26, 2010
    Messages:
    292
    Likes:
    2
    Location:
    Birmingham UK
  14. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,187
    Likes:
    644
    Location:
    Ex Yorks' Hants UK
    ONLINE
    hi Paul,
    Watched your vid.

    Could you post a simple block diagram showing how the PC, PIC and URM are interconnected, something doesn't sound right in the way its connected.??

    E.
     
  15. SwingeyP

    SwingeyP Member

    Joined:
    Apr 26, 2010
    Messages:
    292
    Likes:
    2
    Location:
    Birmingham UK
    Hi Eric. Just realised that I can't parallel up the TX and RX on the pic and URM 37 or I get TX talking to TX etc ....

    So I have removed the URM all together and i'm just trying to get comms working between the PC and the PIC

    Although I can get the 11 00 00 11 or 22 00 00 22 sent to the pic ok I get nothing in return on the PC Serial Port Terminal.

    The code does move on though display temp and distnce each time so it must be receiving soemthing.

    Regards - Paul

    I see why the attached pic was giving weird readings. I am bow just trying to get the PC to emulate the URM 37
     
  16. SwingeyP

    SwingeyP Member

    Joined:
    Apr 26, 2010
    Messages:
    292
    Likes:
    2
    Location:
    Birmingham UK
    I also need to take typing lessons. Sorry for the bad typo's
     
  17. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,187
    Likes:
    644
    Location:
    Ex Yorks' Hants UK
    ONLINE
    Hi Paul,
    I am still a lousy typist, fingers like bananas.

    When you described your set up yesterday, its didn't sound right.

    The hex codes from the PIC will not display on the PC, unless you use a Binary comm and process the incoming data to say ASCII.
     
    Last edited: Apr 8, 2012
  18. SwingeyP

    SwingeyP Member

    Joined:
    Apr 26, 2010
    Messages:
    292
    Likes:
    2
    Location:
    Birmingham UK
    Hi Eric.

    I don't really understand whay that is the case. If I connect up the URM to the TTL converter instead of the PIC I get replies? Why does te URM respond but not the pic?
    Regards - Paul
     
  19. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,187
    Likes:
    644
    Location:
    Ex Yorks' Hants UK
    ONLINE
    hi
    I will post a sketch.........

    Connect it up like this image, send the Commands.

    If all else fails I would be happy to debug the project for you, FOC, post the URM etc to me.

    E.
     
    Last edited: Apr 8, 2012
  20. SwingeyP

    SwingeyP Member

    Joined:
    Apr 26, 2010
    Messages:
    292
    Likes:
    2
    Location:
    Birmingham UK
    Thanks Eric,

    I have wired up as per your diagram. Which makes sense so that I can send a command from the PC to the URM. This data is then returned to the PC Terminal. I only ever see the temp as 000.0 because that's what is contained in the tx to the pic.

    I then decided to wire it the other way round (which is probably what you described in the first place) so that the TX from the PC terminal sends the command to the URM and the PIC receives the data (displayed on the LCD) and copied on the PC terminal.

    This worked a treat. I can command the URM and then see the results on the terminal and on the PC.

    So, putting this all together it must mean that the pic isn't sending the control command correctly. Is this what you ment by send hex / bin values?


    I have put up another video here http://www.youtube.com/watch?v=0rA3...DvjVQa1PpcFM-SqdCb6h8tKumdV0h-GTvHpKxrHBnVMw=

    Which shows this in operation. All I can deduce from this is that the command from the pic 11h 00h 00h 11h etc isn't correct. Maybe it needs to be binary ????

    Thanks for your offer to debug this for me FOC. If I can't get it working this week (im off work) i'd like to take you up on it. Can you email me your address?

    Many thnaks for all your help with this. The learning continues .....

    Regards - Paul
     
  21. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,187
    Likes:
    644
    Location:
    Ex Yorks' Hants UK
    ONLINE
    hi Paul,
    Thats a helpful video.

    I would set the PIC program with a delay after the Hseropen 9600., say WaitMs 200

    Do not send the two 00,00,00,00,00 blocks at all, the URM expects groups of 4 bytes per string.

    Also for the Hserout, I would use.

    setmode:
    Hserout 00h, 00h, 00h, 00h
    Return
    '''
    sendcommandtemp:
    'send temp command to URM37'
    Hserout 11h, 00h, 00h, 11h 'Send 11 00 00 11 to the URM37
    Return

    also reading
    readtemp:
    'Read the data coming back from the URM37'Hserin datain(0) 'Bit1 -14
    Hserin datain(0), datain(1), datain(2), datain(3)
    Return

    Have you been able to display on the PC the output Bytes from the PIC example: the 11h,00h,00h,11h ??

    For a continuous PIC output to the PC input, edit this block.

    start:
    Gosub sendcommandtemp
    ' Gosub readtemp
    ' Gosub calculatetemp
    ' Gosub displaytemp
    ' WaitMs 20
    ' Gosub sendcommanddistance
    ' Gosub readdistance
    ' Gosub calculatedistance
    ' Gosub displaydistance
    WaitMs 2000 ; for a 2 second repeating output

    Goto start



    I will PM you my address

    Eric
     

Share This Page