PCA9685 read

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

PCA9685 read

1,684 Views
markkappel
Contributor I

Hi @all!

 

i am using a PCA9685 for PWM several LEDs. There is no problem to set the on/off registers but i also want to read the values. Unfortunately the returned values are not that i expected....

Is there a special way to read the registers?

 

thanks in advance,

mark

Labels (1)
0 Kudos
3 Replies

850 Views
xiangjun_rong
NXP TechSupport
NXP TechSupport

Hi, Mark,

I am not familiar with the PCA9685 which is an IIC slave device, do you use IIC module of  DSC to communicate with PCA9685? anyway, if you can write data to the device, the reading is some difference from writing.

Pls refer to the Fig 22 in the data sheet of PCA9685. This is the sequence:

slave address+W,Control register, MODE1 reg, Repeated start, slave address+R, reading...

DSC support Repeated start in IIC module.

BR

Xiangjun Rong

0 Kudos

850 Views
markkappel
Contributor I

Hi Xiangjun,

i read the datasheet, but i am wondering if it is possible to read only spevific registers.

Perhaps it is a bit clearer what i am looking for witch the following code:

    public void Set(int pwm_channel, int a_value)
    {
        Debug.Print(pwm_channel + "-" + a_value);
  
        int on = 0;
        int off = a_value;
        if (a_value == 0) { on = 0; off = 4096; }
        write((byte)(__LED0_ON_L + 4 * pwm_channel), (byte)(on & 0xFF));
        write((byte)(__LED0_ON_H + 4 * pwm_channel), (byte)(on >> 8));
        write((byte)(__LED0_OFF_L + 4 * pwm_channel), (byte)(off & 0xFF));
        write((byte)(__LED0_OFF_H + 4 * pwm_channel), (byte)(off >> 8));
    }

    public int Get(int pwm_channel)
    {
        byte l1 = read((byte)(__LED0_ON_L + 4 * pwm_channel));
        byte h1 = read((byte)(__LED0_ON_H + 4 * pwm_channel));
        byte l0 = read((byte)(__LED0_OFF_L + 4 * pwm_channel));
        byte h0 = read((byte)(__LED0_OFF_H + 4 * pwm_channel));

        int on = ((l1 & 0xFF) | ((h1 & 0xFF) << 8));
        int off = ((l0 & 0xFF) | ((h0 & 0xFF) << 8));

       return off;
    }

with

    private void write(byte address, byte b)
    {
        I2CDevice.I2CTransaction[] xActions = new I2CDevice.I2CTransaction[1];
        xActions[0] = I2CDevice.CreateWriteTransaction(new byte[2] { address, b });
        pwmCTRL.Execute(xActions, 100);
    }

    private byte read(byte address)
    {
        I2CDevice.I2CTransaction[] xActions = new I2CDevice.I2CTransaction[1];
        byte[] buffer = new byte[2];
        xActions[0] = I2CDevice.CreateReadTransaction(buffer);
        pwmCTRL.Execute(xActions, 100);
        return buffer[0];
    }

The setter does his job, the getter returns 'variing' values like: 4112 (should be 0) or 257 for written values from 388 to 434.

Please help!

thanks,

mark

0 Kudos

850 Views
xiangjun_rong
NXP TechSupport
NXP TechSupport

Hi, Mark,

It seems that you use mbed or Arduino tools and use high language to communicate with the PCA9865, I do not know how the compiler generates the code to access the device in low level function. Maybe the compiler support can give you answer.

sorry for not helping you.

BR

Xiangjun Rong

0 Kudos