Skip to content

Commit

Permalink
Add ability to set default PWM Freq using #define #define USE_PCA9685…
Browse files Browse the repository at this point in the history
…_FREQ
  • Loading branch information
andrethomas committed Sep 30, 2018
1 parent f62b911 commit ee0ef22
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
1 change: 1 addition & 0 deletions sonoff/user_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@
// #define USE_MCP230xx_DISPLAYOUTPUT // Enable MCP23008/MCP23017 to display state of OUTPUT pins on Web UI (+0k2 code)
// #define USE_PCA9685 // Enable PCA9685 I2C HW PWM Driver - Must define I2C Address in #define USE_PCA9685_ADDR below - range 0x40 - 0x47 (+1k4 code)
// #define USE_PCA9685_ADDR 0x40 // Enable PCA9685 I2C Address to use (Must be within range 0x40 through 0x47 - set according to your wired setup)
// #define USE_PCA9685_FREQ 50 // Define default PWM frequency in Hz to be used (must be within 24 to 1526) - If other value is used, it will rever to 50Hz
// #define USE_MPR121 // Enable MPR121 controller (I2C addresses 0x5A, 0x5B, 0x5C and 0x5D) in input mode for touch buttons (+1k3 code)
// #define USE_CCS811 // Enable CCS811 sensor (I2C address 0x5A) (+2k2 code)
// #define USE_MPU6050 // Enable MPU6050 sensor (I2C address 0x68 AD0 low or 0x69 AD0 high) (+2k6 code)
Expand Down
14 changes: 9 additions & 5 deletions sonoff/xdrv_15_pca9685.ino
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
#define PCA9685_REG_PRE_SCALE 0xFE

uint8_t pca9685_detected = 0;
uint16_t pca9685_freq = 50;
uint16_t pca9685_freq = USE_PCA9685_FREQ;

void PCA9685_Detect(void)
{
Expand All @@ -51,7 +51,7 @@ void PCA9685_Detect(void)
void PCA9685_Reset(void)
{
I2cWrite8(USE_PCA9685_ADDR, PCA9685_REG_MODE1, 0x80);
PCA9685_SetPWMfreq(50);
PCA9685_SetPWMfreq(USE_PCA9685_FREQ);
for (uint8_t pin=0;pin<16;pin++) {
PCA9685_SetPWM(pin,0,false);
}
Expand All @@ -63,9 +63,13 @@ void PCA9685_SetPWMfreq(double freq) {
7.3.5 from datasheet
prescale value = round(25000000/(4096*freq))-1;
*/
pca9685_freq=freq;
uint8_t pre_scale_osc = round(25000000/(4096*freq))-1;
if (1526 == freq) pre_scale_osc=0xFF; // force setting for 24hz because rounding causes 1526 to be 254
if (freq > 23 && freq < 1527) {
pca9685_freq=freq;
} else {
pca9685_freq=50;
}
uint8_t pre_scale_osc = round(25000000/(4096*pca9685_freq))-1;
if (1526 == pca9685_freq) pre_scale_osc=0xFF; // force setting for 24hz because rounding causes 1526 to be 254
uint8_t current_mode1 = I2cRead8(USE_PCA9685_ADDR, PCA9685_REG_MODE1); // read current value of MODE1 register
uint8_t sleep_mode1 = (current_mode1&0x7F) | 0x10; // Determine register value to put PCA to sleep
I2cWrite8(USE_PCA9685_ADDR, PCA9685_REG_MODE1, sleep_mode1); // Let's sleep a little
Expand Down

0 comments on commit ee0ef22

Please sign in to comment.