I had my 16f82a (edit 16f628 thanks Exo) set up with an external comparator.
Now I want to use the internal comparator to save space, the problem is I had all my inputs using port B and my outputs using port A.
From reading the datasheet the comparator can only be used on pins RA0 - 3
I am using interrupts from port b.0
Im really confused as to how to set up my inputs and outputs:
Here is what I want io wise:
18 RA1,AN1 - Output - solenoid driven by transistor
1 Vref,AN2,RA2 Comparator reference voltage (from pot)
2 CMP1,AN3,RA3 Comparator non inverting input (ie when this value is above reference the output is 1)
6 INT,RB0 Interrupt trigger
7 RX,DT,RB1 Output - led
8 TX,CK,RB2 Output - led
Util now I have been able to configure the outputs just by saying all portA is an output (TRISA = 0x00) but now it has suddenly got alot more complicated.
Can someone explain how to set this up, or better yet give me the code to do it as I am pretty confused.
Also how do I access the output of the comparator (i know this is in the datasheet but they are quite hard to comprehend in places)
Im sorry about the typing mistake (when you have been pouring over the data sheets as long as I have, letters and numbers all seem to bleed into one big blur :? )
First of all, since you need only one comparator, its best to connect comparator inputs to RA1 and RA2. A comparator mode between RA3 and RA2 isn't available as you can see in the datasheet page 56.
So The new settings are:
RA1: Comparator reference (from pot)
RA2: Comparator input (when this value is above ref output is 1)
RA3: Output - solenoid driven by transistor
Port B stays the same. You can use another portA pin to drive the solenoid, but the comparator should stay on RA1-RA2
Code:
MOVLW b' 00000101'
MOVWF CMCON ;load comparator settings
BSF STATUS, RP0 ;Switch to bank 1
BSF TRISA, 1
BSF TRISA, 2 ;RA1 - 2 input for comparator
BCF TRISA, 3 ;RA3 output for solenoid
BSF TRISB, 0 ;RB0 input for interrupt trigger
BCF TRISB, 1
BCF TRISB, 2 ;Led outputs
BSF STATUS, RP0 ;Switch back to bank 0
The comparator output can be read from CMCON - bit 7