no signal detection, need help

Status
Not open for further replies.

Greyjo

Newbie level 2
Joined
Apr 24, 2012
Messages
2
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,298
Hello,

I'm trying to build an ultrasonic distance meter by using 16f877a. I can transmit 40kHz signal from an external oscillator and get the

reflected signal with no problem. However, when i try to transmit a signal by setting the output pin (C1 at the program) 'high' for about

200us to activate oscillator and wait for the reflected signal, program stucks at 'while(input(echo)) {}' loop which indicates no signal

detected (constant 'high'=no signal). I'm using LM311 comparator, and its output is directly connected to the microcontroller's input pin

mentioned as 'echo'. Btw, signal detection can be observed from the comparator's output with a square wave (low=signal detected).

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


#use fast_io(b)        
#use fast_io(c)         
#define trig PIN_C1     
#define echo PIN_C0     
#define use_portb_lcd TRUE 
#include <lcd.c>   

int16 distance, time;          



void main()
{
   
   setup_adc_ports(NO_ANALOGS);
   setup_adc(ADC_OFF);
   setup_psp(PSP_DISABLED);
   setup_spi(SPI_SS_DISABLED);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
   setup_timer_1(T1_INTERNAL|T1_DIV_BY_1);     
   setup_timer_2(T2_DISABLED,0,1);
  
   set_tris_c(0x01);  
   set_tris_b(0x00);  
   lcd_init();      
      
   
   printf(LCD_PUTC, "\f TEST");

   
   while(true)     
{

   output_high(PIN_C1);                       //starting the transmission  
   delay_us(200);                            
   output_low(PIN_C1);                      
   
  
   set_timer1(0);                            // no interrupts used, timer1's current value should be the time passed to calculate distance.
   while(input(echo))                       // echo (C0) , the input which is waiting for square wave to detect
   {}

   time=(get_timer1()/5);                       

   distance=(time*0.0341)/2 ;            
                           
   printf(LCD_PUTC, "\fTime:%Lu us\nDistance =%Lu cm",time,distance); 
                                     
  
}
}

The other parts of the circuit and program is working well. I haven't been able to figure out the problem since it's also working quite good

on proteus simulation. This is my first work with microcontollers and i cannot find the error here. Can anyone give me some advice?

Sorry for long text
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…