/************************************************************************** Copyright (C) 2013 SARVESH KULKARNI Associate Professor, ECE Department Villanova University, PA, USA. LEGAL NOTICE: This library is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . ------------------------------------------------------------------------- Header file for the library of routines for Adafruit's PCA9685 16-channel PWM breakout board. The routines should work for *any* PCA9685 based board as long as the correct I2C device address is passed to the constructor by the main program. Version 0.9, Released August 2013. The Adafruit PWM board for which this library is written, is an I2C slave. You need to connect at least 4 pins from your microcontroller to drive the board. On the Raspberry Pi, Rev.2, these pins are as follows: SDA = pin 3, SCL = pin 5 VCC (3.3V) = pin 1, GND = pin 6 In addition, should you want to control any device with a power draw greater than that of an LED (e.g. servo motor) using this PWM board, you do need to provide a 5V power supply with sufficient amperage at the +V and GND pins of the PWM board. *************************************************************************/ #ifndef _PCA9685 #define _PCA9685 #include // for defns of uint8_t and uint16_t // ------ I2C Bus Addresses -------------------- #define GENERAL_CALL_ADDR 0x0 // -- PCA9685 Control Register Addresses -------- #define _MODE1_REG 0x0 #define _MODE2_REG 0x1 #define _SUBADR1 0x2 #define _SUBADR2 0x3 #define _SUBADR3 0x4 #define _LED0_ON_L 0x6 #define _LED0_ON_H 0x7 #define _LED0_OFF_L 0x8 #define _LED0_OFF_H 0x9 #define _ALL_LED_ON_L 0xFA #define _ALL_LED_ON_H 0xFB #define _ALL_LED_OFF_L 0xFC #define _ALL_LED_OFF_H 0xFD #define _PRE_SCALE_REG 0xFE // ------ Other Board/Chip Specific Constants and Control Codes ----------- #define SW_RESET 0x6 #define MODE1_REG_DEFAULT 0x1 // Restart=0, ExtClk=0, AutoIncr=0, Sleep=0, // Sub1-3=0, AllCall=1 #define MODE2_REG_DEFAULT 0xC // Output uninverted, LED o/ps: Totem Pole, // Outputs change on STOP #define OSC_CLK 25000000 // 25 MHz clock on board // ------ Class Specific Error Codes ---------------------------------- #define FD_UNINITIALIZED -1 // Illegal file descriptor; device file inaccessible #define DUTY_CYCLE_ILLEGAL -2 // Duty Cycle > 100 #define PULSE_WIDTH_ILLEGAL -3 // Pulse Width > Max possible Pulse Width in cycle #define DELAY_ILLEGAL -4 // Delay > 100 #define CHANNEL_NUM_ILLEGAL -5 // Channel Num is not 255 (all), and not in 0-15 range #define I2C_TRANSACTION_FAILED -6 // I2C Bus read or write failed // ------ Other Class Specific Constants ------------------------------- #define MAXSIZE 10 // Buffer size ..for reads/writes to PCA9685 registers // CAUTION: If you read from or write to more than 10 registers // in the autoincrement mode, you need a suitably larger // value here to prevent buffer overflow class PWM_Board { public: PWM_Board(uint8_t i2c_addr = 0x40); // Constructor ~PWM_Board(void); // Destructor bool isInitialized(); int readRegister(unsigned int reg_addr); int getFreq(void); // freq in Hz int setFreq(float freq); // freq in Hz // Duty cycle and delay are in percentages, channel_num 0xff means all channels int setDutyCycle(float duty_cycle, unsigned int channel_num = 0xff, float delay = 0); // Pulse width is in milliseconds, channel_num 0xff means all channels int setPulseWidth(float pulse_width, unsigned int channel_num = 0xff, float delay = 0); int fullOn(unsigned int channel_num = 0xff, float delay = 0); int fullOff(unsigned int channel_num = 0xff, float delay = 0); int sleep(void); int wakeUp(void); int softReset(void); int hardReset(void); protected: int initialize(void); private: uint8_t my_i2caddr; int my_fd; }; #endif