<- back to main Article

PWM_Timer2_31kHz

set Timer2 Frequency to 31kHz

/**************************************************************************************************
*
*   PWM_Timer2_31kHz_00
*
*   Version:      00 - Mai 2010
*   Author:       Tom Pawlofsky www.caad.arch.ethz.ch tom-DOT-pawlofsky-AT-arch-DOT-ethz-DOT-ch
*
*   Desc:         change prescaler of Timer 2 to have 31kHz Frequency at pin 3 and 11
*
***************************************************************************************************/


int pinA = 3; // pin 3 and 11 are PWM output controled by Timer2
int pinB = 11; // connect pinA/B to H-Bridge

void setup(){
        //__________________________________TIMER2_for_Motor_PWM_________________________________
        // set TIMER2 for PWM 32 Hz
        //
        // clear all prescaler bits in TCCR2B = the last 3 Bits
        // leave other bits as set by arduino init() in wiring.c
        byte mask = B11111000;
        TCCR2B &= mask; // TCCR2B is now xxxxx000
        //
        // set CS22:20 in TCCR2B see p 156 of datasheet
        TCCR2B |= (0<<CS22) | (0<<CS21) | (1<<CS20); // same as TCCR2B |= B00000001; TCCR2B is now xxxxx001

        //__pinmode
        pinMode(pinA,OUTPUT);
        pinMode(pinB,OUTPUT);

        //
        analogWrite(pinA,128); // 50% Duty
        analogWrite(pinB,32); //12.5 % Duty
}

void loop(){
}


<- back to main Article