added arduino, modified build
This commit is contained in:
BIN
arduino/libraries/SoftPWM/.SoftPWM.cpp.swp
Normal file
BIN
arduino/libraries/SoftPWM/.SoftPWM.cpp.swp
Normal file
Binary file not shown.
BIN
arduino/libraries/SoftPWM/.SoftPWM.h.swp
Normal file
BIN
arduino/libraries/SoftPWM/.SoftPWM.h.swp
Normal file
Binary file not shown.
BIN
arduino/libraries/SoftPWM/.SoftPWM_timer.h.swp
Normal file
BIN
arduino/libraries/SoftPWM/.SoftPWM_timer.h.swp
Normal file
Binary file not shown.
20
arduino/libraries/SoftPWM/LICENSE.txt
Normal file
20
arduino/libraries/SoftPWM/LICENSE.txt
Normal file
@@ -0,0 +1,20 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2014 Brett Hagman
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
this software and associated documentation files (the "Software"), to deal in
|
||||
the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
137
arduino/libraries/SoftPWM/README.md
Normal file
137
arduino/libraries/SoftPWM/README.md
Normal file
@@ -0,0 +1,137 @@
|
||||
# SoftPWM Library #
|
||||
|
||||
----
|
||||
|
||||
## What's New? ##
|
||||
|
||||
Version 1.0.1
|
||||
|
||||
* Changes from Paul Stoffregen
|
||||
* Use IntervalTimer on Teensy 3.x
|
||||
* Use LED_BUILTIN for WLED in example
|
||||
|
||||
Version 1.0.0
|
||||
|
||||
* Initial release
|
||||
|
||||
## Description ##
|
||||
|
||||
A Wiring Framework (and Arduino) Library to produce PWM signals on any arbitrary pin.
|
||||
|
||||
It was originally designed for use controlling the brightness of LEDs, but could be modified to control servos and other low frequency PWM controlled devices as well.
|
||||
|
||||
It uses a single hardware timer (Timer 2 on AVR, or IntervalTimer on Teensy 3.x) on the microcontroller to generate up to 20 PWM channels.
|
||||
|
||||
----
|
||||
|
||||
## Features ##
|
||||
|
||||
* Arbitrary output pins
|
||||
* Up to 20 different channels can be created
|
||||
* True zero level, i.e. off == off
|
||||
* Separate fade rates for on and off
|
||||
|
||||
----
|
||||
|
||||
## Download and Installation ##
|
||||
|
||||
You can use the Arduino Library Manager (Sketch -> Include Library -> Manage Libraries...) to download the library.
|
||||
|
||||
Alternatively, you can download the library directly, and install it yourself.
|
||||
|
||||
* [SoftPWM Library - Latest Version](https://github.com/bhagman/SoftPWM/archive/master.zip)
|
||||
|
||||
Unzip the folder and rename it to `SoftPWM`, then move it to your `arduinosketchfolder/libraries/` folder.
|
||||
|
||||
----
|
||||
|
||||
## Usage Example ##
|
||||
|
||||
```
|
||||
#include "SoftPWM.h"
|
||||
|
||||
void setup()
|
||||
{
|
||||
// Initialize
|
||||
SoftPWMBegin();
|
||||
|
||||
// Create and set pin 13 to 0 (off)
|
||||
SoftPWMSet(13, 0);
|
||||
|
||||
// Set fade time for pin 13 to 100 ms fade-up time, and 500 ms fade-down time
|
||||
SoftPWMSetFadeTime(13, 100, 500);
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
// Turn on - set to 100%
|
||||
SoftPWMSetPercent(13, 100);
|
||||
|
||||
// Wait for LED to turn on - you could do other tasks here
|
||||
delay(100);
|
||||
|
||||
// Turn off - set to 0%
|
||||
SoftPWMSetPercent(13, 0);
|
||||
|
||||
// Wait for LED to turn off
|
||||
delay(500);
|
||||
}
|
||||
```
|
||||
|
||||
----
|
||||
## Function Descriptions ##
|
||||
|
||||
`SoftPWMBegin([defaultPolarity])`
|
||||
|
||||
* Initializes the library - sets up the timer and other tasks.
|
||||
* optional `defaultPolarity` allows all newly defined pins to take on this polarity.
|
||||
* Values: `SOFTPWM_NORMAL`, `SOFTPWM_INVERTED`
|
||||
|
||||
`SoftPWMSet(pin,value)`
|
||||
|
||||
* `pin` is the output pin.
|
||||
* `value` is a value between 0 and 255 (inclusive).
|
||||
|
||||
`SoftPWMSetPercent(pin,percent)`
|
||||
|
||||
* `pin` is the output pin.
|
||||
* `percent` is a value between 0 and 100 (inclusive).
|
||||
|
||||
`SoftPWMSetFadeTime(pin,fadeUpTime,fadeDownTime)`
|
||||
|
||||
* `pin` is the output pin.
|
||||
* `fadeuptime` is the time in milliseconds that it will take the channel to fade from 0 to 255.
|
||||
* Range: 0 to 4000
|
||||
* `fadedowntime` is the time in milliseconds that it will take the channel to fade from 255 to 0.
|
||||
* Range: 0 to 4000
|
||||
|
||||
`SoftPWMSetPolarity(pin,polarity)`
|
||||
|
||||
* `pin` is the output pin.
|
||||
* `polarity` is the polarity for the given pin.
|
||||
|
||||
### Notes ###
|
||||
|
||||
* You can use `ALL` in place of the pin number to have the function act on all currently set channels.
|
||||
* e.g. `SoftPWMSetFadeTime(ALL, 100, 400)` - this will set all created channels to have a fade-up time of 100 ms and a fade-down time of 400.
|
||||
* The polarity setting of the pin is as follows:
|
||||
* `SOFTPWM_NORMAL` means that the pin is LOW when the PWM value is 0, whereas `SOFTPWM_INVERTED` indicates the pin should be HIGH when the PWM value is 0.
|
||||
|
||||
|
||||
----
|
||||
|
||||
## Demonstrations ##
|
||||
|
||||
Arduino Duemilanove LED Blink example - available as library example:
|
||||
|
||||
[](https://www.youtube.com/watch?v=9tTd7aLm9aQ)
|
||||
|
||||
rDuino LEDHead Bounce example - available as library example:
|
||||
|
||||
[](https://www.youtube.com/watch?v=jE7Zw1zNL6c)
|
||||
|
||||
More demos:
|
||||
|
||||
https://www.youtube.com/view_play_list?p=33BB5D2E20609C52
|
||||
|
||||
----
|
||||
303
arduino/libraries/SoftPWM/SoftPWM.cpp
Normal file
303
arduino/libraries/SoftPWM/SoftPWM.cpp
Normal file
@@ -0,0 +1,303 @@
|
||||
/*
|
||||
|| @author Brett Hagman <bhagman@wiring.org.co>
|
||||
|| @contribution Paul Stoffregen (paul at pjrc dot com)
|
||||
|| @url http://wiring.org.co/
|
||||
|| @url http://roguerobotics.com/
|
||||
||
|
||||
|| @description
|
||||
|| | A Software PWM Library
|
||||
|| |
|
||||
|| | Written by Brett Hagman
|
||||
|| | http://www.roguerobotics.com/
|
||||
|| | bhagman@roguerobotics.com, bhagman@wiring.org.co
|
||||
|| |
|
||||
|| | A Wiring (and Arduino) Library, for Atmel AVR8 bit series microcontrollers,
|
||||
|| | to produce PWM signals on any arbitrary pin.
|
||||
|| |
|
||||
|| | It was originally designed for controlling the brightness of LEDs, but
|
||||
|| | could be adapted to control servos and other low frequency PWM controlled
|
||||
|| | devices as well.
|
||||
|| |
|
||||
|| | It uses a single hardware timer (Timer 2) on the Atmel microcontroller to
|
||||
|| | generate up to 20 PWM channels (your mileage may vary).
|
||||
|| |
|
||||
|| #
|
||||
||
|
||||
|| @license Please see the accompanying LICENSE.txt file for this project.
|
||||
||
|
||||
|| @notes
|
||||
|| | Minor modification by Paul Stoffregen to support different timers.
|
||||
|| |
|
||||
|| #
|
||||
||
|
||||
|| @name Software PWM Library
|
||||
|| @type Library
|
||||
|| @target Atmel AVR 8 Bit
|
||||
||
|
||||
|| @version 1.0.1
|
||||
||
|
||||
*/
|
||||
|
||||
#include <avr/io.h>
|
||||
#include <avr/interrupt.h>
|
||||
#include "SoftPWM.h"
|
||||
#include "SoftPWM_timer.h"
|
||||
|
||||
#if defined(WIRING)
|
||||
#include <wiring_private.h>
|
||||
#elif ARDUINO >= 100
|
||||
#include <Arduino.h>
|
||||
#else
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
#endif
|
||||
|
||||
#if F_CPU
|
||||
#define SOFTPWM_FREQ 60UL
|
||||
#define SOFTPWM_OCR (F_CPU/(8UL*256UL*SOFTPWM_FREQ))
|
||||
#else
|
||||
// 130 == 60 Hz (on 16 MHz part)
|
||||
#define SOFTPWM_OCR 130
|
||||
#endif
|
||||
|
||||
volatile uint8_t _isr_softcount = 0xff;
|
||||
uint8_t _softpwm_defaultPolarity = SOFTPWM_NORMAL;
|
||||
|
||||
typedef struct {
|
||||
// hardware I/O port and pin for this channel
|
||||
int8_t pin;
|
||||
uint8_t polarity;
|
||||
volatile uint8_t *outport;
|
||||
uint8_t pinmask;
|
||||
uint8_t pwmvalue;
|
||||
uint8_t checkval;
|
||||
uint8_t fadeuprate;
|
||||
uint8_t fadedownrate;
|
||||
} softPWMChannel;
|
||||
|
||||
softPWMChannel _softpwm_channels[SOFTPWM_MAXCHANNELS];
|
||||
|
||||
|
||||
// Here is the meat and gravy
|
||||
#ifdef WIRING
|
||||
void SoftPWM_Timer_Interrupt(void)
|
||||
#else
|
||||
ISR(TIMER1_COMPA_vect)
|
||||
#endif
|
||||
{
|
||||
|
||||
uint8_t i;
|
||||
int16_t newvalue;
|
||||
int16_t direction;
|
||||
|
||||
if (++_isr_softcount == 0)
|
||||
{
|
||||
// set all channels high - let's start again
|
||||
// and accept new checkvals
|
||||
for (i = 0; i < SOFTPWM_MAXCHANNELS; i++) {
|
||||
if (_softpwm_channels[i].fadeuprate > 0 || _softpwm_channels[i].fadedownrate > 0) {
|
||||
// we want to fade to the new value
|
||||
direction = _softpwm_channels[i].pwmvalue - _softpwm_channels[i].checkval;
|
||||
|
||||
// we will default to jumping to the new value
|
||||
newvalue = _softpwm_channels[i].pwmvalue;
|
||||
|
||||
if (direction > 0 && _softpwm_channels[i].fadeuprate > 0) {
|
||||
newvalue = _softpwm_channels[i].checkval + _softpwm_channels[i].fadeuprate;
|
||||
if (newvalue > _softpwm_channels[i].pwmvalue)
|
||||
newvalue = _softpwm_channels[i].pwmvalue;
|
||||
} else if (direction < 0 && _softpwm_channels[i].fadedownrate > 0) {
|
||||
newvalue = _softpwm_channels[i].checkval - _softpwm_channels[i].fadedownrate;
|
||||
if (newvalue < _softpwm_channels[i].pwmvalue)
|
||||
newvalue = _softpwm_channels[i].pwmvalue;
|
||||
}
|
||||
|
||||
_softpwm_channels[i].checkval = newvalue;
|
||||
} else // just set the channel to the new value
|
||||
_softpwm_channels[i].checkval = _softpwm_channels[i].pwmvalue;
|
||||
|
||||
// now set the pin high (if not 0)
|
||||
if (_softpwm_channels[i].checkval > 0) // don't set if checkval == 0
|
||||
{
|
||||
if (_softpwm_channels[i].polarity == SOFTPWM_NORMAL)
|
||||
*_softpwm_channels[i].outport |= _softpwm_channels[i].pinmask;
|
||||
else
|
||||
*_softpwm_channels[i].outport &= ~(_softpwm_channels[i].pinmask);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
for (i = 0; i < SOFTPWM_MAXCHANNELS; i++)
|
||||
{
|
||||
if (_softpwm_channels[i].pin >= 0) // if it's a valid pin
|
||||
{
|
||||
if (_softpwm_channels[i].checkval == _isr_softcount) // if we have hit the width
|
||||
{
|
||||
// turn off the channel
|
||||
if (_softpwm_channels[i].polarity == SOFTPWM_NORMAL)
|
||||
*_softpwm_channels[i].outport &= ~(_softpwm_channels[i].pinmask);
|
||||
else
|
||||
*_softpwm_channels[i].outport |= _softpwm_channels[i].pinmask;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void SoftPWMBegin(uint8_t defaultPolarity) {
|
||||
// We can tweak the number of PWM period by changing the prescalar
|
||||
// and the OCR - we'll default to ck/8 (CS21 set) and OCR=128.
|
||||
// This gives 1024 cycles between interrupts. And the ISR consumes ~200 cycles, so
|
||||
// we are looking at about 20 - 30% of CPU time spent in the ISR.
|
||||
// At these settings on a 16 MHz part, we will get a PWM period of
|
||||
// approximately 60Hz (~16ms).
|
||||
|
||||
uint8_t i;
|
||||
|
||||
//#ifdef WIRING
|
||||
// Timer2.setMode(0b010); // CTC
|
||||
// Timer2.setClockSource(CLOCK_PRESCALE_8);
|
||||
// Timer2.setOCR(CHANNEL_A, SOFTPWM_OCR);
|
||||
// Timer2.attachInterrupt(INTERRUPT_COMPARE_MATCH_A, SoftPWM_Timer_Interrupt);
|
||||
//#else
|
||||
// SOFTPWM_TIMER_INIT(SOFTPWM_OCR);
|
||||
//#endif
|
||||
|
||||
cli(); //Disable interrupts while setting registers
|
||||
TCCR1A = 0; // Make sure it is zero
|
||||
TCCR1B = 0; // Make sure it is zero
|
||||
//TCCR1B = (1 << WGM21); // Configure for CTC mode (Set it; don't OR stuff into it)
|
||||
//TCCR1B |= (1 << CS21); // Prescaler @ 1024
|
||||
TIMSK1 = (1 << OCIE1A); // Enable interrupt
|
||||
OCR1A = SOFTPWM_OCR; // compare value = 1 sec (16MHz AVR)
|
||||
|
||||
TCCR1B = (1 << CS21); /* start timer (ck/8 prescalar) */ \
|
||||
TCCR1A = (1 << WGM21); /* CTC mode */ \
|
||||
|
||||
sei();
|
||||
|
||||
|
||||
for (i = 0; i < SOFTPWM_MAXCHANNELS; i++) {
|
||||
_softpwm_channels[i].pin = -1;
|
||||
_softpwm_channels[i].polarity = SOFTPWM_NORMAL;
|
||||
_softpwm_channels[i].outport = 0;
|
||||
_softpwm_channels[i].fadeuprate = 0;
|
||||
_softpwm_channels[i].fadedownrate = 0;
|
||||
}
|
||||
|
||||
_softpwm_defaultPolarity = defaultPolarity;
|
||||
}
|
||||
|
||||
|
||||
void SoftPWMSetPolarity(int8_t pin, uint8_t polarity) {
|
||||
uint8_t i;
|
||||
|
||||
if (polarity != SOFTPWM_NORMAL)
|
||||
polarity = SOFTPWM_INVERTED;
|
||||
|
||||
for (i = 0; i < SOFTPWM_MAXCHANNELS; i++) {
|
||||
if ((pin < 0 && _softpwm_channels[i].pin >= 0) || // ALL pins
|
||||
(pin >= 0 && _softpwm_channels[i].pin == pin)) // individual pin
|
||||
{
|
||||
_softpwm_channels[i].polarity = polarity;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void SoftPWMSetPercent(int8_t pin, uint8_t percent, uint8_t hardset) {
|
||||
SoftPWMSet(pin, ((uint16_t) percent * 255) / 100, hardset);
|
||||
}
|
||||
|
||||
|
||||
void SoftPWMSet(int8_t pin, uint8_t value, uint8_t hardset) {
|
||||
int8_t firstfree = -1; // first free index
|
||||
uint8_t i;
|
||||
|
||||
if (hardset) {
|
||||
SOFTPWM_TIMER_SET(0);
|
||||
_isr_softcount = 0xff;
|
||||
}
|
||||
|
||||
// If the pin isn't already set, add it
|
||||
for (i = 0; i < SOFTPWM_MAXCHANNELS; i++) {
|
||||
if ((pin < 0 && _softpwm_channels[i].pin >= 0) || // ALL pins
|
||||
(pin >= 0 && _softpwm_channels[i].pin == pin)) // individual pin
|
||||
{
|
||||
// set the pin (and exit, if individual pin)
|
||||
_softpwm_channels[i].pwmvalue = value;
|
||||
|
||||
if (pin >= 0) // we've set the individual pin
|
||||
return;
|
||||
}
|
||||
|
||||
// get the first free pin if available
|
||||
if (firstfree < 0 && _softpwm_channels[i].pin < 0)
|
||||
firstfree = i;
|
||||
}
|
||||
|
||||
if (pin >= 0 && firstfree >= 0) {
|
||||
// we have a free pin we can use
|
||||
_softpwm_channels[firstfree].pin = pin;
|
||||
_softpwm_channels[firstfree].polarity = _softpwm_defaultPolarity;
|
||||
_softpwm_channels[firstfree].outport = portOutputRegister(digitalPinToPort(pin));
|
||||
_softpwm_channels[firstfree].pinmask = digitalPinToBitMask(pin);
|
||||
_softpwm_channels[firstfree].pwmvalue = value;
|
||||
// _softpwm_channels[firstfree].checkval = 0;
|
||||
|
||||
// now prepare the pin for output
|
||||
// turn it off to start (no glitch)
|
||||
if (_softpwm_defaultPolarity == SOFTPWM_NORMAL)
|
||||
digitalWrite(pin, LOW);
|
||||
else
|
||||
digitalWrite(pin, HIGH);
|
||||
pinMode(pin, OUTPUT);
|
||||
}
|
||||
}
|
||||
|
||||
void SoftPWMEnd(int8_t pin) {
|
||||
uint8_t i;
|
||||
|
||||
for (i = 0; i < SOFTPWM_MAXCHANNELS; i++) {
|
||||
if ((pin < 0 && _softpwm_channels[i].pin >= 0) || // ALL pins
|
||||
(pin >= 0 && _softpwm_channels[i].pin == pin)) // individual pin
|
||||
{
|
||||
// now disable the pin (put it into INPUT mode)
|
||||
digitalWrite(_softpwm_channels[i].pin, 1);
|
||||
pinMode(_softpwm_channels[i].pin, INPUT);
|
||||
|
||||
// remove the pin
|
||||
_softpwm_channels[i].pin = -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void SoftPWMSetFadeTime(int8_t pin, uint16_t fadeUpTime, uint16_t fadeDownTime) {
|
||||
int16_t fadeAmount;
|
||||
uint8_t i;
|
||||
|
||||
for (i = 0; i < SOFTPWM_MAXCHANNELS; i++) {
|
||||
if ((pin < 0 && _softpwm_channels[i].pin >= 0) || // ALL pins
|
||||
(pin >= 0 && _softpwm_channels[i].pin == pin)) // individual pin
|
||||
{
|
||||
|
||||
fadeAmount = 0;
|
||||
if (fadeUpTime > 0)
|
||||
fadeAmount = 255UL * (SOFTPWM_OCR * 256UL / (F_CPU / 8000UL)) / fadeUpTime;
|
||||
|
||||
_softpwm_channels[i].fadeuprate = fadeAmount;
|
||||
|
||||
fadeAmount = 0;
|
||||
if (fadeDownTime > 0)
|
||||
fadeAmount = 255UL * (SOFTPWM_OCR * 256UL / (F_CPU / 8000UL)) / fadeDownTime;
|
||||
|
||||
_softpwm_channels[i].fadedownrate = fadeAmount;
|
||||
|
||||
if (pin >= 0) // we've set individual pin
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
15
arduino/libraries/SoftPWM/SoftPWM.d
Normal file
15
arduino/libraries/SoftPWM/SoftPWM.d
Normal file
@@ -0,0 +1,15 @@
|
||||
SoftPWM.o SoftPWM.d : \
|
||||
/home/mrh/Downloads/arduino-1.8.8-linux64/arduino-1.8.8//libraries/SoftPWM/SoftPWM.cpp \
|
||||
/home/mrh/Downloads/arduino-1.8.8-linux64/arduino-1.8.8//libraries/SoftPWM/SoftPWM.h \
|
||||
/home/mrh/Downloads/arduino-1.8.8-linux64/arduino-1.8.8//libraries/SoftPWM/SoftPWM_timer.h \
|
||||
/home/mrh/Downloads/arduino-1.8.8-linux64/arduino-1.8.8/hardware/arduino/avr/cores/arduino/Arduino.h \
|
||||
/home/mrh/Downloads/arduino-1.8.8-linux64/arduino-1.8.8/hardware/arduino/avr/cores/arduino/binary.h \
|
||||
/home/mrh/Downloads/arduino-1.8.8-linux64/arduino-1.8.8/hardware/arduino/avr/cores/arduino/WCharacter.h \
|
||||
/home/mrh/Downloads/arduino-1.8.8-linux64/arduino-1.8.8/hardware/arduino/avr/cores/arduino/WString.h \
|
||||
/home/mrh/Downloads/arduino-1.8.8-linux64/arduino-1.8.8/hardware/arduino/avr/cores/arduino/HardwareSerial.h \
|
||||
/home/mrh/Downloads/arduino-1.8.8-linux64/arduino-1.8.8/hardware/arduino/avr/cores/arduino/Stream.h \
|
||||
/home/mrh/Downloads/arduino-1.8.8-linux64/arduino-1.8.8/hardware/arduino/avr/cores/arduino/Print.h \
|
||||
/home/mrh/Downloads/arduino-1.8.8-linux64/arduino-1.8.8/hardware/arduino/avr/cores/arduino/Printable.h \
|
||||
/home/mrh/Downloads/arduino-1.8.8-linux64/arduino-1.8.8/hardware/arduino/avr/cores/arduino/USBAPI.h \
|
||||
/home/mrh/Downloads/arduino-1.8.8-linux64/arduino-1.8.8/hardware/arduino/avr/cores/arduino/Arduino.h \
|
||||
/home/mrh/Downloads/arduino-1.8.8-linux64/arduino-1.8.8/hardware/arduino/avr/variants/standard/pins_arduino.h
|
||||
63
arduino/libraries/SoftPWM/SoftPWM.h
Normal file
63
arduino/libraries/SoftPWM/SoftPWM.h
Normal file
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
|| @author Brett Hagman <bhagman@wiring.org.co>
|
||||
|| @url http://wiring.org.co/
|
||||
|| @url http://roguerobotics.com/
|
||||
||
|
||||
|| @description
|
||||
|| | A Software PWM Library
|
||||
|| |
|
||||
|| | Written by Brett Hagman
|
||||
|| | http://www.roguerobotics.com/
|
||||
|| | bhagman@roguerobotics.com, bhagman@wiring.org.co
|
||||
|| |
|
||||
|| | A Wiring (and Arduino) Library, for Atmel AVR8 bit series microcontrollers,
|
||||
|| | to produce PWM signals on any arbitrary pin.
|
||||
|| |
|
||||
|| | It was originally designed for controlling the brightness of LEDs, but
|
||||
|| | could be adapted to control servos and other low frequency PWM controlled
|
||||
|| | devices as well.
|
||||
|| |
|
||||
|| | It uses a single hardware timer (Timer 2) on the Atmel microcontroller to
|
||||
|| | generate up to 20 PWM channels (your mileage may vary).
|
||||
|| |
|
||||
|| #
|
||||
||
|
||||
|| @license Please see the accompanying LICENSE.txt file for this project.
|
||||
||
|
||||
|| @name Software PWM Library
|
||||
|| @type Library
|
||||
|| @target Atmel AVR 8 Bit
|
||||
||
|
||||
|| @version 1.0.1
|
||||
||
|
||||
*/
|
||||
|
||||
#ifndef SOFTPWM_H
|
||||
#define SOFTPWM_H
|
||||
|
||||
#define SOFTPWM_VERSION 10000
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#define SOFTPWM_MAXCHANNELS 20
|
||||
#define SOFTPWM_PWMDEFAULT 0x00
|
||||
|
||||
#define SOFTPWM_NORMAL 0
|
||||
#define SOFTPWM_INVERTED 1
|
||||
|
||||
#define ALL -1
|
||||
|
||||
void SoftPWMBegin(uint8_t defaultPolarity = SOFTPWM_NORMAL);
|
||||
|
||||
void SoftPWMSet(int8_t pin, uint8_t value, uint8_t hardset = 0);
|
||||
|
||||
void SoftPWMSetPercent(int8_t pin, uint8_t percent, uint8_t hardset = 0);
|
||||
|
||||
void SoftPWMEnd(int8_t pin);
|
||||
|
||||
void SoftPWMSetFadeTime(int8_t pin, uint16_t fadeUpTime, uint16_t fadeDownTime);
|
||||
|
||||
void SoftPWMSetPolarity(int8_t pin, uint8_t polarity);
|
||||
|
||||
#endif
|
||||
|
||||
1084
arduino/libraries/SoftPWM/SoftPWM.lst
Normal file
1084
arduino/libraries/SoftPWM/SoftPWM.lst
Normal file
File diff suppressed because it is too large
Load Diff
BIN
arduino/libraries/SoftPWM/SoftPWM.o
Normal file
BIN
arduino/libraries/SoftPWM/SoftPWM.o
Normal file
Binary file not shown.
83
arduino/libraries/SoftPWM/SoftPWM_timer.h
Normal file
83
arduino/libraries/SoftPWM/SoftPWM_timer.h
Normal file
@@ -0,0 +1,83 @@
|
||||
/*
|
||||
|| @author Paul Stoffregen (paul at pjrc dot com)
|
||||
|| @contribution Brett Hagman <bhagman@wiring.org.co>
|
||||
|| @url http://wiring.org.co/
|
||||
||
|
||||
|| @description
|
||||
|| | A Software PWM Library
|
||||
|| |
|
||||
|| | Simple timer abstractions.
|
||||
|| #
|
||||
||
|
||||
|| @license Please see the accompanying LICENSE.txt file for this project.
|
||||
||
|
||||
|| @name Software PWM Library support
|
||||
|| @type Library support
|
||||
|| @target Atmel AVR 8 Bit
|
||||
||
|
||||
|| @version 1.0.1
|
||||
||
|
||||
*/
|
||||
|
||||
#include <avr/io.h>
|
||||
#include <avr/interrupt.h>
|
||||
|
||||
// allow certain chips to use different timers
|
||||
#if defined(__AVR_ATmega32U4__)
|
||||
#define USE_TIMER4_HS // Teensy 2.0 lacks timer2, but has high speed timer4 :-)
|
||||
#elif defined(__arm__) && defined(TEENSYDUINO)
|
||||
#define USE_INTERVALTIMER // Teensy 3.x has special interval timers :-)
|
||||
#else
|
||||
#define USE_TIMER1
|
||||
#endif
|
||||
|
||||
// for each timer, these macros define how to actually use it
|
||||
#if defined(USE_TIMER1)
|
||||
#define SOFTPWM_TIMER_INTERRUPT TIMER1_COMPA_vect
|
||||
#define SOFTPWM_TIMER_SET(val) (TCNT1 = (val))
|
||||
#define SOFTPWM_TIMER_INIT(ocr) ({\
|
||||
cli(); \
|
||||
TCCR1A = 0; \
|
||||
TCCR1B = (1 << WGM12); \
|
||||
TCCR1B |= ((1 << CS10) | (1 << CS12)); \
|
||||
TIMSK1 = (1 << OCIE1A); \
|
||||
OCR1A = 15624; \
|
||||
sei(); \
|
||||
})
|
||||
|
||||
#elif defined(USE_TIMER2)
|
||||
#define SOFTPWM_TIMER_INTERRUPT TIMER2_COMPA_vect
|
||||
#define SOFTPWM_TIMER_SET(val) (TCNT2 = (val))
|
||||
#define SOFTPWM_TIMER_INIT(ocr) ({\
|
||||
TIFR2 = (1 << TOV2); /* clear interrupt flag */ \
|
||||
TCCR2B = (1 << CS21); /* start timer (ck/8 prescalar) */ \
|
||||
TCCR2A = (1 << WGM21); /* CTC mode */ \
|
||||
OCR2A = (15624); /* We want to have at least 30Hz or else it gets choppy */ \
|
||||
TIMSK2 = (1 << OCIE2A); /* enable timer2 output compare match interrupt */ \
|
||||
})
|
||||
#elif defined(USE_TIMER4_HS)
|
||||
#define SOFTPWM_TIMER_INTERRUPT TIMER4_COMPA_vect
|
||||
#define SOFTPWM_TIMER_SET(val) (TCNT4 = (val))
|
||||
#define SOFTPWM_TIMER_INIT(ocr) ({\
|
||||
TCCR4A = 0; \
|
||||
TCCR4B = 0x04; /* CTC Mode */\
|
||||
TCCR4C = 0; \
|
||||
TCCR4D = 0; \
|
||||
TCCR4E = 0; \
|
||||
OCR4C = 0; \
|
||||
OCR4C = (ocr); \
|
||||
TIMSK4 = (1 << OCIE4A); \
|
||||
})
|
||||
#elif defined(USE_INTERVALTIMER)
|
||||
#define SOFTPWM_TIMER_INTERRUPT softpwm_interval_timer
|
||||
#ifdef ISR
|
||||
#undef ISR
|
||||
#endif
|
||||
#define ISR(f) void f(void)
|
||||
#define SOFTPWM_TIMER_SET(val)
|
||||
#define SOFTPWM_TIMER_INIT(ocr) ({\
|
||||
IntervalTimer *t = new IntervalTimer(); \
|
||||
t->begin(softpwm_interval_timer, 1000000.0 / (float)(SOFTPWM_FREQ * 256)); \
|
||||
})
|
||||
#endif
|
||||
|
||||
13
arduino/libraries/SoftPWM/changelog.txt
Normal file
13
arduino/libraries/SoftPWM/changelog.txt
Normal file
@@ -0,0 +1,13 @@
|
||||
SoftPWM Library
|
||||
|
||||
Version Modified By Date Comments
|
||||
------- ------------ ---------- --------
|
||||
0001 B Hagman 2010-03-14 Initial coding (Pi Day!)
|
||||
0002 B Hagman 2010-03-21 License updates, minor fixes
|
||||
0003 B Hagman 2010-04-21 Added hardset control for syncing to matrix scan lines
|
||||
0004 B Hagman 2010-06-27 Fixed: pin 0 could not be used.
|
||||
0005 B Hagman 2011-05-27 Added polarity, and full Wiring support.
|
||||
B Hagman 2012-02-13 Fixed Arduino 1.0+ mess.
|
||||
1.0.0 B Hagman 2017-01-13 Initial release conforming to library standard.
|
||||
1.0.1 P Stoffregen 2017-08-30 Use IntervalTimer on Teensy 3.x and use LED_BUILTIN for WLED
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
#include <SoftPWM.h>
|
||||
|
||||
void setup()
|
||||
{
|
||||
SoftPWMBegin();
|
||||
|
||||
SoftPWMSet(13, 0);
|
||||
|
||||
SoftPWMSetFadeTime(13, 1000, 1000);
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
SoftPWMSet(13, 255);
|
||||
delay(1000);
|
||||
SoftPWMSet(13, 0);
|
||||
delay(1000);
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
#include <SoftPWM.h>
|
||||
|
||||
#define DELAY 100
|
||||
uint8_t leds[8] = {22, 23, 26, 27, 28, 29, 30, 31};
|
||||
|
||||
void setup()
|
||||
{
|
||||
SoftPWMBegin();
|
||||
|
||||
for (int i = 0; i < 8; i++)
|
||||
SoftPWMSet(leds[i], 0);
|
||||
|
||||
SoftPWMSetFadeTime(ALL, 50, 400);
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < 7; i++)
|
||||
{
|
||||
SoftPWMSet(leds[i+1], 255);
|
||||
SoftPWMSet(leds[i], 0);
|
||||
delay(DELAY);
|
||||
}
|
||||
|
||||
delay(400);
|
||||
|
||||
for (i = 7; i > 0; i--)
|
||||
{
|
||||
SoftPWMSet(leds[i-1], 255);
|
||||
SoftPWMSet(leds[i], 0);
|
||||
delay(DELAY);
|
||||
}
|
||||
|
||||
delay(400);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
#include <SoftPWM.h>
|
||||
|
||||
#define DELAY 40
|
||||
|
||||
uint8_t leds[8] = {22, 23, 26, 27, 28, 29, 30, 31};
|
||||
|
||||
void setup()
|
||||
{
|
||||
SoftPWMBegin();
|
||||
|
||||
for (int i = 0; i < 8; i++)
|
||||
SoftPWMSet(leds[i], 0);
|
||||
|
||||
SoftPWMSetFadeTime(ALL, 30, 200);
|
||||
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < 3; i++)
|
||||
{
|
||||
SoftPWMSet(leds[i+1], 255);
|
||||
SoftPWMSet(leds[6-i], 255);
|
||||
SoftPWMSet(leds[i], 0);
|
||||
SoftPWMSet(leds[7-i], 0);
|
||||
delay(DELAY);
|
||||
}
|
||||
|
||||
delay(250);
|
||||
|
||||
for (i = 3; i > 0; i--)
|
||||
{
|
||||
SoftPWMSet(leds[i-1], 255);
|
||||
SoftPWMSet(leds[8-i], 255);
|
||||
SoftPWMSet(leds[i], 0);
|
||||
SoftPWMSet(leds[7-i], 0);
|
||||
delay(DELAY);
|
||||
}
|
||||
|
||||
delay(250);
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
#include <SoftPWM.h>
|
||||
|
||||
#define DELAY 100
|
||||
uint8_t leds[8] = {22, 23, 26, 27, 28, 29, 30, 31};
|
||||
|
||||
void setup()
|
||||
{
|
||||
SoftPWMBegin();
|
||||
for (int i = 0; i < 8; i++)
|
||||
SoftPWMSet(leds[i], 0);
|
||||
|
||||
SoftPWMSetFadeTime(ALL, 50, 400);
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
uint8_t pin = random(8);
|
||||
|
||||
SoftPWMSet(leds[pin], 255);
|
||||
delay(50);
|
||||
SoftPWMSet(leds[pin], 0);
|
||||
|
||||
delay(random(DELAY));
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
#include <SoftPWM.h>
|
||||
|
||||
#ifndef WLED
|
||||
#define WLED LED_BUILTIN
|
||||
#endif
|
||||
|
||||
void setup()
|
||||
{
|
||||
SoftPWMBegin();
|
||||
|
||||
// Sets the PWM value to 0 for the built-in LED (WLED).
|
||||
SoftPWMSet(WLED, 0);
|
||||
|
||||
// Sets the default fade time for WLED to
|
||||
// 100 ms fade-up and 550 ms to fade-down.
|
||||
SoftPWMSetFadeTime(WLED, 100, 550);
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
// Turn on WLED
|
||||
SoftPWMSet(WLED, 255);
|
||||
// Wait for the fade-up and some extra delay.
|
||||
delay(250);
|
||||
// Turn off WLED
|
||||
SoftPWMSet(WLED, 0);
|
||||
// Wait for the fade-down, and some extra delay.
|
||||
delay(650);
|
||||
}
|
||||
|
||||
36
arduino/libraries/SoftPWM/keywords.txt
Normal file
36
arduino/libraries/SoftPWM/keywords.txt
Normal file
@@ -0,0 +1,36 @@
|
||||
########################################
|
||||
# Syntax Coloring Map For SoftPWM
|
||||
########################################
|
||||
#
|
||||
# Fields (Data Members) - KEYWORD2
|
||||
# Methods (Member Functions)
|
||||
# - FUNCTION2 (or KEYWORD2)
|
||||
# Constants - LITERAL2
|
||||
# Datatypes - KEYWORD5 (or KEYWORD1)
|
||||
########################################
|
||||
|
||||
########################################
|
||||
# Datatypes (KEYWORD5 or KEYWORD1)
|
||||
########################################
|
||||
|
||||
SoftPWM KEYWORD1
|
||||
|
||||
########################################
|
||||
# Methods and Functions
|
||||
# (FUNCTION2 or KEYWORD2)
|
||||
########################################
|
||||
|
||||
SoftPWMSet KEYWORD2
|
||||
SoftPWMSetPercent KEYWORD2
|
||||
SoftPWMSetFadeTime KEYWORD2
|
||||
SoftPWMBegin KEYWORD2
|
||||
SoftPWMEnd KEYWORD2
|
||||
SoftPWMSetPolarity KEYWORD2
|
||||
|
||||
########################################
|
||||
# Constants (LITERAL2)
|
||||
########################################
|
||||
|
||||
ALL LITERAL2
|
||||
SOFTPWM_NORMAL LITERAL2
|
||||
SOFTPWM_INVERTED LITERAL2
|
||||
9
arduino/libraries/SoftPWM/library.properties
Normal file
9
arduino/libraries/SoftPWM/library.properties
Normal file
@@ -0,0 +1,9 @@
|
||||
name=SoftPWM
|
||||
version=1.0.1
|
||||
author=Brett Hagman <bhagman@wiring.org.co>
|
||||
maintainer=Brett Hagman <bhagman@wiring.org.co>
|
||||
sentence=A software library to produce a 50 percent duty cycle PWM signal on arbitrary pins.<br />
|
||||
paragraph=A Wiring Framework (and Arduino) Library, for Atmel AVR8 bit series microcontrollers and Teensy 3.x, to produce PWM signals on any arbitrary pin.<br />It was originally designed for controlling the brightness of LEDs, but could be adapted to control servos and other low frequency PWM controlled devices as well.<br />It uses a single hardware timer (Timer 2) on an Atmel AVR 8 bit microcontroller (or IntervalTimer on Teensy 3.x) to generate up to 20 PWM channels (your mileage may vary).<br /><br />Issues or questions: <a href="https://github.com/bhagman/SoftPWM/issues">https://github.com/bhagman/SoftPWM/issues</a><br />
|
||||
category=Signal Input/Output
|
||||
url=https://github.com/bhagman/SoftPWM
|
||||
architectures=avr,arm
|
||||
Reference in New Issue
Block a user