 |
 |
| View previous topic :: View next topic |
| Author |
Message |
aruna1
Joined: 14 Oct 2008 Posts: 103
|
|
Posted: Sun Jan 02, 2011 6:45 pm |
|
|
| Ttelmah wrote: | The problem is that you are putting the received values into int16's, rather than int8's. Change data0 to 3, to be int8 values, and it should work
What is happening, is that you are presenting make32, with 6 bytes (3 * int16 values), which won't fit into an int32.....
You need to change the prints for the bytes, to be %u, otherwise if the top bit is set, they will print as -ve....
I see several other have posted the answer, while I have been cooking my evening meal!....
Best Wishes |
I see the point. Can you tell me how to change these 8 bit values I'm receiving to unsigned values?
And datasheet says its output is two's complement. So far I have received only positive values. But it is possible to receive negative values also? (IC is used in a gyro). |
|
 |
Ttelmah
Joined: 11 Mar 2010 Posts: 20159
|
|
Posted: Mon Jan 03, 2011 4:51 am |
|
|
Doesn't matter....
Let's look at a silly example:
| Code: |
int8 val1;
signed int8 val2;
val1=192;
val2=val1;
printf("%02x,%02x/n/r",val1,val2);
printf("%d\n\r",val1);
printf("%u\n\r",val2);
|
You have a signed value, and an unsigned value. Assign 192, to the former, and then copy this to the later. Now 'whoa a moment'. A 'signed int8', can hold values from -128 to +127, yet we put '192' into it.....
However print the values as hex, and we see they are both 'c0'. The same value. The 'signed/unsigned', has not changed anything.
Then I print the 'unsigned' number, as a signed value. The output, is -64...
Then the 'signed' value as 'unsigned'. The output is 192....
A number being signed or unsigned, makes no difference. Internally it is a binary value, and it is up to _you_ to select the output format to suit.
What is the 'point' of signed then?.
It comes in one area _arithmetic_. If the compiler sees a variable declared as 'signed', it'll use signed arithmetic when performing arithmetic. Also if you cast it into a float for example, it'll hand the 'sign' on.
So:
| Code: |
float fred;
int8 val=4;
int8 val2=-5;
signed int8 val3=-5;
fred = val*val2;
//Will give 236. 0x4 * 0xFB = 0x3EC. The ''3' will be dropped (overflow).
//Result then is 0xFB, converted to a float = 236.....
fred = val*val3;
//Because 'val3' is signed, signed arithmetic will be used. 4*-5 = -20
//Converted to float, 'fred' is -20.
|
So for what you want, 'signed' or unsigned, doesn't matter at all. It is purely up to you, how you handle the _binary_ byte values.
OK. Now we come to the next part. You have three byte values, which together contain an 18bit two's complement number. You can now assemble these to form an int32 value. As you have seen in the first example, this could be transferred directly to a 'signed' int32 value. So the output of the make32, could be put directly into a 'signed int32' result. So the question then is, how do I make the 'sign' bit from the 18bit number 'propagate' into the 32bit result?.
Now the 'sign', is the top bit of the 18bit value. If this is set, the resulting value is meant to be -ve. What has to be done is:
| Code: |
signed int32 value; //Flag as 'signed', so arithmetic etc. will handle.
//Your receive code.
value = make32(data0,data1,data2);
//Now need to 'propagate' the sign
if (bit_test(value,17)) {
//Top bit is set.....
value |= 0xFFFC0000;
//Now, if bit 17 is set, set _all_ the bits to the top of the number
}
printf("%Ld\n\r",value);
|
Key is to understand the binary '-ve' handling, when dealing with two's complement.
-1, if stored as a signed 'int8', is 0xFF.
If stored as a signed 'int16', is 0xFFFF.
If stored as a signed 'int32', is 0xFFFFFFFF.
So if you were given a signed int16 value, and wanted to put it into a signed int32, you can test the top bit, and if this is set, simply set all the bits to the top of the number. Now the compiler does this automatically, if you put a signed int16, into an int32, but doesn't 'know' about int18 values, so you have to do this yourself. I use binary 'or', to set all the bits needed.
Best Wishes |
|
 |
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Mon Jan 03, 2011 6:25 am |
|
|
According to the datasheet, the ADC high byte get's correct sign extension as needed for a signed 24-bit value. But make32() does no sign extension.
Besides Ttelmah's code, this should work as another solution:
| Code: | signed int16 data0;
int8 data1,data2,data3;
signed int32 value;
ack[2]=i2c_write(0xD1);//read command
data0=(signed)i2c_read(1);
data1=i2c_read(1);
data2=i2c_read(1);
data3=i2c_read(1);// config bits
i2c_stop();
value = make32(data0,data1,data2); |
|
|
 |
