Problem with Comparator's internal setup

Status
Not open for further replies.

Ambient

New Member
I am trying to finish this simple code for a project that I will be expanding later on. I can't seem to get the comparator C1 to actually work internally. I am hoping to get this done for tomorrow when I need it. Any ideas why this blasted thing is not working? It flashes once (from LED assertion in main) and will not go on again. This is basically just a night light for this stage of the project.

Pin 18 - 1Mohm pot for testing
Pin 19 - NC
RC3 is a diode and resister.
pullup on MCLR

That is the entire circuit at the moment. Thanks for any help guys. I know it is something stupid I am doing.

Code:
	List	p=16f690
	include	P16F690.INC
	
	__CONFIG	_CP_OFF & _CPD_OFF & _BOR_OFF & _PWRTE_ON & _WDT_OFF & _INTOSCIO & _MCLRE_ON & _FCMEN_OFF & _IESO_OFF;
	errorlevel  -302
	
;-----------------------VARIABLE REGISTER DEFINITIONS---------------------------
;w_temp		EQU	0x70		; variable used for context saving
status_temp	EQU	0x71		; variable used for context saving
pclath_temp	EQU	0x72		; variable used for context saving
RC3			EQU	h'03'
RC6			EQU	h'06'

;================================CODE==================================
	org		0x0000
  	goto	Initialize
;***************************Interrupt_Routine**************************
	org		0x0004			; interrupt vector location

	bcf		INTCON,T0IF
	
;	btfsc	PORTC,RC6		;next line run if SW1 OFF (active low)
;	retfie	
	btfss	CM1CON0, C1OUT
	bcf		PORTC,RC3
	btfsc	CM1CON0,C1OUT
	bsf		PORTC,RC3

	retfie
;***************************INITIALIZATION*****************************
Initialize
	movlw	b'00011000'
	movwf	STATUS			;BANK 0
	movlw	b'00000000'
	movwf	ADCON0			;turns off ADC
	
	movlw	b'00111000'
	movwf	STATUS			;BANK 1
	movlw	b'01000001'
	movwf	OSCCON			;sets oscillator to 1MHz
	movlw	b'00000110'
	movwf	OPTION_REG		;enables Timer0, pullups A and B, 1:256 PS, 
	movlw	b'00001011'
	movwf	TRISA			;PORTA I/O setup
	movlw	h'00'
	movwf	TRISB			;PORTB
	movlw	b'00000000'
	movwf	TRISC			;PORTC:RC4,5,6 mosfet drivers, RC0,1,2 audio input port (A/D in later C code)

	movlw	b'11011000'
	movwf	STATUS			;BANK 2
	movlw	b'00000011'
	movwf	ANSEL			;analog disable ANS2-ANS7, ANS0 Analog input
	movlw	b'00000000'
	movwf	ANSELH			;analog disable ANS8-ANS11
	movlw	b'10000100'
	movwf	CM1CON0			;enable C1 internal, Vref internal
	bcf		CM2CON0,h'07'	;disable C2
;	movlw	b'00110011'		;sets C1+ fixed 0.6V Vref
;	movwf	VRCON

	movlw	b'10101000'		;sets C1- Vref to (VR<3:0>/24)*VDD
	movwf	VRCON

	movlw	b'00011000'
	movwf	STATUS			;BANK 0
	movlw	b'11100000'
	movwf	INTCON			;enable int, Timer0 int
main
	bsf		PORTC,RC3
	NOP
	NOP
	GOTO	$-2
	end
 
Whats it going to do
Code:
 main
	bsf		PORTC,RC3
	NOP
	NOP
	GOTO	$-2
Not much there to do
 
I have never attempted to use the simulator. I will give it a try.

The program only has to run 1 LED at the moment. That is being handled by the interrupt generated from TMR0.

I just set it to put C1OUT on pin 17, and it seems that the comparator does work. The strange thing is that C1OUT varies in voltage when I get the pot near the programmed internal Vref (which is working) and goes to 5V when I get at least further past the trip point. Maybe the PIC is damaged? It is new! I got a reading of 1.42V, then 5V off of pin 17. I don't have a pullup because the spec sheet indicates that the comparator is not directly connected to the pin, but through some logic and then a mux. So this output SHOULD be Digital but it does not seem to be... What is going on here?!


I could now just jump the C1OUT pin to a digital Input, but that seems like cheating...
 
Last edited:
Timer0 generates an intnerrupt which is handled by the interrupt routine.

You can use a lot by watching the simulator execute. You will need to create a stimulus notebook. Not difficult.

3v0
 
