analoge voltage reading

General discussion on mikroBasic.
Post Reply
Author
Message
sunny
Posts: 44
Joined: 26 Mar 2011 07:15

analoge voltage reading

#1 Post by sunny » 03 May 2011 11:39

Hi all.........
some one please tell me that pic reads analog voltage and what thay give figure as answer in the source code....
explaination is here with example
adc_rd = ADC_read(0); // get ADC value from 0th channel
LCD_Out(1,1,text); // print string a on LCD, 1st row, 1st column

tlong = (long)adc_rd * 5000; // covert adc reading to milivolts
tlong = tlong / 1023; // 0..1023 -> 0-5000mV

ch = tlong / 1000; // extract volts digit
LCD_Chr(1,3,48+ch); // write ASCII digit at 1st row, 3rd column
LCD_Chr_CP('.');



this code is from a volt meter...
i want to know why adc_rd is multiplied by 5000,,, and why absolute 5000 onlyyy.
at second step.,,, why it is divided by 1023,,, and absolute 1023....
thanks for answering.....

janni
Posts: 5373
Joined: 18 Feb 2006 13:17
Contact:

Re: analoge voltage reading

#2 Post by janni » 03 May 2011 12:56

sunny wrote:i want to know why adc_rd is multiplied by 5000,,, and why absolute 5000 onlyyy.
at second step.,,, why it is divided by 1023,,, and absolute 1023....
Result of analog voltage to 10-bit number conversion spans from 0 to 1023. If you divide it by 1023.0, you get 0..1. Multiplying this by input voltage span gives the momentary voltage at input of ADC (if the input voltage starts from 0 - otherwise one has also to subtruct the minimum voltage).

Problem is, such approach requires floating-point arithmetics which takes a lot of program memory and execution time. And one needs a complicated routine converting floating-point number to a string, if one wants to display the result on LCD.

Therefore, the best solution is to use fixed-point arithmetic, i.e. only integer multiplication and division. If the input voltage ranges from 0..5V, then multiplying the conversion result by 5000, and then dividing it by 1023, one gets a number ranging from 0 to 5000, i.e. from 0 to 5000mV, which is easy to display - also as 0.000 to 5.000 V.

There are even smarter (faster) algorithms further reducing the code size, like here.

sunny
Posts: 44
Joined: 26 Mar 2011 07:15

Re: analoge voltage reading

#3 Post by sunny » 04 May 2011 19:39

thanks very very thanks....
now can u help me in making led moving msg using pic16f877 or pic16f628 but source code should in mikroC.... because i am using mnikroC
thanks.............

janni
Posts: 5373
Joined: 18 Feb 2006 13:17
Contact:

Re: analoge voltage reading

#4 Post by janni » 04 May 2011 23:18

Please post in mC PRO forum then. I'm sure somebody may direct you to posts on this subject - or you could search this, or old mC forum for yourself. I'm not an enthusiast of C :( so I'd rather avoid writing code in mC.

sunny
Posts: 44
Joined: 26 Mar 2011 07:15

Re: analoge voltage reading

#5 Post by sunny » 09 May 2011 18:11

dear which language u use............

janni
Posts: 5373
Joined: 18 Feb 2006 13:17
Contact:

Re: analoge voltage reading

#6 Post by janni » 10 May 2011 14:00

mikroPascal. mC offers little more than mP and I use assembly for critical parts anyway.
I use C or C++ only when I have to (on platforms where there is no other choice :( ).

Post Reply

Return to “mikroBasic General”