Ttelmah
Joined: 11 Mar 2010 Posts: 20159
|
|
Posted: Mon Jan 03, 2011 9:18 am |
|
|
Don't think it will.
Remember the value coming from the first 'read', is just the top two two bits of the signed number. xxxxxxsn
With the 'sign', in 's'. Not in the top bit where a signed int8 expects it.
You'd still need to propagate the sign up through the rest of the int8 (and create a top byte containing '0xFF' as well.
Nice idea though.
Best Wishes |
|
 |
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Mon Jan 03, 2011 10:53 am |
|
|
| As said, the MCP3421 datasheet tells different. It says, the high byte is transmitted sign extended sssssssn. |
|
 |
aruna1
Joined: 14 Oct 2008 Posts: 103
|
|
Posted: Tue Jan 04, 2011 3:30 am |
|
|
hi guys
problem is worse
when i dont move the gyro it should give reference voltage.
but my out puts are like this
data0 = 1
data1 = 81
data2 = -111
data3 = 28
see the negative sign of data2?
i belive its because 7bit of data2 is 1,compiler takes it as a negative value or something.
data0,data1,data2 are 8bit variables.
any ideas how to correct this? |
|
 |
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Tue Jan 04, 2011 6:21 am |
|
|
There's no negative sign. The sign is transmitted only in data0. data1 and data2 are
just 8 bit data. Displaying them signed or unsigned has no meaning. The actual output is
((1*256)+81)*256+145=86417, which is about 2/3 of positive full scale.
As a first test, I would measure the ADC input voltage with an oscilloscope or
multimeter and compare the digital output with expected values. |
|
 |
aruna1
Joined: 14 Oct 2008 Posts: 103
|
|
Posted: Tue Jan 04, 2011 6:27 am |
|
|
| FvM wrote: | There's no negative sign. The sign is transmitted only in data0. data1 and data2 are
just 8 bit data. Displaying them signed or unsigned has no meaning. The actual output is
((1*256)+81)*256+145=86417, which is about 2/3 of positive full scale.
As a first test, I would measure the ADC input voltage with an oscilloscope or
multimeter and compare the digital output with expected values. |
sorry to bother you but I didn't understand.
so when no movement voltage should be about 1350mV.so that means
00 00000101 01000110
0 5 70
and I didnt get your calculation
sorry for the noobness  |
|
 |
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Tue Jan 04, 2011 9:45 am |
|
|
| Quote: | | I didnt get your calculation |
Write down the bits, as you did for the last measurement, then you will see.
For the latter, you get ((0*256)+5)*256+70 = 1350, which is
1350/131072*Uref=0.01*Uref.
The value Variable from your conversion code should give the same result, unfortunately, why don't you show it in your post? |
|
 |
aruna1
Joined: 14 Oct 2008 Posts: 103
|
|
Posted: Tue Jan 04, 2011 5:52 pm |
|
|
| FvM wrote: | | Quote: | | I didnt get your calculation |
Write down the bits, as you did for the last measurement, then you will see.
For the latter, you get ((0*256)+5)*256+70 = 1350, which is
1350/131072*Uref=0.01*Uref.
The value Variable from your conversion code should give the same result, unfortunately, why don't you show it in your post? |
Sorry for not posting value.
Anyway here is my current code and output when gyro is not moving.
| Code: |
#include "E:\My Electronic projects\gyro test\i2c\main.h"
int ack[3];
int16 data[4];
int32 value;
int8 data0;
int8 data1;
int8 data2;
int8 data3;
void main()
{
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_psp(PSP_DISABLED);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
i2c_start();
ack[0]=i2c_write(0xD0);//set to write mode
ack[1]=i2c_write(0x9C);//config bits-18bit adc,1 pga
i2c_stop();
delay_ms(10);
printf("\n ack0 = %d\r",ack[0]);
printf("\n ack0 = %d\r",ack[1]);
printf("\n ack0 = %d\r",ack[1]);
printf(".............................\n\r");
while(1)
{
//if(!input(PIN_D1))
{
delay_ms(500);
output_toggle(PIN_D0);
//i2c_write(0x00);
i2c_start();
ack[2]=i2c_write(0xD1);//read command
data0=i2c_read(1);
data1=i2c_read(1);
data2=i2c_read(1);
data3=i2c_read(1);// config bits
i2c_stop();
delay_ms(10);
value = make32(data0,data1,data2);
printf("\n data0 = %d\r",data0);
printf("\n data1 = %d\r",data1);
printf("\n data2 = %d\r",data2);
printf("\n data3 = %d\r",data3);
printf("\n value = %Ld\r",value);
//printf("\n value = %Ld\r",value);
printf("\n--------------------------------------------------------\r");
}
}
// TODO: USER CODE!!
}
|
output
data0 = 1
data1 = 81
data2 = -106
data3 = 28
value = 86422 |
|
 |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Jan 04, 2011 6:08 pm |
