+ Post New Thread
Results 1 to 11 of 11
  1. #1
    Junior Member level 3
    Points: 397, Level: 4

    Join Date
    Nov 2014
    Posts
    29
    Helped
    0 / 0
    Points
    397
    Level
    4

    Ultrasonic Distance Measurement Using HC-SR04 Sensor, PIC18F4580 and C18 Compiler

    hello, i have convert the code to c18 mplab but it don't seem to produce the result, can u help me to solve this? i'm using pic18f4580 with xtal 20Mhz
    Code:
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    
    #pragma config OSC= HS
    #pragma config WDT=OFF
    #pragma config LVP=OFF
    
    #define Trig PORTBbits.RB3
    #define Echo PORTBbits.RB2
    
    void T0Delay10us(void);
    void T0Delay5ms(void);
    void uart(void);
    void Call_Init_PIC (void);
    void Call_Init_USART (void);
    void sendPulse(void);
    
    unsigned int distance;
    
    
    void main(void)
    {
    Call_Init_PIC ();
    uart();
    while(1)
    {
    
    sendPulse();
    while(Echo == 1);
    TMR1H= 0;
    TMR1L= 0;
    T1CON = 0x01;
    while(Echo == 0);
    distance = (TMR1H<<8) + TMR1L;
    T1CON = 0x00;                            
    distance = (distance/58)*20;
    //distance = (distance/58);
    printf("Distance = %3d\v\n",distance); 
    Delay10KTCYx(255); 
    PIR1bits.TMR1IF=0;
    T0Delay5ms(); 
    }
    }
    
    void Call_Init_PIC (void)
    {
    TRISBbits.TRISB3=0;
    TRISBbits.TRISB2=1;
    }
    
    
    void uart(void)
    {
    TXSTA=0B00100000;
    SPBRG=32;
    TRISCbits.RC6=0;
    RCSTAbits.SPEN=1;
    }
    
    
    void sendPulse(void)
    	{
    	Trig=1;
    	T0Delay10us();
    	Trig=0;
    	}
    
    
    void T0Delay10us()
    	{
    	T0CON=0x08;
    	TMR0H=0XFF;
    	TMR0L=0XCE;
    	T0CONbits.TMR0ON=1;
    	while(INTCONbits.TMR0IF==0);
    	T0CONbits.TMR0ON=0;
    	INTCONbits.TMR0IF=0;
    	}
    
    
    void T0Delay5ms()
    	{
    	T0CON=0x08;
    	TMR0H=0X9E;
    	TMR0L=0X58;
    	T0CONbits.TMR0ON=1;
    	while(INTCONbits.TMR0IF==0);
    	T0CONbits.TMR0ON=0;
    	INTCONbits.TMR0IF=0;
    	}

  2. #2
    Banned
    Points: 12,436, Level: 26

    Join Date
    Apr 2013
    Posts
    2,528
    Helped
    538 / 538
    Points
    12,436
    Level
    26

    Re: Ultrasonic Distance Measurement using HC-SR04 sensor and PIC16f887

    Use C18 USART library and write code to send cm data to USART.



    •   AltAdvertisment

        
       

  3. #3
    Junior Member level 3
    Points: 397, Level: 4

    Join Date
    Nov 2014
    Posts
    29
    Helped
    0 / 0
    Points
    397
    Level
    4

    Re: Ultrasonic Distance Measurement using HC-SR04 sensor and PIC16f887

    i have tried putrsUSART to send to hyper terminal..but can cant get any output in the terminal.



  4. #4
    Banned
    Points: 12,436, Level: 26

    Join Date
    Apr 2013
    Posts
    2,528
    Helped
    538 / 538
    Points
    12,436
    Level
    26

    Re: Ultrasonic Distance Measurement using HC-SR04 sensor and PIC16f887

    Zip and post your latest complete mplab C18 project files. Did you initialize USART with OpenUSART() function ?



    •   AltAdvertisment

        
       

  5. #5
    Junior Member level 3
    Points: 397, Level: 4

    Join Date
    Nov 2014
    Posts
    29
    Helped
    0 / 0
    Points
    397
    Level
    4

    Re: Ultrasonic Distance Measurement using HC-SR04 sensor and PIC16f887

    yes i did sir, and this is my complete c18 project files



    •   AltAdvertisment

        
       

  6. #6
    Banned
    Points: 12,436, Level: 26

    Join Date
    Apr 2013
    Posts
    2,528
    Helped
    538 / 538
    Points
    12,436
    Level
    26

    Re: Ultrasonic Distance Measurement using HC-SR04 sensor and PIC16f887

    I didn't fine any C18 USART initialize function OpenUSARTx() in your code. Anyway here is a Hi-Tech C Code for PIC16F UART. You can study the code and implement it in your C18 code.

    http://saeedsolutions.blogspot.in/20...imulation.html

    - - - Updated - - -

    Try this code.



  7. #7
    Junior Member level 3
    Points: 397, Level: 4

    Join Date
    Nov 2014
    Posts
    29
    Helped
    0 / 0
    Points
    397
    Level
    4

    Re: Ultrasonic Distance Measurement using HC-SR04 sensor and PIC16f887

    Hello sir, i have update my coding wit openusart, but i stil cant gt the output, i gt the output in terminal like this
    +Von+fo~#Vn+f~+Vn3f~+Von3f ~+Von3fv+Von3Vv+fo
    i have attach my updated coe..hope u can find my mistakes sir..thank you

    - - - Updated - - -

    Click image for larger version. 

