I don't know how you are calculating the valve open and close times/positions/angles? Are they fixed? Are you going to change them?
Unfortunately, the valve timings need to be adjustable to set increments. If it was always one set value, I would probably bypass the microprocessor all together...
Anyway, I would create a table of open and close values. I would make each value in integer 1000ths of a rotation (or 10ths of a percent, if you wish). So something that happens right after TDC would have a value of 0001 and something that happens just before TDC would have a value of 0999. Note that this is ~3x better resolution than using "degrees" and keeps all the math in integers.
Here is what I would do in the code, starting with the detection of TDC.
- Subtract right now from previous TDC to establish rotation period in microseconds.
- Store this TDC time for use next time around.
- Multiply your table of open/close times (in 1000ths of a revolution) against the rotation period.
- Each time one of those calculated times occurs open or close whatever valve.
- wait for the next TDC
Great! Let me research how to set up tables, and I'll try to put one together. Thanks Richard.