This is a library for the PCA9685 ported from the Adafruit Ardiuno library.
Diff: PCA9685Lib.cpp
- Revision:
- 0:1ecf26e0cf3c
- Child:
- 2:ec40a85eba51
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/PCA9685Lib.cpp Fri Feb 13 22:02:35 2015 +0000 @@ -0,0 +1,128 @@ +/* + 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 + ****************************************************/ + + /***************************** + This program was ported from https://github.com/adafruit/Adafruit-PWM-Servo-Driver-Library. + I also added some functions. + Shundo Kishi + */ + +#include "PCA9685Lib.h" + + +void LEDarr::operator=(uint16_t duty) +{ + PCALib->setDuty(LEDnum, duty); +} + +PCA9685Lib::PCA9685Lib(I2C i2cobj, int addr) : _i2caddr(addr) , i2c(i2cobj) +{ + for(int i = 0; i<16 ; i++) + { + LED[i].PCALib = this; + LED[i].LEDnum = i; + } +} + +void PCA9685Lib::i2c_probe(void) +{ + printf("Searching for I2C devices...\n"); + + int count = 0; + for (int address=4; address<256; address+=2) { + if (!i2c.write(address, NULL, 0)) { // 0 returned is ok + printf(" - I2C device found at address 0x%02X\r\n", address); + count++; + } + } + printf("%d devices found\r\n", count); +} + +void PCA9685Lib::begin(void) { + reset(); +} + +void PCA9685Lib::setI2Cfreq(int freq) { + i2c.frequency(freq); +} + +void PCA9685Lib::reset(void) { + write8(PCA9685_MODE1, 0x0); +} + +void PCA9685Lib::setPrescale(uint8_t prescale) { + uint8_t oldmode = read8(PCA9685_MODE1); + uint8_t newmode = (oldmode&0x7F) | 0x10; // sleep + write8(PCA9685_MODE1, newmode); // go to sleep + wait_ms(5); + write8(PCA9685_PRESCALE, prescale); // set the prescaler + write8(PCA9685_MODE1, oldmode); + wait_ms(5); + write8(PCA9685_MODE1, oldmode | 0xa1); +} + +void PCA9685Lib::setPWMFreq(float freq) { + //Serial.print("Attempting to set freq "); + //Serial.println(freq); + float prescaleval = 25000000; + prescaleval /= 4096; + prescaleval /= freq; + //Serial.print("Estimated pre-scale: "); Serial.println(prescaleval); + uint8_t prescale = floor(prescaleval + 0.5) - 1; + //Serial.print("Final pre-scale: "); Serial.println(prescale); + setPrescale(prescale); +} + +void PCA9685Lib::setPWM(uint8_t num, uint16_t on, uint16_t off) { + // hmm doesnt work, whyso? ( Not in AI mode. See line 54 above. ( Works now!! :D ) + + char cmd[5]; + cmd[0] = LED0_ON_L + 4 * num; + cmd[1] = on; + cmd[2] = on >> 8; + cmd[3] = off; + cmd[4] = off >> 8; + i2c.write(_i2caddr, cmd, 5); + + /*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);*/ +} + +// Set pwm duty in us order +void PCA9685Lib::setDuty(uint8_t num, uint16_t duty) { + float pulselength = 10000; // 10,000 us per second + duty = 4094 * duty / pulselength; + setPWM(num, 0, duty); +} + +uint8_t PCA9685Lib::read8(char addr) { + i2c.write(_i2caddr, &addr, 1); + char rtn; + i2c.read(_i2caddr, &rtn, 1); + return rtn; +} + +void PCA9685Lib::write8(char addr, char d) { + char cmd[2]; + cmd[0] = addr; + cmd[1] = d; + i2c.write(_i2caddr, cmd, 2); +} + +