;----------------------------------------------------------------
; File Name : solder_joint.asm
; Date      : 26/01/2006
; Original author    : Thibault Mougel
; 100ms delay for better interference resitance added by Michal Karmazin 
;---------------------------------------------------------------- 
; Description :
;  This file contains the assembler code for a PIC10F200 micro-
;  controller.
;  It is use to detect and count the number of crack appearing
;  on a solder joint.
;
;  The microcontroller has one input, connected to one side of
;  the solder joint. The other side should be grounded. There is
;  an internal weak pull-up on the input, so that when the joint
;  is cracked, the input is high. Otherwise, the input is
;  is grounded through the solder joint and reads low.
;
;  On power-up, all LEDS are OFF.
; 
;  When a crack occurs, the corresponding LEDs is switch on, and
;  the input is never checked again. (LED stays on all the time)
;
;---------------------------------------------------------------

; Define Processor
 	LIST   P=10F200

; Include standard header file
#include p10f200.inc  	

;---------------------------------------------------------------
; Configuration bits :
; Master Clear Reset:			_MCLRE_ON	_MCLRE_OFF
; Program Memory Protected:		_CP_ON     	_CP_OFF
; Watch Dog Timer:				_WDT_ON     _WDT_OFF
; Oscillator:					_IntRC_OSC


 __config _MCLRE_OFF & _CP_OFF & _WDT_OFF & _IntRC_OSC

;---------------------------------------------------------------
; Input Output defintion
LED1         	equ 	0		; LED is connected to GP0
IN1          	equ 	1		; Read voltage from solder joint 
LED2         	equ 	2		; LED is connected to GP2
IN2         	equ 	3		; Read voltage from solder joint
IO_CONFIG		equ		B'1010'

; RAM address
DELAY_LSB 		equ		0x10	; Memory Address: LSB of delay counter
DELAY_16 		equ		0x11	; Memory Address: Bits 8-15 of delay counter
DELAY_MSB 		equ		0x12	; Memory Address: MSB of delay counter
CRACK_STATUS    equ     0x13    ; Memory Address: Store crack value





;---------------------------------------------------------------
; Begin of code
	org       	0x0000  		; Reset vector

Init

; Configure Inputs and Outputs
	andlw		0xFE			; Clear first bit (FOSC4) : GP2 is I/O
	movwf		OSCCAL			; Calibrate oscillator
	clrf		GPIO
   	movlw     	IO_CONFIG		; I/O configuration :
 	tris		6
	movlw		0x80			; Enable Weak pull-up
	option

    bsf         GPIO, LED1      ; Switch off LED
    bsf         GPIO, LED2      ; Switch off LED
	clrf        CRACK_STATUS	; Start without crack

delay_10s
	movlw		0x33            ; 06 for 1s
	movwf		DELAY_MSB	
	movlw		0xAA            ; 11 for 1s
	movwf		DELAY_16	
	movlw		0xF8            ; FE for 1s
	movwf		DELAY_LSB	
dec_LSB_10s
	decfsz		DELAY_LSB, F
	goto		dec_LSB_10s
	decfsz		DELAY_16, F
	goto		dec_LSB_10s
	decfsz		DELAY_MSB, F
	goto		dec_LSB_10s

	goto Test_in1 ;Pri prvnim pusteni programu vynecha 100ms smycku,kvuli instrukci RETLW by podtekl zasobnik.

delay_100ms 	;Cekaci smycka pro odstraneni falesnych poplachu
	movlw		0x85          
	movwf		DELAY_16	
	movlw		0xF8           
	movwf		DELAY_LSB	


dec_LSB_100ms
	decfsz		DELAY_LSB, F
	goto		dec_LSB_100ms
	decfsz		DELAY_16, F
	goto		dec_LSB_100ms
	RETLW 0;

Test_in1:							;zacatek vlastniho testovani
	btfsc       CRACK_STATUS, IN1 	;pokud byl spoj prerusen,uz nebude dana yvstup kontrolovan
	goto        Test_in2
	btfss     	GPIO, IN1			;Zkontroluj zda je spoj v 0
	goto		Test_in2			;pokud je OK,skoc na kontrolu vstupu 2
	call		delay_100ms			;pokud je podezreni na preruseni,skoc do 100ms cekaci smycky
	btfss     	GPIO, IN1			;znovu zkotroluj spoj.pokud je stale prerusen(i po 100ms cekani) rozsviti ledku.pokud ne,skoci nakontrolu 2. spoje
	goto        Test_in2
    bcf         GPIO, LED1      	; Switch on LED            
    bsf         CRACK_STATUS, IN1 	;ulozi do promenne ze pri minule kontrole byl preruseny spoj

Test_in2:
	btfsc       CRACK_STATUS, IN2 	;pokud byl spoj prerusen,uz nebude dana yvstup kontrolovan
	goto        Test_in1
 	btfss     	GPIO, IN2			;Check if input is still low (not cracked)
   	goto      	Test_in1   			;pokud je OK,skoc na kontrolu vstupu 1
	call		delay_100ms			;pokud je podezreni na preruseni,skoc do 100ms cekaci smycky
	btfss     	GPIO, IN2			;znovu zkotroluj spoj.pokud je stale prerusen(i po 100ms cekani) rozsviti ledku.pokud ne,skoci nakontrolu 2. spoje
	goto        Test_in1
	bcf         GPIO, LED2      	; Switch on LED            
    bsf         CRACK_STATUS, IN2	;ulozi do promenne ze pri minule kontrole byl preruseny spoj
   	goto      	Test_in1   			; Move to checking of input 1
    
 end
