Hello Steve,
I suspect that there may be some misunderstanding about the operation of the TIM module. The timer needs to run continuously for proper operation. To delay a pulse by a predetermined period does not normally involve the calculation of pulse width (although it can be done if this data is required). The simpler way is to treat each input pulse as having two time events, corresponding to the leading edge and the trailing edge. So it is really only a question of applying the same delay to each edge.
In my previous post, I described a more general situation where multiple events could occur within the delay period, and a circular buffer would be necessary. However, for the case you seem to have, and assuming that the pulse width is less than the delay period, there would actually be only two input capture events, one for each edge of the pulse. The delayed value for the first event could be written immediately to the timer channel register used for output compare, but the delayed value for the second event would need to be temporarily stored until after the first output compare event has occurred, before being written to the channel register.
Revised input capture ISR processing would be as follows:
- Read timer channel register
- Add required delay value, ignoring carry bit
- If current pulse edge is positive {
Set next input capture for negative edge
Write modified value to output compare channel register
Set next output compare for high state
Enable output compare channel interrupt
}
else {
Set next input capture for positive edge (ready for next pulse)
Write modified value to a temporary register
}
Revised output compare ISR processing would be as follows:
- Read value from the temporary register and write this to the output compare channel register
- Set next output compare for low state
- Disable further output compare interrupts
With this arrangement, for each input pulse, the input capture ISR would execute twice, and the output compare ISR once only. Should you have further difficulty getting this concept to work, you will need to post your code for initialisation of the timer, and the two ISR routines.
Regards,
Mac