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

measuring the time of input signal

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



Joined: 03 Dec 2011
Posts: 27

View user's profile Send private message

measuring the time of input signal
PostPosted: Sat Jan 21, 2012 12:04 am     Reply with quote

hi every one
how to measure the time of input signal on pic16f84 pin ,I tried to write code but i think its not right
Code:
#include <16F84.h>
#use delay(clock=4000000)
void main() {
int n;
setup_timer_0(rtcc_internal|rtcc_DIV_1); // tick every 1us
n=get_timer0();       
output_low(pin_b0);     //  turn off led
while (true)
{
if(input(pin_b1))       //  high input start
{set_timer0(0);         //  start counting ...
if(!input(pin_b1))      //  high input stop
{if (n<250)             //  if its high less than 250us
output_high(pin_b0);}   //  turn on led
}

}
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Jan 21, 2012 12:38 am     Reply with quote

You can use the Pulsin() function from this post:
http://www.ccsinfo.com/forum/viewtopic.php?t=42353
SherpaDoug



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

View user's profile Send private message

PostPosted: Sat Jan 21, 2012 8:08 am     Reply with quote

Your code will keep setting the timer to zero each time through the loop.

You need one loop to look for the bit to go high, which zeros the timer when it exits. Then you need another loop to look for the bit to go low, which reads the timer when it exits.

Or better yet use the hardware capture feature of a timer.
_________________
The search for better is endless. Instead simply find very good and get the job done.
SherpaDoug



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

View user's profile Send private message

PostPosted: Mon Jan 23, 2012 12:10 pm     Reply with quote

Quote:
hi guys
i have no much experience timers and i wonder if we can read the value of the input pulse as variable in microsecond ,for example
if we have 2 input frequency ,first high input on pin b0 last for 250us and second one last for 100us and we want to write a code like this:

if(get_timer0()>=250) // 250us
output_low(pin_b7);

if(get_timer0()<=100) // 100us
output_high(pin_b7);

the pic is 1684 with 4Mh crystal

If you get the timer to clock at 1us intervals then the readings will be in us. With a 4MHz crystal you need the timer to run at 1/4 crystal speed. Read the PIC datasheet section on timers.
_________________
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