Arduino Uno/Nano 16 bit PWN
Tested on Arduinos with ATMEGA 328 and ATMEGA 168
Usage ex: analogWrite16(34954);
You'll need to use unsigned int
because you'll need the range 0, 65535
void setupPWM16() {
DDRB |= _BV(PB1) | _BV(PB2); /* set pins as outputs */
TCCR1A = _BV(COM1A1) | _BV(COM1B1) /* non-inverting PWM */
| _BV(WGM11); /* mode 14: fast PWM, TOP=ICR1 */
TCCR1B = _BV(WGM13) | _BV(WGM12)
| _BV(CS10); /* no prescaling */
ICR1 = 0xffff; /* TOP counter value */
}
void analogWrite16(uint8_t pin, uint16_t val) {
if (val == 0) { /*Fix because at 0 it still emit signal somehow*/
switch (pin) {
case 9: OCR1A = val; break;
case 10: OCR1B = val; break;
}
delay(10);
digitalWrite(pin, LOW);
}
else {
setupPWM16();
switch (pin) {
case 9: OCR1A = val; break;
case 10: OCR1B = val; break;
}
}
}
No Comments