1. 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.
    Dismiss Notice

Light control system for smart home

Discussion in 'Microcontrollers' started by lebohangmaphothoane, Sep 15, 2009.

  1. lebohangmaphothoane

    lebohangmaphothoane New Member

    Joined:
    Aug 20, 2009
    Messages:
    103
    Likes:
    0
    Location:
    Lesotho (Mountain Kingdom)
    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
     
  2. birdman0_o

    birdman0_o Active Member

    Joined:
    Feb 23, 2009
    Messages:
    1,370
    Likes:
    18
    Location:
    Montreal, Quebec
    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?
     
  3. lebohangmaphothoane

    lebohangmaphothoane New Member

    Joined:
    Aug 20, 2009
    Messages:
    103
    Likes:
    0
    Location:
    Lesotho (Mountain Kingdom)
    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?
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. lebohangmaphothoane

    lebohangmaphothoane New Member

    Joined:
    Aug 20, 2009
    Messages:
    103
    Likes:
    0
    Location:
    Lesotho (Mountain Kingdom)

    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?
     
  6. birdman0_o

    birdman0_o Active Member

    Joined:
    Feb 23, 2009
    Messages:
    1,370
    Likes:
    18
    Location:
    Montreal, Quebec
    Ok try something like this:

    Just test the 10th bit, if it is set than the number is >512 otherwise it is less :)
     
  7. lebohangmaphothoane

    lebohangmaphothoane New Member

    Joined:
    Aug 20, 2009
    Messages:
    103
    Likes:
    0
    Location:
    Lesotho (Mountain Kingdom)
    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
     
  8. birdman0_o

    birdman0_o Active Member

    Joined:
    Feb 23, 2009
    Messages:
    1,370
    Likes:
    18
    Location:
    Montreal, Quebec
    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
     
  9. birdman0_o

    birdman0_o Active Member

    Joined:
    Feb 23, 2009
    Messages:
    1,370
    Likes:
    18
    Location:
    Montreal, Quebec
    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
     
  10. lebohangmaphothoane

    lebohangmaphothoane New Member

    Joined:
    Aug 20, 2009
    Messages:
    103
    Likes:
    0
    Location:
    Lesotho (Mountain Kingdom)
    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?
     
  11. birdman0_o

    birdman0_o Active Member

    Joined:
    Feb 23, 2009
    Messages:
    1,370
    Likes:
    18
    Location:
    Montreal, Quebec
    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
     
  12. lebohangmaphothoane

    lebohangmaphothoane New Member

    Joined:
    Aug 20, 2009
    Messages:
    103
    Likes:
    0
    Location:
    Lesotho (Mountain Kingdom)
    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
     
  13. birdman0_o

    birdman0_o Active Member

    Joined:
    Feb 23, 2009
    Messages:
    1,370
    Likes:
    18
    Location:
    Montreal, Quebec
    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 (text):
    ;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!
     
  14. lebohangmaphothoane

    lebohangmaphothoane New Member

    Joined:
    Aug 20, 2009
    Messages:
    103
    Likes:
    0
    Location:
    Lesotho (Mountain Kingdom)
    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
     
  15. birdman0_o

    birdman0_o Active Member

    Joined:
    Feb 23, 2009
    Messages:
    1,370
    Likes:
    18
    Location:
    Montreal, Quebec
    You have the wrong include file by the way. Why not use the pic16f877a one?
     
  16. lebohangmaphothoane

    lebohangmaphothoane New Member

    Joined:
    Aug 20, 2009
    Messages:
    103
    Likes:
    0
    Location:
    Lesotho (Mountain Kingdom)
    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
     
  17. gaspode42

    gaspode42 Member

    Joined:
    Feb 7, 2009
    Messages:
    218
    Likes:
    3
    Location:
    UK
    __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: Sep 18, 2009
  18. birdman0_o

    birdman0_o Active Member

    Joined:
    Feb 23, 2009
    Messages:
    1,370
    Likes:
    18
    Location:
    Montreal, Quebec
    Should be :

    To make the assembler happy
     
  19. lebohangmaphothoane

    lebohangmaphothoane New Member

    Joined:
    Aug 20, 2009
    Messages:
    103
    Likes:
    0
    Location:
    Lesotho (Mountain Kingdom)
    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.
     
  20. gaspode42

    gaspode42 Member

    Joined:
    Feb 7, 2009
    Messages:
    218
    Likes:
    3
    Location:
    UK
    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:

    [​IMG]

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

    Attached Files:

    • bit.png
      bit.png
      File size:
      3.3 KB
      Views:
      292
    Last edited: Sep 21, 2009
  21. birdman0_o

    birdman0_o Active Member

    Joined:
    Feb 23, 2009
    Messages:
    1,370
    Likes:
    18
    Location:
    Montreal, Quebec
    Your formatting is off Gaspode, can you edit it for him?
    Use "preview post"
     

Share This Page