Hi All,
I want to capture one input signal for both edge. But how can I distinguish between rising edge and falling edge when interrupt in?
Currently I read the input pin's state, if the input pin is in high, then this incoming interrupt is rising edge. If the input pint is in low, then \ this incoming interrupt is falling edge. The following code is my example.
interrupt void ISR_Timer3(void){ TFLG1 = 0x08; // Clear flag if( (PTIT & 0x08) ) //rising edge { ... } else // falling { ... }}
Does anyone have other method to distinguish bwteen rising edge and falling edge? Thanks a lot.
Pogo
Solved! Go to Solution.
It depends on how narrow are pulses on your input pin. For long enough pulses (longer than interrupt latency in your application) what you do is quite OK. Another approuch for long enough pulses would be to set input capture to capture one type of edges, then manipulate TCTLx bits in interrupt to change capture polarity. In ISR you could inspect TCTL bits to determine what edge caused this interrupt.
For too narrow pulses you should either tie 2 input capture pins together and set them to capture different edges, or perhaps you add some extra hardware for the task of pulse measurement.
It depends on how narrow are pulses on your input pin. For long enough pulses (longer than interrupt latency in your application) what you do is quite OK. Another approuch for long enough pulses would be to set input capture to capture one type of edges, then manipulate TCTLx bits in interrupt to change capture polarity. In ISR you could inspect TCTL bits to determine what edge caused this interrupt.
For too narrow pulses you should either tie 2 input capture pins together and set them to capture different edges, or perhaps you add some extra hardware for the task of pulse measurement.
Thanks kef
I will try to use the third method which you provided. I have one question if I capture the same signal with two different pin and capture them with differen edges. How to calculate the pulse width ? Is the follwing code correct? Is the tick count for timer 0 and timer 1 at same frequecy? Or I need to use TCNT?
interrupt void ISR_RisingEdge_Timer0(void){ TFLG = 0x01; //clear flag u16RisingTick = TC0;....}interrupt void ISR_FallingEdge_Timer1(void){ TFLG = 0x02;// clear flag u16PulseWidth = TC1 - u16RisingTick; //<- Calculate pulse width....}
Regarding your question about tick count and frequency. What does Input Capture do? On matching rising/falling edge it transfers contents of TCNT register into TCx. So it sort of operating at the same TCNT ticking frequency. Yes, your code should work, though some limitations still do apply:
1) TCNT should be free running. In case TCRE bit is set to reset TCNT to 0 on TCNT==TC7, you need to take the modulus of difference PW = (TC1 - TC0) % TC7; to calculate pulse width
2) Frequency of your input pulses still should be low enough, because input capture doesn't wait until you service input capture interrupt, TCx is overwritten with new TCNT on every matching edge, no matter did you read right TCx capture or not. In contrast to S12C TIM timer, S12D ECT timer has control register that allows to block input capture until old TCx is serviced.
3) Pulse width should be lower than TCNT overflow period. For wider pulses pulse width calculation is much more difficult.
Though it's an old HC11 reference manual, but there you may find good description of how input capture and output compare is supposed to work, including well commented assembler examples. See chapters 10.5 and 10.6:
http://cache.freescale.com/files/microcontrollers/doc/ref_manual/M68HC11RM.pdf