# Project understanding

Discussion in 'General Electronics Discussion' started by ami85t, Feb 19, 2014.

1. ### ami85t

71
0
Feb 19, 2014
HI all,

I'm trying to understand (and remember) whats going on in this project
which I intend using:

http://www.micro-examples.com/articles/index.php/PicoDetector#comment=1:99

I will be grateful to anybody how can help me with a few issues:

1. In the explanation appears:
"Coil L1 replaces crystal in PIC oscillator circuit"
what exactly does that mean?
As far as I understand the method for detecting a metal is by a AC current passing
through a inductor and/or capacitor. What oscillates the current in this circuit? an
initial oscillator of the pic? if yes so isn't it supposed to be a square
signal? if the answer is again yes - does the circuit work, does the
inductor respond to a square wave?

2. I don't manage to understand which type of inductor to use
for this type of project. I sow a few different types which made
me confused:
- surface Inductor ?

Recently I did a project which contains a pic16f877 and
for that purpose I wrote in assembly language and used Mplab environment and ICD2
For burning the code to the pic.
can I use the same method here by copying the text of the code and use in the same
method? (by changing the configuration of Mplab to C code)

Thanks, Amitai

2. ### BobK

7,682
1,688
Jan 5, 2010
A crystal is normally used as part of an oscillator to set the PICs operationg frequency. In this clever little project, it has been replaced by a coil. The oscillator still operates, but now its frequency is dependent on how much metal is close to the coil, becuase that will change the inductance of the coil. This fequency is then compared to another frequency that comes from the internal oscillator for the watchdog timer. The PIC can then sense a change in it's clock frequency, and hence the presence of metal near the coil.

You will have to install the C compiler from Microchip, then you can compile it and program the chip from MPLAB as you did with your assenbly language program.

Bob

71
0
Feb 19, 2014
Thanks!
Amitai

4. ### ami85t

71
0
Feb 19, 2014
Hi,
I build this project up and for some reason it doesn't work.
I used a PIC16f877 and translated the code to Assembly.
I'm suspecting its a problem in the code. ( I checked the circuit and everything seems OK).
can anyone help me with this one?

Thanks, Amitai

LIST P=PIC16F877

include <P16f877.inc>
org 0x00

reset:
nop
goto start
org 0x20

start:
; configuration
;-----------------------------------

bcf STATUS,RP0 ;bank 0
bcf STATUS,RP1

ctr EQU H'0025' ; number of loops between two watchdog resets
previous EQU H'0026' ; previous value of ctr
calibr EQU H'0027' ; calibration value when oscillator runs free
restarts EQU H'0028' ; number of watchdog restarts
en EQU H'0029' ; enable flag, allows detection

bsf STATUS,RP0 ;bank 1
clrf TRISB ; making PORTB output
bcf STATUS,RP0 ;bank 0
clrf PORTB ; reset output

; end configuration
;-----------------------------------

;*************** MAIN PROGRAM *****************
main_loop:
bcf STATUS,RP0 ;bank 0
btfss STATUS, 4 ; POWER UP? WDT TIME OUT OCCURRED?
goto init_loops ; yes
goto check_reset_counter ; no

init_loops:
clrf restarts
movlw 0x01
movwf calibr

check_reset_counter:
movlw 0xFF
subwf restarts, w ; restarts - 255
btfss STATUS, C ; restarts =< 255 ?
incf restarts, f ; yes - increment restarts

movf previous, w
xorwf ctr
subwf calibr, w ; calibr - ( previous xor ctr)
btfss STATUS, C ; is previous XOR ctr > calibr ?
call metal_detcted ; yes

movf ctr, w
movwf previous

movlw 0x0F
subwf restarts, w ; restarts - 15
btfsc STATUS, C ; calibration over?
goto calib_over ; yes
goto calib_still_active ; no

calib_over:
bcf PORTB, 1 ; calib led OFF
movlw 0xFF
movwf en ; enable detect led
goto WDT_set

calib_still_active:
bsf PORTB, 1 ; calib led ON
clrf en ; disable detect led
goto WDT_set

WDT_set:
bsf STATUS,RP0 ;bank 1
movlw 0xF9
movwf OPTION_REG ; Prescaler Rate, PORTB pull-ups are disable? (needed)?

bcf STATUS,RP0 ;bank 0
clrf ctr

