Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Help me fix my code to receive 16bits from the ADC

Status
Not open for further replies.

yviswanathbe

Full Member level 4
Joined
Jun 14, 2007
Messages
221
Helped
10
Reputation
20
Reaction score
6
Trophy points
1,298
Activity points
3,066
regarding c code

Hi Friends,

I am using TI ADS8344 16 bit ADC in my application.
Everything is working fine.But when comes to Result i will be getting 3 bytes from the ADC, i need to use some extract and reassemble technique to get the actual result.

I have done using "bdata" and its working too.but i feel the code which i have written is bigger and complicated.
I am attaching my code, some body can help me with some logic.



//Accessing DataByte2 and DataByte3 in Bit addressable memory location
char bdata msb_databyte2;
char bdata msb_databyte3;

//Extracting DataByte2 to individual bits
sbit msbbyte2_7 = msb_databyte2^7;
sbit msbbyte2_6 = msb_databyte2^6;
sbit msbbyte2_5 = msb_databyte2^5;
sbit msbbyte2_4 = msb_databyte2^4;
sbit msbbyte2_3 = msb_databyte2^3;
sbit msbbyte2_2 = msb_databyte2^2;
sbit msbbyte2_1 = msb_databyte2^1;
sbit msbbyte2_0 = msb_databyte2^0;

void Adc_Convert (UC adc_Ch)
{
UC c1=0;
NSSMD0 = 0;
delay_ms(10);
ADC_Write(adc_Ch);
adc1stbyte=ADC_Read ();
adc2ndbyte=ADC_Read ();
adc3rdbyte=ADC_Read();
NSSMD0 = 1;
delay_ms(10);
temp=(adc1stbyte << 1);
msb_databyte2=adc2ndbyte;
adc_highbyte=(temp | msbbyte2_7);
temp=extract_combine();
msb_databyte3=adc3rdbyte;
adc_lowbyte= temp| msbbyte3;

}
UC extract_combine(void)
{
int i=0,j=128;
bits[7]=msbbyte2_6;
bits[6]=msbbyte2_5;
bits[5]=msbbyte2_4;
bits[4]=msbbyte2_3;
bits[3]=msbbyte2_2;
bits[2]=msbbyte2_1;
bits[1]=msbbyte2_0;
temp1=bits[7]*128+bits[6]*64+bits[5]*32+bits[4]*16+bits[3]*8+bits[2]*4+bits[1]*2;
return temp1;
}

Thanks and Regards,
Viswanath
 

Re: regarding c code

Try this:
Code:
   unsigned short int adc_result;
   adc_result = (unsigned short int)ADC_Read() << 8 | ADC_Read();
   adc_result <<= 1;
   if( ADC_Read() & 0x80 ) adc_result |= 1;
 
Re: regarding c code

Hi yager,

Thanks for the reply.
I will try and reply back.

Thanks and Regards,
Viswanath
 

Re: regarding c code

Hi Yager,

its working.

Thanks alot.

Regards,
Viswanath
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top