PDA

View Full Version : 12f675 programming question



puggy
- 30th November 2004, 07:55
this program is used in a metal detector. An analog signal goes into pin 6 of the 12f675. the way the program should work is that if a button is being pressed a 5V signal will be sent to pin 4. After every a/d pin 4 is checked to see if there is a digital 1 or 5V. If so then the value stored in ADRESL from the a/d should be stored in temp (variable stored in memory). when the button is not pressed the program will take the new data from ADRESL and subtract the value stored in temp from it. after this subtraction if the carry bit in the status register is clear the pic will turn on pin 5 and then turn it off which makes a wave that can be heard throw a speaker.

here is my code

list p=12F675;
#include <p12F675.inc>

__CONFIG _CP_OFF & _WDT_OFF & _BODEN_ON & _PWRTE_ON & _INTRC_OSC_NOCLKOUT & _MCLRE_OFF & _CPD_OFF

#define boy D'255';
#define man 0x20;
temp EQU 0x21

bcf STATUS,RP0
clrf GPIO
movlw 07h
movwf CMCON
movlw 85h
movwf ADCON0
bsf STATUS,RP0
movlw 02h
movwf ANSEL ;sets pin 6 as analog input
movlw 0Ah
movwf TRISIO ;sets pin 4 and 5 as outputs 6 as input
bcf STATUS,RP0

ad
movlw 87h
movwf ADCON0
adfinish
btfsc ADCON0,1
goto adfinish

btfsc GPIO,GPIO3 ;tests if button is pressed
goto reference

movlw 0FFh
bsf STATUS,RP0
andwf ADRESL,1
bsf STATUS,C
bcf STATUS,RP0
movlw temp
bsf STATUS,RP0
subwf ADRESL,1
btfsc STATUS,C ;check if signal is greater than 0
goto ad ;no metal perform next A/D

on
;turn pin on
bcf STATUS,RP0
movlw 04h
movwf GPIO
call delay;

;turn pin off
movlw 00h
movwf GPIO
call delay
goto ad

reference
bsf STATUS,RP0
movf ADRESL,0
bcf STATUS,RP0
movwf temp
goto ad


delay;

movlw boy;
movwf man;
loop;
decfsz man,f;
goto loop;
return;

end

mister_e
- 30th November 2004, 09:44
and the question is ???

puggy
- 30th November 2004, 15:36
the program is not working. ill use a function generator to simulate the analog input. ill tie pin 4 high and let it run for a few seconds. then ill remove the 5V connection to pin for so when i adjust the amplitude on the function generator a sound should come out of a speaker connected to the output pin 5. but there is no sound coming out. im just wondering if any one sees any silly mistakes or has an idea to change a portion of my code

mister_e
- 1st December 2004, 00:34
o.k if i understand you want to measure the amplitude of an incoming frequency right?

in this case a/d conversion time will gives you some crazy results. Also it depend on which part of your frequency cycle the A/D take the voltage measure too... so in this case you can monitor and compare few reading and take the highest one BUT

I suggest you to convert frequency to voltage. You can do it simple with an OP-AMP as a full-wave rectifier and put an capacitor to the output to get *clean* voltage.

regards