lebohangmaphothoane
Member
Can anyone help me find any mistake on the code and the circuit diagram. My aim is to light the LED if it is dark and turn it off if it is bright using LDR sensor. I have written the code and tested it on MPLAB and constructed the circuit diagram as shown on the attachment but the LED did not light, may be the circuit diagram is not complete or the code is wrong, i would be pleased if i can find solution. My code looks like this
Code:
;PROGRAM FUNCTION:light control for smart home
list p=16f887 ; list directive to define processor
#include <p16f887.inc> ; processor specific variable definitions
__config _CONFIG1, _LVP_OFF & _FCMEN_ON & _IESO_OFF & _BOR_OFF & _CPD_OFF & _CP_OFF & _MCLRE_ON & _PWRTE_ON & _WDT_OFF & _INTRC_OSC_NOCLKOUT
__config _CONFIG2, _WRT_OFF & _BOR21V
;Declarations:
NumH equ 20h
NumL equ 21h
org 0x0000
goto Start
;Program Start:
Start
call Init
Main
; Read_ADC
bsf ADCON0, GO_DONE ;initiate conversion
btfsc ADCON0, GO_DONE
goto $-1 ;wait for ADC to finish
movf ADRESH,W
andlw 0x03
movwf NumH
BANKSEL ADRESL
movf ADRESL,W
BANKSEL ADRESH
movwf NumL ;return result in NumL and NumH
;Check if it is dark or bright
btfss NumH,1 ;check the if output voltage exist 2.5 volts
goto led_on
bcf PORTB,2 ; turn 0ff the LED
goto Main
led_on
bsf PORTB,2 ;turn on the LED
goto Main
;Subroutines:
Init
;Initializing porta
BCF STATUS, RP0 ;
BCF STATUS, RP1 ; Bank0
CLRF PORTA ; Initialize PORTA clearing output data latches
BSF STATUS, RP0 ; Select Bank 1
MOVLW 0x06 ; Configure all pinss
MOVWF ADCON1 ; as digital inputs
MOVLW 0xCF ; Value used to initialize data direction
MOVWF TRISA ; Set RA<3:0> as inputs RA<5:4> as outputs TRISA<7:6>are always read as '0'
; Initializing portb
clrw ; zerp
movwf PORTB ; Ensure PORTB is zero before we enable it.
bsf STATUS,RP0 ; Select Bank 1
movlw 0xF0 ; Set port B bits 0-3 as outputs
movwf TRISB ; Set TRISB register.
bcf STATUS,RP0 ; Select Bank 0
;initializing ADC
;Set ADCON0
movlw b'10000001' ;FOSC/32, select AN0, A/D conversion not in progress, ADC is enabled
movwf ADCON0
;Set ADCON1
BANKSEL ADCON1
movlw b'10000000' ;Right justified, select VSS, select VDD
movwf ADCON1
BANKSEL ADCON0
return
END