Need help setting up my ultrasonic distance sensor


Closed Thread
Results 1 to 19 of 19

Hybrid View

  1. #1
    Join Date
    Nov 2009
    Location
    London
    Posts
    251


    Did you find this post helpful? Yes | No

    Default

    Here goes my code for 16F676. Used this IC because I can program it in PICKIT2 & also I have jumpered my sensor around it.
    Code:
    Include "modedefs.bas"
    DEFINE     NO_CLRWDT    1
    DEFINE          OSC     4       ' OSCCON defaults to 4MHz on reset
    DEFINE OSCCAL_1K 1
    
    @ __Config _INTRC_OSC_NOCLKOUT & _WDT_OFF & _PWRTE_ON & _MCLRE_OFF & _BODEN & _CP & _CPD
    
    PAUSE 50
    Tx Var PortA.1
    Rx Var PortA.0
    power var PortC.1
    	TRISA = %000001
    	TRISC = %000001
    	CMCON = 7
    	ANSEL = 0
          OPTION_REG = %10000111  ' RAPU = off, PS 1:256 to TMR0
    	PORTA=0
    	PORTC=0
    	' setup iZCD to interrupt on change
    	INTCON = 0
    
    ; PortC.0 = PWM Receive ; ; ; ; ; PortC.1 = Power_On ; ; ; ;  PortA.0 = Receive Pin ; ; ; ; PortA.1 = Transmit Pin ; ; ; ; PortA.2=Trigger Pin
    
    main:
    High power
    pause 100
    Serout Tx, N9600, ["0x44+0x00+160+ ???"]	;	To set lower threshold distance to appx 3 meters. What should be the SUM & how do you calculate it?
    Serout Tx, N9600, ["0x44+0x01+250+ ???"]	;	To set upper threshold distance to appx 5 meters
    Serout Tx, N9600, ["0x44+0x02+0xaa+ ???"] ; 	To set operation mode to autonomous mode
    
    	if PortA.2=0 then
    		High PortC.2 ; led 1
    	else
    		high PortC.5 ; led 2
    	endif
    goto main
    end
    1) How do you calculate the SUM? any example please
    Once I get to the next point, I will update here if I get stuck. Thanks

  2. #2
    Join Date
    Nov 2005
    Location
    Perth, Australia
    Posts
    429


    Did you find this post helpful? Yes | No

    Default

    try something like this (taking your 1st serout line as an example):

    Serout Tx, N9600, [$44,$00,$A0,$E4]

    $A0 is just 160 in hex.

    To work out the sum: $44+$00+$A0=$E4

    If the sum is bigger than $FF then you take just the lower byte. e.g. if the sum came to $01A2, then you just use $A2.
    "I think fish is nice, but then I think that rain is wet, so who am I to judge?" - Douglas Adams

  3. #3
    Join Date
    Nov 2009
    Location
    London
    Posts
    251


    Did you find this post helpful? Yes | No

    Default

    Hi, I tried this:
    Code:
    main1:
    High power
    Pause 100
    Serout Tx, N9600, ["0x44+0x00+0xA0+0xE4"] ;To set lower threshold distance to appx 3 meters
    Pause 100
    Serout Tx, N9600, ["0x44+0x01+0xD1+0xFE"] ;To set upper threshold distance to appx 5 meters
    Pause 100
    Serout Tx, N9600, ["0x44+0x02+0xaa+0xF0"] ; To set operation mode to autonomous mode
    main:
    	if PortA.2=1 then
    		high PortC.2
    	else
    		 toggle PortC.5
    	endif
    pause 50
    goto main
    end
    And also this:
    Code:
    Serout Tx, N9600, [$44,$00,$A0,$E4] ;To set lower threshold distance to appx 3 meters
    Pause 100
    Serout Tx, N9600, [$44,$01,$D1,$FE] ;To set upper threshold distance to appx 5 meters
    Pause 100
    Serout Tx, N9600, [$44,$02,$aa,$F0] ;To set operation mode to autonomous mode
    but no luck - any advise?

  4. #4
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,653


    Did you find this post helpful? Yes | No

    Default

    Hi,

    The question is ...

    did you use a RS232 chip on YOUR board ??? ( like in the datasheet scheme )

    more ...

    Could you , please , attach YOUR scheme ???

    Alain
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  5. #5
    Join Date
    Nov 2009
    Location
    London
    Posts
    251


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Acetronics View Post
    Hi,

    The question is ...

    did you use a RS232 chip on YOUR board ??? ( like in the datasheet scheme )

    more ...

    Could you , please , attach YOUR scheme ???

    Alain
    Hi, here is my hardware photo, just my sensor jumpered to my PICKIT2 in the following configuration:
    ; PortC.0 = PWM Receive ; ; ; ; ; PortA.0 = Receive Pin ; ; ; ; PortA.1 = Transmit Pin ; ; ; ; PortA.2=Trigger Pin
    PWR_ON is directly connected to 5V on the PICKIT2
    Also I am using TTL I/O, I have arranged the jumpers accordingly.
    Attached Images Attached Images   

  6. #6
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,653


    Did you find this post helpful? Yes | No

    Default

    Hi,

    OMG ...

    I also think you are exceeding the Pickit sourcing authorized current ... Especially when " pinging " ...

    Alain
    Last edited by Acetronics2; - 22nd June 2010 at 17:47.
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  7. #7
    Join Date
    Nov 2009
    Location
    London
    Posts
    251


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Acetronics View Post
    Hi,

    OMG ...

    I also think you are exceeding the Pickit sourcing authorized current ... Especially when " pinging " ...

    Alain
    Oops! will try everything again with seperate supply and update here. May be this is the reason that it is not working.

Members who have read this thread : 0

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