Lisleapex Blog
Published

PCA9685-16-channel PWM Module Servo Driver Board-STM32 IIC I

PCA9685 is a chip with 12-bit precision and 16-channel PWM wave output based on IIC bus communication. When the chip was first launched by N

BeginnerWork in progress2 hours27
PCA9685-16-channel PWM Module Servo Driver Board-STM32 IIC I

Things used in this project

Hardware components

PCA9685
×1

Software apps and online services

Arduino IDE
Arduino IDE

Hand tools and fabrication machines

Solder Wire, Lead Free
Solder Wire, Lead Free

Story

Read more

Schematics

pca9685_mVcZx8Yd6Q.png

Code

Arduino

Arduino
void setPWMFreq(float freq) { freq *= 0.9; float prescaleval = 25000000; prescaleval /= 4096; prescaleval /= freq; prescaleval -= 1; uint8_t oldmode = read8(PCA9685_MODE1); uint8_t newmode = (oldmode&0x7F) | 0x10; //准备进入sleep,设置时钟前必须先进入sleep模式Delay_ms(5);write8(PCA9685_MODE1, newmode);Delay_ms(5);write8(PCA9685_PRESCALE, prescaleval); p Delay_ms(5); oldmode &= 0xef; //清除sleep位write8(PCA9685_MODE1, oldmode);Delay_ms(5);write8(PCA9685_MODE1, oldmode | 0xa1); // This sets the MODE1 register to turn on auto increment.}

void setPWM(uint8_t num, uint16_t on, uint16_t off) { write8(LED0_ON_L+4*num,on); write8(LED0_ON_H+4*num,on>>8); write8(LED0_OFF_L+4*num,off); write8(LED0_OFF_H+4*num,off>>8); }

void set_pwm(u8 num, u8 off) { setPWM(num, 0, (int)(off*20.48)); }

uint8_t read8(uint8_t reg_addr) { u8 data=0; I2C_BufferRead(PCA9685_SLAVE_ADDRESS,&data,reg_addr,1); return data; } void write8(uint8_t reg_addr, uint8_t reg_dat) { I2C_ByteWrite(PCA9685_SLAVE_ADDRESS,reg_dat,reg_addr); }

Credits

Lisleapex Blog
26 projects • 0 followers
Fast Delivery of High-Quality Electronic Components
Contact

Comments

Please log in or sign up to comment.