Device = 18F43K22
Clock = 20
// some LCD options...
#option LCD_DATA = PORTD.4
#option LCD_RS = PORTD.2
#option LCD_EN = PORTD.3
// uses LCD and AD libraries...
Include "LCD.bas"
Include "ADC.bas"
Include "convert.bas"
// ****** change this to select between 0 (AN0, PORTA.0) or 5 (AN5, PORTE.0) ******
const ADC_CHAN = 5
// read the AD port and scale for 0 - 5 volts...
Function ADInAsVolt() As Word
result = (ADC.Read(ADC_CHAN) + 1) * 500 / 1024
End Function
// sampled AD value...
Dim ADVal As Word
Dim led As PORTA.0
dim led1 as portc.0
// setup IO pins
TRISA = %11111111
TRISB = %11111111
TRISC = %00000000
'TRISD = %00000000 // PORTD is set by LCD.BAS
TRISE = %001
// initialise and clear LCD...
DelayMS (500)
LCD.Cls
// ADC IN
if (ADC_CHAN = 0) then
Input(PORTA.0) // ADC IN on portA.0
ANSELA=%00000001 // ANSA0
elseif (ADC_CHAN = 5) then
Input(PORTE.0) // ADC IN on portE.0
ANSELE=%00000001 // ANSE0
endif
/////////////////////////////
'ADC setup
ADCON2 = %10101111
ADCON1 = %00000000
ADCON0 = (ADC_CHAN << 2) or %00000001 // chan + ADON
ADCON0.bits(1) = 1 // GO (start convert)
///////////////////////////////////
// main program loop...
While true
led1=1
DelayMS(1000)
led1=0
DelayMS(1000)
ADVal = ADInAsVolt()
LCD.MoveCursor (1,1)
LCD.Write("DC Volts = ", DecToStr(ADVal / 100), ".", DecToStr(ADVal, 2), " ")
Wend