EditUpdate: Here is an example code illustrating this, I just tested itimplementation of a 16-bit analogWrite(). It
only works on my scopepins 9 and 10, as these are the only pins connected to the
16-bit timer.
/* Configure digital pins 9 and 10 as 16-bit PWM outputs. */
void setupsetupPWM16() {
DDRB |= _BV(PB1) | _BV(PB2); /* set pins as outputs */
TCCR1A = _BV(COM1A1) | _BV(COM1B1) /* non-inverting mode PWM */
| _BV(WGM11); /* mode 14: fast PWM, TOP = ICR1TOP=ICR1 */
TCCR1B = _BV(WGM13) | _BV(WGM12)
| _BV(CS10); /* no prescaling */
ICR1 = 0xffff; /* TOP counter value */
}
void/* loop16-bit version of analogWrite(). Works only on pins 9 and 10. */
void analogWrite16(uint8_t pin, uint16_t val)
{
staticswitch uint16_t(pin) i;{
case 9: OCR1A = i;val; break;
case 10: OCR1B = val; break;
}
}
You may notice that the top of the counter sequence is configured explicitly. You can change this to a smaller value to make the PWM faster, at the cost of decreased resolution.
And here is an example sketch illustrating its use:
void setup() {
setupPWM16();
}
/* outputTest: onsend digitalvery 9slow sawtooth waves. */
void loop() {
OCR1B = 0xffff-static uint16_t i;
/* outputanalogWrite16(9, oni);
digital analogWrite16(10, */0xffff - i);
i++;
delay(1);
}