![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
Chaud
Joined: 11 Jan 2012 Posts: 39
|
How we get a 16-bit output from I2C? |
Posted: Thu Feb 02, 2012 5:38 pm |
|
|
Hello, i need to get a 16-bit output from my gyroscope(XV 3500CB),and i cannot get a value between 0 - 65535 as i want (16 bit), i am doing this:
Code: | i2c_start(); ->i2c initiate
i2c_write(11010001); -> 1101000X is the gyro adress, 1101000'1' means that master can read (this is writed on datasheet, i think im not wrong);
value1 = i2c_read(); -> first 8 bit (LSB)
value2 = i2c_read() << 8; -> last 8 bit (MSB)
i2c_stop(); -> stop i2c
gyro = make16(value2,value1); -> make two 8bit int in one 16-bit int
printf("%ld - %d - %d\r\n",gyro,value2,value1); -> read the values |
Here is the printf output:
255 - 0 - -1
255 - 0 - -1
255 - 0 - -1
255 - 0 - -1
255 - 0 - -1
etc
gyro value is always 255, value 2 is always 0, and value 1 is always "-1", i also dont know if i doing right to get the 16-bit by doing this:
Code: | value1 = i2c_read(); -> first 8 bit (LSB)
value2 = i2c_read() << 8; -> last 8 bit (MSB) |
Do you know how to get a 16-bit output from your I2C??
Last edited by Chaud on Thu Feb 02, 2012 6:08 pm; edited 1 time in total |
|
![](templates/subSilver/images/spacer.gif) |
Chaud
Joined: 11 Jan 2012 Posts: 39
|
|
Posted: Thu Feb 02, 2012 5:45 pm |
|
|
One guy with same gyro did this:
Code: | int Get_Gyro_Data()
{
int data;
Wire.beginTransmission(Gyro_address);
Wire.send(Register);
Wire.endTransmission();
Wire.requestFrom(Gyro_address, 2);
if(Wire.available()>=2)
{
data = Wire.receive();
data |= Wire.receive() << 8;
}
return data;
} |
Wire.requestFrom(Gyro_address, 2); // this means a request for gyro for 2 bytes, but this code is for arduino, in CCS libraries i dont see any method that provide this, to get 2 bytes
For what i read of "i2c_poll" method, is there for things like this,so i tried this code:
Code: | i2c_start(); // Start condition
i2c_write(11010001); // Device address/Read
for(count = 0; count < 2;count++){
while(!i2c_poll());
buffer[count] = i2c_read(1);
}
i2c_stop();
gyro = make16(buffer[1],buffer[0]);
printf("%ld - %d - %d\r\n",gyro,buffer[1],buffer[0]); |
But i always get compile error on:
Code: | while(!i2c_poll()); |
whit this message: Undefined identifier for -- i2c_poll |
|
![](templates/subSilver/images/spacer.gif) |
temtronic
Joined: 01 Jul 2010 Posts: 9170 Location: Greensville,Ontario
|
|
Posted: Thu Feb 02, 2012 6:42 pm |
|
|
hmm..
value2 = i2c_read() << 8; -> last 8 bit (MSB)
Get a piece of graph paper and 'play computer'.
put down ANY binary value for the 2nd I2C_read().
then left shift that value once.
do it again, and again, 8 times total
what result do you get ?
This is what is put into 'value2'.
As to why value1 is always 255, I'd have to read the gyro datasheet or go to the R&D lab to setup and test the device.
The make16() function creates a word(16 bits) from 2 bytes(8 bits each),easy to use and does work...but be sure to put the variables in the correct order. |
|
![](templates/subSilver/images/spacer.gif) |
Chaud
Joined: 11 Jan 2012 Posts: 39
|
|
Posted: Fri Feb 03, 2012 10:16 am |
|
|
I think I didn't get what you really mean :P There must be someone who needed to get more than 1 byte via I2C. I'm pretty sure that value 2 is incorrect, I just followed the Arduino code. I need to know how I do that with CCS C code. |
|
![](templates/subSilver/images/spacer.gif) |
temtronic
Joined: 01 Jul 2010 Posts: 9170 Location: Greensville,Ontario
|
|
Posted: Fri Feb 03, 2012 10:19 am |
|
|
again...
WRONG
value2 = i2c_read() << 8; -> last 8 bit (MSB)
get RID of '<<8'
use
value2 = i2c_read();
you do NOT want to leftshift the data from the i2c device(shift left 8 times and you'll ALWAYS get 0. |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Feb 03, 2012 12:48 pm |
|
|
Quote: | There must be someone who needed to get more than 1 byte via I2C.
|
One of the reasons you are not getting replies is because you are making
really, really dumb errors in your code. This makes people avoid giving
help. They think you don't care enough to proof-read your code. So they
think, "he (Chaud) doesn't care, so why should I care ?", and they don't
give help.
Here is an example from your program:
Quote: |
i2c_write(11010001); // Device address/Read
|
Here, you are giving it an i2c address of 11 million and something.
That's not an i2c address. No way. A number written just like that
is in Decimal format. It's 11 million and something. If you want
binary format (1's and 0's), then you have to put a "0b" in front of
the number. That's very basic knowledge in programming. When
you make that mistake, it makes people groan and turn away.
I'm not trying to beat up on you too much, but I am trying to let you
know that you need to proof-read your code. You need to question
your own code, and think like this: "Is that right ? Do I really use 11
million for the i2c address ?". "That can't be right". "I must find the
correct way to do it".
In addition to that, there are at least two more mistakes in the following
code. If you read previous threads on i2c problems on this forum you
would see the answer. For one thing, no one ever uses i2c_poll() in
a simple PIC Master to Slave device program. You won't see it in any
examples. So why use it ? Again, you need to question everything
you do.
Code: |
i2c_write(11010001); // Device address/Read
for(count = 0; count < 2;count++){
while(!i2c_poll());
buffer[count] = i2c_read(1);
|
The other problem has to do with using a NACK on the last i2c_read().
That answer must have been posted at least 100's of times on this forum.
Sometimes people get tired of posting it one more time. You need to
read the forum archives for i2c problems. Use the search page. |
|
![](templates/subSilver/images/spacer.gif) |
Chaud
Joined: 11 Jan 2012 Posts: 39
|
|
Posted: Sun Feb 05, 2012 2:50 pm |
|
|
Hello PCM Programmer, I agree with you, sorry I'm a very distracted guy, and I completely forget the "0b" , and the problem was that .. sorry my bad, thanks for help friend.
temtronic thanks for your help too
Problem solved |
|
![](templates/subSilver/images/spacer.gif) |
|
|
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
|