waiting_loop: ; start counter, to be interrupted by watchdog
btfss STATUS, 4
goto main_loop
incf ctr, f
goto waiting_loop

;------

metal_detcted:
bsf PORTB, 2 ; turn led detect ON

btfss STATUS, 4 ; time out occurred ?
goto led_off ; yes
goto calib_check ; no

led_off:
bcf PORTB, 2 ; turn led detect OFF
return

calib_check:
movlw 0x0F
subwf restarts, w ; restarts - 15
btfsc STATUS, C ; in calibration mode?
return ; no
rlf calibr
call DELAY_1m
call DELAY_1m
call DELAY_1m
call DELAY_1m
call DELAY_1m
return

;*************** END MAIN PROGRAM *************

DELAY_1m:
movlw 0x2f ; N1 = 47
movwf 0x35
cont2: movlw 0x10 ; N2 = 16
movwf 0x36
cont1: decfsz 0x36,1
goto cont1
decfsz 0x35,1
goto cont2
return

end

5. ### jpanhalt

426
4
Nov 12, 2013
It will be easier to read, if you use code tags {code} code {/code} (Replace the curly brackets with regular brackets).

Code:
```LIST	P=PIC16F877

include	<P16f877.inc>
org	 0x00

reset:
nop
goto	start
org	 0x20

start:
; configuration
;-----------------------------------

bcf	 STATUS,RP0	;bank 0
bcf	 STATUS,RP1

ctr	 EQU H'0025'	 ; number of loops between two watchdog resets
previous	EQU H'0026'	 ; previous value of ctr
calibr	 EQU H'0027'	 ; calibration value when oscillator runs free
restarts	EQU H'0028'	 ; number of watchdog restarts
en	 EQU H'0029'	 ; enable flag, allows detection

bsf	 STATUS,RP0	;bank 1
clrf	 TRISB	 ; making PORTB output
bcf	 STATUS,RP0	;bank 0
clrf	 PORTB	 ; reset output

; end configuration
;-----------------------------------

;*************** MAIN PROGRAM *****************
main_loop:
bcf	 STATUS,RP0	;bank 0
btfss	 STATUS, 4	 ; POWER UP? WDT TIME OUT OCCURRED?
goto	 init_loops	 ; yes
goto	 check_reset_counter	 ; no

init_loops:
clrf	 restarts
movlw	 0x01
movwf	 calibr

check_reset_counter:
movlw	 0xFF
subwf	 restarts, w	 ; restarts - 255
btfss	 STATUS, C	 ; restarts =< 255 ?
incf	 restarts, f	 ; yes - increment restarts

movf	 previous, w
xorwf	 ctr
subwf	 calibr, w	 ; calibr - ( previous xor ctr)
btfss	 STATUS, C	 ; is previous XOR ctr > calibr ?
call	 metal_detcted	 ; yes

movf	 ctr, w
movwf	 previous

movlw	 0x0F
subwf	 restarts, w	 ; restarts - 15
btfsc	 STATUS, C	 ; calibration over?
goto	 calib_over	 ; yes
goto	 calib_still_active	 ; no

calib_over:
bcf	 PORTB, 1	 ; calib led OFF
movlw	 0xFF
movwf	 en	 ; enable detect led
goto	 WDT_set

calib_still_active:
bsf	 PORTB, 1	 ; calib led ON
clrf	 en	 ; disable detect led
goto	 WDT_set

WDT_set:
bsf	 STATUS,RP0	;bank 1
movlw	 0xF9
movwf	 OPTION_REG	 ; Prescaler Rate, PORTB pull-ups are disable? (needed)?

bcf	 STATUS,RP0	;bank 0
clrf	 ctr

waiting_loop:	 ; start counter, to be interrupted by watchdog
btfss	 STATUS, 4
goto	 main_loop
incf	 ctr, f
goto	 waiting_loop

;------

metal_detcted:
bsf	 PORTB, 2	 ; turn led detect ON

btfss	 STATUS, 4	 ; time out occurred ?
goto	 led_off	 ; yes
goto	 calib_check	 ; no

led_off:
bcf	 PORTB, 2	 ; turn led detect OFF
return

calib_check:
movlw	 0x0F
subwf	 restarts, w	 ; restarts - 15
btfsc	 STATUS, C	 ; in calibration mode?
return	 ; no
rlf	 calibr
call	 DELAY_1m
call	 DELAY_1m
call	 DELAY_1m
call	 DELAY_1m
call	 DELAY_1m
return

;*************** END MAIN PROGRAM *************

DELAY_1m:
movlw	 0x2f	 ; N1 = 47
movwf	 0x35
cont2:	movlw	 0x10	 ; N2 = 16
movwf	 0x36
cont1:	decfsz	 0x36,1
goto	 cont1
decfsz	 0x35,1
goto	 cont2
return

end

```
Apparently you set the configuration with your compiler. It would help is you showed it here. Since the 12F and 16F are different, please show your schematic.

