This is a library for our Adafruit 16-channel PWM or any other board that uses the PCA9685 I2C PWM driver chip.

Dependents:   K9_Head_Controller

Committer:
bxd
Date:
Sat Aug 17 06:59:05 2013 +0000
Revision:
0:ed0c8a6481fd
Child:
1:ac6c5e17c3d3
Version 0, untested.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bxd 0:ed0c8a6481fd 1 /***************************************************
bxd 0:ed0c8a6481fd 2 This is a library for our Adafruit 16-channel PWM & Servo driver
bxd 0:ed0c8a6481fd 3
bxd 0:ed0c8a6481fd 4 Pick one up today in the adafruit shop!
bxd 0:ed0c8a6481fd 5 ------> http://www.adafruit.com/products/815
bxd 0:ed0c8a6481fd 6
bxd 0:ed0c8a6481fd 7 These displays use I2C to communicate, 2 pins are required to
bxd 0:ed0c8a6481fd 8 interface. For Arduino UNOs, thats SCL -> Analog 5, SDA -> Analog 4
bxd 0:ed0c8a6481fd 9
bxd 0:ed0c8a6481fd 10 Adafruit invests time and resources providing this open source code,
bxd 0:ed0c8a6481fd 11 please support Adafruit and open-source hardware by purchasing
bxd 0:ed0c8a6481fd 12 products from Adafruit!
bxd 0:ed0c8a6481fd 13
bxd 0:ed0c8a6481fd 14 Written by Limor Fried/Ladyada for Adafruit Industries.
bxd 0:ed0c8a6481fd 15 BSD license, all text above must be included in any redistribution
bxd 0:ed0c8a6481fd 16
bxd 0:ed0c8a6481fd 17 Ported to mbed by Brian Dickman, mbed.org user bxd.
bxd 0:ed0c8a6481fd 18 ****************************************************/
bxd 0:ed0c8a6481fd 19
bxd 0:ed0c8a6481fd 20 #include <Adafruit_PWMServoDriver.h>
bxd 0:ed0c8a6481fd 21
bxd 0:ed0c8a6481fd 22 Adafruit_PWMServoDriver::Adafruit_PWMServoDriver(I2C *i2c, uint8_t addr) {
bxd 0:ed0c8a6481fd 23 _i2c = i2c;
bxd 0:ed0c8a6481fd 24
bxd 0:ed0c8a6481fd 25 // Arduino WIRE library takes address as 7-bit (unshifted), mbed takes 8 bit.
bxd 0:ed0c8a6481fd 26 _i2caddr = addr << 1;
bxd 0:ed0c8a6481fd 27 }
bxd 0:ed0c8a6481fd 28
bxd 0:ed0c8a6481fd 29 void Adafruit_PWMServoDriver::begin(void) {
bxd 0:ed0c8a6481fd 30 reset();
bxd 0:ed0c8a6481fd 31 }
bxd 0:ed0c8a6481fd 32
bxd 0:ed0c8a6481fd 33
bxd 0:ed0c8a6481fd 34 void Adafruit_PWMServoDriver::reset(void) {
bxd 0:ed0c8a6481fd 35 write8(PCA9685_MODE1, 0x0);
bxd 0:ed0c8a6481fd 36 }
bxd 0:ed0c8a6481fd 37
bxd 0:ed0c8a6481fd 38 void Adafruit_PWMServoDriver::setPWMFreq(float freq) {
bxd 0:ed0c8a6481fd 39 //Serial.print("Attempting to set freq ");
bxd 0:ed0c8a6481fd 40 //Serial.println(freq);
bxd 0:ed0c8a6481fd 41
bxd 0:ed0c8a6481fd 42 float prescaleval = 25000000;
bxd 0:ed0c8a6481fd 43 prescaleval /= 4096;
bxd 0:ed0c8a6481fd 44 prescaleval /= freq;
bxd 0:ed0c8a6481fd 45 prescaleval -= 1;
bxd 0:ed0c8a6481fd 46 //printf("Estimated pre-scale: %f\r\n", prescaleval);
bxd 0:ed0c8a6481fd 47 uint8_t prescale = floor(prescaleval + 0.5);
bxd 0:ed0c8a6481fd 48 //printf("Final pre-scale: %f", prescale);
bxd 0:ed0c8a6481fd 49
bxd 0:ed0c8a6481fd 50 uint8_t oldmode = read8(PCA9685_MODE1);
bxd 0:ed0c8a6481fd 51 uint8_t newmode = (oldmode&0x7F) | 0x10; // sleep
bxd 0:ed0c8a6481fd 52 write8(PCA9685_MODE1, newmode); // go to sleep
bxd 0:ed0c8a6481fd 53 write8(PCA9685_PRESCALE, prescale); // set the prescaler
bxd 0:ed0c8a6481fd 54 write8(PCA9685_MODE1, oldmode);
bxd 0:ed0c8a6481fd 55 wait_ms(5);
bxd 0:ed0c8a6481fd 56 write8(PCA9685_MODE1, oldmode | 0xa1); // This sets the MODE1 register to turn on auto increment.
bxd 0:ed0c8a6481fd 57 // This is why the beginTransmission below was not working.
bxd 0:ed0c8a6481fd 58 // Serial.print("Mode now 0x"); Serial.println(read8(PCA9685_MODE1), HEX);
bxd 0:ed0c8a6481fd 59 }
bxd 0:ed0c8a6481fd 60
bxd 0:ed0c8a6481fd 61 void Adafruit_PWMServoDriver::setPWM(uint8_t num, uint16_t on, uint16_t off) {
bxd 0:ed0c8a6481fd 62 //Serial.print("Setting PWM "); Serial.print(num); Serial.print(": "); Serial.print(on); Serial.print("->"); Serial.println(off);
bxd 0:ed0c8a6481fd 63
bxd 0:ed0c8a6481fd 64 uint8_t data[] = { LED0_ON_L+4*num, on, on >> 8, off, off >> 8 };
bxd 0:ed0c8a6481fd 65 _i2c->write(_i2caddr, (const char *)data, 5);
bxd 0:ed0c8a6481fd 66 /*
bxd 0:ed0c8a6481fd 67 WIRE.beginTransmission(_i2caddr);
bxd 0:ed0c8a6481fd 68 WIRE.write(LED0_ON_L+4*num);
bxd 0:ed0c8a6481fd 69 WIRE.write(on);
bxd 0:ed0c8a6481fd 70 WIRE.write(on>>8);
bxd 0:ed0c8a6481fd 71 WIRE.write(off);
bxd 0:ed0c8a6481fd 72 WIRE.write(off>>8);
bxd 0:ed0c8a6481fd 73 WIRE.endTransmission();
bxd 0:ed0c8a6481fd 74 */
bxd 0:ed0c8a6481fd 75 }
bxd 0:ed0c8a6481fd 76
bxd 0:ed0c8a6481fd 77 uint8_t Adafruit_PWMServoDriver::read8(uint8_t addr) {
bxd 0:ed0c8a6481fd 78 char data;
bxd 0:ed0c8a6481fd 79 _i2c->write(_i2caddr, &data, 1, true);
bxd 0:ed0c8a6481fd 80 _i2c->read(_i2caddr, &data, 1);
bxd 0:ed0c8a6481fd 81 return (uint8_t)data;
bxd 0:ed0c8a6481fd 82 }
bxd 0:ed0c8a6481fd 83
bxd 0:ed0c8a6481fd 84 void Adafruit_PWMServoDriver::write8(uint8_t addr, uint8_t d) {
bxd 0:ed0c8a6481fd 85 char data[] = { addr, d };
bxd 0:ed0c8a6481fd 86 _i2c->write(_i2caddr, data, 2);
bxd 0:ed0c8a6481fd 87 }