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
Posted: Mon May 19, 2008 5:03 pm
Try this instead:
Code:
analog_value = (float)(my_read_adc()*5L)/256;
Johnsecada Guest
I want to undestand how it works
Posted: Tue May 20, 2008 1:03 am
Hello
i want to understand how it works ... Could you give me explanation.
What is 5L ?
RLScott
Joined: 10 Jul 2007 Posts: 465
Re: I want to undestand how it works
Posted: Tue May 20, 2008 5:30 am
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:
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