Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
hc sr04 pic code
#1

hc sr04 pic code

#include <16F877.h>
#device adc=8
#FUSES NOWDT, HS, NOPUT, PROTECT, NOBROWNOUT, NOLVP, NOCPD, NOWRT, NODEBUG
#use delay(clock=20000000)

/**
/ Used sonar - HC-SR04 & DYP ME007
/ If you are using DYP ME 007 or any other 5 pin sonar, Please note that not to take the
/ pin named 'out'. Take echo instead.
/ Pin configuration (5 pin)- VCC(+5) TRIG(Trigger pin) ECHO(Output pin) OUT(Not needed) GND (Ground)
/ Pin configuration (4 pin)- VCC(+5) TRIG(Trigger pin) ECHO(Output pin) GND (Ground)
/
/ Code by Bhanu Watawana
/ 0778111887
/ Uva Wellassa University - Sri Lanka
/ Mechatronics
*//

#define LCD_TYPE 2
#include <lcd.c>

int16 distance, time; // Defining variables

// Defining the pins

#define trig pin_B1 // Change as you wish, can use any pin in the MCU
#define echo pin_B0 // Change as you wish, can use any pin in the MCU

void main()
{
lcd_init(); // initiating the LCD

printf(LCD_PUTC, "\f Sonar test \n Code by Bhanu ");// for LCD & MCU restart troubleshooting

delay_ms(1000); // Boot-up delay, for troubleshooting

setup_timer_1(T1_INTERNAL T1_DIV_BY_8); // initiating timer

while(true)
{

output_high(trig); // ping the sonar
delay_us(20); // sending 20us pulse
output_low(trig);

while(!input(ECHO)) // wait for high state of echo pin
{}

set_timer1(0); // setting timer zero

while(input(ECHO)) // Wait for high state of echo pin
{}

time=get_timer1(); // Getting the time

distance=time*0.028 + 1.093 ; // Calculating the distance


printf(LCD_PUTC, "\fTime :%Lu \nDistance = %Lu",time,distance); // Putting the time and
//distance to the LCD
delay_ms(1000);

}
}
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

Powered By MyBB, © 2002-2024 iAndrew & Melroy van den Berg.