I have written a code for ADC conversion from 0-5v, but seem not to work. Can someone help me where I am wrong. Here is the code attached is the circuit diagram Cheers
Define LCD_DBIT 4
Define LCD_RSREG PORTB
Define LCD_RSBIT 3
Define LCD_EREG PORTB
Define LCD_EBIT 2
Define LCD_EBITS 4
Define LCD_LINES 2
' Initialise ADC
ADCON1 = %10000100 ' Set PORTA analog and RIGHT justify result
ADCON0 = %01000001 ' Configure and turn on A/D Module channel o Fosc 8
'Pause 500 ' wait .5 second
'Declear variables
val VAR WORD 'final calculated adc valuesWORD' final calculated adc values
volt VAR WORD ' scaled value real part
voltd1 VAR WORD ' scaled value first decimal place
voltd2 VAR WORD ' scaled value second decimal place
i var byte ' counter
'************************************************* ******************
' Main program code
Start:
GoSub ADC
pause 500
GoTo Start
'************************************************* *****************
ADC
Val = 0 ' Value returned is the average of 5 conversions
For i = 0 TO 4
ADCON0.2 = 1 ' Start convertion
NoteDone:
pause 1
IF ADCON0.2 = 1 Then NoteDone
val = val + ((ADRESH * 256)+(ADRESL))
nEXT I
VAL = VAL/5
' Calculate Volatage to 2 decimal places
volt = val * 5/1024
voltd1 = ( val * 5//1024) * 10/1024
Voltd2 = ((val *5 //1024) * 10//1024) * 10/1024
LCDOut $FE,$C0, # Val, "=", #volt, ".", # voltd1, # voltd2, "Vdc"
Return
Bookmarks