diff options
Diffstat (limited to 'CODE/debug/prewk/motordriver/arduino-softpwm-master')
6 files changed, 0 insertions, 511 deletions
diff --git a/CODE/debug/prewk/motordriver/arduino-softpwm-master/.gitignore b/CODE/debug/prewk/motordriver/arduino-softpwm-master/.gitignore deleted file mode 100644 index 1605aab..0000000 --- a/CODE/debug/prewk/motordriver/arduino-softpwm-master/.gitignore +++ /dev/null @@ -1 +0,0 @@ -SoftPWM.h.gch
\ No newline at end of file diff --git a/CODE/debug/prewk/motordriver/arduino-softpwm-master/README.md b/CODE/debug/prewk/motordriver/arduino-softpwm-master/README.md deleted file mode 100644 index 46e5456..0000000 --- a/CODE/debug/prewk/motordriver/arduino-softpwm-master/README.md +++ /dev/null @@ -1,75 +0,0 @@ -arduino-softpwm -=============== - -Software PWM library for [Arduino](https://arduino.cc/), and other compatible AVR boards. - -AVR microcontrollers provide hardware PWM on some pins but if you need PWM on other pins then it must be implemented in software. This library provides easy and efficient software PWM on any pin. Each channel can be set to a different PWM duty cycle. - -This library is licensed under [BSD3](https://opensource.org/licenses/BSD-3-Clause) - -#### Installation -- Download the most recent version here: https://github.com/Palatis/arduino-softpwm/archive/master.zip -- Using Arduino IDE 1.0.x: - - Sketch > Import Library... > Add Library... > select the downloaded file > Open -- Using Arduino IDE 1.5+: - - Sketch > Include Library > Add ZIP Library... > select the downloaded file > Open - - -#### Usage -See **File > Examples > arduino-softpwm > SoftPWM_example** for demonstration of library usage. - -Everything in this library except for preprocessor defines are wrapped in `namespace Palatis {}`, so if your compiler blames you about symbol not found, try add `Palatis::` before that thing. For example, `Palatis::SoftPWM.size()`. - -Alternatively, you can add `using namespace Palatis;` at the top of the source file to import everything under the `Palatis` namespace, this might be convenient if you're using my other libraries. - -`#define SOFTPWM_OUTPUT_DELAY` - Add this line above `#include <SoftPWM.h>` for a 1 PWM clock cycle delay between outputs to prevent large in-rush currents. - -`SOFTPWM_DEFINE_CHANNEL(CHANNEL, PMODE, PORT, BIT)` - Configure a pin for software PWM use. Consult the datasheet for your microcontroller for the appropriate `PORT` and `BIT` values for the physical pin. This information is shown in the pinout diagram, for example: [ATmega328P datasheet](http://www.atmel.com/Images/Atmel-8271-8-bit-AVR-Microcontroller-ATmega48A-48PA-88A-88PA-168A-168PA-328-328P_datasheet_Summary.pdf) **Figure 1-1** found on page 3. If you want to determine the Arduino pin assigned to the physical pin http://www.pighixxx.com/test/pinoutspg/boards/ provides this information for the most popular Arduino boards or you can look at the pins_arduino.h file in the **variant** folder used by your board. -- Parameter: **CHANNEL** - The channel number is used as an ID for the pin. -- Parameter: **PMODE** - DDRx register of the pin's port. Append the port letter to `DDR`. For example: The [Arduino UNO diagram](http://www.pighixxx.com/test/portfolio-items/uno/?portfolioID=314) shows that Arduino pin 13 is **PB5** which means that the port is **B** so you should use the value `DDRB` for that pin. -- Parameter: **PORT** - The port of the pin. For example: Arduino pin 13 is **PB5** so you should use the value `PORTB` for that pin. -- Parameter: **BIT** - The bit of the pin. For example: Arduino pin 13 is **PB5** so you should use the value `PORTB5` for that pin. - -`SOFTPWM_DEFINE_CHANNEL_INVERT( CHANNEL, PMODE, PORT, BIT )` - Depending on your application you may prefer to invert the output. See `SOFTPWM_DEFINE_CHANNEL()` for description of parameters. - -`SOFTPWM_DEFINE_OBJECT(CHANNEL_CNT)` - Define the softPWM object with the default 256 PWM levels. -- Parameter: **CHANNEL_CNT** - The number of channels that are defined. - -`SOFTPWM_DEFINE_OBJECT_WITH_PWM_LEVELS(CHANNEL_CNT, PWM_LEVELS)` - Define the softPWM object with the specified number of PWM levels. -- Parameter: **CHANNEL_CNT** - The number of channels that are defined. -- Parameter: **PWM_LEVELS** - The number of PWM levels. Using less PWM levels may allow a higher PWM frequency. The maximum value is 256. - -`SOFTPWM_DEFINE_EXTERN_OBJECT(CHANNEL_CNT)` - Add this if you want to use the SoftPWM object outside where it's defined. See `SOFTPWM_DEFINE_OBJECT()` for description of the parameter. - -`SOFTPWM_DEFINE_EXTERN_OBJECT_WITH_PWM_LEVELS(CHANNEL_CNT, PWM_LEVELS)` - Add this if you want to use the SoftPWM object outside where it's defined. See `SOFTPWM_DEFINE_OBJECT_WITH_PWM_LEVELS()` for description of parameters. - -`SoftPWM.begin(hertz)` - Initialize SoftPWM. -- Parameter: **hertz** - The PWM frequency. Setting the value too high will cause incorrect operation, too low will cause a visible flicker. - - Type: long - -`SoftPWM.printInterruptLoad()` - Prints diagnostic information to the serial monitor. This can be used to find the optimal PWM frequency by setting different PWM frequency values in begin() and then checking the resulting interrupt load. Calling this function will momentarily turn off the PWM on all channels. - -`SoftPWM.set(channel_idx, value)` - Set the PWM level of the given channel. -- Parameter: **channel_idx** - The channel to set. - - Type: int -- Parameter: **value** - The PWM level to set. - - Type: byte - -`SoftPWM.size()` -- Returns: Number of channels defined. - - Type: size_t - -`SoftPWM.PWMlevels()` -- Returns: The number of PWM levels. - - Type: unsigned int - -`SoftPWM.allOff()` - Set the PWM value of all channels to 0. - - -#### Troubleshooting -- LEDs have a visible flicker, especially noticeable when the LED is moving relative to the viewer. - - The PWM frequency set in `SoftPWM.begin()` is too low. Use `SoftPWM.printInterruptLoad()` to determine the optimum PWM frequency. You may be able to achieve a higher PWM frequency by setting less PWM levels with `SOFTPWM_DEFINE_OBJECT_WITH_PWM_LEVELS()` or `SOFTPWM_DEFINE_EXTERN_OBJECT_WITH_PWM_LEVELS()`. -- Erratic PWM operation - - The interrupt load is too high. Use `SoftPWM.printInterruptLoad()` to determine the interrupt load. You can decrease the interrupt load by setting less PWM levels with `SOFTPWM_DEFINE_OBJECT_WITH_PWM_LEVELS()` or `SOFTPWM_DEFINE_EXTERN_OBJECT_WITH_PWM_LEVELS()` or setting the PWM frequency lower in `SoftPWM.begin()`. -- LED brightness changes between low brightness PWM values are larger than at brighter PWM values. - - This is caused by the way LEDs work and is not caused by a problem with the library. If possible, use more PWM levels or never allow the LED to get dimmer than the level below which the difference between PWM levels is too distinct. diff --git a/CODE/debug/prewk/motordriver/arduino-softpwm-master/examples/SoftPWM_example/SoftPWM_example.ino b/CODE/debug/prewk/motordriver/arduino-softpwm-master/examples/SoftPWM_example/SoftPWM_example.ino deleted file mode 100644 index 96fc0b2..0000000 --- a/CODE/debug/prewk/motordriver/arduino-softpwm-master/examples/SoftPWM_example/SoftPWM_example.ino +++ /dev/null @@ -1,141 +0,0 @@ -/* - Arduino-SoftPWM: a software PWM library for Arduino - Copyright 2016, Victor Tseng <palatis@gmail.com> - - All rights reserved. - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions - are met: - - 1. Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - - 2. Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in the - documentation and/or other materials provided with the distribution. - - 3. Neither the name of the copyright holder nor the names of its - contributors may be used to endorse or promote products derived - from this software without specific prior written permission. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS - IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, - THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR - PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR - CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; - OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR - OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*/ - -#include <SoftPWM.h> - -/* pins_arduino.h defines the pin-port/bit mapping as PROGMEM so - you have to read them with pgm_read_xxx(). That's generally okay - for ordinary use, but really bad when you're writing super fast - codes because the compiler doesn't treat them as constants and - cannot optimize them away with sbi/cbi instructions. - - Therefore we have to tell the compiler the PORT and BIT here. - Hope someday we can find a way to workaround this. - - Check the manual of your MCU for port/bit mapping. - - The following example demonstrates setting channels for all pins - on the ATmega328P or ATmega168 used on Arduino Uno, Pro Mini, - Nano and other boards. */ -SOFTPWM_DEFINE_CHANNEL(0, DDRD, PORTD, PORTD0); //Arduino pin 0 -SOFTPWM_DEFINE_CHANNEL(1, DDRD, PORTD, PORTD1); //Arduino pin 1 -SOFTPWM_DEFINE_CHANNEL(2, DDRD, PORTD, PORTD2); //Arduino pin 2 -SOFTPWM_DEFINE_CHANNEL(3, DDRD, PORTD, PORTD3); //Arduino pin 3 -SOFTPWM_DEFINE_CHANNEL(4, DDRD, PORTD, PORTD4); //Arduino pin 4 -SOFTPWM_DEFINE_CHANNEL(5, DDRD, PORTD, PORTD5); //Arduino pin 5 -SOFTPWM_DEFINE_CHANNEL(6, DDRD, PORTD, PORTD6); //Arduino pin 6 -SOFTPWM_DEFINE_CHANNEL(7, DDRD, PORTD, PORTD7); //Arduino pin 7 -SOFTPWM_DEFINE_CHANNEL(8, DDRB, PORTB, PORTB0); //Arduino pin 8 -SOFTPWM_DEFINE_CHANNEL(9, DDRB, PORTB, PORTB1); //Arduino pin 9 -SOFTPWM_DEFINE_CHANNEL(10, DDRB, PORTB, PORTB2); //Arduino pin 10 -SOFTPWM_DEFINE_CHANNEL(11, DDRB, PORTB, PORTB3); //Arduino pin 11 -SOFTPWM_DEFINE_CHANNEL(12, DDRB, PORTB, PORTB4); //Arduino pin 12 -SOFTPWM_DEFINE_CHANNEL(13, DDRB, PORTB, PORTB5); //Arduino pin 13 -SOFTPWM_DEFINE_CHANNEL(14, DDRC, PORTC, PORTC0); //Arduino pin A0 -SOFTPWM_DEFINE_CHANNEL(15, DDRC, PORTC, PORTC1); //Arduino pin A1 -SOFTPWM_DEFINE_CHANNEL(16, DDRC, PORTC, PORTC2); //Arduino pin A2 -SOFTPWM_DEFINE_CHANNEL(17, DDRC, PORTC, PORTC3); //Arduino pin A3 -SOFTPWM_DEFINE_CHANNEL(18, DDRC, PORTC, PORTC4); //Arduino pin A4 -SOFTPWM_DEFINE_CHANNEL(19, DDRC, PORTC, PORTC5); //Arduino pin A5 - - -/* Or you may want inverted outputs: */ -/* - SOFTPWM_DEFINE_CHANNEL_INVERT(0, DDRD, PORTD, PORTD0); //Arduino pin 0 - SOFTPWM_DEFINE_CHANNEL_INVERT(1, DDRD, PORTD, PORTD1); //Arduino pin 1 - SOFTPWM_DEFINE_CHANNEL_INVERT(2, DDRD, PORTD, PORTD2); //Arduino pin 2 - SOFTPWM_DEFINE_CHANNEL_INVERT(3, DDRD, PORTD, PORTD3); //Arduino pin 3 - SOFTPWM_DEFINE_CHANNEL_INVERT(4, DDRD, PORTD, PORTD4); //Arduino pin 4 - SOFTPWM_DEFINE_CHANNEL_INVERT(5, DDRD, PORTD, PORTD5); //Arduino pin 5 - SOFTPWM_DEFINE_CHANNEL_INVERT(6, DDRD, PORTD, PORTD6); //Arduino pin 6 - SOFTPWM_DEFINE_CHANNEL_INVERT(7, DDRD, PORTD, PORTD7); //Arduino pin 7 - SOFTPWM_DEFINE_CHANNEL_INVERT(8, DDRB, PORTB, PORTB0); //Arduino pin 8 - SOFTPWM_DEFINE_CHANNEL_INVERT(9, DDRB, PORTB, PORTB2); //Arduino pin 9 - SOFTPWM_DEFINE_CHANNEL_INVERT(10, DDRB, PORTB, PORTB2); //Arduino pin 10 - SOFTPWM_DEFINE_CHANNEL_INVERT(11, DDRB, PORTB, PORTB3); //Arduino pin 11 - SOFTPWM_DEFINE_CHANNEL_INVERT(12, DDRB, PORTB, PORTB4); //Arduino pin 12 - SOFTPWM_DEFINE_CHANNEL_INVERT(13, DDRB, PORTB, PORTB5); //Arduino pin 13 - SOFTPWM_DEFINE_CHANNEL_INVERT(14, DDRC, PORTC, PORTC0); //Arduino pin A0 - SOFTPWM_DEFINE_CHANNEL_INVERT(15, DDRC, PORTC, PORTC1); //Arduino pin A1 - SOFTPWM_DEFINE_CHANNEL_INVERT(16, DDRC, PORTC, PORTC2); //Arduino pin A2 - SOFTPWM_DEFINE_CHANNEL_INVERT(17, DDRC, PORTC, PORTC3); //Arduino pin A3 - SOFTPWM_DEFINE_CHANNEL_INVERT(18, DDRC, PORTC, PORTC4); //Arduino pin A4 - SOFTPWM_DEFINE_CHANNEL_INVERT(19, DDRC, PORTC, PORTC5); //Arduino pin A5 -*/ - -/* Here you make an instance of desired channel counts you want - with the default 256 PWM levels (0 ~ 255). */ -//SOFTPWM_DEFINE_OBJECT(20); - -/* Or you can make one with only 100 PWM levels (0 ~ 99). - By using less PWM levels, you may be able to use higher - pwm frequencies. */ -SOFTPWM_DEFINE_OBJECT_WITH_PWM_LEVELS(20, 100); - -/* If you want to use the SoftPWM object outside where it's defined, - add the following line to the file. */ -//SOFTPWM_DEFINE_EXTERN_OBJECT(16); -SOFTPWM_DEFINE_EXTERN_OBJECT_WITH_PWM_LEVELS(20, 100); - -void setup() { - Serial.begin(19200); - - // begin with 60hz pwm frequency - Palatis::SoftPWM.begin(60); - - // print interrupt load for diagnostic purposes - Palatis::SoftPWM.printInterruptLoad(); -} - -static volatile uint8_t v = 0; -void loop() { - for (uint8_t i = 0; i < Palatis::SoftPWM.size(); ++i) { - Serial.print(micros()); - Serial.print(" loop(): "); - Serial.println(i); - - unsigned long const WAIT = 1000000 / Palatis::SoftPWM.PWMlevels() / 2; - unsigned long nextMicros = 0; - for (int v = 0; v < Palatis::SoftPWM.PWMlevels() - 1; ++v) { - while (micros() < nextMicros); - nextMicros = micros() + WAIT; - Palatis::SoftPWM.set(i, v); - } - for (int v = Palatis::SoftPWM.PWMlevels() - 1; v >= 0; --v) { - while (micros() < nextMicros); - nextMicros = micros() + WAIT; - Palatis::SoftPWM.set(i, v); - } - } -} diff --git a/CODE/debug/prewk/motordriver/arduino-softpwm-master/keywords.txt b/CODE/debug/prewk/motordriver/arduino-softpwm-master/keywords.txt deleted file mode 100644 index 61d42b7..0000000 --- a/CODE/debug/prewk/motordriver/arduino-softpwm-master/keywords.txt +++ /dev/null @@ -1,30 +0,0 @@ -####################################### -# Syntax Coloring Map SoftPWM -####################################### -####################################### -# Datatypes (KEYWORD1) -####################################### -Palatis KEYWORD1 -SoftPWM KEYWORD1 -####################################### -# Methods and Functions (KEYWORD2) -####################################### -begin KEYWORD2 -set KEYWORD2 -size KEYWORD2 -PWMlevels KEYWORD2 -allOff KEYWORD2 -update KEYWORD2 -printInterruptLoad KEYWORD2 - -####################################### -# Constants (LITERAL1) -####################################### -SOFTPWM_DEFINE_PINMODE LITERAL1 -SOFTPWM_DEFINE_CHANNEL LITERAL1 -SOFTPWM_DEFINE_CHANNEL_INVERT LITERAL1 -SOFTPWM_DEFINE_OBJECT_WITH_PWM_LEVELS LITERAL1 -SOFTPWM_DEFINE_OBJECT LITERAL1 -SOFTPWM_DEFINE_EXTERN_OBJECT_WITH_PWM_LEVELS LITERAL1 -SOFTPWM_DEFINE_EXTERN_OBJECT LITERAL1 -SOFTPWM_OUTPUT_DELAY LITERAL1 diff --git a/CODE/debug/prewk/motordriver/arduino-softpwm-master/library.properties b/CODE/debug/prewk/motordriver/arduino-softpwm-master/library.properties deleted file mode 100644 index 174c19b..0000000 --- a/CODE/debug/prewk/motordriver/arduino-softpwm-master/library.properties +++ /dev/null @@ -1,9 +0,0 @@ -name=arduino-softpwm -version=1.0.1 -author=Victor Tseng <palatis@gmail.com> -maintainer=Victor Tseng <palatis@gmail.com> -sentence=Software PWM library for Arduino. -paragraph=Provides PWM on any pin. -category=Signal Input/Output -url=https://github.com/Palatis/arduino-softpwm -architectures=avr diff --git a/CODE/debug/prewk/motordriver/arduino-softpwm-master/src/SoftPWM.h b/CODE/debug/prewk/motordriver/arduino-softpwm-master/src/SoftPWM.h deleted file mode 100644 index c315a4e..0000000 --- a/CODE/debug/prewk/motordriver/arduino-softpwm-master/src/SoftPWM.h +++ /dev/null @@ -1,255 +0,0 @@ -/* - Arduino-SoftPWM: a software PWM library for Arduino - Copyright 2016, Victor Tseng <palatis@gmail.com> - - All rights reserved. - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions - are met: - - 1. Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - - 2. Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in the - documentation and/or other materials provided with the distribution. - - 3. Neither the name of the copyright holder nor the names of its - contributors may be used to endorse or promote products derived - from this software without specific prior written permission. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS - IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, - THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR - PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR - CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; - OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR - OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*/ - -#ifndef _SOFTPWM_H_ -#define _SOFTPWM_H_ - -#include <Arduino.h> - -// helper macros -#define SOFTPWM_DEFINE_PINMODE(CHANNEL, PMODE, PORT, BIT) \ - namespace Palatis { \ - template <> \ - inline void pinModeStatic<CHANNEL>(uint8_t const mode) { \ - if (mode == INPUT) { \ - bitClear(PMODE, BIT); bitClear(PORT, BIT); \ - } \ - else if (mode == INPUT_PULLUP) { \ - bitClear(PMODE, BIT); bitSet(PORT, BIT); \ - } \ - else { \ - bitSet(PMODE, BIT); \ - } \ - } \ - } - -#define SOFTPWM_DEFINE_CHANNEL(CHANNEL, PMODE, PORT, BIT) \ - namespace Palatis { \ - template <> \ - inline void bitWriteStatic<CHANNEL>(bool const value) { \ - bitWrite( PORT, BIT, value ); \ - } \ - } \ - SOFTPWM_DEFINE_PINMODE(CHANNEL, PMODE, PORT, BIT) - -#define SOFTPWM_DEFINE_CHANNEL_INVERT(CHANNEL, PMODE, PORT, BIT) \ - namespace Palatis { \ - template <> \ - inline void bitWriteStatic<CHANNEL>(bool const value) { \ - bitWrite(PORT, BIT, !value); \ - } \ - } \ - SOFTPWM_DEFINE_PINMODE(CHANNEL, PMODE, PORT, BIT) - -#if defined(__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__) -#define SOFTPWM_DEFINE_OBJECT_WITH_PWM_LEVELS(CHANNEL_CNT, PWM_LEVELS) \ - namespace Palatis { \ - CSoftPWM<CHANNEL_CNT, PWM_LEVELS> SoftPWM; \ - } \ - ISR(TIM1_COMPA_vect) { \ - interrupts(); \ - Palatis::SoftPWM.update(); \ - } -#else - #if defined (__AVR_ATmega8__) || defined (__AVR_ATmega8A__) - #define TIMSK1 TIMSK - #endif -#define SOFTPWM_DEFINE_OBJECT_WITH_PWM_LEVELS(CHANNEL_CNT, PWM_LEVELS) \ - namespace Palatis { \ - CSoftPWM<CHANNEL_CNT, PWM_LEVELS> SoftPWM; \ - } \ - ISR(TIMER1_COMPA_vect) { \ - interrupts(); \ - Palatis::SoftPWM.update(); \ - } -#endif - -#define SOFTPWM_DEFINE_OBJECT(CHANNEL_CNT) \ - SOFTPWM_DEFINE_OBJECT_WITH_PWM_LEVELS(CHANNEL_CNT, 0) - -#define SOFTPWM_DEFINE_EXTERN_OBJECT_WITH_PWM_LEVELS(CHANNEL_CNT, PWM_LEVELS) \ - namespace Palatis { \ - extern CSoftPWM<CHANNEL_CNT, PWM_LEVELS> SoftPWM; \ - } - -#define SOFTPWM_DEFINE_EXTERN_OBJECT(CHANNEL_CNT) \ - SOFTPWM_DEFINE_EXTERN_OBJECT_WITH_PWM_LEVELS(CHANNEL_CNT, 0) - -// here comes the magic @o@ -namespace Palatis { - -template <int channel> inline void bitWriteStatic(bool value) {} -template <int channel> inline void pinModeStatic(uint8_t mode) {} - -template <int channel> -struct bitWriteStaticExpander { - void operator() (bool value) const { - bitWriteStatic<channel>(value); - bitWriteStaticExpander < channel - 1 > ()(value); - } - - void operator() (uint8_t const &count, uint8_t const * const &channels) const { -#ifdef SOFTPWM_OUTPUT_DELAY - bitWriteStatic<channel>((count + channel) < channels[channel]); -#else - bitWriteStatic<channel>(count < channels[channel]); -#endif - bitWriteStaticExpander < channel - 1 > ()(count, channels); - } -}; - -template <> -struct bitWriteStaticExpander < -1 > { - void operator() (bool) const {} - void operator() (uint8_t const &, uint8_t const * const &) const {} -}; - -template <int channel> -struct pinModeStaticExpander { - void operator() (uint8_t const mode) const - { - pinModeStatic<channel>(mode); - pinModeStaticExpander < channel - 1 > ()(mode); - } -}; - -template <> -struct pinModeStaticExpander < -1 > { - void operator() (uint8_t const mode) const {} -}; - -template <unsigned int num_channels, unsigned int num_PWM_levels> -class CSoftPWM { - public: - void begin(const unsigned long hertz) { - allOff(); //this prevents inverted channels from momentarily going LOW - asm volatile ("/************ pinModeStaticExpander begin ************/"); - const uint8_t oldSREG = SREG; - noInterrupts(); - pinModeStaticExpander < num_channels - 1 > ()( OUTPUT ); - SREG = oldSREG; - asm volatile ("/************ pinModeStaticExpander end ************/"); - - /* the setup of timer1 is stolen from ShiftPWM :-P - http://www.elcojacobs.com/shiftpwm/ */ - asm volatile ("/************ timer setup begin ************/"); - TCCR1A = 0b00000000; - TCCR1B = 0b00001001; - OCR1A = (F_CPU - hertz * PWMlevels() / 2) / (hertz * PWMlevels()); - bitSet(TIMSK1, OCIE1A); - asm volatile ("/************ timer setup end ************/"); - - _count = 0; - } - - void set(const int channel_idx, const uint8_t value) { - _channels[channel_idx] = value; - } - - size_t size() const { - return num_channels; - } - - unsigned int PWMlevels() const { - return num_PWM_levels ? num_PWM_levels : 256; - } - - void allOff() { - asm volatile ("/********** CSoftPWM::allOff() begin **********/"); - const uint8_t oldSREG = SREG; - noInterrupts(); - for (int i = 0; i < num_channels; ++i) - _channels[i] = 0; - bitWriteStaticExpander < num_channels - 1 > ()(false); - SREG = oldSREG; - asm volatile ("/********** CSoftPWM::allOff() end **********/"); - } - - /* This function cannot be private because the ISR uses it, and I have - no idea about how to make friends with ISR. :-( */ - void update() __attribute__((always_inline)) { - asm volatile ("/********** CSoftPWM::update() begin **********/"); - const uint8_t count = _count; - bitWriteStaticExpander < num_channels - 1 > ()(count, _channels); - ++_count; - if (_count == PWMlevels()) - _count = 0; - asm volatile ("/********** CSoftPWM::update() end **********/"); - } - - #if defined(HAVE_HWSERIAL0) - /* this function is stolen from ShiftPWM :-P - http://www.elcojacobs.com/shiftpwm/ */ - void printInterruptLoad() { - unsigned long time1, time2; - - bitSet(TIMSK1, OCIE1A); // enable interrupt - time1 = micros(); - delayMicroseconds(5000); - time1 = micros() - time1; - - bitClear(TIMSK1, OCIE1A); // disable interrupt - time2 = micros(); - delayMicroseconds(5000); - time2 = micros() - time2; - - const double load = static_cast<double>(time1 - time2) / time1; - const double interrupt_frequency = static_cast<double>(F_CPU) / (OCR1A + 1); - const double cycles_per_interrupt = load * F_CPU / interrupt_frequency; - - Serial.println(F("SoftPWM::printInterruptLoad():")); - Serial.print(F(" Load of interrupt: ")); - Serial.println(load, 10); - Serial.print(F(" Clock cycles per interrupt: ")); - Serial.println( cycles_per_interrupt ); - Serial.print(F(" Interrupt frequency: ")); - Serial.print(interrupt_frequency); - Serial.println(F(" Hz")); - Serial.print(F(" PWM frequency: ")); - Serial.print(interrupt_frequency / PWMlevels()); - Serial.println(F(" Hz")); - Serial.print(F(" PWM levels: ")); - Serial.println(PWMlevels()); - - bitSet(TIMSK1, OCIE1A); // enable interrupt again - } - #endif - - private: - uint8_t _channels[num_channels]; - uint8_t _count; -}; - -} // namespace Palatis -#endif // #ifndef _SOFTPWM_H_ |
