• Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Light control system for smart home

Status
Not open for further replies.
HI everyone. I am to build a light control system. first of all i want to program using assembly language. the tools that iam going to use are a light sensor(0..10 LUX Description D0141i), LEDs, PIC16F877A. the system should work like this, during day light or when it is light/bright, the LEDs must be off and when it is dark or during the night, the LEDs must be on. The light sensor should help to determine when it is dark or light. PIC16F877A must take the output from the light sensor and then it should turn the LEDs on or off depending on the input that comes from the light sensors. Can any one help me with the code, written in assembly language.

hear is what i tried but i could not finish it

;PROGRAM FUNCTION:light control for smart home

list P=16F877a
include "C:\Program Files\Microchip\MPASM Suite\p16f5x.inc"
__config _CP_OFF & _WDT_OFF & _RC_OSC

;Declarations:

porta equ 05
portb equ 06





;Subroutines:
Init
movlw b'10000001';
movwf ADCON0 ;
BANKSEL ADCON1
movlw b'10000101'
movwf ADCON1
BANKSEL ADCON0


; Initializing porta
BCF STATUS, RP0 ;
BCF STATUS, RP1 ; Bank0
CLRF PORTA ; Initialize PORTA by
; clearing output
; data latches
BSF STATUS, RP0 ; Select Bank 1
MOVLW 0x06 ; Configure all pins
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'.
return
;Program Start:

Start
call Init ;sets the values of ADCON0 and ADCON1
;initialize porta




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
return
 
i know how to turn on or off the LED, i left it there because i was stack. since the out put range of the light sensor is 0V to 5V. lets asume if the sensor output a range of 0 t0 2.5v that implies it is dark and the LEDs have to be on and if it output a voltage above 2.5 v then that implies it is bright and the LEDs have to be on. since in that code i have converted the analog values to digital and the value is stored in the file register NumL. I wanted to make an if statement (if the converted value in the NumL register is between 0v and 2.5v then bsf porta,4 this means that LED on RA4 would be ON. else bcf porta,4 meanig that the LED must be off) i have a problem of making this few steps in assembly language, can u help me out?
 
i know how to turn on or off the LED, i left it there because i was stack. since the out put range of the light sensor is 0V to 5V. lets asume if the sensor output a range of 0 t0 2.5v that implies it is dark and the LEDs have to be on and if it output a voltage above 2.5 v then that implies it is bright and the LEDs have to be OFF. since in that code i have converted the analog values to digital and the value is stored in the file register NumL. I wanted to make an if statement (if the converted value in the NumL register is between 0v and 2.5v then bsf porta,4 this means that LED on RA4 would be ON. else bcf porta,4 meanig that the LED must be off) i have a problem of making this few steps in assembly language, can u help me out?
 
for testing whether the bit is set , iam familiar with this instruction btfss porta,0 (this test whether the bit RA0 of port a is set) and usually the port is an 8 bit port, so im wondering how im going to check if bit 10 of a file register NumL is set. should i write btfss NumL,10
 

birdman0_o

Active Member
When the ADC is complete it stores the result in ADRESH and ADRESL based on which you set as the Most significant byte, you will just need to test the 2nd bit

I.e.
BTFSS ADRESH,1
 
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

looking at that code. to my understand i think the final result or the number is stored in NumL which is the one where iam supposed to check the 10nth bit is that correct?
 
can u have a look at this code and the corrections i have made and tell me if u think it will work


;PROGRAM FUNCTION:light control for smart home

list P=16F877a
include "C:\Program Files\Microchip\MPASM Suite\p16f5x.inc"
__config _CP_OFF & _WDT_OFF & _RC_OSC

;Declarations:

porta equ 05
portb equ 06





;Subroutines:
Init
movlw b'10000001';
movwf ADCON0 ;
BANKSEL ADCON1
movlw b'10000101'
movwf ADCON1
BANKSEL ADCON0


; Initializing porta
BCF STATUS, RP0 ;
BCF STATUS, RP1 ; Bank0
CLRF PORTA ; Initialize PORTA by
; clearing output
; data latches
BSF STATUS, RP0 ; Select Bank 1
MOVLW 0x06 ; Configure all pins
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'.
return


