![]() | ![]() | ![]() |
| | #1 |
|
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 |
|
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 |
|
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 |
|
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?
| |
| |
| | #5 |
|
Ok try something like this: Just test the 10th bit, if it is set than the number is >512 otherwise it is less | |
| |
| | #6 |
|
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
| |
| |
| | #7 |
|
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 | |
| |
| | #8 |
|
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 | |
| |
| | #9 |
|
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? | |
| |
| | #10 |
|
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 | |
| |
| | #11 |
|
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 | |
| |
| | #12 |
|
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 | |
| |
| | #13 |
|
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
| |
| |
| | #14 |
|
You have the wrong include file by the way. Why not use the pic16f877a one?
| |
| |
| | #15 |
|
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
| |
| |
|
| 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 |