View previous topic :: View next topic |
Author |
Message |
nehallove
Joined: 16 Jan 2008 Posts: 61
|
comparator code problem in 18f25k20 |
Posted: Tue Apr 07, 2009 12:20 pm |
|
|
Hi All,
I am trying to use comparator interrupt. Earlier I used 18f2520 and I got the comparator working. Now we are moving to processor 18f25k20. I need to port the code for 18f25k20.
I need to use comparator.
Below is my test code:
I am trying to use pin Ra0 as inverting input with variable voltage and pin Ra3 as reference voltage 2.5 internal. I am using PICDEM 2 plus demo board. My PICC compiler version is 4.085.
Code: |
#INT_COMP
void isr()
{
clear_interrupt(INT_COMP);
flag = COMP1;
}
void main() {
setup_adc_ports(NO_ANALOGS|VSS_VDD);
setup_comparator(CP1_C1_A0);
//setup_vref(FALSE); // input and a4 is output
enable_interrupts(INT_COMP); // Initialize the Comparator interrupt
enable_interrupts(GLOBAL); // Enable the requested interrupts
output_low(PIN_C1);
output_high(PIN_C1);
while (TRUE) {
}
} |
If somebody can figure out what I am doing wrong it would be a great help.
Thank you,
nehal
P.S.
The library file for 18f25k20 has following defines for comparators.
Code: | // Comparator Variables: C1OUT, C2OUT
// Constants used in setup_comparator() are:
//
#define NC_NC_NC_NC 0x404
#define NC_NC 0x404
//Pick one constant for COMP1
#define CP1_A1_A0 0x4000088
#define CP1_C1_A0 0x0100089
#define CP1_C2_A0 0x020008A
#define CP1_C3_A0 0x040008B
#define CP1_A1_VREF 0x400008C
#define CP1_C1_VREF 0x010008D
#define CP1_C2_VREF 0x020008E
#define CP1_C3_VREF 0x040008F
//Optionally OR with one or both of the following
#define CP1_OUT_ON_A2 0x2000020
#define CP1_INVERT 0x0000010
#define CP1_1V2REF 0x0000040
//OR with one constant for COMP2
#define CP2_A1_C0 0x4008800
#define CP2_C1_C0 0x0108900
#define CP2_C2_C0 0x0208A00
#define CP2_C3_C0 0x0408B00
#define CP2_A1_VREF 0x4008C00
#define CP2_C1_VREF 0x0108D00
#define CP2_C2_VREF 0x0208E00
#define CP2_C3_VREF 0x0408F00
//Optionally OR with one or both of the following
#define CP2_OUT_ON_C4 0x0082000
#define CP2_INVERT 0x0001000
#define CP2_1V2REF 0x0004000
#bit C1OUT = 0xf7b.6
#bit C2OUT = 0xf7a.6 |
_________________ nehal |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Apr 07, 2009 2:08 pm |
|
|
Quote: | #INT_COMP
void isr()
{
clear_interrupt(INT_COMP);
flag = COMP1;
} |
Post the declaration statement for COMP1. |
|
|
nehallove
Joined: 16 Jan 2008 Posts: 61
|
|
Posted: Tue Apr 07, 2009 2:34 pm |
|
|
Code: | #bit COMP1 = 0xF7B.6 //! comparator1 output bit
even if i don't put it, it never goes to interrupt routine.
#INT_COMP
void isr()
{
clear_interrupt(INT_COMP);
//flag = COMP1;
}
void main() {
setup_adc_ports(NO_ANALOGS|VSS_VDD);
setup_comparator(CP1_C1_A0);
//setup_vref(FALSE); // input and a4 is output
enable_interrupts(INT_COMP); // Initialize the Comparator interrupt
enable_interrupts(GLOBAL); // Enable the requested interrupts
output_low(PIN_C1);
output_high(PIN_C1);
while (TRUE) {
}
} |
_________________ nehal |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Apr 07, 2009 2:58 pm |
|
|
Quote: | pin Ra3 as reference voltage 2.5 internal. |
What does this statement mean ? Do you want to use the internal Vref
generator to make 2.5 volts, and put it on the + pin of Comparator 1 ?
I don't see you where in your code that have set it up to do this. |
|
|
nehallove
Joined: 16 Jan 2008 Posts: 61
|
|
Posted: Tue Apr 07, 2009 3:36 pm |
|
|
i have picdem 2 plus prototype board. and it has external 2.5 V applied to the port, so i am using that one. _________________ nehal |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Apr 07, 2009 3:49 pm |
|
|
One more question. How do you know the interrupt does not occur ?
I don't see anywhere in your code where you turn on an LED if you
get the interrupt, etc. |
|
|
nehallove
Joined: 16 Jan 2008 Posts: 61
|
|
Posted: Tue Apr 07, 2009 4:03 pm |
|
|
i use PCWHD debugger and put break point in the interrupt routine at the clear interrupt statement. _________________ nehal |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Apr 07, 2009 4:55 pm |
|
|
I think CCS is setting up the opposite comparator from the one that
you desire. If you specify Comp1, their code sets up Comp2. And also
the reverse.
Here is their code for vs. 4.085 (and vs. 4.090 is the same).
Notice that we try to setup comparator 1, but it writes 0x89
to the CM2CON0 register, which is for Comp 2. That's incorrect.
Code: |
.................... setup_comparator(CP1_C1_A0);
00DA: BCF CM2CON1.C1RSEL
00DC: BCF CM2CON1.C2RSEL
00DE: CLRF CM1CON0
00E0: MOVLW 89
00E2: MOVWF CM2CON0
00E4: MOVF TRISA,W
00E6: IORLW 01
00E8: MOVWF TRISA
00EA: MOVF TRISC,W
00EC: IORLW 03
00EE: MOVWF TRISC
|
The same thing occurs below when we tell it to setup Comp 2.
It writes 0x89 to the CM1CON0 register. That's incorrect.
Code: |
.................... setup_comparator(CP2_C1_C0);
00F0: BCF CM2CON1.C1RSEL
00F2: BCF CM2CON1.C2RSEL
00F4: MOVLW 89
00F6: MOVWF CM1CON0
00F8: CLRF CM2CON0
00FA: MOVF TRISA,W
00FC: IORLW 01
00FE: MOVWF TRISA
0100: MOVF TRISC,W
0102: IORLW 03
0104: MOVWF TRISC |
In addition to those bugs, both sets of code are setting pins C0 and C1
as inputs. But C0 and C1 are not used by the comparators. That's
incorrect.
But as a work-around, just to see if you can make Comparator 1 work,
try this line. This will write 0x89 to the CM1CON0 register. I believe
that's the setup that you want.
Code: | setup_comparator(CP2_C1_C0); |
|
|
|
nehallove
Joined: 16 Jan 2008 Posts: 61
|
|
Posted: Wed Apr 08, 2009 4:33 pm |
|
|
Thank you PCM Programmer,
i figured the setting i want ( A0 as -ve and A3 as +ve comparator pin), my version of compiler ( 4.085 ) gives me with setup_comparator(CP2_A1_C0 ) statement.
thanks again for your help.
nehal _________________ nehal |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Apr 08, 2009 4:36 pm |
|
|
If you don't have a current maintenance agreement in effect with CCS,
you should report this bug to CCS support. They might upgrade you to
the latest version when they fix it. If you report it, show them all the
details. |
|
|
|