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

How to translate this little code from Arduino IDE?

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



Joined: 25 Feb 2014
Posts: 34
Location: Brazil

View user's profile Send private message

How to translate this little code from Arduino IDE?
PostPosted: Tue Feb 25, 2014 8:30 am     Reply with quote

Hi everyone!

I'm trying to translate a part of code for Arduino below to PIC by CCS Compiler, could someone help me?

it's a function to read a register of a sensor by I2C protocol

Code:

int readRegister(int deviceAddress, byte address){

    int v;
    Wire.beginTransmission(deviceAddress);
    Wire.write(address); // register to read
    Wire.endTransmission();

    Wire.requestFrom(deviceAddress, 1); // read a byte

    while(!Wire.available()) {
        // waiting
    }
    v = Wire.read();
    return v;
}


Tks! Laughing
temtronic



Joined: 01 Jul 2010
Posts: 9164
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Tue Feb 25, 2014 8:45 am     Reply with quote

Easy way is to look at some of the I2C examples that CCS supplies in the 'examples' folder.
Also, be sure to download PCM programmer's I2C bus scanner program in the 'code library' on this site ! It 'looks' for any I2C devices on the bus and reports back where theu are located. It's a 'must have' tool for doing I2C with PICs.

hth
jay
demedeiros



Joined: 27 Dec 2013
Posts: 71

View user's profile Send private message

PostPosted: Tue Feb 25, 2014 6:44 pm     Reply with quote

In addition to what temtronic mentioned, it is important to look up the specifications for the I2C bus, like when to send repeated start conditions, nacks etc.

Arduino tends to obscure the low level functions like start, stop etc. I found it a little tricky moving from Arduino I2C to CCS I2C.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Feb 25, 2014 11:22 pm     Reply with quote

You should tell us what the sensor is. A driver for it may already exist
for it in the forum archives.
Ttelmah



Joined: 11 Mar 2010
Posts: 19346

View user's profile Send private message

PostPosted: Wed Feb 26, 2014 1:57 am     Reply with quote

The key difference is that the 'from' is done in the background, while by default the CCS code does things in the foreground, and only does one byte at a time - the 'RequestFrom function', potentially handles multiple bytes, and generates 'NACK' on the last, and then stops, flagging this with 'Wire.available'. So:
Code:

int readRegister(int deviceAddress, byte address){

    int v;
    I2C_start();
    I2C_write(deviceAddress); //these are combined in 'BeginTransmission'
    I2C_write(address); // register to read
    I2C_stop(); //EndTransmission

    I2C_start();
    I2C_write(deviceAddress+1); //the 'request from' automatically sets read
    //The point about the 'byte count' with the Arduino function, is
    //it tells it when to NACK (last byte), and then stop
   
    v = I2C_read(0); //This is the NACK - only done on last byte. As only
    //one byte, just send with this read.
    I2C_stop(); //and stop.
    return v;
}

The other thing you have to consider is what format 'deviceAddress' is in.

The PIC uses 8bit addresses (shifted left). Texas use 7bit addresses. Not sure what format the address is for the Arduino. If it is 7bit right shifted, then the address sent in the first write, needs to be 'deviceAddress*2', and in the write to trigger the read, needs to be '(deviceAddress*2)+1'. If the address is already left shifted, then it needs to be as posted in my code. You need to look up what the chip address actually 'is', and what format it is in the code calling this routine....

Best Wishes
cleberalbert



Joined: 25 Feb 2014
Posts: 34
Location: Brazil

View user's profile Send private message

PostPosted: Wed Feb 26, 2014 6:20 pm     Reply with quote

Tks very much guys, Your explanation helped me to understand important things and I used the Ttelmah's code. Now i've another question reported in another topic. It's about strange values that i've been getting from the sensor.

link: http://www.ccsinfo.com/forum/viewtopic.php?p=184557#184557

I hope you can help me again over there!
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