/*
* mouse trap.asm
*
*	Created: 15/10/2018 12:43:14
*	Author : swift
*	Target : attiny 461 @ 8mhz
*	
*	Project :
*				to monitor a PIR motion detector mounted on a 100mm W x 100mm H x 400mm L box
*				to raise a pin to trigger a transistor to trigger a solenoid to close a door
*
*				PIR detector is SR-505
*				solenoid is 3-6v @ 1a retractable with 4001 safety diode
*
*	notes:
*				solenoids are notorious for bad behaviour with mcu
*				must ensure VCC is decoupled with capacitor close to the avr pins
*				otherwise the solenoid switch off may cause the power lines to bounce
*				will may cause a reset.
*
*				reset has 6k8 pull-up - there is 1uf between pin 5 and pin 6(vcc and gnd)
*				the transistor is a mosfet which avoids the need for current limit on the trigger pin
*
*	Pin usage:
*
*				port A.0  pin 20 is led to monitor initialise routine
*				port A.2  pin 18 is PIR signal pin - input
*				port A.3  pin 17 is transistor base with 6k8 pull down
*
*	reg usage:
*
*				R16 general use
*				R17 delay counter
*				R26-R29 delay routines
*
*/
.EQU	LED_PIN = 0
.EQU	PIR_PIN = 2
.EQU	OP_PIN =3
.CSEG
.ORG	$0000						; no interrupts so straight in

START:
        LDI		R16,LOW(RAMEND)		; initialise the stack pointer
        OUT		SPL,R16
        LDI		R16,HIGH(RAMEND)
        OUT		SPH,R16

		LDI		R16,0b00001001		; initialise port A
		OUT		DDRA,R16			; bit 3 output (pin 17) and bit 0 (pin 20) all others input
		CBI		PORTA,OP_PIN		; clear the trigger
		SBI		PORTA,LED_PIN		; debug - verify when entering start-up
		RCALL	INIT_PIR			; wait for detector to steady
		CBI		PORTA,LED_PIN		; debug  - verify when exit start-up

LOOP:
		NOP
		SBIS	PINA,PIR_PIN		; check the PIR pin for high
		RJMP	LOOP				; no movement detected
		RCALL	DETECT
END:
		NOP
		RJMP	END


   ;subroutines                                                          ;
   ;---------------------------------------------------------------------;

   ;------------------------------;
   ; WAIT 1 MS ROUTINE            ;
   ;------------------------------;
   ; uses: R26/R27
					
WAIT1MS:
		PUSH	R26
		PUSH	R27
		LDI		XL,LOW(2000)		; at 8 mhz there are 8000 cycles per millisecond
		LDI		XH,HIGH(2000)		; at 4 cycles per loop then 2000 loops = 8000 cycles = 1 ms
W1MSCONT:
		SBIW	XL,1				; 2 cycle instruction
		BRNE	W1MSCONT			; 2 cycle instruction
		POP		R27
		POP		R26
		RET

   ;------------------------------;
   ; WAIT 1 SECOND ROUTINE        ;
   ;------------------------------;
   ; uses : R28/R29
   
WAIT1SEC:							; wait 1000 ms
		PUSH	R28
		PUSH	R29
		LDI		YL,LOW(1000)
		LDI		YH,HIGH(1000)
WAIT1SECCONT:
		RCALL	WAIT1MS
		SBIW	YL,1
		BRNE	WAIT1SECCONT
		POP		R29
		POP		R28
		RET

   ;------------------------------;
   ; Movement detected		      ;
   ; activate solenoid
   ;------------------------------;
   ; uses : R25

DETECT:
		SBI		PORTA,OP_PIN		; trigger the solenoid to close the door
		RCALL	WAIT1SEC			; wait 1 second
		CBI		PORTA,OP_PIN		; clear the trigger, turn off solenoid save battery
		RET

   ;------------------------------;
   ; wait for PIR detector	      ;
   ; to clear at turn on		  ;
   ;------------------------------;
   ; uses : R17 counter

INIT_PIR:
		LDI		R17,60
IP1:
		RCALL	WAIT1SEC			; at turn on PIR detector registers high for varying period
		DEC		R17					; but sometimes starts low and triggers after a few seconds
		BRNE	IP1					; so wait 1 minute to be sure
IP2:
		SBIC	PINA,PIR_PIN		; get PIR pin status
		RJMP	IP2					; not ready - still registering high so wait for low
		RET

