Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

initial commit

  • Loading branch information...
commit 9e4b01d437e24c0d6acd57c3e5cfe2b75949c979 1 parent e5d5541
Limor "Ladyada" Fried ladyada authored
78 Adafruit_PWMServoDriver.cpp
View
@@ -0,0 +1,78 @@
+/***************************************************
+ This is a library for our Adafruit 16-channel PWM & Servo driver
+
+ Pick one up today in the adafruit shop!
+ ------> http://www.adafruit.com/products/815
+
+ These displays use I2C to communicate, 2 pins are required to
+ interface. For Arduino UNOs, thats SCL -> Analog 5, SDA -> Analog 4
+
+ Adafruit invests time and resources providing this open source code,
+ please support Adafruit and open-source hardware by purchasing
+ products from Adafruit!
+
+ Written by Limor Fried/Ladyada for Adafruit Industries.
+ BSD license, all text above must be included in any redistribution
+ ****************************************************/
+
+#include <Adafruit_PWMServoDriver.h>
+#include <Wire.h>
+
+Adafruit_PWMServoDriver::Adafruit_PWMServoDriver(uint8_t addr) {
+ _i2caddr = addr;
+}
+
+boolean Adafruit_PWMServoDriver::begin(void) {
+ Wire.begin();
+ reset();
+}
+
+
+void Adafruit_PWMServoDriver::reset(void) {
+ write8(PCA9685_MODE1, 0x0);
+}
+
+void Adafruit_PWMServoDriver::setPWMFreq(float freq) {
+ //Serial.print("Attempting to set freq ");
+ //Serial.println(freq);
+
+ float prescaleval = 25000000;
+ prescaleval /= 4096;
+ prescaleval /= freq;
+ prescaleval -= 1;
+ Serial.print("Estimated pre-scale: "); Serial.println(prescaleval);
+ uint8_t prescale = floor(prescaleval + 0.5);
+ Serial.print("Final pre-scale: "); Serial.println(prescale);
+
+ uint8_t oldmode = read8(PCA9685_MODE1);
+ uint8_t newmode = (oldmode&0x7F) | 0x10; // sleep
+ write8(PCA9685_MODE1, newmode); // go to sleep
+ write8(PCA9685_PRESCALE, prescale); // set the prescaler
+ write8(PCA9685_MODE1, oldmode);
+ delay(5);
+ write8(PCA9685_MODE1, oldmode | 0x80);
+ // Serial.print("Mode now 0x"); Serial.println(read8(PCA9685_MODE1), HEX);
+}
+
+void Adafruit_PWMServoDriver::setPWM(uint8_t num, uint16_t on, uint16_t off) {
+ write8(LED0_ON_L+4*num, on);
+ write8(LED0_ON_H+4*num, on >> 8);
+ write8(LED0_OFF_L+4*num, off);
+ write8(LED0_OFF_H+4*num, off >> 8);
+}
+
+uint8_t Adafruit_PWMServoDriver::read8(uint8_t addr) {
+ Wire.beginTransmission(_i2caddr);
+ Wire.write(addr);
+ Wire.endTransmission();
+
+ Wire.requestFrom((uint8_t)_i2caddr, (uint8_t)1);
+ return Wire.read();
+}
+
+void Adafruit_PWMServoDriver::write8(uint8_t addr, uint8_t d) {
+ Wire.beginTransmission(_i2caddr);
+ Wire.write(addr);
+ Wire.write(d);
+ Wire.endTransmission();
+}
61 Adafruit_PWMServoDriver.h
View
@@ -0,0 +1,61 @@
+/***************************************************
+ This is a library for our Adafruit 16-channel PWM & Servo driver
+
+ Pick one up today in the adafruit shop!
+ ------> http://www.adafruit.com/products/815
+
+ These displays use I2C to communicate, 2 pins are required to
+ interface. For Arduino UNOs, thats SCL -> Analog 5, SDA -> Analog 4
+
+ Adafruit invests time and resources providing this open source code,
+ please support Adafruit and open-source hardware by purchasing
+ products from Adafruit!
+
+ Written by Limor Fried/Ladyada for Adafruit Industries.
+ BSD license, all text above must be included in any redistribution
+ ****************************************************/
+
+#ifndef _ADAFRUIT_PWMServoDriver_H
+#define _ADAFRUIT_PWMServoDriver_H
+
+#if ARDUINO >= 100
+ #include "Arduino.h"
+#else
+ #include "WProgram.h"
+#endif
+
+
+#define PCA9685_SUBADR1 0x2
+#define PCA9685_SUBADR2 0x3
+#define PCA9685_SUBADR3 0x4
+
+#define PCA9685_MODE1 0x0
+#define PCA9685_PRESCALE 0xFE
+
+#define LED0_ON_L 0x6
+#define LED0_ON_H 0x7
+#define LED0_OFF_L 0x8
+#define LED0_OFF_H 0x9
+
+#define ALLLED_ON_L 0xFA
+#define ALLLED_ON_H 0xFB
+#define ALLLED_OFF_L 0xFC
+#define ALLLED_OFF_H 0xFD
+
+
+class Adafruit_PWMServoDriver {
+ public:
+ Adafruit_PWMServoDriver(uint8_t addr = 0x40);
+ boolean begin(void);
+ void reset(void);
+ void setPWMFreq(float freq);
+ void setPWM(uint8_t num, uint16_t on, uint16_t off);
+
+ private:
+ uint8_t _i2caddr;
+
+ uint8_t read8(uint8_t addr);
+ void write8(uint8_t addr, uint8_t d);
+};
+
+#endif
21 README.md
View
@@ -1,4 +1,19 @@
-Adafruit-PWM-Servo-Driver-Library
-=================================
+This is a library for our Adafruit 16-channel PWM & Servo driver
-Adafruit PWM Servo Driver Library
+ Pick one up today in the adafruit shop!
+ ------> http://www.adafruit.com/products/815
+
+These displays use I2C to communicate, 2 pins are required to
+interface. For Arduinos, thats SCL -> Analog 5, SDA -> Analog 4
+
+Adafruit invests time and resources providing this open source code,
+please support Adafruit and open-source hardware by purchasing
+products from Adafruit!
+
+Written by Limor Fried/Ladyada for Adafruit Industries.
+BSD license, check license.txt for more information
+All text above must be included in any redistribution
+
+To download. click the DOWNLOADS button in the top right corner, rename the uncompressed folder Adafruit_PWMServoDriver. Check that the Adafruit_PWMServoDriver folder contains Adafruit_PWMServoDriver.cpp and Adafruit_PWMServoDriver.h
+
+Place the Adafruit_PWMServoDriver library folder your <arduinosketchfolder>/libraries/ folder. You may need to create the libraries subfolder if its your first library. Restart the IDE.
26 license.txt
View
@@ -0,0 +1,26 @@
+Software License Agreement (BSD License)
+
+Copyright (c) 2012, Adafruit Industries
+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 holders 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 ''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 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.
Please sign in to comment.
Something went wrong with that request. Please try again.