Set register prior to I2C_MasterWriteBuf

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
FlHe_4723206
Level 1
Level 1
First like received First like given

uint8 writeRegister8(uint8 i2c_address, uint8 reg, uint8 data){

        if(!I2C_MasterSendStart(i2c_address,0)){

            I2C_MasterWriteByte(reg);

            I2C_MasterWriteByte(data);

            return I2C_MasterSendStop();

        }else{

            I2C_MasterSendStop();

            return 1;

        }

    }else{

        return 99u; //placeholder

    }

}

with the loop in my main code being:

    for(uint8 i=0; i<BUF_LENGTH; i++){

        status = writeRegister8(i2c_address, PWM_REGISTER+i,gamma32[brightness]);

    }

I want to replace the loop+function call with I2C_MasterWriteBuf function. However, I2C_MasterWriteBuf does not have a register field. I have tried all sorts of combinations of writing register address first as a byte, restarting the bus, stopping and starting again, writing the buffer directly after setting the register address... Nothing works in terms of that the slave device never gets the right values.

Basic question is: How do I use

I2C_MasterWriteBuf

in an intended way when I need to set a register to write to first?

0 Likes
1 Solution
lock attach
Attachments are accessible only for community members.
BragadeeshV
Moderator
Moderator
Moderator
First question asked 1000 replies posted 750 replies posted

Hi flhe_4723206​,

Instead of having separate variables for register and data, you can have a single structure or single array to hold both these values. You can pass this array/ structure containing both these values as parameter to the I2C_MasterWriteBuf . See attached file for reference.

Let us know if this solves your problem.

Regards,

Bragadeesh

Regards,
Bragadeesh

View solution in original post

4 Replies
MotooTanaka
Level 9
Level 9
Distributor - Marubun (Japan)
First comment on blog Beta tester First comment on KBA

Hi,

I wonder if i2c function i2c_writeRegs() in my sample below can be a hint for you.

MCU Tester, a Swiss Army Knife for PSoC (CY8CKIT-044 version)

moto

Thank you, that is a very interesting project and I'm sure I will dive into it.

lock attach
Attachments are accessible only for community members.
BragadeeshV
Moderator
Moderator
Moderator
First question asked 1000 replies posted 750 replies posted

Hi flhe_4723206​,

Instead of having separate variables for register and data, you can have a single structure or single array to hold both these values. You can pass this array/ structure containing both these values as parameter to the I2C_MasterWriteBuf . See attached file for reference.

Let us know if this solves your problem.

Regards,

Bragadeesh

Regards,
Bragadeesh

Hi BragadeeshV_41

Thanks a lot for your hint! After I adapted your code to my function it worked immediately and without any need for further tweaks. Have a good day!