From a20fb1b1540e721ae373c3474e4dc201a95d2c61 Mon Sep 17 00:00:00 2001 From: Gordon Henderson Date: Fri, 24 Aug 2012 17:49:26 +0100 Subject: [PATCH] Added software PWM module into wiringPi - library code and an example. --- examples/Makefile | 12 +++-- examples/softPwm.c | 44 +++++++++++++++++ wiringPi/Makefile | 5 +- wiringPi/softPwm.c | 116 +++++++++++++++++++++++++++++++++++++++++++++ wiringPi/softPwm.h | 27 +++++++++++ 5 files changed, 198 insertions(+), 6 deletions(-) create mode 100644 examples/softPwm.c create mode 100644 wiringPi/softPwm.c create mode 100644 wiringPi/softPwm.h diff --git a/examples/Makefile b/examples/Makefile index c1b2182..450e0dc 100644 --- a/examples/Makefile +++ b/examples/Makefile @@ -35,11 +35,11 @@ LIBS = -lwiringPi # Should not alter anything below this line ############################################################################### -SRC = test1.c test2.c speed.c lcd.c wfi.c piface.c gertboard.c nes.c delayTest.c +SRC = test1.c test2.c speed.c lcd.c wfi.c piface.c gertboard.c nes.c delayTest.c softPwm.c -OBJ = test1.o test2.o speed.o lcd.o wfi.o piface.o gertboard.o nes.o delayTest.o +OBJ = test1.o test2.o speed.o lcd.o wfi.o piface.o gertboard.o nes.o delayTest.o softPwm.o -all: test1 test2 speed lcd wfi piface gertboard nes +all: test1 test2 speed lcd wfi piface gertboard nes softPwm test1: test1.o @echo [link] @@ -73,6 +73,10 @@ nes: nes.o @echo [link] $(CC) -o $@ nes.o $(LDFLAGS) $(LIBS) -lm +softPwm: softPwm.o + @echo [link] + $(CC) -o $@ softPwm.o $(LDFLAGS) $(LIBS) -lm -lpthread + delayTest: delayTest.o @echo [link] @@ -84,7 +88,7 @@ delayTest: delayTest.o @$(CC) -c $(CFLAGS) $< -o $@ clean: - rm -f $(OBJ) *~ core tags test1 test2 speed lcd wfi piface gertboard nes delayTest + rm -f $(OBJ) *~ core tags test1 test2 speed lcd wfi piface gertboard nes delayTest softPwm tags: $(SRC) @echo [ctags] diff --git a/examples/softPwm.c b/examples/softPwm.c new file mode 100644 index 0000000..e558c4b --- /dev/null +++ b/examples/softPwm.c @@ -0,0 +1,44 @@ + +#include +#include +#include + +#include +#include + +#define RANGE 100 +#define NUM_LEDS 12 + +int ledMap [NUM_LEDS] = { 0, 1, 2, 3, 4, 5, 6, 7, 10, 11, 12, 13 } ; + +int values [NUM_LEDS] = { 0, 17, 32, 50, 67, 85, 100, 85, 67, 50, 32, 17 } ; + +int main () +{ + int i, j ; + + if (wiringPiSetup () == -1) + { + fprintf (stdout, "oops: %s\n", strerror (errno)) ; + return 1 ; + } + + for (i = 0 ; i < NUM_LEDS ; ++i) + { + softPwmCreate (ledMap [i], 0, RANGE) ; + printf ("%3d, %3d, %3d\n", i, ledMap [i], values [i]) ; + } + + for (;;) + { + for (i = 0 ; i < NUM_LEDS ; ++i) + softPwmWrite (ledMap [i], values [i]) ; + + delay (50) ; + + i = values [0] ; + for (j = 0 ; j < NUM_LEDS - 1 ; ++j) + values [j] = values [j + 1] ; + values [NUM_LEDS - 1] = i ; + } +} diff --git a/wiringPi/Makefile b/wiringPi/Makefile index 3798ce0..b80caea 100644 --- a/wiringPi/Makefile +++ b/wiringPi/Makefile @@ -38,12 +38,12 @@ LIBS = SRC = wiringPi.c wiringPiFace.c wiringSerial.c wiringShift.c \ gertboard.c \ piNes.c \ - lcd.c piHiPri.c piThread.c + lcd.c piHiPri.c piThread.c softPwm.c OBJ = wiringPi.o wiringPiFace.o wiringSerial.o wiringShift.o \ gertboard.o \ piNes.o \ - lcd.o piHiPri.o piThread.o + lcd.o piHiPri.o piThread.o softPwm.o all: $(TARGET) @@ -76,6 +76,7 @@ install: $(TARGET) install -m 0644 wiringShift.h /usr/local/include install -m 0644 gertboard.h /usr/local/include install -m 0644 piNes.h /usr/local/include + install -m 0644 softPwm.h /usr/local/include install -m 0644 lcd.h /usr/local/include install -m 0644 libwiringPi.a /usr/local/lib diff --git a/wiringPi/softPwm.c b/wiringPi/softPwm.c new file mode 100644 index 0000000..56bf4d8 --- /dev/null +++ b/wiringPi/softPwm.c @@ -0,0 +1,116 @@ +/* + * softPwm.c: + * Provide 2 channels of software driven PWM. + * Copyright (c) 2012 Gordon Henderson + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with wiringPi. + * If not, see . + *********************************************************************** + */ + +#include +#include + +#include "wiringPi.h" +#include "softPwm.h" + +#define MAX_PINS 64 + +// The PWM Frequency is derived from the "pulse time" below. Essentially, +// the frequency is a function of the range and this pulse time. +// The total period will be range * pulse time in uS, so a pulse time +// of 100 and a range of 100 gives a period of 100 * 100 = 10,000 uS +// which is a frequency of 100Hz. +// +// It's possible to get a higher frequency by lowering the pulse time, +// however CPU uage will skyrocket as wiringPi uses a hard-loop to time +// periods under 100uS - this is because the Linux timer calls are just +// accurate at all, and have an overhead. +// +// Another way to increase the frequency is to reduce the range - however +// that reduces the overall output accuracy... + +#define PULSE_TIME 100 + +static int marks [MAX_PINS] ; +static int range [MAX_PINS] ; + +int newPin = -1 ; + + +/* + * softPwmThread: + * Thread to do the actual PWM output + ********************************************************************************* + */ + +static PI_THREAD (softPwmThread) +{ + int pin, mark, space ; + + pin = newPin ; + newPin = -1 ; + + piHiPri (50) ; + + for (;;) + { + mark = marks [pin] ; + space = range [pin] - mark ; + + if (mark != 0) + digitalWrite (pin, HIGH) ; + delayMicroseconds (mark * 100) ; + + if (space != 0) + digitalWrite (pin, LOW) ; + delayMicroseconds (space * 100) ; + } + + return NULL ; +} + +void softPwmWrite (int pin, int value) +{ + marks [pin] = value ; +} + + +/* + * softPwmCreate: + * Create a new PWM thread. + ********************************************************************************* + */ + +int softPwmCreate (int pin, int initialValue, int pwmRange) +{ + int res ; + + pinMode (pin, OUTPUT) ; + digitalWrite (pin, LOW) ; + + marks [pin] = initialValue ; + range [pin] = pwmRange ; + + newPin = pin ; + res = piThreadCreate (softPwmThread) ; + + while (newPin != -1) + delay (1) ; + + return res ; +} diff --git a/wiringPi/softPwm.h b/wiringPi/softPwm.h new file mode 100644 index 0000000..a6d8cd4 --- /dev/null +++ b/wiringPi/softPwm.h @@ -0,0 +1,27 @@ +/* + * softPwm.h: + * Provide 2 channels of software driven PWM. + * Copyright (c) 2012 Gordon Henderson + *********************************************************************** + * This file is part of wiringPi: + * https://projects.drogon.net/raspberry-pi/wiringpi/ + * + * wiringPi is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * wiringPi is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with wiringPi. + * If not, see . + *********************************************************************** + */ + +extern int setupSoftPwm (int pin) ; +extern int softPwmCreate (int pin, int value, int range) ; +extern void softPwmWrite (int pin, int value) ; -- 2.30.2