vgs1988
Posts: 12
Joined: Wed Mar 15, 2017 6:26 pm

pigpio hardware PWM generation

Sun Apr 02, 2017 8:40 am

Hello.

Would you please help me about writing a simple c code to make hardware pwm signal in pigpio library?

Thanks

User avatar
joan
Posts: 14021
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: pigpio hardware PWM generation

Sun Apr 02, 2017 10:08 am

For hardware PWM see http://abyz.co.uk/rpi/pigpio/cif.html#gpioHardwarePWM

Code: Select all

#include <stdio.h>
#include <stdint.h>

#include <pigpio.h>

int main(int argc, char *argv[])
{
   if (gpioInitialise() < 0) return 1;

   printf("start piscope\n");
   printf("press return to continue\n");
   getchar();

   gpioHardwarePWM(12, 5000, 500000);
   gpioHardwarePWM(13, 10000, 100000);

   printf("STOP piscope\n");
   printf("press return to continue\n");
   getchar();

   gpioHardwarePWM(12, 0, 0);
   gpioHardwarePWM(13, 0, 0);

   gpioTerminate();

   return 0;
}
Ignore the piscope prompts (just press return).

vgs1988
Posts: 12
Joined: Wed Mar 15, 2017 6:26 pm

Re: pigpio hardware PWM generation

Sun Apr 02, 2017 9:06 pm

Thanks Mr Joan for your help.

I appreciate your nice package.

I put the above code inn a file hardpwm.c and copied it to PIGPIO folder.

when I run the above code using

Code: Select all

cc -o hardpwm hardpwm.c 
command, I get the following error:

Code: Select all

/tmp/ccMqAptM.o: In function `main':
hardpwm.c:(.text+0x14): undefined reference to `gpioInitialise'
hardpwm.c:(.text+0x4c): undefined reference to `gpioHardwarePWM'
hardpwm.c:(.text+0x5c): undefined reference to `gpioHardwarePWM'
hardpwm.c:(.text+0x80): undefined reference to `gpioHardwarePWM'
hardpwm.c:(.text+0x90): undefined reference to `gpioHardwarePWM'
hardpwm.c:(.text+0x94): undefined reference to `gpioTerminate'
collect2: ld returned 1 exit status
Would you please help me about that?

Stiller
Posts: 206
Joined: Fri Oct 14, 2016 9:03 am

Re: pigpio hardware PWM generation

Mon Apr 03, 2017 6:20 am

hello,
although I am not familiar with pigpio, and I'm not quite sure about your compiler or linker flags, but did you try and add the flag
-lpigpio
or not? Just a guess, as I said, I am not familiar with pigpio.

vgs1988
Posts: 12
Joined: Wed Mar 15, 2017 6:26 pm

Re: pigpio hardware PWM generation

Mon Apr 03, 2017 7:59 am

You are right.

The problem was solved by adding proper options.
I use this code for generating software pwm pulse on pin GPIO 24, but it doesnt show anything on oscilloscope:

Code: Select all

#include <stdio.h>
#include <stdint.h>

#include <pigpio.h>

int main(int argc, char *argv[])
{

   if (gpioInitialise() < 0) return 1;

gpioSetPWMfrequency(24, 10000);
gpioSetPWMrange(24, 100);
gpioPWM(24, 50);
return 0;
}
What is wrong with the above code?

Also I am looking for a simple c code for soft pwm generation with arbitrary duty cycle. It seems that pigpio package soft pwm generation macros are defined in a manner that you can choose between a limited number of duty cycles.

Would you please help me about that?
Thanks

Return to “C/C++”