1

I found this question: Set PWM frequency to 25 kHz

It is useful, however, I want to use it to generate ~14KHz?

int d = 0;            // pwm value

void setup()
{
  //  Sets Timer1 to Phase Correct
  //  F_CLOCK / ( Prescaler * ORCR1A * 2 ) = Freq in Hz
  //  16000000 / (1 * 512 * 2 ) = 15625 Hz
  TCCR1A = _BV (WGM10) | _BV (WGM11) | _BV (COM1B1);  // Phase Correct
  TCCR1B = _BV (WGM13) | _BV (CS10);                  // Phase Correct / Prescale 1
  OCR1A = 571;                                        // Sets Top to 512 --571
  OCR1B = 0; // Sets Pwm = 
    pinMode(10, OUTPUT);
    pinMode(0, INPUT)
}  

void loop()
{
  d = (analogRead(0)); 
  d = map(d, 0, 1023, 0, 512);
  OCR1B = d;
}

I used this sketch to test the frequency which works good. If I change the pinMode(10, OUTPUT);, into: pinMode(9, OUTPUT); and there is no output, why?

10
  • The top of timer1 is set to 320. I think that a value of 571 will result in about 14010.5 Hz. Can you tell which sketch you are using and please tell us what it is for.
    – Jot
    Jan 16, 2019 at 17:44
  • Thank you Jot, I'll use the last code in the 'Set PWM frequency to 25 kHz' to control a motor driver which asked a PWM frequency about 14KHz.
    – laoadam
    Jan 16, 2019 at 18:16
  • Please keep in mind that this site is for questions and answers. Preferably good and clear questions and one or more good and clear answers. This is the pinmapping for an arduino uno and nano: arduino.cc/en/Hacking/PinMapping168 Pin 10 is OC1B and pin 9 is OC1A. That are outputs of timer1. In the datasheet of the atmega328p you can find how the pwm modes are used and how the output pin (or internal interrups) can be used with pwm. You map the signal up to 512, but the pwm counts to 571, so your maximum pwm is 89%.
    – Jot
    Jan 17, 2019 at 8:12
  • 1
    Take this sketch, replace 320 by 571 and do not change anything else. Jan 17, 2019 at 8:27
  • Thank you Edgar, both pin 9 and 10 working now.
    – laoadam
    Jan 17, 2019 at 13:31

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.