3 * Provide 2 channels of software driven PWM.
4 * Copyright (c) 2012 Gordon Henderson
5 ***********************************************************************
6 * This file is part of wiringPi:
7 * https://projects.drogon.net/raspberry-pi/wiringpi/
9 * wiringPi is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU Lesser General Public License as
11 * published by the Free Software Foundation, either version 3 of the
12 * License, or (at your option) any later version.
14 * wiringPi is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with wiringPi.
21 * If not, see <http://www.gnu.org/licenses/>.
22 ***********************************************************************
33 // The PWM Frequency is derived from the "pulse time" below. Essentially,
34 // the frequency is a function of the range and this pulse time.
35 // The total period will be range * pulse time in uS, so a pulse time
36 // of 100 and a range of 100 gives a period of 100 * 100 = 10,000 uS
37 // which is a frequency of 100Hz.
39 // It's possible to get a higher frequency by lowering the pulse time,
40 // however CPU uage will skyrocket as wiringPi uses a hard-loop to time
41 // periods under 100uS - this is because the Linux timer calls are just
42 // accurate at all, and have an overhead.
44 // Another way to increase the frequency is to reduce the range - however
45 // that reduces the overall output accuracy...
47 #define PULSE_TIME 100
49 static int marks [MAX_PINS] ;
50 static int range [MAX_PINS] ;
57 * Thread to do the actual PWM output
58 *********************************************************************************
61 static PI_THREAD (softPwmThread)
63 int pin, mark, space ;
73 space = range [pin] - mark ;
76 digitalWrite (pin, HIGH) ;
77 delayMicroseconds (mark * 100) ;
80 digitalWrite (pin, LOW) ;
81 delayMicroseconds (space * 100) ;
90 * Write a PWM value to the given pin
91 *********************************************************************************
94 void softPwmWrite (int pin, int value)
96 pin &= (MAX_PINS - 1) ;
100 else if (value > range [pin])
101 value = range [pin] ;
103 marks [pin] = value ;
109 * Create a new PWM thread.
110 *********************************************************************************
113 int softPwmCreate (int pin, int initialValue, int pwmRange)
117 pinMode (pin, OUTPUT) ;
118 digitalWrite (pin, LOW) ;
120 marks [pin] = initialValue ;
121 range [pin] = pwmRange ;
124 res = piThreadCreate (softPwmThread) ;