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 support@ccsinfo.com

How to calculate the analog value from read_Adc()

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Johnsecada
Guest







How to calculate the analog value from read_Adc()
PostPosted: Mon May 19, 2008 4:35 pm     Reply with quote

hello

I has 8-bit adc . I read the digital value (int8) from read_adc() function. I want to

I found this statement analog_value = (float)(read_adc()*5/256)
my reference voltage is 5V.
How it works ?
read_adc()returns 8-bits binary number like this 00000001 . When we do *5 the binary number should be automatically convert to int.

Could someone tell me what actually happen here?

10x all
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon May 19, 2008 5:03 pm     Reply with quote

Try this instead:
Code:
analog_value = (float)(my_read_adc()*5L)/256;
Johnsecada
Guest







I want to undestand how it works
PostPosted: Tue May 20, 2008 1:03 am     Reply with quote

Hello

i want to understand how it works ... Could you give me explanation.
What is 5L ?
RLScott



Joined: 10 Jul 2007
Posts: 465

View user's profile Send private message

Re: I want to undestand how it works
PostPosted: Tue May 20, 2008 5:30 am     Reply with quote

Johnsecada wrote:
Hello

i want to understand how it works ... Could you give me explanation.
What is 5L ?

The default size of type int is 8 bits. If you have read_adc()*5/256, then all integer arithmetic will be conducted with 8-bit values. But read_adc() * 5 can overflow the 8-bit limit of 255. 5L means 5 "Long", so that 5 is treated as an int16. This forces read_adc() also to be promoted to int16 before the "*" operation, so there will be no overflow. Another way to express it is with a cast:

read_adc() * (int16)5

Robert Scott
Real-Time Specialties
Johnsecada
Guest







Great
PostPosted: Tue May 20, 2008 6:16 am     Reply with quote

10x very much Smile
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
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