0

But when i set clock to 1920 and range 200, I got the frequency 5KHz rather than 50HZ from oscilloscope, does anyone explain that, what's the problem. Thanks

my code as below:

#include<stdio.h>
#include<stdlib.h>
#include<wiringPi.h>

#define port 23
#define mode PWM_MODE_BAL

int main(int arglen, char *argv[])
{
      int index = 0;
      if ( arglen < 2)
      {
              printf("input port number \n");
              exit(1);
      }
      if(-1==wiringPiSetup())
      {
             printf("setup error\n");
             exit(-1);
      }

    int divisor = 1920;
    int range = 200;
    int dutycycle = 512;
    wiringPiSetup();
    pwmSetRange(range);
    pinMode(port,PWM_OUTPUT);

    pwmSetMode(mode);
    pwmSetClock(divisor);
    for (index= 0;index<10000;index++)
    {
        pwmWrite(port,dutycycle);
        delay(340);
        pwmWrite(port,0);
    }
    return 0;

}

1 Answer 1

2

The crystal oscillator is 19.2 MHz on all Pi models earlier than the Pi4B. The Pi4B is based on the new BCM2711 which has a 54 MHz oscillator. It is likely that all future BCM2711 based Pis will also be 54 MHz.

Don't use balanced PWM if you want a particular frequency, use mark space.

Balanced will try to achieve the desired duty cycle as "smoothly" as possible. For instance if you ask for a dutycycle of 50% that can be achieved at any frequency, but it will appear "smoother" at 100 kHz then it would at 1 Hz.

See page 139 of BCM2835 ARM Peripherals

4
  • Thanks @joan. If I want to output a PWM with 4.4M ,when I set range to 255, according the the formula:pwmFrequency in Hz = 19.2e6 Hz / pwmClock / pwmRange, 19200000/4400000/255, I got 1. It seams wrong.
    – Ming Ding
    Jul 13, 2019 at 15:51
  • I am not that familiar with the wiringPi internals. pigpio will give a figure of 4385965 if you select 4.4M as a frequency (sudo pigpiod; pigs hp 12 4400000 500000).
    – joan
    Jul 13, 2019 at 16:58
  • would u pls give more details. I want to output PWM with frequency 4.4M, but when I used the PWM_MODE_BAL mode, I can not get the correct frequency.
    – Ming Ding
    Jul 14, 2019 at 13:12
  • As I said I am not that familiar with wiringPi.
    – joan
    Jul 14, 2019 at 13:42

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.