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 support@ccsinfo.com

Ultrasonic timer or counter (CCP capturing time?)

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



Joined: 17 Nov 2010
Posts: 38
Location: 121

View user's profile Send private message

Ultrasonic timer or counter (CCP capturing time?)
PostPosted: Thu Mar 17, 2011 6:22 am     Reply with quote

Hi,

Knowing that speed of sound is around 340m/s or (34000cm/s)
that mean sound take around 1/34000= 29.4us to travel 1 cm.

Now, the ultrasonic range finder use for finding the short range distance.

In my program, timer will start count when the moment i transmit the 40khz wave go out.

As usual, i using timer0 to starting the count. my interrupt is set to 1 micro second. (i use 20Mhz crystal). So i set as below

Code:
setup_counters(RTCC_INTERNAL,RTCC_DIV_1);


Code:
#INT_RTCC
void counter_ISR()
{
   set_rtcc(250);
 
      xmit_lsb++;
      if (xmit_lsb==65535)
      {
         xmit_msb++;
         xmit_lsb=0;
      }
 
}


*Notes xmit_lsb 7 xmit_msb is a variable help me store the value time that consume from PIC transmit back to PIC 40khz signal from the ultrasonic receiver circuit.

In later, i use the formula to calculate distance:

time = (xmit_lsb*0.000001) + (xmit_msb*0.065536)

distance = time * 34000/2

and the output will display in my lcd. But before proceed to ultrasonic circuit, i trying to verify the timer to mathematic conversion. But seel like the timer is not really run in the expected that i want.

It is more more slower. i wonder why?

is it i can not just set for a very short interrupt since 1microsecond just 5 times of the PIC oscillate speed which is 0.2 microsecond (20Mhz)

Anyone can give me advice?


Last edited by sysysy on Thu Mar 17, 2011 11:30 pm; edited 1 time in total
ALPL



Joined: 29 Mar 2009
Posts: 30

View user's profile Send private message

PostPosted: Thu Mar 17, 2011 7:24 am     Reply with quote

Hi, I would rather suggest to use the capture-module to accurately measure the time the ultrasonic wave needs to hit a target and to return to the PIC.
SherpaDoug



Joined: 07 Sep 2003
Posts: 1640
Location: Cape Cod Mass USA

View user's profile Send private message

PostPosted: Thu Mar 17, 2011 9:22 am     Reply with quote

Interrupts are slower than people think. A PIC takes dozens to hundreds of instruction times to enter and exit an interrupt. As ALPL says, the Capture module is the way to do this.
_________________
The search for better is endless. Instead simply find very good and get the job done.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Mar 17, 2011 12:04 pm     Reply with quote

You didn't say where you got your range finder module, but it probably
has drivers for PicBasic Pro, or Basic Stamp, and they use the Pulsin
command. Well, try using the Pulsin command for CCS, as given in
this thread:
http://www.ccsinfo.com/forum/viewtopic.php?t=42353
sysysy



Joined: 17 Nov 2010
Posts: 38
Location: 121

View user's profile Send private message

PostPosted: Thu Mar 17, 2011 12:52 pm     Reply with quote

Thanks for the suggestion. i will read through the details and try it.


Hi PCM Programmer,

Quote:
You didn't say where you got your range finder module


Actually i build the circuit by my own. i didn't buy the module.
the signal that received by the ultrasonic will be amplified 1000 times by the op-amplifier and the pass through the peak detector. Finally, it will give an comparator output when there is a signal receive. and this output will direct connect to my PIC. so now the crucial part is programming inside the PIC.

i will go read about the suggestion that u provide me. If got any problem will ask u again.o

Between, is it my PIC(16f877a) has a CCP feature, then i am recommended to use the CCP method is better?

Thanks alot.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Mar 17, 2011 1:23 pm     Reply with quote

This post shows how to use the CCP to measure pulse width.
http://www.ccsinfo.com/forum/viewtopic.php?t=31470&start=6
sysysy



Joined: 17 Nov 2010
Posts: 38
Location: 121

View user's profile Send private message

PostPosted: Thu Mar 17, 2011 11:29 pm     Reply with quote

Hi,

From the Pulse width post, it shows how to capture the pulse width within 500ms from the CCP pin. hmm..i think i did digest the code.

But may i know how about to use it in capture the time such as ( it take 30ms time from transmit out to return back the 40khz signal into PIC) how actually to capture this exact time?

From what i think is, the timer must start to count once we transmit the 40khz signal out and stop once the 40khz reflected back signal detected by the pin. so, let say we use 20Mhz (0.2microseconds every oscillation) ...is it 16bit timer1 only can count from ( 65536 x 0.2us) = 0.0131072 seconds only?

but how to capture this with ccp mode?
SherpaDoug



Joined: 07 Sep 2003
Posts: 1640
Location: Cape Cod Mass USA

View user's profile Send private message

PostPosted: Fri Mar 18, 2011 5:45 pm     Reply with quote

I don't know your PIC, but most have a prescaler, a divider before the timer used to slow it down.
_________________
The search for better is endless. Instead simply find very good and get the job done.
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