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 CCS Technical Support

need help in decimal to binary conversion
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
aruna1



Joined: 14 Oct 2008
Posts: 103

View user's profile Send private message

need help in decimal to binary conversion
PostPosted: Sun Jan 02, 2011 6:46 am     Reply with quote

Hi
I'm trying to read analog voltage using MCP3421 ADC chip. it is a 18 bit adc chip.it uses I2C to communicate.

when reading ADC value it send 18 bits of data as 3 sets of bytes.I captured them.since I2C_read() function returns values in integer format, 18 bits of data looks like this
byte0 = 1
byte1 = 149
byte2 = 81

so if i convert each byte to binary,then merge them and finally convert merged binary value to decimal I get the correct ADC value.(i use a calculator and did it manually)

my question is how to convert these values to binary and merged converted data to recover original 18bit value and hence calculate the adc reading.

is there any built in function for this? perhaps a ready-made code part?
thank you very much
ezflyr



Joined: 25 Oct 2010
Posts: 1019
Location: Tewksbury, MA

View user's profile Send private message

PostPosted: Sun Jan 02, 2011 7:16 am     Reply with quote

Hi,

Yes, look at the Make16 function, it's ready-made for this type of operation.

John
aruna1



Joined: 14 Oct 2008
Posts: 103

View user's profile Send private message

PostPosted: Sun Jan 02, 2011 7:47 am     Reply with quote

ezflyr wrote:
Hi,

Yes, look at the Make16 function, it's ready-made for this type of operation.

John

hi
thank you ezflyr. I will try.
Ttelmah



Joined: 11 Mar 2010
Posts: 19535

View user's profile Send private message

PostPosted: Sun Jan 02, 2011 8:13 am     Reply with quote

You will actually need the make32 function, not make16, since the value you have is 18bits. Just put a 'zero' for the top byte.
You'll need to hold the result in an int32, since CCS, doesn't intrinsically have any numeric 'types' between an int16, and an int32.

Best Wishes
aruna1



Joined: 14 Oct 2008
Posts: 103

View user's profile Send private message

PostPosted: Sun Jan 02, 2011 11:10 am     Reply with quote

Ttelmah wrote:
You will actually need the make32 function, not make16, since the value you have is 18bits. Just put a 'zero' for the top byte.
You'll need to hold the result in an int32, since CCS, doesn't intrinsically have any numeric 'types' between an int16, and an int32.

Best Wishes

It didn't work with make32.

Here is my code,
output value of make32 for (data0=1,data1=81,data2=142) is 65617.
This is incorrect.
Then I tried three 8bit values 255,255,255 manually and it gave correct value.
Can you point me the error
Code:

#include "E:\My Electronic projects\gyro test\i2c\main.h"

int ack[3];
int16 data[4];
int32 value;
int16 data0;
int16 data1;
int16 data2;
int16 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
   //data[4]=i2c_read(0);
   i2c_stop();
   delay_ms(10);
   value = make32(data0,data1,data2);
 
   
   
   printf("\n data0 = %Ld\r",data0);
   printf("\n data1 = %Ld\r",data1);
   printf("\n data2 = %Ld\r",data2);
   printf("\n data3 = %Ld\r",data3);
   printf("\n value = %Ld\r",value);
   printf("\n--------------------------------------------------------\r");
   }
   }

   // TODO: USER CODE!!

}
ezflyr



Joined: 25 Oct 2010
Posts: 1019
Location: Tewksbury, MA

View user's profile Send private message

PostPosted: Sun Jan 02, 2011 11:56 am     Reply with quote

Hi,

Yeah, I (obviously) meant Make32. I was hoping to alert you to these built in CCS functions, and then have you consult the CCS manual for further assistance.....

I think you've got the order of your 8 bit integer values reversed. Note that the manual says 'MSB first', and the example shown clearly illustrates that order.....

Your 'test case' is unfortunate because '255' yields the same result with Make32 in any order!!

Good Luck!

John
aruna1



Joined: 14 Oct 2008
Posts: 103

View user's profile Send private message

PostPosted: Sun Jan 02, 2011 11:58 am     Reply with quote

ezflyr wrote:
Hi,

Yeah, I (obviously) meant Make32. I was hoping to alert you to these built in CCS functions, and then have you consult the CCS manual for further assistance.....

I think you've got the order of your 8 bit integer values reversed. Note that the manual says 'MSB first', and the example shown clearly illustrates that order.....

Your 'test case' is unfortunate because '255' yields the same result with Make32 in any order!!

Good Luck!

John

hi
order is correct
device sends MSB first and LSB last
so data0 contains MSB & data2 contains LSB
gpsmikey



Joined: 16 Nov 2010
Posts: 588
Location: Kirkland, WA

View user's profile Send private message

PostPosted: Sun Jan 02, 2011 12:00 pm     Reply with quote

A quick read shows you don't have the same byte order in your example that is wrong as you listed above (1,81,142) vs (1,149,81) also, from the example in the compiler manual where they show "x = make32(1,2,3,4); // x is 0x01020304" it looks like the default is hex - are the numbers you are using decimal ???

mikey
_________________
mikey
-- you can't have too many gadgets or too much disk space !
old engineering saying: 1+1 = 3 for sufficiently large values of 1 or small values of 3
aruna1



Joined: 14 Oct 2008
Posts: 103

View user's profile Send private message

PostPosted: Sun Jan 02, 2011 12:03 pm     Reply with quote

gpsmikey wrote:
A quick read shows you don't have the same byte order in your example that is wrong as you listed above (1,81,142) vs (1,149,81) also, from the example in the compiler manual where they show "x = make32(1,2,3,4); // x is 0x01020304" it looks like the default is hex - are the numbers you are using decimal ???

mikey

hi
It seems second order I listed is wrong (typing error).

And data is integer.
ezflyr



Joined: 25 Oct 2010
Posts: 1019
Location: Tewksbury, MA

View user's profile Send private message

PostPosted: Sun Jan 02, 2011 12:21 pm     Reply with quote

Hi,

I haven't used this function in a while, but you've got everything right there, why not just play with it and see if it works the way you expect if you pass the function the hex representation of your numerical values? Just hard code some values for test purposes and see if it works.

BTW, there is nothing magic about any of the 'Make' functions. You can roll your own equivalents very easy to do exactly what YOU need to do. You've really got to get in the habit of playing around with this stuff, and trying different things!!

John
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Sun Jan 02, 2011 12:28 pm     Reply with quote

Your code is supposed to give correct data in 18 Bit mode of the ADC, if data0 to data2 are int8 data. You have defined them as
int16, which can't work.
Code:
   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();
   value = make32(data0,data1,data2);

Code:
it looks like the default is hex - are the numbers you are using decimal ???

make32() is assembling int8 or int16 data to int32. It doesn't care for decimal, hex or whatever. Binary data are just binary data.
aruna1



Joined: 14 Oct 2008
Posts: 103

View user's profile Send private message

PostPosted: Sun Jan 02, 2011 12:30 pm     Reply with quote

FvM wrote:
Your code is supposed to give correct data in 18 Bit mode of the ADC, if data0 to data2 are int8 data. You have defined them as
int16, which can't work.
Code:
   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();
   value = make32(data0,data1,data2);

Code:
it looks like the default is hex - are the numbers you are using decimal ???

make32() is assembing int8 or int16 data to int32. It doesn't care for decimal, hex or whatever. Binary data are just binary data.

hi i tried int8 but final value begin to give negative value.i tried making it unsigned int8 but it didnt solve.so i changed it to int16 Embarassed
gpsmikey



Joined: 16 Nov 2010
Posts: 588
Location: Kirkland, WA

View user's profile Send private message

PostPosted: Sun Jan 02, 2011 12:40 pm     Reply with quote

Quote:
make32() is assembling int8 or int16 data to int32. It doesn't care for decimal, hex or whatever. Binary data are just binary data.


Well, sort of - unless the compiler is thinking you are giving it a hex number when you say "81" and you are thinking decimal "81" (0x51). It is those "out of sight helps" that will bite you :-) If the byte that was listed as "81" was printed using the %ud, then you have converted it for *your* viewing pleasure - if you stuff that back into something that is expecting hex, you will not get the result you expect. That said, 01010001 in binary is indeed 0x51 hex or 81 decimal as you indicated - it is the sneaky translation that can get you ... (been there, done that ... felt foolish when I realized ! )

mikey
_________________
mikey
-- you can't have too many gadgets or too much disk space !
old engineering saying: 1+1 = 3 for sufficiently large values of 1 or small values of 3
Ttelmah



Joined: 11 Mar 2010
Posts: 19535

View user's profile Send private message

PostPosted: Sun Jan 02, 2011 1:00 pm     Reply with quote

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
gpsmikey



Joined: 16 Nov 2010
Posts: 588
Location: Kirkland, WA

View user's profile Send private message

PostPosted: Sun Jan 02, 2011 1:50 pm     Reply with quote

Evening meal ?!? Not even time for hot buttered rum here yet Very Happy ... well, maybe it is !

mikey
_________________
mikey
-- you can't have too many gadgets or too much disk space !
old engineering saying: 1+1 = 3 for sufficiently large values of 1 or small values of 3
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
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