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