|
|
There is a problem with your code. Look at the line in bold below:
| Quote: |
ack[2]=i2c_write(0xD1);//read command
data0=i2c_read(1);
data1=i2c_read(1);
data2=i2c_read(1);
data3=i2c_read(1);// config bits
i2c_stop();
|
Look at this diagram in the MCP3421 data sheet:
| Quote: |
FIGURE 5-3: Timing Diagram For Reading From The MCP3421 With 18-Bit Mode. |
On the right side, for the 5th byte, it says:
| Quote: | | To end: NAK by Master |
This means that on the last i2c_read(), you are supposed to do a "NAK".
How is this done ? Look at the CCS manual. It says:
| Quote: |
i2c_read( )
Syntax: data = i2c_read();
data = i2c_read(ack);
data = i2c_read(stream, ack);
Parameters: ack -Optional, defaults to 1.
0 indicates do not ack.
1 indicates to ack.
|
So you need to fix the parameter on the last i2c_read() so it does a NAK. |
|
 |
aruna1
Joined: 14 Oct 2008 Posts: 103
|
|
Posted: Tue Jan 04, 2011 6:27 pm |
|
|
| PCM programmer wrote: | There is a problem with your code. Look at the line in bold below:
| Quote: |
ack[2]=i2c_write(0xD1);//read command
data0=i2c_read(1);
data1=i2c_read(1);
data2=i2c_read(1);
data3=i2c_read(1);// config bits
i2c_stop();
|
Look at this diagram in the MCP3421 data sheet:
| Quote: |
FIGURE 5-3: Timing Diagram For Reading From The MCP3421 With 18-Bit Mode. |
On the right side, for the 5th byte, it says:
| Quote: | | To end: NAK by Master |
This means that on the last i2c_read(), you are supposed to do a "NAK".
How is this done ? Look at the CCS manual. It says:
| Quote: |
i2c_read( )
Syntax: data = i2c_read();
data = i2c_read(ack);
data = i2c_read(stream, ack);
Parameters: ack -Optional, defaults to 1.
0 indicates do not ack.
1 indicates to ack.
|
So you need to fix the parameter on the last i2c_read() so it does a NAK. |
Hi PCM Programmer
well I did NAK but result is the same.
anyway I understand now.data2 is in twos complement form. but make32 returns correct value.
so that did solved it.
anyway is there any way to turn data2 to normal binary? not two's complement?
and I wonder how it become twos complement in the first place
I wish there is a method to get I2C data as array of binary bits instead of int8 |
|
 |
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Wed Jan 05, 2011 3:10 am |
|
|
| Quote: | | anyway is there any way to turn data2 to normal binary? not two's complement? |
There's no difference in this case. Two's complement (signed integer) and plain binary (unsigned integer) are just two different interpretations of the same data. In so far, neither i2c_read() nor make32() need to distinguish between them. The only possible pitfall is in a missing sign extension, as discussed before.
I see, that your conversion is working correctly, so the most plausible explanation is, that the analog input voltage to the ADC is different from what you expect, or the ADC is configured wrong somehow.
I previously suggested to check the converted ADC output code against the input voltage, as measured with an oscilloscope or multimeter. Did you ever try?
As PCM programmer correctly mentioned, the I2C standard requires to send a NAK with the last byte transferred. But usually, a missing NAK is ignored in read operations. The MCP3421 datasheet only tells, that ACK is required to continue the data transmission, not about consequences of missing NAK. It's better to follow the datasheet strictly and use the NAK, but I'm not surprized that it has no effect. |
|
 |
aruna1
Joined: 14 Oct 2008 Posts: 103
|
|
Posted: Wed Jan 05, 2011 4:19 am |
|
|
| FvM wrote: | | Quote: | | anyway is there any way to turn data2 to normal binary? not two's complement? |
There's no difference in this case. Two's complement (signed integer) and plain binary (unsigned integer) are just two different interpretations of the same data. In so far, neither i2c_read() nor make32() need to distinguish between them. The only possible pitfall is in a missing sign extension, as discussed before.
I see, that your conversion is working correctly, so the most plausible explanation is, that the analog input voltage to the ADC is different from what you expect, or the ADC is configured wrong somehow.
I previously suggested to check the converted ADC output code against the input voltage, as measured with an oscilloscope or multimeter. Did you ever try?
As PCM programmer correctly mentioned, the I2C standard requires to send a NAK with the last byte transferred. But usually, a missing NAK is ignored in read operations. The MCP3421 datasheet only tells, that ACK is required to continue the data transmission, not about consequences of missing NAK. It's better to follow the datasheet strictly and use the NAK, but I'm not surprized that it has no effect. |
Hi I did measure the voltage and its about 1353mV
and make32 returns correct value. |
|
 |
|
|
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
|