nblrehman
Newbie level 2
- Joined
- May 10, 2014
- Messages
- 2
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1
- Activity points
- 26
Hello..
this is my very first thread...
I am a student.. I am stuck on this project of mine..
I am using PIC16F877A with 20MHz crystal.. and HC-SR04 sensor..
there is something wrong with my code.. When I set the range from 1 to 2 cm.. the program works perfect.. but as soon as I increase the range to 3 or greater than that... the LED ie, RC0 remains set.
the program is shown below... PLEASE HELP....
btw.. it is created on MPLABX using XC8 compiler..
the project file is attached...!!
this is my very first thread...
I am a student.. I am stuck on this project of mine..
I am using PIC16F877A with 20MHz crystal.. and HC-SR04 sensor..
there is something wrong with my code.. When I set the range from 1 to 2 cm.. the program works perfect.. but as soon as I increase the range to 3 or greater than that... the LED ie, RC0 remains set.
the program is shown below... PLEASE HELP....
btw.. it is created on MPLABX using XC8 compiler..
the project file is attached...!!
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 #include <stdio.h> #include <stdlib.h> #include <xc.h> #define _XTAL_FREQ 20000000 __PROG_CONFIG(1,0x3f32); /* * */ int main(int argc, char** argv) { int width; int range; int time; TRISB=0x04; TRISC=0x00; Loop: PORTB=0x00; width=0; time=0; range=0; RB1=1; //send pulse with a 20us width __delay_us(20); PORTB=0x00; while (RB2==0) //wait until an echo is received { } while (RB2==1) //calculate the width of the echo received { __delay_us(10); width++; } range=(width*10)/58; //calculate range if (range<2) //if range less than 100m turn on LED { RC0=1; __delay_ms(500); } else {PORTC=0x00; __delay_ms(100);} goto Loop; return (EXIT_SUCCESS); }
Attachments
Last edited: