CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

TCA9548A

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Blackjackmaster



Joined: 14 May 2023
Posts: 30

View user's profile Send private message

TCA9548A
PostPosted: Wed Jul 26, 2023 8:22 pm     Reply with quote

Hello,

I am working on creating an array from Channel read of the TCA9548A.

Code:
#define MIN_I2C_ADDRESS 0x00
#define MAX_I2C_ADDRESS 0x70


Code:
void main()
{
     
       
    TCA9548A_SelectChannel(0);// Select the desired channel
    unsigned char i2c_addr;
    unsigned char i2c_devices[16] = {0}; // Array to store active I2C devices
    int num_devices_found = 0;

    // Initialize I2C communication
    i2c_init(TRUE);

    // Loop through possible I2C addresses and check for active devices
    for (i2c_addr = MIN_I2C_ADDRESS; i2c_addr <= MAX_I2C_ADDRESS; i2c_addr++) {
        i[b]f (!i2c_start(i2c_addr))[/b] {
            // Failed to start I2C communication
            continue;
        }
        if (i2c_write(i2c_addr << 1)) {
            // Successfully sent address, device acknowledged
            i2c_devices[num_devices_found++] = i2c_addr;
        }
        i2c_stop();
    }

    // Display the active I2C devices
    if (num_devices_found > 0) {
        printf("Active I2C devices found:\n");
        for (int i = 0; i < num_devices_found; i++) {
            printf("0x%02X\n", i2c_devices[i]);
        }
    } else {
        printf("No active I2C devices found in the specified address range.\n");
    }

    while (TRUE) {
       
    }


I have an error in this line of code:
Code:
if (!i2c_start(i2c_addr)


I do not know what the issue is. Any suggestions?
PrinceNai



Joined: 31 Oct 2016
Posts: 480
Location: Montenegro

View user's profile Send private message

PostPosted: Wed Jul 26, 2023 11:44 pm     Reply with quote

You can't test i2c_start(), there is nothing to test. Also, the only parameter this function accepts is I2C stream name. Check your previous thread. I2c_start() is never tested, ACK status from i2c_write(i2c_addr) is.
Ttelmah



Joined: 11 Mar 2010
Posts: 19539

View user's profile Send private message

PostPosted: Thu Jul 27, 2023 12:22 am     Reply with quote

I think he has taken as his basis, code from another compiler.
On some compilers, their I2c_start command is equivalent to an I2c_start,
followed by I2c_write(address), so returns the ack status from this.
The PIC and CCS, follow exactly what the I2C specs do. I2c_start is
just a basic signal on the I2c lines signifying a start condition. As Prince
Nai says, nothing to return. It is the following I2c_write with the device
address that returns the ACK bit if a device is there and responds.

Look in the code library. Sticky post, fourth down from the top.
"I2C bus scanner program". This shows exactly how to do this in CCS C.

Also note that I think he is using 7bit I2c addresses here. The address
needs to increment by 2, and count to 0xE0. Also should not really start
at 0, 0 in I2C is the broadcast address. All the bottom few addresses are
reserved, and devices would not normally be down there. This is
why the example pointed to starts at 0x10.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group