PDA

View Full Version : Comparator 16F628



mat janssen
- 23rd November 2004, 19:54
I want to use one comparator input in my program.

How can I find the output of my comparator?

This is my code: (it not ok)

CMCON 5 'comp. C2 in use rest is digital

puls var bit


'here is the problem this is not working

puls = CMCON.7 'bit 7 is the output of C2

How can I get the output of the comp.C2 in my programm?

mister_e
- 23rd November 2004, 23:46
just want to be sure that you write
CMCON=5

is puls is declare as BIT VAR.

is by any chance you can post your whole code ??? schematics???

regards

mat janssen
- 24th November 2004, 10:32
Here is a part of the code and the diagram you asked for.

Ingvar
- 24th November 2004, 12:55
You should verify that the signal is strong enough, a mic produces very little voltage. I'd say that a scope is necessary. You might get away with "CMCON = 6" and monitor the comparator output on RA4. If you can see changes on that pin(dont forget the pullupresistor) you should be able to catch them in your program.

If the signal is very "spikey" there is a very big chance(risk) that your program is doing something else when the "spike" appears. This could be cured by looking at the compataror interruptflag(PIR1.6) instead of the comparator output(CMCON.7).

It also looks like you could reduce your mainloop to ......

BEGIN:
IF PULS = 1 Then COUNTERT = COUNTERT +1
LCDOut, COUNTERT
GoTo BEGIN

..... but i assume this is only some code you're using to verify the comparator.

/Ingvar

mat janssen
- 24th November 2004, 13:55
Yes, it is to test if I can use a comparator. Dont look to the details because the signal what is comming into the sysstem is very high.
I DO NOT HAVE A HARDWARE PROBLEM.
The probem is: how can I catch the comparator output in my programm.

Ingvar
- 24th November 2004, 16:01
Hi Mat,

I corrected the typo "CMCOM.7", it should ofcource be "CMCON.7", PBP should give you an error.

I use SEROUT instead of LCDOUT, no big deal there exept the fact that you had "LCDOut, COUNTERT". Two things about that line, first - it's "syntax error", the comma shouldn't be there. Second - you have no ASCII operator such as #,DEC,HEX,BIN or whatever. This line would most of the time show garbage on your display. You should also change "DEFINE LCD_DBIT 4" to "DEFINE LCD_DBIT 0" since you use the lower half of PortB.

However, none of this LCDstuff should affect the operation of the Comparator. I got so puzzled by your problem that i just had to give it a testrun. I can now inform you that this code works just fine, i ran it on a 16F628 ten minutes ago. If you can't get this code to work, it just has to be a hardware problem.




@ DEVICE PIC16F628,XT_OSC
@ DEVICE PIC16F628,MCLR_ON
@ DEVICE PIC16F628,BOD_OFF
@ DEVICE PIC16F628,LVP_OFF
@ DEVICE PIC16F628,CPD_OFF
@ DEVICE PIC16F628,PROTECT_OFF

DEFINE OSC 4

HULP1 VAR BIT
HULP2 VAR BIT
PULS VAR CMCON.7
COUNTERT VAR BYTE

CMCON = 5

TRISA = %11111111
TRISB = %00000000

Clear

BEGIN:
IF PULS = 1 AND HULP1 = 0 Then
HULP2 = 1
Else
HULP2 = 0
EndIF

IF PULS = 1 Then
HULP1 = 1
Else
HULP1 = 0
EndIF

IF HULP2 = 1 Then COUNTERT = COUNTERT + 1

serout2 PortB.0,84,["CMP2 = ",BIN1 PULS, " HULP1 = ",BIN1 HULP1]
serout2 PortB.0,84,[" HULP2 = ",BIN1 HULP2, " COUNTERT = ",DEC3 COUNTERT, 13, 10]
PAUSE 100

GoTo BEGIN




/Ingvar

Oops, crap, not that it matters much but i just realized that i made a mistake in my last post. Your code couldn't be reduced to what i suggested. I read the first IF-THEN statement wrong. Sorry.

mister_e
- 24th November 2004, 16:49
hehe i was just writing this to you...

sometime when we have too much time the face in the code... we don't see clear!!!

Ingvar
- 24th November 2004, 18:30
Perhaps it isn't very good for the brain to zap yourself with a highvoltagegenerator too many times. I've just finished building one for a friend ........ zzzapp.

mat janssen
- 24th November 2004, 19:50
Thanks all of you for the help you gave me.
It was a dumm mistake from my side. I typed cmcom i.s.o. cmcon and I didn't saw it .
Thanks again.