• Properties of Timers 2, 3, 4, and 5
    Valid pins for PWM analogWrite:

    analogWrite(A4,0.1,{ freq : 10 });
    
    ERROR: Pin A4 is not capable of PWM Output
    Suitable pins are:
    A0 A1 A2 A3 A6 A7(AF) A8 A9 A10
    A11 B0(AF) B1(AF) B3(AF) B4(AF) B5(AF) B6 B7 B8
    B9 B10(AF) B11(AF) B13 B14 B15 C6(AF) C7(AF) C8(AF)
    C9(AF)
    Or pins with DAC output are:
    A4 A5
    You can also use analogWrite(pin, val, {soft:true}) for Software PWM on this pin
    =undefined
    
    

    Note the AF notation AF = Alternate Function
    Using the attached file isTimerEnabled1.js and modifying the analog Write statements
    Timer 5 uses pins:
    A0, A1, A2, A3 or AF pins
    Timer 4 uses pins:
    B6, B7, B8, B9 or AF pins
    Timer 3 uses pins:
    A6, A8, A9, A10 or AF pins B4, B5, B0
    Timer 2 uses pins:
    Or AF pins B3, B10, B11.
    Pins B1, A7, B13, B14, B15, C6, C7, C8, C9 are not seen by timers 2, 3, 4, or 5 when
    used in a PWM analog Write statement.
    When isTimerEnabled1.js is used with the following analogWrite commands,

    analogWrite(A0,0.1,{ freq : 10 }); 
    analogWrite(A1,0.2,{ freq : 10 }); 
    analogWrite(A2,0.3,{ freq : 10 }); 
    analogWrite(A3,0.4,{ freq : 10 });
    The output:
    >echo(0);
    Timer 2 Disabled
    Timer 3 Disabled
    Timer 4 Disabled
    Timer 5 Enabled
    CR1=81
    CR2=0
    SMCR=0
    DIER=0
    SR=1
    ERRCW=0
    CCMR1=6868
    CCMR2=6868
    CCER=1111
    CNT=1da5
    PSC=6d
    ARR=ffae
    CCR1=1991
    CCR2=3322
    CCR3=4cb4
    CCR4=6645
    DCR=0
    DMAR=81
    
    

    Note the CCR1 thru CCR4 reflect the different duty cycles specified in the analogWrite commands.
    If the analog Write commands are changed to

    analogWrite(A6,0.1,{ freq : 10 }); 
    analogWrite(A8,0.2,{ freq : 10 }); 
    analogWrite(A9,0.3,{ freq : 10 }); 
    analogWrite(A10,0.4,{ freq : 10 });
    The Output:
    >echo(0);
    Timer 2 Disabled
    Timer 3 Enabled
    CR1=81
    CR2=0
    SMCR=0
    DIER=0
    SR=1
    ERRCW=0
    CCMR1=68
    CCMR2=0
    CCER=1
    CNT=16e2
    PSC=6d
    ARR=ffae
    CCR1=1991
    CCR2=0
    CCR3=0
    CCR4=0
    DCR=0
    DMAR=81
    Timer 4 Disabled
    Timer 5 Disabled
    
    

    Note that CCR1 is non zero but CCR2, CCR3, and CCR4 are zero.

    The program PWMflash.js looks for pulses divides them and flashes the red LED.
    I used this to test the PWM properties.

    var FLED=require("flashLED");
    
    var x=new FLED(5);
    
    analogWrite(A0,0.1,{ freq : 10 }); 
    analogWrite(A1,0.1,{ freq : 1 }); 
    setWatch(x.divide, C3,
    { repeat: true, edge:'rising', debounce:1});
    
    /*
    function FLED(a) {
      this.A=a;
      this.count=0;
      this.LEDstate=0;
    }
    
    exports = FLED;
    FLED.prototype.divide = function() {
     this.count++;
     if(this.count<this.A)return;
     this.count=0;
     this.LEDstate=this.LEDstate^1;
     digitalWrite(LED1,this.LEDstate);
    };//divide
    
    exports=FLED;
    
    */
    
    

    While the duty cycles of pins A0, A1, A2, and A3 can be independently altered, they must all use the same frequency. The frequency is that of the last invoked analogWrite PWM command.


    2 Attachments

About