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_transfer()

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



Joined: 03 Dec 2013
Posts: 154

View user's profile Send private message

i2c_transfer()
PostPosted: Sat Jan 20, 2018 6:32 pm     Reply with quote

I was given an example by ccs for using i2c_transfer() which I cannot post here.

I also noticed there is very little mention of the i2c_transfer() and only found one posting on this forum from 2010.

I guess i2c_transfer() is so simple to use everybody can use it but me :-)

The CCS help file is not very helpful.

There are two lines in the help file:

i2c_transfer(address, wData, wCount, rData, rCount) - Performs an I2C transfer to and from a device, function does start, restart, write, read, and stop I2C operations; when in I2C master mode.

i2c_transfer_out(Address, wData, wCount) - Performs an I2C transfer to a device, function does start, write, and stop I2C operations; when in I2C master mode.

So how do I format the data for I2C address 0XA0 to be written to the devices register at 0X28 and the value is 0X10 ?

In the old i2C way is looks like this:
Code:

 i2c_start();
 i2c_write(0XA0);
 i2c_write(0X28);
 i2c_write(0X01);
 i2c_stop();

Easy to understand.

How does that fit into:

i2c_transfer(address, wData, wCount, rData, rCount);
or
i2c_transfer_out(Address, wData, wCount);

Any help would be appreciated.
PCM programmer



Joined: 06 Sep 2003
Posts: 20259

View user's profile Send private message

PostPosted: Sat Jan 20, 2018 10:55 pm     Reply with quote

This is something new with vs. 5.076. The post in 2010 is not related to
this new function.

As far as I can tell, this is how you would translate your code to use
the new function:
Code:

#include <18F46K22.h>
#fuses INTRC_IO,NOWDT,PUT,BROWNOUT
#use delay(clock=4M)

#use i2c(Master, I2C1, slow)

//======================================
void main(void)
{
int8 write_data[] = {0x28, 0x01};
 
i2c_transfer_out(0xA0, write_data, 2);

while(TRUE);
}


I have not tested this in hardware. This analysis comes from looking at
the .LST file and the help file.
Ttelmah



Joined: 11 Mar 2010
Posts: 12953

View user's profile Send private message

PostPosted: Sun Jan 21, 2018 1:48 am     Reply with quote

The post in 2010, has nothing to do with the current function.
The poster was trying to translate code from another processor/compiler, and had included i2c_transfer in the remarks (showing what he was translating).

i2c_transfer is brand new. Appeared in 5.076. As such 'treat with care' until tested.
Often functions are added for a while when effectively 'beta', and then a mention is added to the readme, when they are 'ready to use'.
However it looks to be a simple 'block' encapsulation of the I2C transmit, so ought really to not have much to go wrong.

Remember in I2C, the actual transfer knows nothing about the device register. As far as I2C is concerned, you just write the register address, followed by the byte(s) you want to write to this and subsequent registers. PCM_programmer shows exactly what it seems to need. The transfer can optionally be used for read as well, and here seems potentially to work like this:
Code:


#include <18F46K22.h>
#fuses INTRC_IO,NOWDT,PUT,BROWNOUT
#use delay(clock=4M)

#use i2c(Master, I2C1, slow, stream=TEST)

//======================================
void main(void)
{
    int8 write_data[] = {0x28}; //select register 28
    int8 read_data[4];
 
    i2c_transfer(TEST, 0xA0, write_data, 1, read_data, 4);

    while(TRUE);
}

However it is going to be very interesting to test and see if it does do the restart, and the NACK on the last byte. This should be selecting the same register as PCM_programmer (0x28), then switching to read four bytes from it.
I show it using a stream name as well, for 'completeness'.
soonc



Joined: 03 Dec 2013
Posts: 154

View user's profile Send private message

Thanks PCM_Programmer and Ttelmah
PostPosted: Sun Jan 21, 2018 9:45 am     Reply with quote

Thanks for clarification.
Yes it's a new function for the 46K series.
I'm slowly plodding through porting code from PIC24 to the PIC18F46K42, and as I found out there are several different "things" to deal with.
So far things are going OK but slow without the debugger.
I have one pin dedicated to RS232 debugger as a means of doing the old fashioned printf() debugging.

I found a small error in the setup NCO function and reported that to CCS.

I am testing code today to setup the PCF85263 RTC.
RTC is the one item that the 46K42 does not have. I can live with that as the PIC24 does not have NCO which is far more important for my application.

I've tested my code using software I2C and that is where it looked complex using the new function because of the restart and NAK.

I'll be testing later today... Thanks again for the help.
soonc



Joined: 03 Dec 2013
Posts: 154

View user's profile Send private message

tested I2C_Transfer
PostPosted: Sun Jan 21, 2018 8:22 pm     Reply with quote

Code:

#define SCL_PIN  PIN_B1
#define SDA_PIN  PIN_B2
#pin_select SCL1OUT = SCL_PIN
#pin_select SDA1OUT = SDA_PIN

//Note 3K3 pull up resistors on SCL and SDA

#use i2c(Master, sda=SDA_PIN, scl=SCL_PIN ) // as per ccs example

int8 w_data[128]; // large in the hope this may work for later testing

w_data[0]=0X00; // starting address
w_data[1]='X';  // the first value
i2c_transfer(0XAE, w_data, 2);

fprintf(DBG,"Write done\r\n"); // never reaches here

w_data[0]=0;   
memset (r_data,0,16);               
i2c_transfer(0XAE, w_data, 1, r_data, 1); // read a byte starting at location 0

I've noticed strange things in the .h file The rCount is asking for an int8 pointer. I tried both it made no difference.
Code:

_bif void i2c_transfer(unsigned int8 Address, unsigned int8 *wData, unsigned int8 wCount, unsigned int8 *rData, unsigned int8 *rCount);

Conclusion: It's too early. I'll carry on with the software version for now as I have to make progress.

I'll revisit this as new compiler releases happen.
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