LIST P=16F877A
INCLUDE "P16F877a.inc"
; __CONFIG _CP_OFF & _WDT_OFF & _HS_OSC & _PWRTE_ON
status EQU 0x03
TRISA EQU 0X85
PORTA EQU 0X05
TRISB EQU 0X86
PORTB EQU 0X06
CHK1 EQU 0X21
CNT1 EQU 0CH
CNT2 EQU 0DH
count EQU 0x26
NumH equ 0x22
CounterA equ 0x23
CounterB equ 0x24
CounterC equ 0x25
cblock
d1
d2
d3
d4
endc
org 0x0000
nop
BSF STATUS,RP0
MOVLW H'FF'
MOVWF TRISA
movlw H'00';
movwf TRISB;
banksel PORTA
BCF STATUS,RP0
clrf PORTB
banksel ADCON1
movlw b'00000000' ;set adc left justify
movwf ADCON1
banksel ADCON0
movlw b'10000001' ;set 20Mhz,ANo input
movwf ADCON0
goto start
getAD movlw 007
movwf count
down decfsz count
goto down
bsf ADCON0,GO
btfsc ADCON0,GO
goto $-1
return
delay10s movlw 0xF0 ;DELAY 50SEC
movwf d1
movlw 0xDB
movwf d2
movlw 0xA8
movwf d3
movlw 0x02
movwf d4
Delay_0
decfsz d1, f
goto $+2
decfsz d2, f
goto $+2
decfsz d3, f
goto $+2
decfsz d4, f
goto Delay_0
;5 cycles
goto $+1
goto $+1
nop
return
Sub1
btfss status,0 ;bit set if carry=1
goto Sub2 ; carry no =1
bsf PORTB,1 ;turn on LED1
call delay10s
BCF PORTB,1
NOP
NOP
NOP
NOP
NOP
RETLW 0x00
Sub2 btfsc status,0 ;bit set if carry =0
goto start ; carry no = 0
bsf PORTB,2 ;LED 2 on
call delay10s
BCF PORTB,2 ;clear all portb
NOP
NOP
NOP
NOP
NOP
RETLW 0x00
start call getAD ; get adc result
movf ADRESH,w
movwf NumH
sublw D'8' ;sub NumH with 8
btfss status,2 ;bit set if w=0
goto Sub1 ;if w np = 0
bsf PORTB,0 ;on led 0
call delay10s
BCF PORTB,0
NOP
NOP
NOP
NOP
NOP
goto start
end