16F876A CCP1 Capture/Interrupt Question
Hi,
I am experimenting with using the 16F876A to measure the turn off time of a phototransistor. I have tried two ways, just to see how much difference there would be in the time. The first way is using the RB0 input to generate an interrupt, and the 2nd is using the capture feature on pin RC2/CCP1.
1st method:
1. Turn off the LED that points at the phototransistor
2. Reset the timer1 high and low bytes to zero
3. When program jumps to ISR, read value of Timer 1. Result is 204uS
2nd method:
1. Turn off the LED that points at the phototransistor
2. Reset the timer1 high and low bytes to zero
3. Poll the CCP1IF flag in a loop until it goes high, and then read the value of the CCP1 high and low byte registers. Result is 188uS, which is almost exactly the same as I measure on an oscilloscope.
With 2nd method, I had expected to do it the same way as the first... I thought program would jump to ISR when pin RC2 went high, but it did not work. So my question is, why doesn't the program jump to the Interrupt handler when the CCP1IF flag gets set? If it did then I would not need to poll the pin in a loop. Would I be doing something wrong or is that how it is supposed to work? Thanks for any answers.
Oscilloscope is a better way
If an event is slow, say many milliseconds, then software measurements such as you are trying can give reasonable results. However I would expect the turnon or turnoff time of a phototransistor with say a 4k7 load resistor to be only a few tens of microseconds. This is way too fast to easily measure in software IMHO. Unless you get deep into the assembly code produced by PBP and your assembler (PM or MPASM) you will not know how many machine code instructions lie between the event and the display of that event. That makes such a measurement in code a rather hit or miss affair.
There are no published timings for PBP commands because many of them are variable. For example the instruction "If A > B then" takes different times for a byte compare vs a word compare.
If at all possible, use an independent hardware device, an oscilloscope or logic analyser, to get believable and repeatable measurements.
HTH
BrianT