Go Down

Topic: Ultrasonic range sensor HC-SR04 using PIC16F877A (Read 4234 times) previous topic - next topic

ABS_BLT

May 09, 2012, 11:14 pm Last Edit: May 09, 2012, 11:28 pm by ABS_BLT Reason: 1
Hi,
I m trying to use the HC-SR04 with the PIC16F877A to mesure the altitude of my plane from the ground
I connected the Vcc pin of HC-SR04 to +5V (coming form the voltage regulator KIA7805A), the GND pin to the ground pin of the voltage regulator, the trig pin to PIN A0 of PIC16F877A, the echo pin to PIN A1.
I wrote this code using CCS and I downloaded it to my PIC:

#include <16F877A.h>

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES HS                       //High speed Osc (> 4mhz for PCM/PCH) (>10mhz for PCD)
#FUSES NOPUT                    //No Power Up Timer
#FUSES NOPROTECT                //Code not protected from reading
#FUSES NODEBUG                  //No Debug mode for ICD
#FUSES NOBROWNOUT               //No brownout reset
#FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOCPD                    //No EE protection
#FUSES NOWRT                    //Program memory not write protected

#use delay(clock=20000000)

#use rs232 (baud=9600, xmit=PIN_C6, rcv=PIN_C7)



void main()
{
  char c; int16 i;
  set_tris_a(0b00000010);
  setup_timer_1(T1_INTERNAL|T1_DIV_BY_4);
 
  while(1)
  {
     c=getch();
     if(c=='Y')
     {
        output_high(PIN_A0);
        delay_us(10);
        output_low(PIN_A0);
        while(!input(PIN_A1));
        timer1(0);
        while(input(PIN_A1)!=b);
        i=get_timer1();
        i=i/(int32)(73);        //=distance in cm     because: cm=us/58=timer*0.8/58
        printf("%c%c",(int)(i>>8,(int)(i));
     }
  }
}



the problem is that it gives me always the same value ( 6 cm ). So please I need help to solve this problem.
thank you

AWOL

Quote
d I downloaded it to my PIC:

I really don't know how to break this to you.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.


AWOL


Try the following link, it will help you
Interfacing HC-04 Ultrasonic Sensor with PIC Microcontroller

I think that after two-and-a-bit years, they will already have found that.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Go Up