upm  0.5.1
Sensor/Actuator repository for libmraa (v0.9.1)
 All Data Structures Files Functions Variables Enumerations Enumerator Macros Groups Pages
Public Types | Public Member Functions
PCA9685 Class Reference

API for the PCA9685 16-channel, 12-bit PWM LED Controller. More...

Detailed Description

This controller is also used on the Adafruit* Motor Shield v2.3 board to control up to 4 DC motors, 2 stepper motors, and 2 servo motors.

This module was tested with the Adafruit Motor Shield v2.3

pca9685.jpg
// Instantiate an PCA9685 on I2C bus 0
upm::PCA9685 *leds = new upm::PCA9685(PCA9685_I2C_BUS,
PCA9685_DEFAULT_I2C_ADDR);
// put device to sleep
leds->setModeSleep(true);
// setup a period of 50Hz
leds->setPrescaleFromHz(50);
// wake device up
leds->setModeSleep(false);
// Setup a 50% duty cycle -- on time at 0, off time at 2048 (4096 / 2)
// Set for all channels
leds->ledOnTime(PCA9685_ALL_LED, 0);
leds->ledOffTime(PCA9685_ALL_LED, 2048);
// but, turn channel 3 full off and channel 4 full on
cout << "Turning channel 3 off, and channel 4 on." << endl;
cout << "All other channels will be PWM'd at a 50% duty cycle." << endl;
leds->ledFullOff(3, true);
leds->ledFullOn(4, true);
// now, just sleep for 5 seconds, reset channels 3 and 4, and exit.
cout << "Sleeping for 5 seconds..." << endl;
sleep(5);
cout << "Exiting..." << endl;
// clear the bits we set earlier
leds->ledFullOff(3, false);
leds->ledFullOn(4, false);

Public Types

enum  PCA9685_REG_T {
  REG_MODE1 = 0x00, REG_MODE2 = 0x01, REG_I2C_SA1 = 0x02, REG_I2C_SA2 = 0x03,
  REG_I2C_SA3 = 0x04, REG_ALLCALL = 0x05, REG_LED0_ON_L = 0x06, REG_LED0_ON_H = 0x07,
  REG_LED0_OFF_L = 0x08, REG_LED0_OFF_H = 0x09, REG_LED1_ON_L = 0x0a, REG_LED1_ON_H = 0x0b,
  REG_LED1_OFF_L = 0x0c, REG_LED1_OFF_H = 0x0d, REG_LED2_ON_L = 0x0e, REG_LED2_ON_H = 0x0f,
  REG_LED2_OFF_L = 0x10, REG_LED2_OFF_H = 0x11, REG_LED3_ON_L = 0x12, REG_LED3_ON_H = 0x13,
  REG_LED3_OFF_L = 0x14, REG_LED3_OFF_H = 0x15, REG_LED4_ON_L = 0x16, REG_LED4_ON_H = 0x17,
  REG_LED4_OFF_L = 0x18, REG_LED4_OFF_H = 0x19, REG_LED5_ON_L = 0x1a, REG_LED5_ON_H = 0x1b,
  REG_LED5_OFF_L = 0x1c, REG_LED5_OFF_H = 0x1d, REG_LED6_ON_L = 0x1e, REG_LED6_ON_H = 0x1f,
  REG_LED6_OFF_L = 0x20, REG_LED6_OFF_H = 0x21, REG_LED7_ON_L = 0x22, REG_LED7_ON_H = 0x23,
  REG_LED7_OFF_L = 0x24, REG_LED7_OFF_H = 0x25, REG_LED8_ON_L = 0x26, REG_LED8_ON_H = 0x27,
  REG_LED8_OFF_L = 0x28, REG_LED8_OFF_H = 0x29, REG_LED9_ON_L = 0x2a, REG_LED9_ON_H = 0x2b,
  REG_LED9_OFF_L = 0x2c, REG_LED9_OFF_H = 0x2d, REG_LED10_ON_L = 0x2e, REG_LED10_ON_H = 0x2f,
  REG_LED10_OFF_L = 0x30, REG_LED10_OFF_H = 0x31, REG_LED11_ON_L = 0x32, REG_LED11_ON_H = 0x33,
  REG_LED11_OFF_L = 0x34, REG_LED11_OFF_H = 0x35, REG_LED12_ON_L = 0x36, REG_LED12_ON_H = 0x37,
  REG_LED12_OFF_L = 0x38, REG_LED12_OFF_H = 0x39, REG_LED13_ON_L = 0x3a, REG_LED13_ON_H = 0x3b,
  REG_LED13_OFF_L = 0x3c, REG_LED13_OFF_H = 0x3d, REG_LED14_ON_L = 0x3e, REG_LED14_ON_H = 0x3f,
  REG_LED14_OFF_L = 0x40, REG_LED14_OFF_H = 0x41, REG_LED15_ON_L = 0x42, REG_LED15_ON_H = 0x43,
  REG_LED15_OFF_L = 0x44, REG_LED15_OFF_H = 0x45, REG_ALL_LED_ON_L = 0xfa, REG_ALL_LED_ON_H = 0xfb,
  REG_ALL_LED_OFF_L = 0xfc, REG_ALL_LED_OFF_H = 0xfd, REG_PRESCALE = 0xfe, REG_TESTMODE = 0xff
}
 
