CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

PIC18F8720 Analog Comparator

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
demarware
Guest







PIC18F8720 Analog Comparator
PostPosted: Mon Jan 24, 2005 9:31 pm     Reply with quote

Hello,

I'm trying implement a comparator interrupt; based on the example EX_COMP.C. (and searching this board)

I'm using an external ref. (200mV, <10K Z), and an input of 0-4V.

Here's the code
#include <18F8720.h>
#device *=16
#device icd=true

#fuses HS, BROWNOUT, NOWDT, PUT, DEBUG, NOLVP
#use delay(clock=19660800)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

short safe_conditions=TRUE;

#INT_COMP
void isr() {

safe_conditions=FALSE;
printf("WARNING!! Voltage level is above ref. \r\n");
}


main() {

printf("\r\nRunning voltage test...\r\n\n");

//setup_comparator(A1_VR_OUT_ON_A2);
//setup_vref(VREF_HIGH|15);

setup_comparator(F6_F5_NC_NC_OUT_ON_F2);

enable_interrupts(INT_COMP);
enable_interrupts(GLOBAL);

while(TRUE)
{
if(safe_conditions)
printf("Voltage level is below ref. \r\n");
safe_conditions=TRUE;
delay_ms(500);
}
}
]

This works as expected, however I need an interrupt when the input is below the ref. So I thought I would only need to switch the inputs?? But that doesn't work, I get the same results.

Except if I debug/break, then switch the wires, then resume execution - I get the irq when the input is < ref. Restarting from a reset produces the opposite.

Where am I confused?

And, the datasheet says "The comparator interrupt flag is set whenever there is a change in the output value of either comparator." This code seems to produce an interrupt whenever X>Y only (even if it is always >), and not on the change back to X<Y. Is that due to the ccs implementation?

Oh yeh, v3.191. (this is the version I should be using right?)

Thanks in advance.
[/code]
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Jan 24, 2005 10:15 pm     Reply with quote

Both the data sheet and the reference manual state that you must
clear the "change condition" by reading (or writing) the CMCON register.
So I think you should add the lines shown below in bold:

#byte CMCON = 0xFB4

#INT_COMP
void isr() {
char c;
c = CMCON;

safe_conditions=FALSE;
printf("WARNING!! Voltage level is above ref. \r\n");
}

PIC18 reference manual for the comparator module:
http://ww1.microchip.com/downloads/en/DeviceDoc/39524.pdf
demarware
Guest







PostPosted: Mon Jan 24, 2005 11:29 pm     Reply with quote

Thanks, Now I have both changes.
demarware
Guest







PostPosted: Tue Jan 25, 2005 10:47 am     Reply with quote

FYI

use bit_test () on CMCON to detect change "direction".

#int_COMP
COMP_isr()
{

byte b;
b = CMCON;

if(bit_test (b, 6)) //c1out
{

//
}
else
{

//
}

}
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group