Question's about how use I2C Library!!!

General discussion on mikroC PRO for AVR.
Post Reply
Author
Message
public2010
Posts: 94
Joined: 05 May 2009 18:31
Location: Somewhere in Europe

Question's about how use I2C Library!!!

#1 Post by public2010 » 22 Apr 2020 19:20

Hi, I'm glad to be back on this forum after a long time with a little problem, about the functioning of I2C.

I have an Atmega 328 and a dedicated audio processor 7924. I want to connect the two devices on I2C and I didn't understand exactly the 7294 protocol that I attached below (extracted from datasheet TDA7294).
tda7924 extract.pdf
(21.57 KiB) Downloaded 82 times
I also analyzed the example presented in the mikroC for AVR software, the "I2C Library" category, where the following is said at one point:

Code: Select all

Soft_I2C_Write(0xA0);           // Address PCF8583, see PCF8583 datasheet
Soft_I2C_Write(2);              // Start from address 2
First of all, I don't know how to identify in the TDA7294 datasheet that 0xA0 address that was identified in the case of PCF8583? !! Or could I use the same address?

After doing this step, in order to send the subaddress with the number (example "INPUT MULTIPLEXER & AUX OUT" - see page no.3 from file above) I will have to somehow generate a kind of interrupt to simulate that "ACK" from the first page ? And as I write that subaddress - be 10001001 which is equal to 0x89 ?? And how do I send DATA1 ... DATAn?
Is it possible that at first I read these addresses from TDA7429?

I would like some advice, please!

public2010
Posts: 94
Joined: 05 May 2009 18:31
Location: Somewhere in Europe

Re: Question's about how use I2C Library!!!

#2 Post by public2010 » 23 Apr 2020 16:15

Anyone ?

I tried the following code and it doesn't work (I used "Soft_I2C_Init ();"):

Code: Select all

Soft_I2C_Start();               // Issue start signal
Soft_I2C_Write(0xA0);           // Address TDA7429, see datasheet
Soft_I2C_Write(2);              // Start from address 2, see datasheet
Soft_I2C_Start();               // Issue repeated start signal
Soft_I2C_Write(0xA1);           // Address TDA7429 for reading R/W=1
I don't know how to send subadress from datasheet and DATA1 ... DATAn?

User avatar
filip.grujcic
Posts: 822
Joined: 14 May 2018 08:34

Re: Question's about how use I2C Library!!!

#3 Post by filip.grujcic » 24 Apr 2020 11:11

Hello,

I suggest you use the hardware I2C module instead of Software I2C.
In this MCU it is called TWI (two-wire interface) and there is a library called TWI as well in the compiler.

You cannot use just any slave address, you need the very address of the chip you are trying to communicate with.
From what I see, this chip can have two slave addresses based on the state of the ADDR pin - 0x40 if ADDR = 0 or 0x41 if ADDR = 1 (these are 7-bit addresses, which means 8-bit address for Write would be 0x80 or 0x82 respectively).
Check in which state your ADDR pin is in order to determine which slave address to use.

I assume it is possible to read out any register, like the one you suggested:

Code: Select all

TWI_Start();
TWI_Write(0x80);  // Slave address (with ADDR = 0) + Write bit
TWI_Write(0x09);  // INPUT MULTIPLEXER & AUX OUT register address with MSB = 0 (no incremental bus)
TWI_Stop();

TWI_Start();			// Repeated start
TWI_Write(0x81);		// Slave address + Read bit
reg_data = TWI_Read(0u);	// No acknowledge (read only one byte)
TWI_Stop();
Kind regards,
Filip Grujcic

public2010
Posts: 94
Joined: 05 May 2009 18:31
Location: Somewhere in Europe

Re: Question's about how use I2C Library!!!

#4 Post by public2010 » 26 Apr 2020 12:31

Thank you very much for the answer.
I switched to using the TWI library, as you recommended.

Regarding you code example wrote above, I don't understand this part:

Code: Select all

reg_data = TWI_Read(0u);	// No acknowledge (read only one byte)
That is, it reads only one bit of its 8 can the value of the 8-bit address be read and in this case could i replace "0u" with the value of the subaddress ? Or I will have to send the status of each bit in the address according to the example posted by me below!

