LIST p=16f877a
; Include file, change directory if needed
INCLUDE "p16f877A.inc"
__CONFIG _XT_OSC & _WDT_OFF & _PWRTE_ON & _CP_OFF
ERRORLEVEL -302
; Start at the reset vector
ORG 0x000
NOP
Start
BANKSEL PORTD
CLRF PORTD ;Clear PORTD
MOVLW B'01000001' ;Fosc/8, A/D enabled, Sample Channel 0
MOVWF ADCON0
BANKSEL OPTION_REG
MOVLW B'10000111' ;TMR0 prescaler, 1:256
MOVWF OPTION_REG
CLRF TRISD ;PORTD all outputs
MOVLW B'00001110' ;Left justify, 1 analog channel
MOVWF ADCON1 ;VDD and VSS references
BANKSEL PORTD
Main
BCF INTCON,T0IF
Loop
BTFSS INTCON,T0IF ;Wait for Timer0 to timeout
GOTO Loop
BSF ADCON0,GO ;Start A/D conversion for channel 0
Wait
BTFSS PIR1,ADIF ;Wait for conversion to complete
GOTO Wait
MOVF ADRESH,W ;Write A/D result to PORTD
MOVWF PORTD ;LEDs
BCF PIR1,ADIF ;Clear the completion flag
GOTO Loop ;Do it again
END