' Picbasic Pro program to read pots on 16F877 ADC

' Define LCD pins
Define  LCD_DREG        PORTD
Define  LCD_DBIT        4
Define  LCD_RSREG       PORTE
Define  LCD_RSBIT       0
Define  LCD_EREG        PORTE
Define  LCD_EBIT        2
TRISA = %11111111
' Allocate variables
x       var     word
y       var     word
z       var     word
ch4     var     word
asd var word
        ADCON1 = %10000001;%10000011;4              ' Set PortA 0, 1, 3 to A/D inputs

        Low PORTE.1             ' LCD R/W line low (W)
        Pause 100               ' Wait for LCD to start

        Goto    mainloop        ' Skip subroutines


' Subroutine to read a/d converter
getad:
        Pauseus 50              ' Wait for channel to setup

        ADCON0.2 = 1            ' Start conversion
        Pauseus 50              ' Wait for conversion

        Return

' Subroutine to get pot x value
getx:
        ADCON0 = %01000001;$41            ' Set A/D to Fosc/8, Channel 0, On
        Gosub getad
        x.highbyte = ADRESH
        x.lowbyte = ADRESL
        Return

' Subroutine to get pot y value
gety:
        ADCON0 = %01001001;$49            ' Set A/D to Fosc/8, Channel 1, On
        Gosub getad
        y.highbyte = ADRESH
        y.lowbyte = ADRESL
        Return

' Subroutine to get pot z value
getz:
        ADCON0 = %01010001;$59            ' Set A/D to Fosc/8, Channel 3, On
        Gosub getad
        z.highbyte = ADRESH
        z.lowbyte = ADRESL
        Return
        
' Subroutine to get pot ch4 value
getch4:
;        ADCON0 = %01111001;$59            ' Set A/D to Fosc/8, Channel 3, On
        adcin 6,asd
        Gosub getad
        z.highbyte = ADRESH
        z.lowbyte = ADRESL
        Return

mainloop:
        Gosub   getx            ' Get x value
        Gosub   gety            ' Get y value
        Gosub   getz            ' Get z value
        Gosub   getch4           ' Get z value
        serout PORTC.6,2,["CH0=", #x, " CH1=", #y ,"   ","CH2=", #z, " CH4=", #asd,"   ",13]
;        Lcdout $fe, 1,  "CH0=", #x, " CH1=", #y ,"   "
;        lcdout $FE, $C0,"CH2=", #z, " CH4=", #ch4,"   " ' Send to LCD
        Pause   400             ' Do it about 10 times a second

        Goto    mainloop        ' Do it forever

        End

;        adcin
