Page 1 of 1
Comparator interrupt
Posted: Mon Sep 19, 2011 1:32 pm
by Jan Lichtenbelt
Hi All,
Can some one help me to write a comparator interrupt.
I want the ouputs C4 and C5 become high/low when the comparator inputs A3 and A4 become higher/lower compared to Vref (e.g. internal Vref CVRON= 1110 xxxx with x=0..15). The interrupt has to take action on bit 6 and 7 of the comparator CMRON (xx11 1110).
Thanks a lot
Jan
PS. Something like this?
September 23, 2011. Picture deleted, because it was completely wrong and could give confusion.
Re: Comparator interrupt
Posted: Wed Sep 21, 2011 2:39 pm
by Jan Lichtenbelt
Hi All,
Waiting until an answer takes always to much time. But better than waiting is trying to find yourself the solution. In the meanwhile the Forum database becomes larger and lareger. Searching for subjects will not always direct you to the applicable answer, especially if there are to much answers. An other way of searching is by means of Google with:
Search term site:
www.matrixmultimedia.com/mmforum
Now I found advices for a comparator interrupt. With Google I found the solution in this Forum
http://www.matrixmultimedia.com/mmforum ... dler#p9038
And what is better than testing it yourself. I made a Flowcode program, put 0.5 Volt on comparators inputs RA0 and RA1 and a variable voltage on inputs RA2 and RA3. Because the output RA4 has an open collector I put a 10kOhm resistor to +5V. But the program did work, but the results were unexpected. Both outputs show all the time 4Volt, independent of the input voltage on RA2 and RA3????
To solve the problem I put additional cmcon=0 for 2 seconds and the outputs change for RA4 to 5V and RA5 to 2V????
Cmcon=0 should give "0" on both outputs and comcon=0x03 should give 0 or 1 on both outputs, but not something between.
What do I wrong in thinking and/or in the program?
Kind regards
Jan Lichtenbelt
Re: Comparator interrupt
Posted: Fri Sep 23, 2011 7:37 pm
by Jan Lichtenbelt
Ok, it is not a simple problem. The outputs RA4 and RA5 become 0 during 2 seconds, if you use this flowcode.
The voltage somewhere between 0 and 5 Volt of both outputs was due to the fact that the inputs RA0 and RA1 were not defined. Now varying this input voltage shows the outputs to change between 0 and 5 Volt, as expected for comparators.
Still the interrupt does not work. I'm reading the datasheet of 16F877A. I see already a problem. The first line of the enable code clear_bit(pir1,CMIF) seems to me not possible, because the pir1 register does not contain the CMIF bit (pir2 does).
I try to find a solution. If someone else can help, I appreciate that very much.
Kind regards
Jan Lichtenbelt
Re: Comparator interrupt
Posted: Fri Sep 23, 2011 8:08 pm
by Jan Lichtenbelt
Ok, changing pir1 into pir2 helps. The interrupt works now. I think on comparator 2 only. I have to look if comparator 1 can also give an interrupt.
To be continued....
Jan Lichtenbelt
Re: Comparator interrupt
Posted: Fri Sep 23, 2011 8:26 pm
by Jan Lichtenbelt
To my surprise works the interrupt on both comparators!
Remark September 26, 2011: The microcontroller datasheet indeed shows the interrupt will take place on either change of both comparators.
To seperate the interrupt of the 2 comparators, see example some comments below.
Re: Comparator interrupt
Posted: Sun Sep 25, 2011 1:48 pm
by Jan Lichtenbelt
Hi All,
An example program with interrupts on 2 comparators with comparator reference voltage (CVRCON register)
In this program Vref changes from 0 to 3.125 Volt in steps of about 0.2 Volt (208mV)
If Vref crosses the input voltage on either RA0 or RA1 and interrupt will occur.
Kind regards
Jan Lichtenbelt
Re: Comparator interrupt
Posted: Sun Sep 25, 2011 2:57 pm
by medelec35
Hi Jan .
This thread is very useful for anyone who requires comparator mode with interrupt.
Thanks for sharing all your hard work with us
Martin
Re: Comparator interrupt
Posted: Sun Sep 25, 2011 8:30 pm
by Jan Lichtenbelt
Hi Martin,
By publishing, I indeed hope some else can use this.
In the meanwhile I checked that bit CMIF can be either in PIR1 or PIR2 register, depending on the microcontroller used. The same holds for CIE bit in PIE1 or PIE2 register.
Both bits are used in the comparator interrupt .
Kind regards
Jan
Re: Comparator interrupt
Posted: Mon Sep 26, 2011 11:05 am
by Jan Lichtenbelt
It is possible to check the change of both comparators (high to low, or low to high) separetaly.
I tried first to realise it in the interrupt handler, but up to now without succes. So I let see the behaviour of both comparators in the interrupt macro. See Version 5.
Kind regards,
Jan
Re: Comparator interrupt
Posted: Wed Sep 28, 2011 12:06 pm
by Jan Lichtenbelt
I changed the interrupt handelr code
from:
if (pir2 & (1 << CMIF))
{
FCM_%n(); // call selected macro
clear_bit(pir2, CMIF); // clear interrupt
}
into:
if (pir2 & (1 << CMIF))
{
if (C1OUT==0)
{
FCM_%n(); // call selected macro
}
clear_bit(pir2, CMIF); // clear interrupt
}
The result is that the microprocessor stops as soon as this interrupt is called. I even increased the stack (swcs 6 2), without succes.
Can someone help me what the reason is that the process stops?
Kind regards
Jan
Re: Comparator interrupt
Posted: Wed Sep 28, 2011 2:53 pm
by Benj
Hello Jan,
The line
may be looking at a define version of the c1out register which will likely always return one state.
for BoostC all register names are lower case.
Re: Comparator interrupt
Posted: Thu Sep 29, 2011 10:44 am
by Jan Lichtenbelt
Hi Ben,
if (c1out==0)
gives an error: undentified identifier. I changed it into:
if test_bit(cmcon, C1OUT)
and now I get an interrupt if C1out changes from 0 to an other value. The test_bit gives me the acutal value of c1out. Or is there a more easy way to get the actual value?
Kind regards
Jan
Re: Comparator interrupt
Posted: Thu Sep 29, 2011 12:16 pm
by Benj
Hi Jan,
Aha I see the value was a bit name rather then a register name, should have looked at the datasheet before replying.
You can do this if it is any easier.
if (cmcon.C1OUT)
Problem is it won't work under the HiTech compiler. To do this you would have to change to this.
if (CMCONbits.C1OUT)
Re: Comparator interrupt
Posted: Thu Sep 29, 2011 4:40 pm
by Jan Lichtenbelt
Thanks to Ben, now a comparator interrupt on the specific C1 output from Low to High change (See flowcode....V6).
This can be changed easily for all other three possibilities like C1 L->H, C2 L->H and C2 H->L.
Succes
Jan Lichtenbelt
Re: Comparator interrupt
Posted: Thu Oct 20, 2011 12:48 pm
by Jan Lichtenbelt
In the examples above, the Vref will be changed from 0 to 15/24x5 Volt=3.125 Volt in steps of 5/24 Volt= 0.208 Volt. This can be done by changing the CVRON register.
I made two programs and one works fine and the other, nearly the same, gave all the time wrong comparator interrupts. It took me a long time to find the solution, and as always the answer is
afterwards simple.
To change the Vref I used in Flowcode a counter and simple increase this in a loop with 1 up to 15 and then down to 0 again. The last 4 bits of this counter are used for the CVRCON register, which is responsible for the Vref.
To get a new CVRCON value first the last four bits have to be cleared with ... & 0xF0 and then the new value has to be added with... | FCV_COUNTER, or better only the last four bits ... | (FCV_COUNTER & 0X0F). I used two C-codes to calculate the new CVRCON:

- CVRCON.jpg (96.03 KiB) Viewed 10492 times
(ComptTemVar2256 is used for temporary storage)
The second one, clear
always first CVRCON resulting in a Vref =0 Volt before changing the Vref value. See also the assembler code. The red step one is unwanted. This can give unexpected comparator interrups.
In the first one, the new value will be calculated
before replacing CVRON, as it should be done!
Jan