Coding to set PWM Frenquencies

Hi there,

I’m working on a project to run a BLDC motor using an Arduino UNO R3 with an H-Bridge interface and a published sketch.

As I understand it the Arduino can produce a range of specific frequencies from it PWM pins (see attached) and I was interested to understand how the code sets the frequency. The output from pins 9,10 and 11 on the Arduino feed to pin 3 on each of three IR2104 drivers. I am led to believe that each of these Arduino output pins supplies a 31,372.55 Hz signal.

The relevant code from the sketch is shown below:

// Timer1 module setting: set clock source to clkI/O / 1 (no prescaling)
  TCCR1A = 0;
  TCCR1B = 0x01;
  // Timer2 module setting: set clock source to clkI/O / 1 (no prescaling)
  TCCR2A = 0;
  TCCR2B = 0x01; //0x01; uhura:0x05; 0x03:976Hz
  // Analog comparator setting

I don’t know enough about the syntax to be able to understand how this code sets this frequency.

Thanks

Jules

This should help you.

Yes thanks, that is helpful. However, it does not clarify the term "no prescaling" used in the code and also how the hexadecimal values used relate to the resulting frequencies. E.g. 0x03:976Hz and uhura:0x05

Julian

Sometimes you want a timer to run at a speed slower than 16 million counts per second. The bottom three bits of TCCR1B are the Clock Select bits and they select a 'prescale' value to divide down the system clock before it is used to clock the timer. The available scales are 0->0 (no clock), 1->1 (16 MHz), 2->8 (2 MHz), 3->64 (250 kHz), 4->256 (62.5 kHz), and 5->1024 (15.625 kHz).