Pages: [1]   Go Down
Author Topic: Ultrasonic range sensor HC-SR04 using PIC16F877A  (Read 3387 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
« Last Edit: May 09, 2012, 04:28:05 pm by ABS_BLT » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24327
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
d I downloaded it to my PIC:
I really don't know how to break this to you.
Logged

"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.

Pages: [1]   Go Up
Jump to: