Continue to Site

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.

  • 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.

control system

Status
Not open for further replies.
Can any one help me by correcting this code i have written
Code:
;PROGRAM FUNCTION:light control for smart home


	list		P=16F877a
	include	   "P16F877a.inc"
			__config 0x3D18			;sets the configuration settings (oscillator type etc.)



;Declarations:

        
NumH    equ     20h
NumL    equ     21h
dc1   equ     22h
NumH1   equ     23h
NumL1   equ     24h
PostX   equ     25h
Mark240 equ     26h
PostY   equ     27h


      
	org		0x0000
	goto Start	

;Program Start:

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

Init
;--------------------------------------------------------------------------
    	   ; Initializing porta and portb
    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 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         


    movlw	b'00000111'	;sets up timing register
	   option


     ;Initializing ADC
   	movlw   b'10010001';
	movwf   ADCON0 ;
    BANKSEL ADCON1
	movlw   b'10000101'
	movwf   ADCON1
	BANKSEL ADCON0
    
    return   
;--------------------------------------------------------------------------
   
;Subroutines:   
   
motion 	movlw   b'10010001';
	    movwf   ADCON0 ;
        BANKSEL ADCON1
    	movlw   b'10000101'
    	movwf   ADCON1
    	BANKSEL ADCON0



       bsf	ADCON0, GO_DONE		;initiate conversion
       btfsc   ADCON0, GO_DONE
       goto    $-1			;wait for ADC to finish
       movf    ADRESH,W
       andlw   0x03
       movwf   NumH1
       BANKSEL ADRESL
       movf    ADRESL,W
       BANKSEL	ADRESH
	   movwf	NumL1			;return result in NumL1 and NumH1  
    
      return
   
;------------------------------------------------------------------------
Light_on
       bsf PORTA,4       ;turn on the LED
       goto Main

       return
;-------------------------------------------------------------------------
_4hr_delay
        movlw	d'144000'	
		movwf	PostX		;sets up variable postscaler			
		movlw	d'240'		;sets up fixed marker
		movwf	Mark240		;
TimeLoop

		movfw	Mark240		;waits for TMR0 to count up
		subwf	TMR0,w		;240 times
		btfss	STATUS,Z	;
		goto	TimeLoop	;hasn't, so keeps looping

		movlw	d'240'		;resets Mark240
		addwf	Mark240,f	;

		decfsz	PostX,f		;does this X times
		goto	TimeLoop	;

		retlw	0		;returns after required time
;-------------------------------------------------------------------------
second_delay  
        movlw	d'144000'	
		movwf	PostY		;sets up variable postscaler	
       	movlw	d'240'		;sets up fixed marker
		movwf	Mark240		;

Time
		movfw	Mark240		;waits for TMR0 to count up
		subwf	TMR0,w		;240 times
		btfss	STATUS,Z	;
		goto	Time	;hasn't, so keeps looping

		movlw	d'240'		;resets Mark240
		addwf	Mark240,f	;
        goto    motion
        goto    After_6hrrs
dky	
    	decfsz	PostY,f		;does this X times
		goto	Time	;

		goto	Main
;---------------------------------------------------------------------------
wait      movlw   5   ; Outer loop iteration count
d11	      movwf   dc1		 

	      decfsz  dc1,F         
	      goto    $-1
	      return
;---------------------------------------------------------------------------
After_6hrrs
        btfss NumH1,1
        goto ledoff
        bsf PORTA,4
        goto dky
ledoff  
        bcf PORTA,4
        goto dky    
      
;---------------------------------------------------------------------------

closeblinds
       movlw b'0000'
       movwf PORTB
       call wait
       goto motion
       btfss  NumH1,1
       goto Light_on
       bsf PORTA,4
       goto _4hr_delay
       bcf PORTA,4
       goto motion
       
After_6hrs
       btfss NumH1,1
       goto led_off
       bsf PORTA,4
       goto second_delay
       
led_off
       bcf PORTA,4
       goto second_delay


;--------------------------------------------------------------------------
openblinds
       movlw b'0001'
       movwf PORTB
       call wait

       movlw b'0100'
       movwf PORTB
       call wait
   
       movlw b'0010'
       movwf PORTB
       call wait
   
       movlw b'1000'
       movwf PORTB
       call wait
       
       return
;-----------------------------------------------------------------------------



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 closeblinds
    goto openblinds
    bcf PORTA,4       ; turn 0ff the LED
    
    btfss NumH1,1
    goto led_on  
    
    goto Main
led_on   
    bsf PORTA,4       ;turn on the LED
    goto motion
    btfss NumH1,1
    goto Main  
    bcf PORTA,4



       END
or modify it for me so that it follows the flowchart on the attachment. I wrote the above code following the flowchart but it seems like i have made so many mistakes. for closing and opening of blinds i used unipolar stepper motor. For turning on and off lights i used LEDs and i used LDR to detect light and infrared photo detector (rs: 2017729) to detect motion
 

Attachments

  • room.jpg
    room.jpg
    35.7 KB · Views: 260
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top