enum  PCA9685_MODE1_T {
  MODE1_ALL_CALL = 0x01, MODE1_SUB3 = 0x02, MODE1_SUB2 = 0x04, MODE1_SUB1 = 0x08,
  MODE1_SLEEP = 0x10, MODE1_AI = 0x20, MODE1_EXTCLK = 0x40, MODE1_RESTART = 0x80
}
 
enum  PCA9685_MODE2_T {
  MODE2_OUTNE0 = 0x01, MODE2_OUTNE = 0x02, MODE2_OUTDRV = 0x04, MODE2_OCH = 0x08,
  MODE2_INVRT = 0x10, MODE2_RESERVE0 = 0x20, MODE2_RESERVE1 = 0x40, MODE2_RESERVE2 = 0x80
}
 

Public Member Functions

 PCA9685 (int bus, uint8_t address=PCA9685_DEFAULT_I2C_ADDR, bool raw=false)
 
 ~PCA9685 ()
 
bool writeByte (uint8_t reg, uint8_t byte)
 
bool writeWord (uint8_t reg, uint16_t word)
 
uint8_t readByte (uint8_t reg)
 
uint16_t readWord (uint8_t reg)
 
bool setModeSleep (bool sleep)
 
bool ledFullOn (uint8_t led, bool val)
 
bool ledFullOff (uint8_t led, bool val)
 
bool ledOnTime (uint8_t led, uint16_t time)
 
bool ledOffTime (uint8_t led, uint16_t time)
 
bool setPrescale (uint8_t prescale)
 
bool setPrescaleFromHz (float hz, float oscFreq=PCA9685_INTERNAL_OSC)
 
void enableRestart (bool enabled)
 

Member Enumeration Documentation

PCA9685 registers

MODE1 bits

MODE2 bits

Constructor & Destructor Documentation

PCA9685 ( int  bus,
uint8_t  address = PCA9685_DEFAULT_I2C_ADDR,
bool  raw = false 
)

PCA9685 constructor

Parameters
busI2C bus to use
addressAddress for this device
~PCA9685 ( )

PCA9685 destructor

Member Function Documentation

bool writeByte ( uint8_t  reg,
uint8_t  byte 
)

Writes a byte value into a register

Parameters
regRegister location to write into
byteByte to write
Returns
True if successful
bool writeWord ( uint8_t  reg,
uint16_t  word 
)

Writes a word value into a register. Note: the device must have the auto-increment bit set in the MODE1 register to work.

Parameters
regRegister location to write into
wordWord to write
Returns
True if successful
uint8_t readByte ( uint8_t  reg)

Reads a byte value from a register

Parameters
regRegister location to read from
Returns
Value in a specified register
uint16_t readWord ( uint8_t  reg)

Reads a word value from a register. Note: the device must have the auto-increment bit set in the MODE1 register to work.

Parameters
regRegister location to read from
Returns
Value in a specified register
bool setModeSleep ( bool  sleep)

Puts the device in or out of the sleep mode. The device is always in the sleep mode upon power-up.

Parameters
sleepTrue to put the device in the sleep mode, false to put out
Returns
True if successful

Here is the caller graph for this function:

bool ledFullOn ( uint8_t  led,
bool  val 
)

Sets or clears the FULL ON bit for a given LED

Parameters
ledLED number; valid values are 0-15, PCA9685_ALL_LED
valTrue to set the bit, false to clear it
Returns
True if successful

Here is the caller graph for this function:

bool ledFullOff ( uint8_t  led,
bool  val 
)

Sets or clears the FULL OFF bit for a given LED. If the FULL ON bit is also set, FULL OFF has precedence.

Parameters
ledLED number; valid values are 0-15, PCA9685_ALL_LED
valTrue to set the bit, false to clear it
Returns
True if successful

Here is the caller graph for this function:

bool ledOnTime ( uint8_t  led,
uint16_t  time 
)

Sets the 'LED on' time (0-4,095). See the PCA9685 datasheet for details.

Parameters
ledLED number; valid values are 0-15, PCA9685_ALL_LED
time12-bit value at which point the LED turns on
Returns
True if successful

Here is the caller graph for this function:

bool ledOffTime ( uint8_t  led,
uint16_t  time 
)

Sets the 'LED off' time (0-4,095). See the PCA9685 datasheet for details.

Parameters
ledLED number; valid values are 0-15, PCA9685_ALL_LED
time12-bit value at which point the LED turns off
Returns
True if successful

Here is the caller graph for this function:

bool setPrescale ( uint8_t  prescale)

Sets the prescale value. See the PCA9685 datasheet for details. The prescale can only be set when the device is in the sleep mode.

Parameters
prescalePrescale value
Returns
True if successful
bool setPrescaleFromHz ( float  hz,
float  oscFreq = PCA9685_INTERNAL_OSC 
)

Sets the prescale value based on a desired frequency in Hz. The prescale can only be set when the device is in the sleep mode.

Parameters
hzDesired frequency in Hz
oscFreqOscillator frequency; default is 25 MHz
Returns
True if successful

Here is the caller graph for this function:

void enableRestart ( bool  enabled)
inline

Enables or disables the restart capability of the controller

Parameters
enabledTrue to enable, false to disable It is enabled by default.

The documentation for this class was generated from the following files: