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

Discuss PIC and electronic related things

Moderators: David Barker, Jerry Messina

Post Reply
dbachman
Posts: 4
Joined: Thu Jun 26, 2008 1:39 pm
Location: Idaho

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

Post by dbachman » Fri Oct 03, 2008 6:02 pm

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
Last edited by dbachman on Sat Oct 04, 2008 3:11 am, edited 1 time in total.

rmteo
Posts: 237
Joined: Fri Feb 29, 2008 7:02 pm
Location: Colorado, USA

Post by rmteo » Fri Oct 03, 2008 6:23 pm

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

dbachman
Posts: 4
Joined: Thu Jun 26, 2008 1:39 pm
Location: Idaho

Post by dbachman » Fri Oct 03, 2008 6:27 pm

No, I have not seen this. I will give it a try though.

Thanks

dbachman
Posts: 4
Joined: Thu Jun 26, 2008 1:39 pm
Location: Idaho

Post by dbachman » Fri Oct 10, 2008 2:23 pm

Why do you suppose the above code does not work though?

Raistlin
Registered User
Registered User
Posts: 69
Joined: Tue Apr 01, 2008 1:13 pm

Post by Raistlin » Mon Nov 09, 2009 4:12 pm

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
If you can read this you are too close

Post Reply