Electronic Projects, forums and more.

Go Back   Electronic Circuits Projects Diagrams Free > Electronics Categories > Micro Controllers


Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc.

Reply
 
Tools
Old 16th September 2009, 12:10 AM   #1
Default Light control system for smart home

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
lebohangmaphothoane is offline  
Old 16th September 2009, 12:43 AM   #2
Default

You do not have an END directive for the compiler and you have "return" instead of goto main to create a loop. Where is the code that turn on or off the led?
__________________
Mike
My website: www.ElectroBird.net
birdman0_o is online now  
Old 16th September 2009, 01:18 AM   #3
Default

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?
lebohangmaphothoane is offline  
Old 16th September 2009, 01:20 AM   #4
Default

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?
lebohangmaphothoane is offline  
Old 16th September 2009, 01:31 AM   #5
Default

Ok try something like this:

Just test the 10th bit, if it is set than the number is >512 otherwise it is less
__________________
Mike
My website: www.ElectroBird.net
birdman0_o is online now  
Old 16th September 2009, 02:38 AM   #6
Default

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
lebohangmaphothoane is offline  
Old 16th September 2009, 02:41 AM   #7
Default

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
__________________
Mike
My website: www.ElectroBird.net
birdman0_o is online now  
Old 16th September 2009, 02:46 AM   #8
Default

Look out because ADRESH is in Bank0 and ADRESL is in Bank1

To test ADRESH which is in Bank0
Set ADCON0,ADFM

I.E.
BSF ADCON0,ADFM
__________________
Mike
My website: www.ElectroBird.net
birdman0_o is online now  
Old 16th September 2009, 02:48 AM   #9
Default

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?
lebohangmaphothoane is offline  
Old 16th September 2009, 02:58 AM   #10
Default

It would be in NumH, the ADC returns a 10 bit value. The lower 8 are stored in NumL, the upper 2 are stored in NumH.

Test NumH, bit 1
__________________
Mike
My website: www.ElectroBird.net
birdman0_o is online now  
Old 16th September 2009, 03:19 AM   #11
Default

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
lebohangmaphothoane is offline  
Old 16th September 2009, 03:29 AM   #12
Default

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!
__________________
Mike
My website: www.ElectroBird.net
birdman0_o is online now  
Old 17th September 2009, 12:30 AM   #13
Default

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
lebohangmaphothoane is offline  
Old 17th September 2009, 12:52 PM   #14
Default

You have the wrong include file by the way. Why not use the pic16f877a one?
__________________
Mike
My website: www.ElectroBird.net
birdman0_o is online now  
Old 19th September 2009, 02:50 AM   #15
Default

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
lebohangmaphothoane is offline  
Reply

Tags
control, home, light, smart, system

Thread Tools
Display Modes


Similar
Title Starter Forum Replies Latest
Guys this is my home made 2.1 Home Theater System pasanlaksiri Electronic Projects Design/Ideas/Reviews 40 11th November 2009 05:07 AM
Traffic light control system with timer display hocklin12 Electronic Projects Design/Ideas/Reviews 6 7th May 2009 09:10 PM
smart traffic light: HELP! Crystal86 Electronic Projects Design/Ideas/Reviews 3 22nd January 2009 04:17 PM
smart home using blue tooth jayanthi Electronic Projects Design/Ideas/Reviews 4 4th September 2008 04:29 PM
traffic light control system samcheetah Electronic Projects Design/Ideas/Reviews 3 3rd May 2004 07:30 PM



All times are GMT. The time now is 12:25 AM.


Electronic Circuits  |  Learning Electronics
eXTReMe Tracker