Comparator 16F628


Closed Thread
Results 1 to 9 of 9
  1. #1
    Join Date
    Oct 2003
    Location
    holland
    Posts
    251

    Default Comparator 16F628

    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?

  2. #2
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    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
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  3. #3
    Join Date
    Oct 2003
    Location
    holland
    Posts
    251


    Did you find this post helpful? Yes | No

    Default

    Here is a part of the code and the diagram you asked for.
    Last edited by mat janssen; - 24th November 2004 at 19:46.

  4. #4
    Join Date
    Jul 2003
    Location
    Sweden
    Posts
    237


    Did you find this post helpful? Yes | No

    Post

    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

  5. #5
    Join Date
    Oct 2003
    Location
    holland
    Posts
    251


    Did you find this post helpful? Yes | No

    Default

    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.

  6. #6
    Join Date
    Jul 2003
    Location
    Sweden
    Posts
    237


    Did you find this post helpful? Yes | No

    Post

    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.

    Code:
    	@ 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.

  7. #7
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    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!!!
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  8. #8
    Join Date
    Jul 2003
    Location
    Sweden
    Posts
    237


    Did you find this post helpful? Yes | No

    Talking

    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.

  9. #9
    Join Date
    Oct 2003
    Location
    holland
    Posts
    251


    Did you find this post helpful? Yes | No

    Default

    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.

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts