例子1:假设LED0输出,并且格式是((delay
time)+(PWM duty cycle) <= 100%)
延迟时间(delay time) 是10%个周期,PWM工作周期是20%个周期(高电平20%, 低电平80%)
一个周期分为4096(0xFFFh)个单位,延迟时间(delay time): = 10% = 4096 * 10% = 409.6 = 401 = 0x19Ah
所以在寄存器LED0_ON_H=1H,LED0_ON_l =0x99H(因为计数是从0开始的,0x19Ah - 1 = 0x199h)
LED高电平的时间是20% = 4096*20%=819.2 = 819
LED低电平的时间的位置 是0x4CCh(410+819 - 1 = 1228)
所以LED0_OFF_H = 4h LED0_OFF_l = CCh
四、PCA9685的i2c-tools使用
4.1 参考例程
void Adafruit_PWMServoDriver::begin(void) {
WIRE.begin();
reset();
}
void Adafruit_PWMServoDriver::reset(void) {
write8(PCA9685_MODE1, 0x0);
}
void Adafruit_PWMServoDriver::setPWMFreq(float freq) {
//Serial.print("Attempting to set freq ");
//Serial.println(freq);
freq *= 0.9; // Correct for overshoot in the frequency setting (see issue #11).
float prescaleval = 25000000;
prescaleval /= 4096;
prescaleval /= freq;
prescaleval -= 1;
if (ENABLE_DEBUG_OUTPUT) {
Serial.print("Estimated pre-scale: "); Serial.println(prescaleval);
}
uint8_t prescale = floor(prescaleval + 0.5);
if (ENABLE_DEBUG_OUTPUT) {
Serial.print("Final pre-scale: "); Serial.println(prescale);
}
uint8_t oldmode = read8(PCA9685_MODE1);
uint8_t newmode = (oldmode&0x7F) | 0x10; // sleep
write8(PCA9685_MODE1, newmode); // go to sleep
write8(PCA9685_PRESCALE, prescale); // set the prescaler
write8(PCA9685_MODE1, oldmode);
delay(5);
write8(PCA9685_MODE1, oldmode | 0xa1); // This sets the MODE1 register to turn on auto increment.
// This is why the beginTransmission below was not working.
// Serial.print("Mode now 0x"); Serial.println(read8(PCA9685_MODE1), HEX);
}
void Adafruit_PWMServoDriver::setPWM(uint8_t num, uint16_t on, uint16_t off) {
//Serial.print("Setting PWM "); Serial.print(num); Serial.print(": "); Serial.print(on); Serial.print("->"); Serial.println(off);
WIRE.beginTransmission(_i2caddr);
WIRE.write(LED0_ON_L+4*num);
WIRE.write(on);
WIRE.write(on>>8);
WIRE.write(off);
WIRE.write(off>>8);
WIRE.endTransmission();
}
4.2 使用i2c-tools调试PCA9685
# reset
i2cset -f -y I2CBUS CHIP-ADDRESS 0x00 0x0 b
# setPWMFreq
i2cget -f -y I2CBUS CHIP-ADDRESS 0x00 b
#get oldmode
i2cset -f -y I2CBUS CHIP-ADDRESS 0x00 ((oldmode&0x7f)|
0x10) b
# freq
# freq *= 0.9 // prescaleval = 25000000/4096/freq; prescaleval -=1;
# uint8_t prescale = floor(prescaleval + 0.5);
i2cset -f -y I2CBUS CHIP-ADDRESS 0xFE prescale b
i2cset -f -y I2CBUS CHIP-ADDRESS 0x00 oldmode b
i2cset -f -y I2CBUS CHIP-ADDRESS 0x00 (oldmode|0xa1) b
# setPWM(num on, off)
i2cset -f I2CBUS CHIP-ADDRESS LED0_ON_L+4*num on w
i2cset -f I2CBUS CHIP-ADDRESS LED0_ON_L+4*num off w
4.3 调试结果
[root@firefly-rk3288:~]# i2cdetect -y -a 4
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: 00 -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: 40 -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: 70 -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
[root@firefly-rk3288:~]# i2cget -f -y 4 0x40 0x00 b
0x11
[root@firefly-rk3288:~]# i2cset -f -y 4 0x40 0x00 0x0 b
[root@firefly-rk3288:~]# i2cget -f -y 4 0x40 0x00 b
0x00
#所以oldmode是0x00,((oldmode&0x7f)|0x10) --> 0x10
[root@firefly-rk3288:~]# i2cset -f -y 4 0x40 0x00 0x10 b
[root@firefly-rk3288:~]# i2cget -f -y 4 0x40 0x00 b
0x10
# 简单一点prescale直接pdf上的值 0x30
[root@firefly-rk3288:~]# i2cset -f -y 4 0x40 0xfe 0x30 b
[root@firefly-rk3288:~]# i2cset -f -y 4 0x40 0x00 0x00 b
[root@firefly-rk3288:~]# i2cset -f -y 4 0x40 0x00 0xa1 b
# setPWM(num on, off), num=0, on=0xff off=0x00
# 全高的话on=0x0000 off=0x0fff
[root@firefly-rk3288:~]# i2cset -f -y 4 0x40 0x06 0x00 b
[root@firefly-rk3288:~]# i2cset -f -y 4 0x40 0x07 0x00 b
[root@firefly-rk3288:~]# i2cset -f -y 4 0x40 0x08 0xff b
[root@firefly-rk3288:~]# i2cset -f -y 4 0x40 0x09 0x0f b
# 结果是输出了1v
[root@firefly-rk3288:~]# i2cset -f -y 4 0x40 0x06 0x00 b
[root@firefly-rk3288:~]# i2cset -f -y 4 0x40 0x07 0x00 b
[root@firefly-rk3288:~]# i2cset -f -y 4 0x40 0x08 0x00 b
[root@firefly-rk3288:~]# i2cset -f -y 4 0x40 0x09 0x00 b
# 结果是输出了0v
不知道什么原因,最大只有1v。
原作者:习惯就好zz