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?

VE7JRO
  • 2,497
  • 15
  • 24
  • 29
laoadam
  • 45
  • 5
  • 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 '19 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 '19 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: https://www.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 '19 at 08:12
  • 1
    Take [this sketch](https://arduino.stackexchange.com/a/25623/7508), replace 320 by 571 and _do not change anything else_. – Edgar Bonet Jan 17 '19 at 08:27
  • Thank you Edgar, both pin 9 and 10 working now. – laoadam Jan 17 '19 at 13:31
  • When I put this PWM code into my driver code, there was conflict of using timer1 with VirtualWire library. How can I use timer2 to get 14khz? I am using UNO. – laoadam Jan 18 '19 at 13:29
  • laoadam, to get someone's attention use for example @Jot then I will be notified. Timer2 is only 8 bits and has other output pins. You can move the VirtualWire to Timer2. Upgrade to RadioHead in RH_ASK mode and change it to Timer2 (search for "timer2" on this page: https://www.airspayce.com/mikem/arduino/RadioHead/classRH__ASK.html ). RadioHead uses a lot more memory than VirtualWire. – Jot Jan 20 '19 at 04:40
  • @Jot Thank you Jot, I have shift the PWM frequency set by Timer2, output by P3 and P11, it's works well now at ~3.9 KHz, no yet what I need ~14khz, at lest made progress. There is new problem came up when I use this sketch on my 30A motor driver which has (PWM1+DIR1) for motor 1, but the original sketch used (PWM1+AIN1+AIN2) to control motor 1. – laoadam Jan 20 '19 at 23:42
  • With timer1 the resolution was 571 steps and with timer2 it is below 256. I forgot about all those pwm modes, so I'm not sure if timer2 has the same pwm mode as timer1. You make it seem as if you want to control a stepper motor with pwm. I don't understand that. You can ask a new question for the pwm/dir/ain signals. Tell what motor it is, what the driver is, what the signals from the arduino are, with links, schematics, pictures, photos, everything. – Jot Jan 21 '19 at 00:05
  • @ Jot, thank you Jot, the timer2 does not works that way. I have post another question. – laoadam Jan 21 '19 at 14:00

0 Answers0