' Convert AN0 and display the result on an LCD in Hex once a second
'
' The watchdog timer should be DISABLED


' ---------------------------------------------------------------
' LCD Variables required by <libs\interface\4BitLCD.bas>

Dim LCD_D4 @ PORTD.0 As Boolean, LCD_D4_DIR @ TRISD.0 As Boolean
Dim LCD_D5 @ PORTD.1 As Boolean, LCD_D5_DIR @ TRISD.1 As Boolean
Dim LCD_D6 @ PORTD.2 As Boolean, LCD_D6_DIR @ TRISD.2 As Boolean
Dim LCD_D7 @ PORTD.3 As Boolean, LCD_D7_DIR @ TRISD.3 As Boolean
Dim LCD_E  @ PORTA.1 As Boolean, LCD_E_DIR  @ TRISA.1 As Boolean
Dim LCD_RW @ PORTA.2 As Boolean, LCD_RW_DIR @ TRISA.2 As Boolean
Dim LCD_RS @ PORTA.3 As Boolean, LCD_RS_DIR @ TRISA.3 As Boolean

Include <libs\interface\4BitLCD.bas>	' load the LCD library

' The program starts here
Sub main()
   Call init()			' initialize everything here
	
   While 1	
      GO=1			' start ADC conversion
      While GO=1: End While	' wait on the ADC
      Call updateDisplay()	' update the display		
      delayS(1)			' wait for 1 second			
      End While
	
End Sub

Sub init()

   'ADC Configuration
   '=================
   ' Processor: 18F452
   ' Fosc: Fosc/32
   ' Channel: 0
   ' ADC: ON
   ' Justify: Right
   ' Analog Channels: AN0
   ' Digital Channels: AN1,AN2,AN3,AN4,AN5,AN6,AN7
   ' Verf+: Vdd
   ' Vref-: Vss
   ADCON0=0x81
   ADCON1=0x8E
	  
   Call LCDinit()		' initialize the LCD
	  
End Sub

Sub updateDisplay()
   Dim s(10) As String
	
   Call LCDcls()		' clear the LCD
   Call strHex(ADRES,s)		' convert the ADC value to hex
	
   Call LCDputs("ADC: ") : Call LCDputs(s)	' display the value
End Sub