Categories

Learn About PCA9685 I/O Expander Applications With RT-Thread (PWM, I2C)

Blog | Oct.19.2020

p>

Hardware features.

  1. Each PWM has a 12-bit resolution (4096 level);
  2. Up to 16 PWM output, all PWM frequencies are unity, each space ratio can be independently controlled;
  3. I2C methods;
  4. Support 2.3V to 5.5V voltage, logic level 3.3V;
  5. Reset mode: Power-on reset, software reset.

PWM For Rudder Control

First, control signal.

  1. PWM frequency for rudder control is 50Hz (20000us a cycle)
  2. For 12-bit resolution, time control accuracy (i.e., time resolution) is:

  3. The PWM of the rudder is around0.5ms ~ 2.5ms, the time stretching range is 2000us.
  4. The stretching range with 4.88us time resolution that can be divided into nearly 410 parts.

  5. Time stretching range 2000us corresponds to the steering range of 0 to 180 degrees, so the angle resolution of the rudder is about 0.439 degrees.

Second, check the chip usage in the PCA9685 manual.

  • What does the chip look like?

  • Pin functionality.

  • I2C address.

    Address pins consist of A0 to A5, up to 64 addresses. There are 62 addresses available because the chip itself retains the All Call 7-bit address 0xE0 and the Software Reset 7-bit address 0x06.

    By default (i.e., A0 to A5 all grounded), its 7-bit address is 0x40.

  • The target register.

    MODE1 register, address 0x00, it is readable and writeable.

    MODE2 register, address 0x01, it is readable and writeable.

    Each PWM has four 8-bit control registers, the addresses please check the datasheet, the register is readable and writeable.

    These four 8-bit registers are stated in the manual as two 12-bit registers. The two 12-bit registers separately control the high and low hours of the output, with a maximum value of 4095.

    Let’s take a further look at what these two 12-bit registers mean according to the following Example 1, If starting to set the bits to 1 from the Xth bit each cycle, X should be written into the LEDx_ON register. if starting to clear the bits to 0 from the Xth bit each cycle, X should be written into the LEDx_OFF register.

  • The four examples in the following image represent the relationship between the settings and the output of these two 12-bit registers in general.

    The four examples in the figure below represent the output in the case of ON register value < OFF register value.

    The four examples in the figure below represent the output in the case of ON register value > OFF register value.

  • PWM frequency sets register as PRE_SCALE, address 0xFE, it is readable and writeable.

    The chip has a built-in 25MHz oscillator, the frequency value of update_rate is in Hz. Therefore, for PWM at 50Hz, this register should be set to 121.

PAC9685 Driver Component of RT-Thread

The hardware connection is shown as above, where we use the most common PCA9658 rudder control module, connected to a logic analyzer for viewing the data.

First Part

The first communication contents are shown below:

Look at the marked yellow box above, “Write 00 to address 40” means to write data 0 to the address of the register with the device address of 0 and the address of 40.

Correspondingly, the marked blue box could be interpreted as read the register value for the device address of 40 and the address of 0, the data value in the device sends back 0 to the target register.

Check out the code below to find out about its communication, as shown follows:

Second Part

According to the datasheet of PCA9685, we get to know that SLEEP mode should be enabled first when setting the PWM output frequency. The process to restart from SLEEP mode is specified in datasheet.

In RT-Thread’s PCA9685 component package, the set frequency-related operation is encapsulated in the function pca9685_set_pwm_freq().

Third Part

By analyzing the code, we could found that the RT-Thread driver component package for the PCA9685, inherits the spirit of RT-Thread opensource operating system tiny and elegant features and encapsulates the functions according to the datasheet of the PCA9685, making it much easier for developers to quickly master the knowledge of the PCA9685.

Get your first-hand experience with RT-Thread PCA9685 component.

RT-Thread Contact Info:

Website: https://www.rt-thread.io

Github: https://github.com/RT-Thread

Twitter: https://twitter.com/rt_thread

Facebook: https://www.facebook.com/RT-Thread-IoT-OS-110395723808463

Youtube: https://www.youtube.com/channel/UCdDHtIfSYPq4002r27ffqPw