Page 1 of 1

Can anyone tell me why this ADC code does not work?

Posted: Fri Oct 03, 2008 6:02 pm
by dbachman
I have made a few changes but it still doesn't appear to convert. Can anyone help me find a soultion? Is the value in ADRESH Binary? If so, how would the If statement work? I am new to this but chugging along. :shock:

Thanks

Here is the new code:


Device = 18f1320
//clock = 4
Dim Value As Integer

//Subroutine to read a/d converter
Sub getad()
DelayUS(50) ' Wait for channel to setup
ADCON0.2 = 1 ' Start conversion
DelayUS(50) ' Wait for conversion

End Sub


//Subroutine to get pot value
Sub getvalue()
ADCON0 = $D '(00001101) Set A/D to On, Channel 3, Vdd/Vss
ADCON2 = $b1 '(10110001) Set A/D to Fosc/8, 6Tad, right justified
getad
Value = ADRESH
End Sub

TRISA = $8 '(00001000) Set RA3 as input
ADCON1 = $8 '(00001000) Configure AN3 to Digital
Value = 0

While
getvalue ' Get value
Wend

If Value > 10 Then High(PORTB.3)
EndIf

Posted: Fri Oct 03, 2008 6:23 pm
by rmteo
Have you tried using/modifying the example program provided with SF?

Code: Select all

// if device and clock are omitted, then the compiler defaults to 
// 18F452 @ 20MHz - they are just used here for clarity...
device = 18F452
clock = 20

// some LCD options...
#option LCD_DATA = PORTD.4
#option LCD_RS = PORTE.0
#option LCD_EN = PORTE.1

// uses LCD and AD libraries...
include "LCD.bas"
include "ADC.bas"
include "convert.bas"
          
// read the AD port and scale for 0 - 5 volts...
function ADInAsVolt() as word
   result = (ADC.Read(0) + 1) * 500 / 1024
end function

// sampled AD value...
dim ADVal as word

// initialise and clear LCD...
ADCON1 = $07       // PORTE as digital (LCD)
TRISA.0 = 1        // configure AN0 as an input 
ADCON1.7 = 1       // set analogue input on PORTA.0 
delayms (500)
LCD.Cls

// main program loop...
while true
   ADVal = ADInAsVolt
   LCD.MoveCursor (1,1)
   LCD.Write("DC Volts = ", DecToStr(ADVal / 100), ".", DecToStr(ADVal, 2), " ")
   delayms(250)
wend

Posted: Fri Oct 03, 2008 6:27 pm
by dbachman
No, I have not seen this. I will give it a try though.

Thanks

Posted: Fri Oct 10, 2008 2:23 pm
by dbachman
Why do you suppose the above code does not work though?

Posted: Mon Nov 09, 2009 4:12 pm
by Raistlin
While
getvalue ' Get value
Wend

If Value > 10 Then High(PORTB.3)
EndIf
How does the code escape for the while wend loop ?
to me you have a loop reading a value but no way to act on it.
try

While
getvalue ' Get value


If Value > 10 Then High(PORTB.3)
else low(portb.3)
EndIf

Wend