以下是引用strongchen在2006-4-5 10:44:00的发言: 没错。 以下是引用hitmissile在2006-4-4 20:07:00的发言: 我看了一下输入捕捉的Bean,该输入捕捉通道命名为Petrol1_Detect,以下两个函数是PE自动生成的函数 #define Petrol1_Detect_Reset() (Petrol1_Detect_CntrState = TCNT , ERR_OK) #define Petrol1_Detect_GetCaptureValue(Value) \ (*(Value) = TC0 , *(Value) -= Petrol1_Detect_CntrState , ERR_OK) 在Reset()函数中只是把TCNT的值赋给了一个变量,而不是将TCNT复位,而在GetCaptureValue()中只是简单地将TC0-TCNT,即使TC0小于TCNT,也能得到正确的值,但是捕捉周期不能大于时钟计数周期,若大于就得配合计数溢出中断次数才能得到正确的值,这么理解没错吧?
溢出次数如何获取呢 我也是用PE生产的 Well, I would use something like input capture method. The idea is: - enable the free running timer (TIM) - at the start save the current value of timer count register TCNT (first_value) - enable the timer overflow interrupt - count the overflow interrupts (overflows++) - at the end save the current value of TCNT (second_value) - now calculate the interval: (0x10000 - first_value) + (overflows * 0x10000) + second_value If the timer didn't overflow then it's simple: second_value - first_value It will be necessary to find out appropriate timer prescaler (I don't know what intervals you want to measure). In fact, this method can be used also for ISR's length measuring. But there's problem with the timer overflow interrupt as I wrote in my previous reply. If the length of ISR is not greater than the period between two overflows then I can imagine this method:
interrupt void test(void) {
TFLG2_TOF = 1;
//clear timer overflow flag (interrupt is disabled) first_value = TCNT;
//do something here
second_value = TCNT;
if(TFLG2_TOF == 1)
length = 0x10000 - first_value + second_value;
else
length = second_value - first_value }
|