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 support@ccsinfo.com

Help with PIC18 analog comparator

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



Joined: 01 Oct 2003
Posts: 172
Location: Punta Gorda, Florida USA

View user's profile Send private message Send e-mail

Help with PIC18 analog comparator
PostPosted: Sat Apr 23, 2005 10:55 am     Reply with quote

Hi

Although I have been using Pics for a while, I am doing my first application using the built in comparators and I am somewhat confused as to how to use the CCS directives, specifically how the comparator interrupt isr works.

I need to use comparator one to sense a particular voltage level into RA0 the reference for this comparator is common to the second one using the internal reference which I selected to be 2.5V. I want comparator one to trigger whenever RA0 exceeds the reference and generate a interrupt. For comparator two I need to sense a voltage at RA1 and whenever this voltage drops below the reference, I want this comparator to generate an interrupt. I noticed that CCS has a ISR routine for the comparator module, but it seems that it only applies to comparator one. Reading the PIC18F2420 data sheet it indicates that either one of the comparators changing will generate an interrupt.
So my question is: 1. To know which of the comparators has triggered one has to test the status of bits C1OUT and C2OUT within the ISR routine and then upon leaving the ISR the CCS handler will take care of resetting things??

2. I can not seem to find the CCS configuration to set up the comparators so one detects above the ref and the other below the ref.

3. Does any one know if there is any interaction between the comparator module and the HLVD (High-Low voltage detect module) Can both be used at the same time? I see no reference to that in the data sheet.


This is how I have setup the analog channels might be using the adc on AN3:
setup_adc_ports(AN0_TO_AN3|VSS_VDD);
setup_adc(ADC_CLOCK_INTERNAL|ADC_TAD_MUL_0);

And this is how I have setup the analog comparators and vref:
setup_comparator(A0_VR_A1_VR);
setup_vref(VREF_HIGH|12);

Any help on the above is greatly appreciated.......... Thank you Confused
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Apr 23, 2005 1:50 pm     Reply with quote

In the 18F2420 data sheet, in Figure 20-1, shows all the comparator
configurations. It looks like setup "100" is the one you want.

Quote:

I noticed that CCS has a ISR routine for the comparator module, but it
seems that it only applies to comparator one.

There's only one CMIF bit. If it's set, the interrupt dispatcher jumps to
the #INT_COMP routine. It's up to you to put in code inside that routine
to check which comparator caused the interrupt.

Quote:
So my question is: 1. To know which of the comparators has
triggered one has to test the status of bits C1OUT and C2OUT within the
ISR routine

Go to the Microchip Reference manual page, and get the 18C comparator
reference manual.
http://www.microchip.com/stellent/idcplg?IdcService=SS_GET_PAGE&nodeId=1956
It says:
"The comparator interrupt flag is set whenever the comparators value
changes relative to the last value loaded into CMxOUT bits. Software will
need to maintain information about the status of the output bits, as read
from CMCON<7:6>, to determine the actual change that has occurred."
In fact, the data sheet also says this, in section 20.6.

Quote:
and then upon leaving the ISR the CCS handler will take care of resetting things??
The data sheet says what to do:
"The user, in the Interrupt Service Routine, can clear the interrupt in the following manner:
a) Any read or write of CMCON will end the mismatch condition.
b) Clear flag bit CMIF."

CCS will take care of clearing the CMIF flag, but you have to put in
code to clear the "mismatch" condition.
Do this by defining the CMCON register address with a #byte statement.
Then declare a local variable in your INT_COMP isr. Then, at the end
of the isr, the last thing you should do is put in a line of code that reads
CMCON and puts the result into the local variable. ie:

c = CMCON;

This will satisfy item (a), above.
CCS will take care of doing section (b).

Quote:
2. I can not seem to find the CCS configuration to set up the
comparators so one detects above the ref and the other below the ref.

Since the comparator interrupt occurs on any change of state, high or low,
you will have to handle this in code, inside the ISR. Save the previous
state of the comparator outputs and check if the new change that caused
the interrupt came from:
1. The comparator that is associated with the voltage input.
2. And did the voltage go from high to low, or from low to high ?
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