I ran this with mplab sim and when turns on it's low so is that not why your led go's out
 
Last edited:
You mean the C1OUT internally goes low? I tested my program with the C1OUT external just now. My C1OUT (pin 17) seems to go from 0V to 5V, but it is not digital at all. In the config I have it set to digital output, but it varies with the voltage I put in on the C1- pin... so something here is very wrong. Do I really need a pullup on C1OUT?

I am trying to find the simulator right now.
 
Wow...CM1CON0 is in bank 2....so I have to switch to bank 2 to read the C1OUT internally? That's a crappy design IMHO....

So maybe that is my only problem?
 
EDIT: Wrong day to be a smart butt.

Next time use an 18F.
 
Last edited:
What circuit you using

It is just a voltage divider of a 100k and an LDR going to the inverting pin, witha 10k pullup on MCLR and an LED with a 200 ohm resister on RC3. Nothing complicated, but it still will not work. I am really puzzled as to why the comparator output on pin 17 was varying instead of giving me a digital output like a comparator should...
 
Can you look at the output on a scope? If your supply is dodgy the switching on of the LED could drop the supply enough that the comparator switches back, effectively giving you a 50/50 output region that looks like an analogue voltage on a multimeter.

Mike.
 
Last edited:
I played with this thing for hours last nite with the led going from vss to rc3 and a LDR with resistor from vdd to ra1 it had 4.05 volts on ra1 and the led would stay on but it I couldn't get it to turn off the led. Even with volts lower on the ra1 dark it was about 2.50 volts
full light it's about 4.00 volts
 
Last edited:
Ah. I will have to check it out on a scope then. I am using the Inchworm to power it, but that might be having problems. But I also had the same problem using a regulated 5V wall wart (from a D-link router, 1A).

Thanks for the help guys. For now I will have to use a simple comparator and keep working on the PIC.

Be80be did you make sure that the input to RA1 got below 1.67V? That is the Vref set by:
Code:
	movlw	b'10101000'		;sets C1- Vref to (VR<3:0>/24)*VDD
	movwf	VRCON
You mentioned that you got down to 2.5V, but that is not enough to trigger the comparator.
 
I got this to work with a 16f684 i'm work on changing it to a 16f690

Code:
INCLUDE "p16f684.inc"

 __CONFIG _FCMEN_OFF & _IESO_OFF & _BOD_OFF & _CPD_OFF & _CP_OFF & _MCLRE_OFF & _PWRTE_ON & _WDT_OFF & _INTOSCIO

;  Variables
 CBLOCK 0x020
 ENDC

  PAGE
;  Mainline

 org     0

  nop                           ;  For ICD Debug

  clrf    PORTA
  movlw   b'00001010'           ;  Comparator with Vref Module
  movwf   CMCON0

  bsf     STATUS, RP0
  clrf    ANSEL ^ 0x80
  movlw   b'10101000'           ;  Specify 1.5 Volt Transition
  movwf   VRCON ^ 0x80
  movlw   0x0F                  ;  RC4/RC5 Outputs
  movwf   TRISA ^ 0x80
  bcf     STATUS, RP0

Loop:
  movlw   0                     ;  Start with LED off
  btfsc   CMCON0, C1OUT         ;  if C1Vin+ > Reference
   movlw  1 << 4                ;    then turn on LED
  movwf   PORTA

  goto    Loop


  end
It's hard to get the comparator to work right on the 16f690
 

Attachments

  • light compar.PNG
    11.4 KB · Views: 117
Why use a comparator when you can read the value on an analog input ?
 
Well I don't no I was just trying to learn about comparators and he happen to post. But I
tell you what your so right it's a lot easier to read a analog input. I don't think a 16f690 is easy to get the comparator to work you change registers to much. The code I put up works with 16f684 a little backwards the led go's on in light and off in dark. But it did compare light to dark maybe some one can use it.

Oh P.S the hard part is the voltage divider 6.6kohms resistor and 22kohms in dark LDR 3kohms with the lite on
 
Last edited:
I was thinking you wanted to make is work so I kept quite for a while.

What I like about using an analog input is that you can fake the comparator in software (INPUT>THRESHOLD) and adjust the threshold on the fly.
 
Well I quit the basic stamp and got in to picking haven't looked back. I'm going to look into
fake the comparator in software (INPUT>THRESHOLD) and adjust the threshold on the fly.
I could use that. Thanks for the ideal 3v0
 
Let me plant a seed.

Supose you want to detect if a light has been turned on. The system could watch and learn to know what natural light levels were day and night. Once you had that you could adjust the threshold based on what time it was.
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…