Ultrasonic sensor, serial communication.


Closed Thread
Results 1 to 3 of 3
  1. #1

    Default Ultrasonic sensor, serial communication.

    Hi Folks,
    I need a little help with this task. I want to read data from Ultrasonic distance meter which put out every 100ms serial data.
    The protocol is here:
    Name:  Protocol.jpg
Views: 289
Size:  148.0 KB
    The captured data looks like:
    Name:  Data_Packet.jpg
Views: 351
Size:  58.9 KB
    My program trying to capture this data looks like:
    Code:
    #CONFIG
      __config _CONFIG1, _FOSC_INTOSC & _WDTE_OFF & _PWRTE_ON & _MCLRE_OFF & _CP_OFF & _CPD_OFF & _BOREN_ON & _CLKOUTEN_OFF & _IESO_ON & _FCMEN_ON
      __config _CONFIG2, _WRT_OFF & _PLLEN_ON & _STVREN_ON & _BORV_19 & _LVP_OFF
    #ENDCONFIG
    
    ;MCU PIC 12F1840
    
    DEFINE OSC 4        ; Use internal clock 4MHz 
    OSCCON  = %01101010 ; 4 MHz internal
    OSCTUNE	= %00000000	; Internal osc tunning
    
    TRISA = %100000     ; RA.5 input rest output
    ANSELA = %00000  
    OPTION_REG.7=1      ; disable internal pull-ups
    APFCON = %10001100 
    
    string var byte[16]
    
    ' Set receive register to receiver enabled
    DEFINE HSER_RCSTA 90h 
    ' Set transmit register to transmitter enabled
    DEFINE HSER_TXSTA 24h                             
    DEFINE HSER_BAUD 9600 
    
    clear
    
    start:
    
    hSerin 100,start,[wait ($ff),str string\16]
    
    pause 100
           
    HSEROUT [hex string,13,10]
    
    pause 500
    
    goto start
    obviously it doesn't work because my poor programming skills.
    I searched the forum to find something to point me how to capture these data but no success.
    Can someone point me how to do that?
    Thanks,
    Louis
    Last edited by louislouis; - 26th September 2020 at 17:44.

  2. #2
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,516


    Did you find this post helpful? Yes | No

    Default Re: Ultrasonic sensor, serial communication.

    Try something like:
    Code:
    Distance VAR WORD
    Sum VAR BYTE
    
    Start:
      HSERIN 1000, NoData, [wait ($FF), Distance.BYTE1, Distance.BYTE0, SUM]
      ' Here you could evalute SUM to verify packet is correct but lets not do that now.  
       HSEROUT["Distance: ", DEC Distance, "mm", 13]
    Goto Start
    
    
    NoData:
      HSEROUT["No data from sensor", 13]
      Goto Start
    /Henrik.

  3. #3


    Did you find this post helpful? Yes | No

    Default Re: Ultrasonic sensor, serial communication.

    Thanks Henrik,

    It works.

    How simple and elegant solution.

Similar Threads

  1. Need help setting up my ultrasonic distance sensor
    By Megahertz in forum mel PIC BASIC Pro
    Replies: 18
    Last Post: - 26th June 2010, 15:06
  2. Ultrasonic distance sensor with PIC16F84A
    By MrRoboto in forum mel PIC BASIC
    Replies: 3
    Last Post: - 29th June 2009, 10:01
  3. Water Level Detection using Ultrasonic Sensor
    By Balachandar in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 12th November 2007, 05:59
  4. sensor ultrasonic 8051&PIC
    By hd_uni_pro in forum Schematics
    Replies: 1
    Last Post: - 13th September 2006, 13:58
  5. ultrasonic sensor
    By PoTeToJB in forum mel PIC BASIC Pro
    Replies: 8
    Last Post: - 9th May 2006, 21:26

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts