7 #ifndef __PCA9685PWM_H__
8 #define __PCA9685PWM_H__
15 #define PCA9685_MODE1 0x00
16 #define PCA9685_MODE2 0x01
17 #define PCA9685_LED0_ON_L_REG 0x06
18 #define PCA9685_LED0_ON_H_REG 0x07
19 #define PCA9685_LED0_OFF_L_REG 0x08
20 #define PCA9685_LED0_OFF_H_REG 0x09
23 #define PCA9685_RESTART 0x80
24 #define PCA9685_EXTCLK 0x40
25 #define PCA9685_AUTOINCR 0x20
26 #define PCA9685_SLEEP 0x10
27 #define PCA9685_SUB1 0x08
28 #define PCA9685_SUB2 0x04
29 #define PCA9685_SUB3 0x02
30 #define PCA9685_ALLCALL 0x01
33 #define PCA9685_INVERT 0x10
34 #define PCA9685_OCH 0x08
35 #define PCA9685_OUTDRV 0x04
36 #define PCA9685_OUTNE 0x03
38 #define PCA9685_MODE1_RESTART 0x80
39 #define GENERAL_CALL 0x00
40 #define PCA9685_SWRST 0b110
45 static const uint16_t pwm_12bit_table[101] PROGMEM = {
46 0, 2, 18, 28, 37, 47, 58, 68, 79, 90, 101, 113, 125, 137, 149, 162, 175, 189,203, 217, 231, 246, 262,
47 277, 293, 310, 327, 344, 362, 381, 399, 419, 438, 459, 479, 501, 523, 545, 568, 592, 616, 641, 666,
48 693, 719, 747, 775, 804, 834, 864, 896, 928, 961, 994, 1029, 1064, 1101, 1138, 1177, 1216, 1256,
49 1298, 1340, 1384, 1428, 1474, 1521, 1569, 1619, 1669, 1721, 1775, 1830, 1886, 1943, 2002, 2063, 2125,
50 2189, 2254, 2322, 2390, 2461, 2534, 2608, 2684, 2762, 2843, 2925, 3009, 3096, 3185, 3276, 3369, 3465,
51 3563, 3664, 3768, 3874, 3983, 4095
55 static uint8_t _init_done = 0;
57 template<u
int8_t STEPS = 200,
bool LINEAR = false,
bool INVERSE = false,
byte I2C_ADDRESS = 0x40>
61 uint8_t read_register(uint8_t address) {
62 Wire.beginTransmission(I2C_ADDRESS);
64 Wire.endTransmission(
false);
65 Wire.requestFrom(I2C_ADDRESS, (uint8_t)1);
66 if (Wire.available() < 1)
70 void write_register(uint8_t address, uint8_t value) {
71 Wire.beginTransmission(I2C_ADDRESS);
72 Wire.write((uint8_t)address);
73 Wire.write((uint8_t)(value));
74 Wire.endTransmission();
77 Wire.beginTransmission(GENERAL_CALL);
78 Wire.write(PCA9685_SWRST);
79 Wire.endTransmission();
82 void set_channel_value(uint8_t channel, uint16_t value) {
84 write_register(PCA9685_LED0_ON_L_REG + channel * 4, 0x00);
85 write_register(PCA9685_LED0_ON_H_REG + channel * 4, 0x00);
86 write_register(PCA9685_LED0_OFF_L_REG + channel * 4, (value & 0xFF));
87 write_register(PCA9685_LED0_OFF_H_REG + channel * 4, (value >> 8));
94 void init(uint8_t channel) {
96 if (_init_done == 0) {
102 write_register(PCA9685_MODE1, PCA9685_ALLCALL | PCA9685_AUTOINCR);
103 write_register(PCA9685_MODE2, PCA9685_OUTDRV);
106 uint8_t r = read_register(PCA9685_MODE1);
107 DPRINT(F(
"PCA9685 "));
110 DPRINT(F(
"found at 0x")); DHEXLN(I2C_ADDRESS);
114 DPRINTLN(F(
" not found"));
119 void set(uint8_t value) {
123 pwm = map(value, 0, STEPS, 0, 4096);
128 pwm = pgm_read_word(&pwm_12bit_table[value / 2]);
131 uint16_t x_high = pgm_read_word(&pwm_12bit_table[(value / 2) + 1]);
132 pwm = (x_high + pwm) / 2;
135 if (INVERSE) pwm = ~pwm ^ 0xF000;
137 set_channel_value(_channel, pwm);