Name:	uss.png 
Views:	7 
Size:	54.1 KB 
ID:	111669
    hello sir, i have tried the given code,this is the output i get in terminal :(



  8. #8
    Banned
    Points: 12,436, Level: 26

    Join Date
    Apr 2013
    Posts
    2,528
    Helped
    538 / 538
    Points
    12,436
    Level
    26

    Re: Ultrasonic Distance Measurement using HC-SR04 sensor and PIC16f887

    Don't use putrsUSART(), instead use putsUSART().

    I am attaching two versions of code. One is MPLAB C18 and another mikroC. The mikroC version is working in Proteus and also hardware and giving somewhat correct values but C18 version is not giving correct values. Don't know why.

    - - - Updated - - -

    I have written a HC SR04 code which uses Timer1 and INTx pin and it is giving very accurate readings. I will post the code soon. I have tested it both in Proteus and also hardware with PIC16F877A.



  9. #9
    Junior Member level 3
    Points: 397, Level: 4

    Join Date
    Nov 2014
    Posts
    29
    Helped
    0 / 0
    Points
    397
    Level
    4

    Re: Ultrasonic Distance Measurement Using HC-SR04 Sensor, PIC18F4580 and C18 Compiler

    hello sir..i have tried both, HC sr04 does not produce output at all..and the simple usonic produces output as the image below, the bad part is, i tried to plug the ground connection off the sensor,yet it still printing result in terminal, meaning the data is not from the sensor.



    •   AltAdvertisment

        
       

  10. #10
    Junior Member level 3
    Points: 397, Level: 4

    Join Date
    Nov 2014
    Posts
    29
    Helped
    0 / 0
    Points
    397
    Level
    4

    HC-SR04 ultrasonic sensor problem.

    Why my ultrasonic sensor HC-SR04 sensor cannot detect length more that 230 cm, it can detect up to 230 cm only but in the datasheet. it is stated it can detect up to 400 cm, im using pi18f4580 with crystal 20Mhz. thank you.



  11. #11
    Advanced Member level 4
    Points: 7,825, Level: 21
    Achievements:
    7 years registered
    paulfjujo's Avatar
    Join Date
    Jun 2008
    Location
    France 01120
    Posts
    1,291
    Helped
    261 / 261
    Points
    7,825
    Level
    21

    Re: Ultrasonic Distance Measurement Using HC-SR04 Sensor, PIC18F4580 and C18 Compiler

    hello,
    don't forget to use LATBbits.LATb3 for trig define
    if you are using Pic18F.
    Measure max less than 4M ?
    Was is the power supply ?
    i also tested HC-SR04 and get less than 3M. as maximum.
    i think is a problem of power signal on sender part..
    needs a hardware change to boost the signal

    Materual comming from RPC are low cost and also low quality.All modules can differe in performances values..



--[[ ]]--