PDA

View Full Version : Comparator Issues



Freman
- 23rd May 2008, 08:25
G'day there.

I've got both comparators hooked up on my PIC16F628A

I've attached the simplest schematic I could draw to describe the setup I have (The full version also uses digital pins, hwserial, 2 LDR's hooked up for POT and the pwm.)

The code I am using (again stripped down) is this.



@ device pic16F628A, hs_osc, wdt_off, pwrt_on, lvp_off, protect_off, bod_on, cpd_off, pwrt_off, mclr_off

DEFINE OSC 20

COMP1 VAR CMCON.7
COMP2 VAR CMCON.6

INPUT PORTA.0
INPUT PORTA.1
INPUT PORTA.2

DEFINE HSER_RCSTA 90h
DEFINE HSER_TXSTA 20h
DEFINE HSER_BAUD 9600

ON INTERRUPT GOTO interrupt_handler
INTCON = %11000000
PIE1.5 = 1

VRCON = %10100011 ' Should be around 0.6v
CMCON = %00000011

tmp VAR BYTE ' A throw away byte
tmp2 VAR BYTE ' You guessed it...

main_loop:
' Do stuff in here like read the LDR's with the POT command every 50 iterations
' and handle PWM for fading if needed.
pause 10
goto main_loop

DISABLE
interrupt_handler:
WHILE RCIF ' While there are bytes to be read
serialByte1 = RCREG ' Read a byte
IF serialByte1 = 7 THEN
tmp = COMP1
FOR tmp2 = 1 to 3 ' Give it 3 chances to read a 1
pause 100
tmp = tmp | COMP1
next tmp2
HSEROUT [tmp ^ %00000001] ' Invert the byte and send it to the serial port
endif
IF serialByte1 = 8 THEN
tmp = COMP2
FOR tmp2 = 1 to 3 ' Give it 3 chances to read a 1
pause 100
tmp = tmp | COMP2
next tmp2
HSEROUT [tmp ^ %00000001] ' Invert the byte and send it to the serial port
ENDIF
RETURN
ENABLE


Basically, it runs in a 10 ms loop for dimming led's attached to the pwm and reading two LDR's with the POT method. If serial input is detected it interrupts and goes to deal with the byte/s received, if one of those bytes is a 7 it reads the first comparator 3 times, if it's an 8 it reads the other.

Now, the problem I have is with input to pin 18 at 0.00v (as measured by my multimeter) the output looks like this 1110001011101011 (read from the serial port at the rate of 1 per second)
With pin 18 at 0.76v (the input is at 1.1v) it's fine, and happy to read 1111111111111111

Pin 17 is even worse off. at 0.12v (which is as low as it goes) it reads 1111111111111111
and it doesn't change when it goes to high or low...

Edit: PS I think I have it set up so that both comparators are hooked up to the internal vref and that it's running at 0.62v

So um... can anyone tell me why a) pin 18 is noisy and all over the shop... and b) pin 17 just doesn't work?

Thanks :)

Ingvar
- 23rd May 2008, 09:08
You have set up the comparator to use two comparators with a common reference at pin AN2. The problem is that you have no reference voltage at that pin, VRCON bit 6 should be set to output the Vref. Remember that you can't use this pin for any I/O operations anymore.

Freman
- 23rd May 2008, 12:58
Thanks for that.

That part of the tech sheet is a little confusing, especially when 010 means you can multiplex 2 inputs and use the internal vref.