For example, or rather, to continue your example, I accessed the subaddress called "INPUT MULTIPLEXER & AUX OUT" with the method from the datasheet "No incremental bus". According to the TDA datasheet, this subaddress can take the following 4 values corresponding to the selection of entries 1 ... 4. I have represented these values in the image below where I understood that D3 ... D6 can take any value between 0 and 1, the same as D7 (I chose 0). So, I think (but I'm not sure) that I'm going to have to consider only the first three bits. On the right side of the image, in separate columns, I made the necessary conversion so that I could represent all 8 bits as an address.
Input Multiplexer subadress values for INT1-4.jpg
Input Multiplexer subadress values for INT1-4.jpg (46.87 KiB) Viewed 2807 times
So, the first question is if in the case of selecting an INTPUTs it is enough to rewrite the subaddress with one of the four values in the attached image? And if so, what are the steps I can take to do that? For example, I could use the following code inspired by your example but I don't know if it's written correctly !!!

Code: Select all

TWI_Start();         // issue TWI start signal
TWI_Write(0x80);     // slave address (with ADDR = 0) + Write bit
TWI_Write(0x09);     // INPUT MULTIPLEXER & AUX OUT

// Now I send bits corresponding to INTPUT 1 selection.I don't know if it's correct !!!
TWI_Write(1);        // bit D0 = 0
TWI_Write(2);        // bit D1 = 1
TWI_Write(3);        // bit D2 = 1
TWI_Stop();

User avatar
filip.grujcic
Posts: 822
Joined: 14 May 2018 08:34

Re: Question's about how use I2C Library!!!

#5 Post by filip.grujcic » 28 Apr 2020 11:55

Hello,

TWI_Read() function reads a byte, not a bit. Same goes for TWI_Write function, it writes a byte, not a bit.

TWI_Read(0u) means NO ACKNOWLEDGE, meaning you (as the master) are telling the slave you do not wish to read from it anymore. If you wanted to read 3 bytes in a row, it would be:

Code: Select all

byte1 = TWI_Read(1u);  // ACK
byte2 = TWI_Read(1u);  // ACK
byte3 = TWI_Read(0u);  // NO ACK
To do a write to a register, you would use:

Code: Select all

TWI_Start();         // issue TWI start signal
TWI_Write(0x80);     // slave address (with ADDR = 0) + Write bit  / Slave address
TWI_Write(0x09);     // INPUT MULTIPLEXER & AUX OUT / Register address
TWI_Write(0x06);     // IN1 / Data to write
TWI_Stop();
Regards,
Filip Grujcic

public2010
Posts: 94
Joined: 05 May 2009 18:31
Location: Somewhere in Europe

Re: Question's about how use I2C Library!!!

#6 Post by public2010 » 28 Apr 2020 18:34

Your example is very suggestive. Thanks.

However, two last question before moving everything to the practical area. For example, if I want to adjust the AUDIO VOLUME I will have to read the current value of the volume. However, I did not understand if this TDA7429 stores this date. Or will I have to write them in an MCU EEPROM ?

And, suppose this TDA stores the current value of the VOLUME OUTPUT AUDIO SIGNAL, I could read it using the following code ?

Code: Select all

TWI_Start();         // issue TWI start signal
TWI_Write(0x80);     // slave address (with ADDR = 0) + Write bit  / Slave address
TWI_Write(0x00);     // INPUT ATTENUATION / Register address
volume = TWI_Read(0x00);	// Read INTPUT ATTENUATION STEPS - current audio volume
TWI_Stop();

User avatar
filip.grujcic
Posts: 822
Joined: 14 May 2018 08:34

Re: Question's about how use I2C Library!!!

#7 Post by filip.grujcic » 21 May 2020 15:23

Hello,

The correct code for performing an I2C read is:

Code: Select all

TWI_Start();
TWI_Write(0x80);  // Slave address (with ADDR = 0) + Write bit
TWI_Write(0x00);  // INPUT ATTENUATION / Register address
TWI_Stop();

TWI_Start();			// Repeated start
TWI_Write(0x81);		// Slave address + Read bit
reg_data = TWI_Read(0u);	// No acknowledge (read only one byte)
TWI_Stop();
You can read more about I2C protocol in documents you can find anywhere online. This is not something specific to our library, it's simply how I2C protocol works.

Regards,
Filip Grujcic

Post Reply

Return to “mikroC PRO for AVR General”