PDA

View Full Version : PIC18F13K22 comparator problem



harryweb
- 15th June 2011, 15:35
Hi,

May be it's because I don't understand the data sheet ?
I'm trying to compare with 18F13K22
My ref is connected to portA.1 (C12IN0-)
The other signal connected to portA.0 (C1IN+)

Doesn't work

Here is my code:

ANSEL = %00000011
ANSELH = %00000000
TRISA=%11111011 ' Set Inputs
TRISC=%00000000 ' Set Inputs
TRISB=%00000000 ' Set Inputs
CM1CON0 = %1000000

C1OUT VAR CM1CON0.6

mainloop:
IF C1OUT = 1 THEN
HIGH PORTB.6
ELSE
LOW PORTB.6
endif
PAUSE 200
goto mainloop
END

Thank's for help

mister_e
- 15th June 2011, 16:46
As crazy as it seems, you want to disable the ADC on these specific pin, but enable the comparator. it also seems you miss a bit in your CM1CON0 register? ;)

Give this a shot

C1OUT VAR CM1CON0.6

PORTA = 0
PORTB = 0
PORTC = 0

TRISA = %00000011 'PORTA<1:0> = Input
TRISC = 0
TRISB = 0

ANSEL = 0 ' Disable
ANSELH = 0 ' All ADCs

CM1CON0 = %10001000
' -1-------- C1ON: Enable comparator 1
' --x------- C1OUT: Read only
' ---0------ C1OE: C1OUT set to internal
' ----0----- C1POL: C1OUT Logic not inverted
' -----1---- C1SP: Operate in normal power mode
' ------0--- C1R: C1Vin+ Connected to C1IN+ (A.0)
' -------00- C1CH<1:0>: C1Vin- connected to C12IN0- (A.1)
MainLoop:
LATB.6 = C1OUT
PAUSE 200
GOTO MainLoop
END

harryweb
- 16th June 2011, 08:47
Thank you Steve !

Yes it's crazy to disable the ADC... I realized the opposite of what is written in the data sheet. (may be because it's in English ?)

Ok, now it's working fine.

I have a question to ask you. What is "LATB.6" ? (This is true for any LAT(PORT number)?

Regards

Herve

mister_e
- 16th June 2011, 09:42
In fact it is crazy ... or not. You want to set a specific I/O to use ONLY what you want to. Sure we read analog thing... but with the comparator.. not the ADC... ANSEL & ANSELH set a pin as analog to use with tthe internal ADC... kinda thing to understand/remember.


I realized the opposite of what is written in the data sheet. (may be because it's in English ?)

Fais-toi en pas avec ça... avec un peu de pratique, le tout deviendra plus clair ;)


I have a question to ask you. What is "LATB.6" ? (This is true for any LAT(PORT number)?

LATx is the output PORT Latch. Usually with PIC18 (and all other PIC with LATx register) you want to write to LATx, but read from PORTx. In THIS specific case, you shouldn't experiment any problem if you use PORTB.6, but it's something to keep in mind.

On apprends les bonne habitudes en premier ;)