Help with PWM on Uno

Hello everyone !

I'm trying to use analog output (PWM) on pin 10 with this parameters:

FastPWM 10bits
62500Hz

With FastPWM 8bits I can reach, but 10bits not
I readout the datasheet but no conclude

See config I already reach:

void setup()
{
  TCCR1A=0b10000001;
  TCCR1B=0b00001001;
  OCR1A=255;
  TIMSK1=0;
  
}

void loop()
{
  analogWrite(10,128);
}

This works (FastPWM 8bits with 62500hz)

But when change to FastPWM 10bits:

void setup()
{
  TCCR1A=0b10000011;
  TCCR1B=0b00001001;
  OCR1A=1023;
  TIMSK1=0;
  
}

void loop()
{
  analogWrite(10,128);
}

I had a lower frequency (cannot reach 62500hz)

Could anyone please help me with that ?
Thanks !

You have two different fast pwm modes (fast pwm to 255 and fast pwm to 1023).

With the same prescaler, the timer can not count up to 255 and 1023 at the same frequency. You should be seeing frequencies of 62500 and 62500/4 = 16625

Yes, you are right 62500/4

But which mode should I use instead to get the desired results ?

Thanks !

You can not get the desired results(10 bit duty cycle resolution) at 62500Hz on a 16 MHz Arduino.

If this is not an exercise, and you really need this frequency and resolution for an application , you will need to switch to a different processor.

With FastPWM you can have either a fixed frequency by the fixed 8/9/10 bit TOP or you can have a variable frequency with any 16 bit TOP value in OCR1A or ICR1.

You are right, 16MHz/1024 is much too low. A controller with a timer frequency of at least 62.5*1024 kHz = 64 MHz is required.

If I let OCR1A=255 is it possible to reach 62.5khz with 10bits FastPWM mode ?

Then TOP is fixed to 0x3FF and the max. frequency is 16MHz/1024. OCR1A then gives the duty cycle of the A channel, from 0 to 0x3FF

No. "10-bit PWM" means 1024 counts per cycle. With a 16 MHz clock the fastest 10-bit PWM you can get is 16 MHz / 1024 = 15.625 kHz.

With a 16 MHz clock the most PWM resolution you can get at 62.5 kHz is 256 counts: 8-bit.

1. Fig-1 shows Mode-7 operation for 10-Bit Fast PWM; where, the TOP is fixed at 0x03FF (1023) and duty cycle is controlled by OCR1A/OCR1B.


Figure-1:

2. Fig-2 is the equation for frequency of Fast PWM signal.
fpwmEqu
Figure-2:

3. Now, you can do the calculation to see that it is not possible to generate 62.5 kHz PWM signal in Mode-7.

4. You can choose Mode-14 or Mode-15 from Fig-1.

Very good, but anyway if I use mode 15 for example and set OCR1A=1023 (To have 10bits)
In this case I'll still having lower frequency(15625hz) ?

@oliveirah
1. I hope that you know the meaning of "resolution" here in the context of PWM signal.
2. I hope that you know the meaning of "duty cycle" of a PWM signal.
3. If you choose Mode-15, the frequency will be controlled by OCR1A Register and the duty cycle will be controlled by OCR1B Register. The smallest amount of time by which the duty cycle of this single slope Fast PWM signal can be changed (this is called the resolution) is:
62.50 us (10-3/(62500x256)).

4. You can set the following parameters (derived using Fig-2 of post #9) to get 62500 Hz Fast PWM signal at 25% duty cycle in Mode-15.

N = 1, OCR1A = 255, OCR1B = 64

1 Like

Got it !

Thanks GolamMostafa

You are welcome! Have a good fun with Arduino World!!

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.