Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Adafruit_PWMServoDriver.cpp
00001 /*************************************************** 00002 This is a library for our Adafruit 16-channel PWM & Servo driver 00003 00004 Pick one up today in the adafruit shop! 00005 ------> http://www.adafruit.com/products/815 00006 00007 These displays use I2C to communicate, 2 pins are required to 00008 interface. For Arduino UNOs, thats SCL -> Analog 5, SDA -> Analog 4 00009 00010 Adafruit invests time and resources providing this open source code, 00011 please support Adafruit and open-source hardware by purchasing 00012 products from Adafruit! 00013 00014 Written by Limor Fried/Ladyada for Adafruit Industries. 00015 BSD license, all text above must be included in any redistribution 00016 00017 Ported to mbed by Brian Dickman, mbed.org user bxd. 00018 ****************************************************/ 00019 00020 #include <Adafruit_PWMServoDriver.h> 00021 00022 Adafruit_PWMServoDriver::Adafruit_PWMServoDriver(I2C *i2c, uint8_t addr) { 00023 _i2c = i2c; 00024 00025 // Arduino WIRE library takes address as 7-bit (unshifted), mbed takes 8 bit. 00026 // in other words, use the Adafruit library default value 0x40 to talk with the breakout 00027 // if you use the default resistor config. 00028 _i2caddr = addr << 1; 00029 } 00030 00031 void Adafruit_PWMServoDriver::begin(void) { 00032 reset(); 00033 } 00034 00035 00036 void Adafruit_PWMServoDriver::reset(void) { 00037 00038 // leave the ALL CALL address enabled (we might use that!) 00039 // if you don't want ALL CALL, write 00 to offset 00 00040 write8(PCA9685_MODE1, 0x1); 00041 } 00042 00043 void Adafruit_PWMServoDriver::setPWMFreq(float freq) { 00044 //Serial.print("Attempting to set freq "); 00045 //Serial.println(freq); 00046 00047 float prescaleval = 25000000; 00048 prescaleval /= 4096; 00049 prescaleval /= freq; 00050 prescaleval -= 1; 00051 //printf("Estimated pre-scale: %f\r\n", prescaleval); 00052 uint8_t prescale = floor(prescaleval + 0.5); 00053 //printf("Final pre-scale: %f", prescale); 00054 00055 uint8_t oldmode = read8(PCA9685_MODE1); 00056 uint8_t newmode = (oldmode&0x7F) | 0x10; // sleep 00057 write8(PCA9685_MODE1, newmode); // go to sleep 00058 write8(PCA9685_PRESCALE, prescale); // set the prescaler 00059 write8(PCA9685_MODE1, oldmode); 00060 //wait_ms(5); 00061 write8(PCA9685_MODE1, oldmode | 0xa1); // This sets the MODE1 register to turn on auto increment. 00062 // This is why the beginTransmission below was not working. 00063 // Serial.print("Mode now 0x"); Serial.println(read8(PCA9685_MODE1), HEX); 00064 } 00065 00066 void Adafruit_PWMServoDriver::setPWM(uint8_t num, uint16_t on, uint16_t off) { 00067 //Serial.print("Setting PWM "); Serial.print(num); Serial.print(": "); Serial.print(on); Serial.print("->"); Serial.println(off); 00068 00069 int data[] = { LED0_ON_L+4*num, on, on >> 8, off, off >> 8 }; 00070 _i2c->write(_i2caddr, (const char *)data, 5); 00071 /* 00072 WIRE.beginTransmission(_i2caddr); 00073 WIRE.write(LED0_ON_L+4*num); 00074 WIRE.write(on); 00075 WIRE.write(on>>8); 00076 WIRE.write(off); 00077 WIRE.write(off>>8); 00078 WIRE.endTransmission(); 00079 */ 00080 } 00081 00082 uint8_t Adafruit_PWMServoDriver::read8(uint8_t addr) { 00083 char data; 00084 if(_i2c->write(_i2caddr, (char *)&addr, 1, true)) 00085 printf("I2C ERR: no ack on write before read.\n"); 00086 if(_i2c->read(_i2caddr, &data, 1)) 00087 printf("I2C ERR: no ack on read\n"); 00088 return (uint8_t)data; 00089 } 00090 00091 void Adafruit_PWMServoDriver::write8(uint8_t addr, uint8_t d) { 00092 char data[] = { addr, d }; 00093 if(_i2c->write(_i2caddr, data, 2)) 00094 { 00095 printf("I2C ERR: No ACK on i2c write!"); 00096 } 00097 }
Generated on Sat Jul 30 2022 05:47:41 by
1.7.2