I see where you set your outputs, but not where you turn off the analog inputs and comparators. Here is how it was done for the 12F683:
Code:
```   CMCON0 = 7 ;
ANSEL = 0 ;
```
John

Edit: Attached as Notebook text -- hopefully easier to read
View attachment 877 code.txt

Last edited: Mar 20, 2014
6. ### ami85t

71
0
Feb 19, 2014
Thanks,
As far as I see only ports A and E can be analog
and my output is via portB. the related registers to portb
are the trisb and option reg which I referred to.
About the comparators: as far as I understand there's no need to
relate to them, (in the original code the CMCON0 statement disables the comparator)
since the measuring is the change since the last WDT end, no?

I was thinking that maybe the problem is connected to the
code conversion of the WDT and the and power-up.

Code:
```LIST	P=PIC16F877

include	<P16f877.inc>
org		0x00

reset:
nop
goto	start
org		0x20

start:
; configuration
;-----------------------------------

bcf		STATUS,RP0	;bank 0
bcf		STATUS,RP1

ctr			EQU H'0025'				; number of loops between two watchdog resets
previous	EQU H'0026'				; previous value of ctr
calibr		EQU H'0027'				; calibration value when oscillator runs free
restarts	EQU H'0028'				; number of watchdog restarts
en			EQU H'0029'				; enable flag, allows detection

bsf		STATUS,RP0	;bank 1
clrf		TRISB					; making PORTB output
bcf		STATUS,RP0	;bank 0
clrf		PORTB					; reset output

; end configuration
;-----------------------------------

;*************** MAIN PROGRAM *****************
main_loop:
bcf		STATUS,RP0	;bank 0
btfss		STATUS, 4				; POWER UP? WDT TIME OUT OCCURRED?
goto		init_loops				; yes
goto		check_reset_counter		; no

init_loops:
clrf		restarts
movlw		0x01
movwf		calibr

check_reset_counter:
movlw		0xFF
subwf		restarts, w				; restarts - 255
btfss		STATUS, C				; restarts =< 255 ?
incf		restarts, f				; yes - increment restarts

movf		previous, w
xorwf		ctr
subwf		calibr, w				; calibr - ( previous xor ctr)
btfss		STATUS, C				; is previous XOR ctr > calibr ?
call		metal_detcted			; yes

movf		ctr, w
movwf		previous

movlw		0x0F
subwf		restarts, w				; restarts - 15
btfsc		STATUS, C				; calibration over?
goto		calib_over				; yes
goto		calib_still_active		; no

calib_over:
bcf			PORTB, 1				; calib led OFF
movlw		0xFF
movwf		en						; enable detect led
goto		WDT_set

calib_still_active:
bsf			PORTB, 1				; calib led ON
clrf		en						; disable detect led
goto		WDT_set

WDT_set:
bsf		STATUS,RP0	;bank 1
movlw		0xF9
movwf		OPTION_REG				; Prescaler Rate, PORTB pull-ups are disable? (needed)?

bcf		STATUS,RP0	;bank 0
clrf		ctr

waiting_loop:							; start counter, to be interrupted by watchdog
btfss		STATUS, 4
goto		main_loop
incf		ctr, f
goto		waiting_loop

;------

metal_detcted:
bsf			PORTB, 2				; turn led detect ON

btfss		STATUS, 4				; time out occurred ?
goto	    led_off					; yes
goto	    calib_check				; no

led_off:
bcf			PORTB, 2				; turn led detect OFF
return

calib_check:
movlw		0x0F
subwf		restarts, w				; restarts - 15
btfsc		STATUS, C				; in calibration mode?
return								; no
rlf			calibr
call		DELAY_1m
call		DELAY_1m
call		DELAY_1m
call		DELAY_1m
call		DELAY_1m
return

;*************** END MAIN PROGRAM *************

DELAY_1m:
movlw		0x2f			;  N1 = 47
movwf		0x35
cont2:	movlw		0x10			;  N2 = 16
movwf		0x36
cont1:	decfsz		0x36,1
goto		cont1
decfsz		0x35,1
goto		cont2
return

end```

