Go Down

Topic: Pin 11 in this code to generate exact 1kHz pwm? (Read 4758 times) previous topic - next topic

maxmike

I found this code in a post - it does generate exactly 1kHz pwm - could somebody point out how to change it for pin 11 - sorry am noob on registers.

Code: [Select]
void setup()
{
 // configure hardware timer2 to generate a fast PWM on OC2B (Arduino digital pin 3)
 // set pin high on overflow, clear on compare match with OCR2B
 TCCR2A = 0x23;
 TCCR2B = 0x0C;  // select timer2 clock as 16 MHz I/O clock / 64 = 250 kHz
 OCR2A = 249;  // top/overflow value is 249 => produces a 1000 Hz PWM
 pinMode(3, OUTPUT);  // enable the PWM output (you now have a PWM signal on digital pin 3)
 OCR2B = 125;  // set the PWM to 50% duty cycle
}

void loop()
{
float floatr = 1;
 OCR2B = 125;  // set the PWM to 50% duty cycle
 for (int j = 0; j < 1000; j++)
     floatr++;
 OCR2B = 50;  // set the PWM to 20% duty cycle
 for (int j = 0; j < 1000; j++)
     floatr++;
 pinMode(3, INPUT);  // turn off the PWM
 //do some stuff;
 pinMode(3, OUTPUT);  // turn the PWM back on
}


Go Up