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

Timer1 on PIC16F877A

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



Joined: 09 Nov 2007
Posts: 6

View user's profile Send private message

Timer1 on PIC16F877A
PostPosted: Sun Nov 11, 2007 9:18 am     Reply with quote

hi
Can I use the Timer1 on PIC16F877A to measure time between pulses (of the rising edge detection)?Bcoz I needed to get the unit in 'cycle per seconds' for example. I've seen the example,but there is no realization of using rising edge and rising edge,but there is for rising edge and falling edge (which is duty cycle measurement).

I wanted to measure the time between pulses (which is from a sensor to give a rising edge).

Can somebody help me to perform this? Sample of code would be nice...thanks

or maybe someone can comment on the following codes.Any opinions are welcomed as I'm still a beginner:


#include <16f877A.h>
#include <stdlib.h>
#fuses hs,noprotect,nowdt,nolvp
#use delay (clock=20000000)
#include <lcd.c>

int16 overflow_count;
long rise1,fall,cur_spd,pulse_width;

#int_ccp1
void isr()
{
rise1 = CCP_1;
fall = CCP_2;

pulse_width = fall - rise1;

set_timer1(0);
}

#int_timer1
void timer1_isr() {
overflow_count++;
}


void main()
{
lcd_init();
setup_ccp1(CCP_CAPTURE_RE); // Configure CCP1 to capture rise
setup_ccp2(CCP_CAPTURE_FE);
setup_timer_1(T1_EXTERNAL|T1_DIV_BY_1); // Start timer 1


enable_interrupts(INT_CCP1); // Setup interrupt on rising edge
enable_interrupts(GLOBAL);

while(TRUE) {
overflow_count=0;
//delay_ms(1000);

enable_interrupts(INT_TIMER1);

if(pulse_width >= 35479.5 || pulse_width <= 10164)
{
disable_interrupts(GLOBAL); //disable ccp2 interrupt

if(rise1<fall){
if(overflow_count==0)
pulse_width=fall-rise1;
else
pulse_width=fall-rise1+262*overflow_count;
}

else
pulse_width=(2^16)-rise1+fall+262*(overflow_count-1);

pulse_width=pulse_width/5;
cur_spd=4/pulse_width;
printf(lcd_putc,"wheel speed: %lu \n",cur_spd);
}//end if
}//end while
}//end main
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Nov 11, 2007 12:40 pm     Reply with quote

http://www.ccsinfo.com/forum/viewtopic.php?t=29963&highlight=tachometer
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