PDA

View Full Version : Analog Digital Conversion ??



gunayburak
- 25th December 2012, 00:32
Hi , I'm new in this forum , and to the pic programming too ...

I was trying to understand a code already written by an anonymous programmer to manage an ADC of a PIC ..
But there is that part of which function that I can't understand , particularly consisting a mathematical code ...

BTW It's basically a voltmeter measuring the voltage on a connected pot or sth

Here is the program ;
--------------------------------------------------------------------------------------------------------------

TRISA=000001
TRISB=0
TRISC=0
TRISD=0

@ DEVICE pic16F877
@ DEVICE pic16F877, WDT_on
@ DEVICE pic16F877, PWRT_ON
@ DEVICE pic16F877, PROTECT_OFF
@ DEVICE pic16F877, XT_OSC
'-------------------------------------------------------------------------------
DEFINE LCD_DREG PORTB
DEFINE LCD_DBIT 4
DEFINE LCD_EREG PORTB
DEFINE LCD_EBIT 3
DEFINE LCD RWREG PORTB
DEFINE LCD_RWBIT 2
DEFINE LCD_RSREG PORTB
DEFINE LCD_RSBIT 1
DEFINE LCD_BITS 4
DEFINE LCD_LINES 2
'-------------------------------------------------------------------------------
DEFINE ADC_BITS 10
DEFINE ADC_CLOCK 3
DEFINE ADC_SAMPLEUS 100
'-------------------------------------------------------------------------------
ADCON1=001110
'-------------------------------------------------------------------------------
HAM var word
VOLT var word
MVOLT var byte
'-------------------------------------------------------------------------------
LOW PORTB.2
LCDOUT $FE,1
PAUSE 200
'-------------------------------------------------------------------------------


START:
ADCIN 0,HAM

CTRL:

IF ADCON0.2=1 THEN CTRL

LCDOUT $FE,1," HAM =",# HAM
HAM=HAM+1


VOLT=(HAM*/ 1250)/100
MVOLT=VOLT // 10

VOLT=VOLT/10

LCDOUT $FE,$C0," VOLT=",#VOLT,",",# MVOLT
PAUSE 500
GOTO START




------------------------------------------------------------------------------------
The part that I don't understand is the code scripted as below ;

HAM=HAM+1


Volt=(ham */ 1250)/100
Mvolt=Volt // 10

Volt=Volt/10

--------------------
NOW HERE ARE MY QUESTIONS ;

1. Where does this decimal "1" come from and why do we add it to the "ham" variable
2. Where does this 1250 , 100 come from ?
3. What are these operators written as "//" and "*/"

I'd be so appreciated if you could help me with these ....
Thanks in advance ...

rsocor01
- 25th December 2012, 06:55
Hi,

Welcome to the forum. The operators "*/" and "//" can be found in the 'Math Operators' section of the PBP manual.

http://melabs.com/resources/pbpmanual/

Now, about your other questions, many of us don't have the good habit of writing a good comment after every line of code. So, I am not sure what the programmer wanted to do there. Maybe somebody else here has a better answer.

Robert

mackrackit
- 26th December 2012, 05:55
If we knew what this code was written for we might be able to answer your other questions, but random code ...

aratti
- 28th December 2012, 16:52
1 is clearly an offset. The reason for using it is unknown, it could depend on the necessity to alline the reading with other device

1250 is a coefficient for converting the ADC reading into volts. The actual coefficient should be 12.5 but due to the integer math limitation the author did multiply it by 100 to obtain 1250 and then he devide the product by 100.

All these numbers are related to a specific application, so you should ignore them, since very likely, your application will require different numbers as coefficient and eventually an offset.

Cheers

Al.