0

I have a problem with HCSR04 ultrasonic distance sensor. I wrote a code that works perfectly in Proteus simulator but when I upload it in real microchip it stop working properly. Looks like it sticks at one while loop and never get a echo signal. Btw when I'm uploading the code with PicKit that few seconds while code is uploading looks like system is working because led is blinking, after few seconds after upload the system stops and only one led shines without blinking.

#define _XTAL_FREQ 8000000

#include <xc.h>
#include <stdlib.h>

#pragma config FOSC = INTIO67
#pragma config WDTEN = OFF
#pragma config LVP = OFF

#define RB0 PORTBbits.RB0

#define RA1 PORTAbits.RA1
#define RA2 PORTAbits.RA2
#define RA6 PORTAbits.RA6


void delay (unsigned int ms)
{
    unsigned int i;
    while (ms--) {
        for (i = 0; i < 398; i++);
    }
}

void main(void) {
    
    float time;
    float distance;
    
    OSCCONbits.IRCF2 = 1;
    OSCCONbits.IRCF1 = 1;
    OSCCONbits.IRCF0 = 0;
    
    ANSELA = 0; //RA0,RA1- analoginis iejimas
    ANSELB = 0;
    ANSELC = 0;
    
    LATA = 0;
    TRISA = 0b00010000;
    LATB = 0;
    TRISB = 0;
    LATC = 0;
    TRISC = 0;
    
    T0CON = 0x80; // Konfiguruojamas Timer0
    
    while(1){
        
        TMR0H = 0;
        TMR0L = 0;

        RB0 = 0; //Trigger 0
        __delay_us(2);
        RB0 = 1; //TRIGGER 1
        __delay_us(10);
        RB0 = 0; //TRIGGER 0
        
        while(!PORTAbits.RA4); //Waiting for Echo
        TMR0ON = 1;
        TMR0=0;

        while(PORTAbits.RA4); //Waiting for Echo goes LOW
        TMR0ON = 0; //Timer Stops
        time = TMR0;
        
        distance = ((float)time/58.82);
        distance = distance + 1;
        
        RA2 = 0;
        RA1 = 1;
        delay(distance);
        RA1 = 0;
        RA2 = 1;
        delay(distance);
    }
}
Mike
  • 2,146
  • 1
  • 14
  • 29
proestpa
  • 1
  • 4
  • 1
    Passing float to a busy-delay function won't work well. Also, your MCU doesn't have a FPU to begin with, so you should get rid of `float` and replace it with fixed point. That is `time / 5882` or `time / 5882000ul` or whatever accuracy you need. Also, shouldn't it be distance = speed * time...? Where's the magic number 58.82 coming from? When all of that is fixed, you should look at on-chip hardware peripheral timers instead of bit-banging with busy-delays. – Lundin Mar 26 '21 at 08:38
  • 1
    PORTAbits.RA4 might never go low if no echo is received (stays high). You might want to have a timeout while waiting for an echo. One possibility is using the WATCHDOG timer to get you out of the while loop that causes blocking. – glen_geek Mar 26 '21 at 14:50
  • Thanks, glen_geek. All i had to do is insert one line with delay before echo and now it works in real life. :) – proestpa Mar 27 '21 at 08:01
  • 2
    On SE you need to post the solution as an answer and then "accept" it to mark it solved. Otherwise the system keeps popping it back into the main page looking for a solution. – Transistor Mar 27 '21 at 08:41
  • 1
    Welcome to SE/EE! Please take the [tour] to learn how this site works. It is not a forum. – the busybee Mar 27 '21 at 09:15
  • @RussellMcMahon, I think you've deleted the one-line answer posted by the OP. It is a complete answer but you could be forgiven for thinking it was a comment. OP just needs to accept the one-liner. – Transistor Mar 28 '21 at 18:49
  • @Transistor Thanks. Yes I did. | No I sort of did and didn't :-). It was 1st posted as a comment and THEN as an answer. I considered leaving it BUT instead deleted it, left the comment version AND added it to the top of the question. BUT I have now undeleted it - so it now appears in 3 places :-). I'll let it settle and I or someone can tidy it up after a whle. – Russell McMahon Mar 29 '21 at 12:55

1 Answers1

0

All I had to do is insert one line with delay before echo and now it works in real life.

Mike
  • 2,146
  • 1
  • 14
  • 29
proestpa
  • 1
  • 4
  • 3
    @proestpa, please accept your own answer to indicate to the system that your question has been answered. SE doesn't do '[solved]' in the title to indicate that. Thanks. – Transistor Mar 29 '21 at 21:11