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 support@ccsinfo.com

[I2C] Help with multibyte read

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



Joined: 11 Mar 2010
Posts: 12502

View user's profile Send private message

PostPosted: Wed Apr 20, 2016 2:59 am     Reply with quote

In I2C, there are two addresses.

The device address, and
the register address in the device.
The device address also has a 'flag' as the low bit, saying to read or write.

So the sequence to write a set of bytes to a device is:

Start
Send device 'write' address
Send register address in device to write
Write data
Stop

The sequence to read a set of bytes is:

Start
Send device 'write' address
Send register address to read from
Restart
Send device 'read' address (write address+1)
Read bytes (NACK the last byte)
Stop

So you are 90% there.
For your 'write' code:
Code:

void sendData()
{
    i2c_start();
    i2c_write(muxerAddress);
    i2c_write(0); //assuming you want to start at register 0
    for(int i = 0; i < sizeof(gameStatus); i++)
    {
        i2c_write(gameStatus[i]);
    }
    i2c_stop();
}

To 'read' the block back:

Code:

void getData()
{
    i2c_start();
    i2c_write(muxerAddress);
    i2c_write(0); //assuming you want to start at register 0
    i2c_start(); //the restart
    i2c_write(muxerAddress + 1);
    //Now in read mode at register 0
    for(int i = 0; i < sizeof(gameStatus); i++)
    {
        if (i==(sizeof(gameStatus)-1))
            gameStatus[i]=i2c_read(0); //NACK the last byte
        else
            gameStatus[i]=i2c_read();
    }
    i2c_stop();
}


There are ways of slightly short-cutting parts of the transaction (you could assume that all transactions begin at register 0), but in general I2C prefers/requires the transactions to be handled fully. The PIC hardware in particular differs between chips on how abbreviated transactions are done, so it is better to use the standard form.
Particularly sending the NACK on the last read keeps the hardware synced up better.

For error checking, look at PCM_programmers I2C scanner program in the code library. Note how this works, even when testing addresses with no device, by checking the bit returned when the device address is written. Do the same in your master code, and you can tell if the device is acknowledging correctly before trying to write/read data.
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