View previous topic :: View next topic |
Author |
Message |
cleberalbert
Joined: 25 Feb 2014 Posts: 34 Location: Brazil
|
How to translate this little code from Arduino IDE? |
Posted: Tue Feb 25, 2014 8:30 am |
|
|
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](images/smiles/icon_lol.gif) |
|
![](templates/subSilver/images/spacer.gif) |
temtronic
Joined: 01 Jul 2010 Posts: 9177 Location: Greensville,Ontario
|
|
Posted: Tue Feb 25, 2014 8:45 am |
|
|
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 |
|
![](templates/subSilver/images/spacer.gif) |
demedeiros
Joined: 27 Dec 2013 Posts: 71
|
|
Posted: Tue Feb 25, 2014 6:44 pm |
|
|
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. |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Feb 25, 2014 11:22 pm |
|
|
You should tell us what the sensor is. A driver for it may already exist
for it in the forum archives. |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19382
|
|
Posted: Wed Feb 26, 2014 1:57 am |
|
|
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 |
|
![](templates/subSilver/images/spacer.gif) |
cleberalbert
Joined: 25 Feb 2014 Posts: 34 Location: Brazil
|
|
Posted: Wed Feb 26, 2014 6:20 pm |
|
|
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! |
|
![](templates/subSilver/images/spacer.gif) |
|