Comparator interrupt
Moderator: Benj
- Jan Lichtenbelt
- Posts: 797
- Joined: Tue Feb 17, 2009 8:35 pm
- Location: Haren GN, the Netherlands
- Has thanked: 128 times
- Been thanked: 264 times
- Contact:
Comparator interrupt
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.
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.
Last edited by Jan Lichtenbelt on Fri Sep 23, 2011 7:26 pm, edited 2 times in total.
- Jan Lichtenbelt
- Posts: 797
- Joined: Tue Feb 17, 2009 8:35 pm
- Location: Haren GN, the Netherlands
- Has thanked: 128 times
- Been thanked: 264 times
- Contact:
Re: Comparator interrupt
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
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
- Attachments
-
- Test_Comparator_Interrupt.fcf
- (9.5 KiB) Downloaded 321 times
- Jan Lichtenbelt
- Posts: 797
- Joined: Tue Feb 17, 2009 8:35 pm
- Location: Haren GN, the Netherlands
- Has thanked: 128 times
- Been thanked: 264 times
- Contact:
Re: Comparator interrupt
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
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
- Attachments
-
- Test_Comparator_Interrupt V2.fcf
- (15 KiB) Downloaded 293 times
- Jan Lichtenbelt
- Posts: 797
- Joined: Tue Feb 17, 2009 8:35 pm
- Location: Haren GN, the Netherlands
- Has thanked: 128 times
- Been thanked: 264 times
- Contact:
Re: Comparator interrupt
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
To be continued....
Jan Lichtenbelt
- Attachments
-
- Test_Comparator_Interrupt V3.fcf
- (15 KiB) Downloaded 305 times
- Jan Lichtenbelt
- Posts: 797
- Joined: Tue Feb 17, 2009 8:35 pm
- Location: Haren GN, the Netherlands
- Has thanked: 128 times
- Been thanked: 264 times
- Contact:
Re: Comparator interrupt
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.
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.
Last edited by Jan Lichtenbelt on Mon Sep 26, 2011 11:01 am, edited 1 time in total.
- Jan Lichtenbelt
- Posts: 797
- Joined: Tue Feb 17, 2009 8:35 pm
- Location: Haren GN, the Netherlands
- Has thanked: 128 times
- Been thanked: 264 times
- Contact:
Re: Comparator interrupt
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
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
- Attachments
-
- Test_Comparator_Interrupt V4.fcf
- (11.85 KiB) Downloaded 303 times
-
- Matrix Staff
- Posts: 9520
- Joined: Sat May 05, 2007 2:27 pm
- Location: Northamptonshire, UK
- Has thanked: 2585 times
- Been thanked: 3815 times
- Contact:
Re: Comparator interrupt
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
This thread is very useful for anyone who requires comparator mode with interrupt.
Thanks for sharing all your hard work with us
Martin
Martin
- Jan Lichtenbelt
- Posts: 797
- Joined: Tue Feb 17, 2009 8:35 pm
- Location: Haren GN, the Netherlands
- Has thanked: 128 times
- Been thanked: 264 times
- Contact:
Re: Comparator interrupt
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
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
- Jan Lichtenbelt
- Posts: 797
- Joined: Tue Feb 17, 2009 8:35 pm
- Location: Haren GN, the Netherlands
- Has thanked: 128 times
- Been thanked: 264 times
- Contact:
Re: Comparator interrupt
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
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
- Attachments
-
- Test_Comparator_Interrupt V5.fcf
- (17.07 KiB) Downloaded 306 times
- Jan Lichtenbelt
- Posts: 797
- Joined: Tue Feb 17, 2009 8:35 pm
- Location: Haren GN, the Netherlands
- Has thanked: 128 times
- Been thanked: 264 times
- Contact:
Re: Comparator interrupt
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
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
- Benj
- Matrix Staff
- Posts: 15312
- Joined: Mon Oct 16, 2006 10:48 am
- Location: Matrix TS Ltd
- Has thanked: 4803 times
- Been thanked: 4314 times
- Contact:
Re: Comparator interrupt
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.
The line
Code: Select all
if (C1OUT==0)
for BoostC all register names are lower case.
Code: Select all
if (c1out==0)
Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
- Jan Lichtenbelt
- Posts: 797
- Joined: Tue Feb 17, 2009 8:35 pm
- Location: Haren GN, the Netherlands
- Has thanked: 128 times
- Been thanked: 264 times
- Contact:
Re: Comparator interrupt
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
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
- Benj
- Matrix Staff
- Posts: 15312
- Joined: Mon Oct 16, 2006 10:48 am
- Location: Matrix TS Ltd
- Has thanked: 4803 times
- Been thanked: 4314 times
- Contact:
Re: Comparator interrupt
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)
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)
Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
- Jan Lichtenbelt
- Posts: 797
- Joined: Tue Feb 17, 2009 8:35 pm
- Location: Haren GN, the Netherlands
- Has thanked: 128 times
- Been thanked: 264 times
- Contact:
Re: Comparator interrupt
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
This can be changed easily for all other three possibilities like C1 L->H, C2 L->H and C2 H->L.
Succes
Jan Lichtenbelt
- Attachments
-
- Test_Comparator_Interrupt V6.fcf
- (15.02 KiB) Downloaded 239 times
- Jan Lichtenbelt
- Posts: 797
- Joined: Tue Feb 17, 2009 8:35 pm
- Location: Haren GN, the Netherlands
- Has thanked: 128 times
- Been thanked: 264 times
- Contact:
Re: Comparator interrupt
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:
(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
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:
(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