Below, I give in succession the background to my questions,
the problem, and my questions. I can provide more details on
the configuration as needed, but I didn't want to make this
post any longer than it already is (sorry!). Thanks in
advance for any advice you can offer.
- First, the background to my questions.
I have built an Arduino device (atmega328, 16MHz)
where I use all three timers on the chip:
Timer 0 for specific delays using delay and delayMicroseconds
Timer 1 for a timed pattern matching that needs accuracy and
the two-byte range of the timer.
Timer 2 to generate a PWM signal on pin 11 at a target frequency.
For Timer 2, I use Fast PWM mode with the TOP count defined
by the OCR2A register and the output toggling digital pin 11
when the timer == TOP. This gives a 50% duty cycle at the
right frequency. BUT! I cannot use Pin 3 (the other PWM
signal on Timer 2), because that pin does not have the
toggle function that pin 11 does and so the achievable
frequency cannot match the target frequency. (I can give
full details on this if it would help, but I didn't want to
give too much here.)
All this works just fine.
- Second, the problem. I now need to interface with an SPI
device, which will, if I'm not mistaken, require digital pin
11 (along with several others). This makes it impossible to
get the PWM signal I need as things stand, because pin 3
cannot be substituted for pin 11 for the reasons stated above.
Several potential solutions I've thought of are as follows:
- Generate the PWM signal in hardware (e.g., with a
calibrated 555 timer tying one digital pin to the 555
RESET pin to control on and off).
This is feasible, but I don't like it for various reasons.
- Swap the roles of Timer 0 and Timer 2. This would move
the PWM to digital pin 6 with no conflict.
At the moment, this is my preferred choice.
- Write my own delay() etc. functions for Timer 2 and use
Timer 0 for PWM.
This seems essentially equivalent to #2.
- Move to a Mega, which has more timers.
This is not really appropriate to the device, so unless
all is lost, I'd rule this one out.
I also don't think it's feasible to dual task Pin 11 by
changing its PWM state as needed while still getting the
timing accuracy I need. (Could be wrong this but...)
- Finally, my questions. To swap Timer 0 and Timer 2 would
seem to require copying and adjusting wiring.c in the
Arduino code so that millis(), delay(), and
delayMicroseconds() work as they do now. (This is akin to
"rewriting" these functions for Timer 2 from scratch.) In
particular, the timer configuration, ISR's, and some variable
names would need to be changed. Tedious but seemingly
A. Is this feasible? Are there difficulties here that I'm
not seeing? Would this mess up anything else?
B. Is there an easier way to deal with this problem? Any
C. If I copy and rewrite wiring.c, is it sufficient to
include that copy in the sketch subdirectory prior to
compilation to have it linked in place of the default?
If not, how should I get my wiring.c to be used
with the Arduino IDE?
Thanks for your patience with this long post. I would
very much appreciate your input or ideas you have.
The SPI part came in rather late in the design process,
and I'd hate to have it ruin all the work that went
before for lack of pins.