File size:
74.2 KB
Views:
134
7. ### jpanhalt

426
4
Nov 12, 2013
"Doesn't work" can mean a lot of things. For example, it could mean it doesn't compile, it doesn't detect metal, and so forth. Can you describe what you mean?

Have you simulated the program?

John

8. ### ami85t

71
0
Feb 19, 2014
sorry, I'll try and be more specific.
The compilation and programing of the code and the PIC are fine.
When I connect the pic to the circuit nothing happens.
The outputs of the B port stay '0'V logic.

Amitai

9. ### BobK

7,682
1,688
Jan 5, 2010
Most likely your PIC is not running. You need to set various configuration bits, and you should do this in the code.

Bob

10. ### ami85t

71
0
Feb 19, 2014
I got some code correction advise and now using a DMM I can see that the frequency on the inductor changes while having metal near,
but the led doesn't respond at all. the output of both the legs is
'0' continuously.

Amitai

new code:
Code:
```	LIST P=PIC16F877
include <P16f877.inc>

__CONFIG _LVP_OFF &_WDT_ON &_PWRTE_ON &_CP_OFF & _HS_OSC
; basic config values  Watchdog ON, Lvp OFF, XT OSC crystal 4mhz and below

cblock 0x20	; define user registers/variables

ctr  		; number of loops between two watchdog resets
previous	; previous value of ctr
calibr  	; calibration value when oscillator runs free
restarts 	; number of watchdog restarts
en 			; enable flag, allows detection
endc

org 0x00
nop
goto start

start:
; configuration
;-----------------------------------

banksel  TRISB
clrf	 TRISB 	  ; making PORTB output
banksel	 0		 ;bank 0
clrf 	PORTB  	; reset output

; end configuration
;-----------------------------------

;*************** MAIN PROGRAM *****************
main_loop:
bcf STATUS,RP0 ;bank 0
btfss	 STATUS, 4 ; POWER UP? WDT TIME OUT OCCURRED?
goto	 init_loops ; yes
goto	 check_reset_counter ; no

init_loops:
clrf restarts
movlw 0x01
movwf calibr

check_reset_counter:
movlw 0xFF
subwf restarts, w ; restarts - 255
btfss STATUS, C ; restarts =< 255 ?
incf restarts, f ; yes - increment restarts

movf previous, w
xorwf ctr
subwf calibr, w ; calibr - ( previous xor ctr)
btfss STATUS, C ; is previous XOR ctr > calibr ?
call metal_detcted ; yes

movf ctr, w
movwf previous

movlw 0x0F
subwf restarts, w ; restarts - 15
btfsc STATUS, C ; calibration over?
goto calib_over ; yes
goto calib_still_active ; no

calib_over:
bcf PORTB, 1 ; calib led OFF
movlw 0xFF
movwf en ; enable detect led
goto WDT_set

calib_still_active:
bsf PORTB, 1 ; calib led ON
clrf en ; disable detect led
goto WDT_set

WDT_set:
bsf STATUS,RP0 ;bank 1
movlw 0xF9
movwf OPTION_REG ; Prescaler Rate, PORTB pull-ups are disable? (needed)?

bcf STATUS,RP0 ;bank 0
clrf ctr

waiting_loop: ; start counter, to be interrupted by watchdog
btfss STATUS, 4
goto main_loop
incf ctr, f
goto waiting_loop

;------

metal_detcted:
bsf PORTB, 2 ; turn led detect ON

btfss STATUS, 4 ; time out occurred ?
goto led_off ; yes
goto calib_check ; no

led_off:
bcf PORTB, 2 ; turn led detect OFF
return

calib_check:
movlw 0x0F
subwf restarts, w ; restarts - 15
btfsc STATUS, C ; in calibration mode?
return ; no
rlf calibr
call DELAY_1m
call DELAY_1m
call DELAY_1m
call DELAY_1m
call DELAY_1m
return

;*************** END MAIN PROGRAM *************

DELAY_1m:
movlw 0x2f ; N1 = 47
movwf 0x35
cont2: movlw 0x10 ; N2 = 16
movwf 0x36
cont1: decfsz 0x36,1
goto cont1
decfsz 0x35,1
goto cont2
return

end
```