CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

ultrasonic range finder

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
aasief



Joined: 12 Feb 2009
Posts: 12
Location: cape town

View user's profile Send private message Send e-mail

ultrasonic range finder
PostPosted: Wed Apr 29, 2009 6:22 am     Reply with quote

Could someone tell me whats wrong with this code
Code:


#include "16f877.h"
#use delay(clock=4000000)
#define use_portb_lcd
#include "lcd.c"
#int_ccp1

unsigned int time;
void main()
{   int x;
   
    lcd_init();

   while(1)
  {
    output_low(pin_a5);
    setup_ccp1(ccp_capture_re);
    setup_timer_1(t1_internal);
    for(x=0;x<5;x++)
    {
   output_high(pin_d2);
   delay_us(90);
   output_low(pin_d2);
   delay_us(25);
   }
   delay_us(1000);
    output_high(pin_a5);
    enable_interrupts(global);
    enable_interrupts(int_ccp1);
    delay_ms(60);
   
  }
}   

void isr_ccp1()
{
   time=ccp_1;
   printf(lcd_putc"t=%5u  ",time);
   set_timer1(0);
   clear_interrupt(int_ccp1);
   
}
bungee-



Joined: 27 Jun 2007
Posts: 206

View user's profile Send private message

PostPosted: Wed Apr 29, 2009 7:43 am     Reply with quote

This code need some serious rewriting. Couple of years I wrote similar program for the 16F84A in the assembly.

At the first glimpse your frequency is not 40kHz. I suppose that you use 40kHz ultrasound transmiter/receiver pair. Your code transmits at ~ 8,7kHz.

Here is the flow how you should do this:

Code:

           Init the PIC
                |
                |
       Send ping (8 pulses at 40kHz) <---------+
          Set timer1 to 0                      |
                |                              |
                |                              |
        Delay (see comment *)                  |
                |                              |
                |                              |
        Check if pong received <--------+      |
                |                       |      |
                +----- NO --------------+      |
                |                              |
        Calculate distance from time           |
                |                              |
                |                              |
          Display distance                     |
                |                              |
                |                              |
                +------------------------------+


* Delay is used because first reply is not the correct one. Determine that based on experience.
aasief



Joined: 12 Feb 2009
Posts: 12
Location: cape town

View user's profile Send private message Send e-mail

PostPosted: Thu Apr 30, 2009 5:06 am     Reply with quote

Thanx for ur response.

The delays I'm using are correct because I get 38.7 kHz out. I don't know why I do because looking at it, the PIC isn't doing what it's supposed to be doing.
I'm having a problem with capturing the pulse. I am getting a received pulse and I measured it and I get the correct time delay for distance measured.
I suspect that whatever is making my delays not work, is the same thing that's making my capture not work. Is there something wrong with my void isr loop ?

Thanx
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group