syahrul.s
Joined: 09 Nov 2007 Posts: 6
|
Timer1 on PIC16F877A |
Posted: Sun Nov 11, 2007 9:18 am |
|
|
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 |
|