# pwm.pl use Time::HiRes qw( usleep); use Inline C => Config => LIBS => '-lbcm2835'; use Inline C; # # Simple loops 10 times through some intensity values for # the led. # # You can give a integer (0 .. 1024) on the command line # to set the final brightness level at the end of the # script. # # No secured in any way against bad input, errors, ... # print "start\n"; if (!pwm_prepare()) { print "prepare led succeeded\n"; my @levels = (0,1,2,4,8,16,32,64,128,256,512,1024); for (my $i = 0; $i < 10; $i++) { foreach my $level (@levels) { # print "level: $level\n"; pwm_set_led_intensity($level); usleep(50000); } } pwm_set_led_intensity($ARGV[0]); pwm_clean(); print "the end\n"; } __END__ __C__ #include #include // PWM output on RPi Plug P1 pin 12 (which is GPIO pin 18) // in alt fun 5. // Note that this is the _only_ PWM pin available on the RPi IO headers #define PIN RPI_GPIO_P1_12 // and it is controlled by PWM channel 0 #define PWM_CHANNEL 0 // This controls the max range of the PWM signal #define RANGE 1024 int pwm_prepare(); void pwm_clean(); void pwm_set_led_intensity (int level); int pwm_prepare() { if (!bcm2835_init()) return 1; // Set the output pin to Alt Fun 5, to allow PWM channel 0 to be output there bcm2835_gpio_fsel(PIN, BCM2835_GPIO_FSEL_ALT5); // Clock divider is set to 16. // With a divider of 16 and a RANGE of 1024, in MARKSPACE mode, // the pulse repetition frequency will be // 1.2MHz/1024 = 1171.875Hz bcm2835_pwm_set_clock(BCM2835_PWM_CLOCK_DIVIDER_16); bcm2835_pwm_set_mode(PWM_CHANNEL, 1, 1); bcm2835_pwm_set_range(PWM_CHANNEL, RANGE); return 0; } void pwm_clean() { bcm2835_close(); return; } void pwm_set_led_intensity (int level) { if (0 <= level && level <= 1024) { bcm2835_pwm_set_data(0,level); } return; }