Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

How to write a CCS code for ultrasonic sensor HC-SR04

Status
Not open for further replies.

digiad

Newbie level 5
Joined
Apr 6, 2014
Messages
9
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
117
Hi all,
I'm trying to implement a HC-SR04 ultrasonic sensor with PIC16F877A. The aim is to detect the distance and write the distance to a 16*2 LCD display. I would like to measure the distance between the sensor and the detected and write it to a LCD display. Moreover, if the distance is less than 70 cm, I want to light up a led. Below is the CCS code. However, it does not work correcty. Can you just help me figuring out what is wrong in the code? Thank you.



Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#use fast_io(b)
#define use_portb_lcd TRUE
#include <lcd.c>
 
#define trig pin_C1    
#define echo pin_C0
float distance, time;   
 
void main()
{
 
   setup_adc_ports(NO_ANALOGS);
   setup_adc(ADC_OFF);
   setup_psp(PSP_DISABLED);
   
   set_tris_b(0x00);
   lcd_init(); 
   printf(LCD_PUTC, "\fWelcome ");
   delay_ms(1000); 
 
  
  setup_timer_1(T1_INTERNAL|T1_DIV_BY_8);
  
   while(1)
   {
    output_high(trig);                       
    delay_us(20);                            // sending 20us pulse 
    output_low(trig); 
   
    set_timer1(0); 
    while(!input(ECHO))                       // wait for high state of echo pin 
    {} 
    set_timer1(0);                           // setting timer zero 
 
    while(input(ECHO))                       
    {} 
 
     time=get_timer1();                       // Getting the time 
 
     distance=time*0.028 + 1.093 ;            // Calculating the distance 
                            
                            
   printf(LCD_PUTC, "\fTime :%f \nDistance = %f",time,distance); // Putting the time and 
                                     //distance to the LCD 
   delay_ms(1000); 
 
   if (distance < 70)
   {
   output_high(pin_c3);
   }
   else 
   {
   output_low(pin_c3);
    }
    }
    }

 
Last edited by a moderator:

I'm from in china, I focus on the parking lot,so i use the ultrasonic sensor.First you must transfer eight the square wave,then the HR04 will transfer a square wave for indicate the distance.so after you transfer eight square wave and "while(!input(ECHO))" ,you must clear the timer value and turn on the timer ,"while(!input(ECHO))",you must stop the timer ,then you can read the timer value....
 

hello,

What is your FOSC or quartz value ?
Cast the result of get_timer()

Code:
time=(float) get_timer1();

what result do you get for some known distances
ie :
10cm
50cm
100cm
on your display
Code:
  printf(LCD_PUTC, "\fTime :%f \nDistance = %f",time,distance); // Putting the time and

Maybe you better had to printout the value of timer1 by using an unsigned int16..
to test RAW value , before transform to cm
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top