pikeyd165 – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | // pwm.c |
2 | // |
||
3 | // Example program for bcm2835 library |
||
4 | // Shows how to use PWM to control GPIO pins |
||
5 | // |
||
6 | // After installing bcm2835, you can build this |
||
7 | // with something like: |
||
8 | // gcc -o pwm pwm.c -l bcm2835 |
||
9 | // sudo ./pwm |
||
10 | // |
||
11 | // Or you can test it before installing with: |
||
12 | // gcc -o pwm -I ../../src ../../src/bcm2835.c pwm.c |
||
13 | // sudo ./pwm |
||
14 | // |
||
15 | // Connect an LED between GPIO18 (pin 12) and GND to observe the LED changing in brightness |
||
16 | // |
||
17 | // Author: Mike McCauley |
||
18 | // Copyright (C) 2013 Mike McCauley |
||
19 | // $Id: RF22.h,v 1.21 2012/05/30 01:51:25 mikem Exp $ |
||
20 | |||
21 | #include <bcm2835.h> |
||
22 | #include <stdio.h> |
||
23 | |||
24 | // PWM output on RPi Plug P1 pin 12 (which is GPIO pin 18) |
||
25 | // in alt fun 5. |
||
26 | // Note that this is the _only_ PWM pin available on the RPi IO headers |
||
27 | #define PIN RPI_GPIO_P1_12 |
||
28 | // and it is controlled by PWM channel 0 |
||
29 | #define PWM_CHANNEL 0 |
||
30 | // This controls the max range of the PWM signal |
||
31 | #define RANGE 1024 |
||
32 | |||
33 | int main(int argc, char **argv) |
||
34 | { |
||
35 | if (!bcm2835_init()) |
||
36 | return 1; |
||
37 | |||
38 | // Set the output pin to Alt Fun 5, to allow PWM channel 0 to be output there |
||
39 | bcm2835_gpio_fsel(PIN, BCM2835_GPIO_FSEL_ALT5); |
||
40 | |||
41 | // Clock divider is set to 16. |
||
42 | // With a divider of 16 and a RANGE of 1024, in MARKSPACE mode, |
||
43 | // the pulse repetition frequency will be |
||
44 | // 1.2MHz/1024 = 1171.875Hz, suitable for driving a DC motor with PWM |
||
45 | bcm2835_pwm_set_clock(BCM2835_PWM_CLOCK_DIVIDER_16); |
||
46 | bcm2835_pwm_set_mode(PWM_CHANNEL, 1, 1); |
||
47 | bcm2835_pwm_set_range(PWM_CHANNEL, RANGE); |
||
48 | |||
49 | // Vary the PWM m/s ratio between 1/RANGE and (RANGE-1)/RANGE |
||
50 | // over the course of a a few seconds |
||
51 | int direction = 1; // 1 is increase, -1 is decrease |
||
52 | int data = 1; |
||
53 | while (1) |
||
54 | { |
||
55 | if (data == 1) |
||
56 | direction = 1; // Switch to increasing |
||
57 | else if (data == RANGE-1) |
||
58 | direction = -1; // Switch to decreasing |
||
59 | data += direction; |
||
60 | bcm2835_pwm_set_data(PWM_CHANNEL, data); |
||
61 | bcm2835_delay(1); |
||
62 | } |
||
63 | |||
64 | bcm2835_close(); |
||
65 | return 0; |
||
66 | } |