;Program Start:

Start
call Init ;sets the values of ADCON0 and ADCON1
;initialize porta




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


btfss NumH,1
goto led_on
bcf porta,4 ; turn 0ff the LED
goto Main
led_on
bsf porta,4 ;turn on the LED

goto Main


END
 

birdman0_o

Active Member
1) You don't need to define PORTA and PORTB
2) Subroutines should normally be at the end of the code.
3) What kind of oscillator are you using?
4) Post your code like this from now on:

Code:
;PROGRAM FUNCTION:light control for smart home

list P=16F877a
include "C:\Program Files\Microchip\MPASM Suite\p16f5x.inc"
__config _CP_OFF & _WDT_OFF & _RC_OSC

;Declarations:

porta equ 05
portb equ 06





;Subroutines:
Init
movlw b'10000001';
movwf ADCON0 ;
BANKSEL ADCON1
movlw b'10000101'
movwf ADCON1
BANKSEL ADCON0


; Initializing porta
BCF STATUS, RP0 ;
BCF STATUS, RP1 ; Bank0
CLRF PORTA ; Initialize PORTA by
; clearing output
; data latches
BSF STATUS, RP0 ; Select Bank 1
MOVLW 0x06 ; Configure all pins
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'.
return


;Program Start:

Start
call Init ;sets the values of ADCON0 and ADCON1
;initialize porta




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


btfss NumH,1
goto led_on
bcf porta,4 ; turn 0ff the LED
goto Main
led_on
bsf porta,4 ;turn on the LED

goto Main


END
The code looks like it might work!
 
I was not sure about the oscillator which is suitable for use, but i have found that crystal oscillator is most accurate and reliable because it is not affected by external variables. so i will choose to use it. That means i will use this instruction _config_XT_OSC instead of _config_RC_OSC
 
I am not sure on the configuration, should i disable watchdog timer or not? and could u explain what im doing when i write this instruction __config _CP_OFF & _WDT_OFF & _RC_OSC may i did not understand it very well from where i read it and could u show me how to set or declare the oscillator for PIC16F877A, i think i have to use it, like i told u i am not sure which oscillator i must use
 

gaspode42

Member
I am not sure on the configuration, should i disable watchdog timer or not? and could u explain what im doing when i write this instruction __config _CP_OFF & _WDT_OFF & _RC_OSC may i did not understand it very well from where i read it and could u show me how to set or declare the oscillator for PIC16F877A, i think i have to use it, like i told u i am not sure which oscillator i must use
__config_CP_OFF - No code protection, this is correct
_WDT_OFF - No watchdog timer, this is also OK
_RC_OSC - Use external R/C oscillator (R/C=resistor & capacitor) - unfortunately nowhere in the thread does it give your hardware configuration so I don't know if this is correct or not. If you have an external crystal it would either be XT or HS (depending on your crystals frequency) :)
 
Last edited:
output voltage of the light sensor i am going to use ranges from 0v to 5v and when converting this number to digital using a ten bit number. 5v represent 1023 and 2.5v represent 512 and 0v represent 0, so follows the other numbers. i want the code to work like this, if output voltage from the sensor ranges from 2.5v to 0v then i should use ADC to get a number from 512 to 0 and then turn the LED on. else if it ranges from 5v to 2.5v then i sholud use ADC to get a number from 1023 to 512 and then turn off the LED. I know how to convert analog to digital but i do not know how to use that digital number to turn on or off the LED. if it was an eight bit number that would be easy for me because all the bits are stored in the same register. so for a ten bit number, eight bit are stored in one register and the other two are stored in a different register, therefore i do not know how to turn on or off the LED using ten bit number, please help me with the instructions.
 

gaspode42

Member
Hi

This had already been explained to you by Birdman0_o in this thread. To test if you number is > 512 you need to test the 10th digit. This you will fin in the 2nd place of ADRESH as follows:



You can test with either BTFSC or BTFSS bepending on if you are testing for it being high or low.
 

Attachments

Last edited:
Status
Not open for further replies.

EE World Online Articles

Loading
Top