# setting a PWM using Timer 2, but it doesn´t work where is my fault?

here is my code, my goal is to be able to provide pwm using Hardware Timer and Register directly. I have only use it with the function and it work fine, but I want now to learn more the hardware.
Thank you.
Soko

#include <avr/io.h>
int pinPWM9 = 9;
int pinPWM10 = 10;
int pinPWM13 = 13;

void setup() {
Serial.begin(9600);

pinMode(pinPWM9,OUTPUT); //set pins as output
pinMode(pinPWM10,OUTPUT);
pinMode(pinPWM13,INPUT);

/* this part of code initialize: prescaler CS2x, Output compare
Register OCR2A/B and the timer preconfiguration.
*/
TCCR2B &= ~(1<<CS20);  // set Prescaler to 0
TCCR2B &= ~(1<<CS21);
TCCR2B &= ~(1<<CS22);

//Fast PWM Mode will be use non Inverting Mode
TCCR2A |= (1<<COM2A1);        //set this pins mean:clear OC2A on compare Match
TCCR2A &=~(1<<COM2A0);  // and set OC2A at BOTTOM

TCCR2A |=(1<<COM2B1);
TCCR2A &=~(1<<COM2B0);

//Wave Form Generation Mode 3
TCCR2A |=(1<<WGM21)|(1<<WGM20);
TCCR2B &=~(1<<WGM22);

// initialize the Output Compare Register
//a Match can be used to generate an output compare interrupt, or
// to generate a waveform output on the OC2A pin
OCR2A = 0;
OCR2B = 0;

//Set Prescaler to output a desire Frequency
TCCR2B &= ~(1<<CS22);  // set Prescaler to 010=2=clk/8
TCCR2B |=  (1<<CS21);
TCCR2B &= ~(1<<CS20);
}

void loop() {
//now set Output Compare Register to perform the PWM generate from
// the Wave Generator Mode
OCR2A = 240;
OCR2B = 240;
}

I am using Arduino Mega2560 (SaintSmart) with the latest software update running on Windows Vista

edit by mod:
please include your code using the [ code ] tags and post your question in the correct section, using the proper language. Thank you.

TCCR2B &= ~(1<<CS20);  // set Prescaler to 0
TCCR2B &= ~(1<<CS21);
TCCR2B &= ~(1<<CS22);

That gives “No clock source (Timer/Counter stopped)” according to the datasheet.

what you mind ist the first part of the code which set first of all the prescaler at zero, if you look in the setup function you will see that I set the prescaler at 2 that mean CS22=0, CS21 =1 and CS20 =0 to obtain system clockdividet by 8 (CKL/8).
soko.

Sorry, I didn't spot that bit.

Are you sure that the AS2 bit of the ASSR register is zero? I guess it should be because it is initialized that way, unless the bootloader has messed with it.

I’m going to make a suggestion:
Replace this:

TCCR2B &= ~(1<<CS20);  // set Prescaler to 0
TCCR2B &= ~(1<<CS21);
TCCR2B &= ~(1<<CS22);

//Fast PWM Mode will be use non Inverting Mode
TCCR2A |= (1<<COM2A1);        //set this pins mean:clear OC2A on compare Match
TCCR2A &=~(1<<COM2A0);  // and set OC2A at BOTTOM

TCCR2A |=(1<<COM2B1);
TCCR2A &=~(1<<COM2B0);

//Wave Form Generation Mode 3
TCCR2A |=(1<<WGM21)|(1<<WGM20);
TCCR2B &=~(1<<WGM22);

// initialize the Output Compare Register
//a Match can be used to generate an output compare interrupt, or
// to generate a waveform output on the OC2A pin
OCR2A = 0;
OCR2B = 0;

//Set Prescaler to output a desire Frequency
TCCR2B &= ~(1<<CS22);  // set Prescaler to 010=2=clk/8
TCCR2B |=  (1<<CS21);
TCCR2B &= ~(1<<CS20);

With this:

//Reset the registers of the timer
TCCR2B = 0;
TCCR2A = 0;
//Wave Form Generation Mode 3 and Fast PWM Mode will be use non Inverting Mode
TCCR2A =(1<<WGM21)|(1<<WGM20)|(1<<COM2B1)|(1<<COM2A1);

// initialize the Output Compare Register
//a Match can be used to generate an output compare interrupt, or
// to generate a waveform output on the OC2A pin
OCR2A = 0;
OCR2B = 0;

//Set Prescaler to output a desire Frequency
TCCR2B = (1<<CS21);

There is no point trying to keep the values of certain bits unchanged in the register when you want to be setting the timer up to specific, known settings. Doing so may inadvertently lead to incorrect settings caused by bits not being what you expect. It will also produce slow and inefficient code.

Later on in the program if you happened to want to change a setting, then is the time to only change certain bits as you definitely know what the other bits are.

Furthermore:
digital pin 10 is OC1B, not OC2B. You need to be using digital pin 3 for that.
digital pin 9 is OC1A, not OC2A. You need to be using digital pin 11 for that.
This is the problem you have.

Finally:
You have named a variable PWMPin13 and yet put that as an input? consider renaming it.

Thank for all of you,
I tried both code (setting the AS2 like dc42 suggested me and tried the code that Tom Carpenter posted) both works, I will try to see the result with an Oxilloscope to confirm the waveform and the Frequency.
to Tom: I use the Arduino Mega2560 (SaintSmart)and in the datasheet , pin 9 & 10 are conected to OC2A and OC2B.
thank you every body
Soko.

sokoardouino:
I use the Arduino Mega2560

Ahh, missed that, apologies.