Check if a pin has pwm

Are there any way to check by code if a pin has pwm capability?

This array within pins_arduino.c tells whether pins have timers on them. See if you you can read that array. This one is the array for my 1284 based board. Smaller parts have less, bigger parts have more.

const uint8_t PROGMEM digital_pin_to_timer_PGM[] = { NOT_ON_TIMER, /* 0 - PD0 / NOT_ON_TIMER, / 1 - PD1 / NOT_ON_TIMER, / 2 - PD2 / NOT_ON_TIMER, / 3 - PD3 / NOT_ON_TIMER, / 4 - PB0 / NOT_ON_TIMER, / 5 - PB1 / NOT_ON_TIMER, / 6 - PB2 / TIMER0A, / 7 - PB3 */ // PWM

TIMER1A, /* 8 - PD5 / // PWM TIMER2B, / 9 - PD6 / // PWM TIMER0B, / 10 - PB4 / // PWM NOT_ON_TIMER, / 11 - PB5 / NOT_ON_TIMER, / 12 - PB6 / NOT_ON_TIMER, / 13 - PB7 / NOT_ON_TIMER, / 14 - PA0 / NOT_ON_TIMER, / 15 - PA1 */

NOT_ON_TIMER, /* 16 - PA2 / NOT_ON_TIMER, / 17 - PA3 / NOT_ON_TIMER, / 18 - PA4 / NOT_ON_TIMER, / 19 - PA5 / NOT_ON_TIMER, / 20 - PA6 / NOT_ON_TIMER, / 21 - PA7 / NOT_ON_TIMER, / 22 - PC0 / NOT_ON_TIMER, / 23 - PC1 */

NOT_ON_TIMER, /* 24 - PC2 / NOT_ON_TIMER, / 25 - PC3 / NOT_ON_TIMER, / 26 - PC4 / NOT_ON_TIMER, / 27 - PC5 / NOT_ON_TIMER, / 28 - PC6 / NOT_ON_TIMER, / 29 - PC7 / TIMER1B, / 30 - PD4 / // PWM TIMER2A / 31 - PD7 */ // PWM

};

Or you just create an Array with the PWM numbers yourself...

byte PWMpins[] = { 1,2,3,4,5 };

ok you should fix the nr's...

Guess these options would work, but still not really what I am looking for.

I am making a function for my 3d printer, to turn different pins on and off through the gcode controlling it. But I would like the code to know if it got pwm and then use analogWrite instead of digitalWrite for that pin.

I could make an array with the pin numbers, but this firmware is already covering multiple different controllers, with different chips on them, where some of them I don't even know. And I would hate to write a new array of pin numbers for each controller, and look into what the new chips can do when someone makes a new printer controller board with a new chip on.

nd I would hate to write a new array of pin numbers for each controller,

Then I think you are up sh1t creek.

Even if there was a way (and there may be if you're really clever) you would have to know what registers to play with, and to do that you need processor-specific code. Back to square one.

An array sounds a lot easier. It can be automatically included based on a #define that's available (somewhere) that tells you the processor.

Or access the array Crossroads referred to.


Rob

if you follow the code of analogWrite() you come across the array crossroads mentiond: const uint8_t PROGMEM digital_pin_to_timer_PGM[]

But that is not all, you will find also some macro's to access this array properly, e.g. digitalPinToTimer()

So stripping analogWrite leads to

#include "pins_arduino.h"

boolean isPWMPIN(uint8_t pin)
{
  return (digitalPinToTimer(pin) != NOT_ON_TIMER);
}

Not tested, you may need some more includes but you can do that ;)

If you use analogWrite on a non-PWM pin it is equivalent to digitalWrite, setting the output high if the 'analog' value is 128 or higher, which might be enough for your purposes.