View previous topic :: View next topic |
Author |
Message |
pilar
Joined: 30 Jan 2008 Posts: 197
|
Problems with a comparator after a reset |
Posted: Fri Jan 08, 2016 9:15 am |
|
|
Hi,
I'm trying to implement a battery level indicator to show when the battery is below or above level reference (3.6V).
Here is my code, it works only partially, my problem is when I initialize or reset the pic, the value that shows me is wrong, I have to increase the battery voltage above 3.6V, only after that, any variation of voltage, it is detected correctly.
Could someone tell me what is my mistake or I do to the value after reset is initialized correctly.
Code: | #include <12F629.h>
#fuses INTRC_IO, NOWDT, NOPROTECT
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_A2)
#define BATTERY PIN_A1
#define LED_R PIN_A4
#define LED_V PIN_A5
#define ON output_high
#define OFF output_low
#INT_COMP
void isr() {
if (C1OUT == 0){
printf("Battery Voltage level is above 3.6 V\r\n");
OFF (LED_R);
ON (LED_V);
}
else {
printf("WARNING!! Battery Voltage level is below 3.6 V\r\n");
OFF (LED_V);
ON (LED_R);
}
}
main() {
OFF (LED_R);
ON (LED_V);
setup_comparator(A1_VR);
setup_vref(VREF_HIGH|15);
enable_interrupts(INT_COMP);
enable_interrupts(GLOBAL);
while(TRUE);
}
|
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19504
|
|
Posted: Fri Jan 08, 2016 9:29 am |
|
|
I'd suspect this is the same as with a lot of other interrupts.
After you have configured the peripheral, the interrupt may well be set. This is true for things like standard interrupts where if you program the 'edge' you must always clear the interrupt afterwards, since the act of setting the edge will often trigger the interrupt (this is in the data sheet). Similarly the UART will often 'see' a character at boot, if the transceiver being used doesn't pull the RX line 'high' before the PIC starts to boot. So try:
Code: |
setup_comparator(A1_VR);
setup_vref(VREF_HIGH|15);
delay_us(10); //just allow a little time for the voltages to settle
clear_interrupt(INT_COMP); //now ensure the interrupt is clear.
enable_interrupts(INT_COMP);
enable_interrupts(GLOBAL);
|
|
|
|
pilar
Joined: 30 Jan 2008 Posts: 197
|
|
Posted: Fri Jan 08, 2016 9:57 am |
|
|
Hi Ttelmah,
The problem occurs only at the beginning. After a reset, I have to increase the value above 3.6V, only then the comparator to detect voltage fluctuations. If after a reset, I do not increase the voltage above 3.6V, for more time that passes, any voltage change is not detected by the comparator.
Which is the problem? |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9225 Location: Greensville,Ontario
|
|
Posted: Fri Jan 08, 2016 12:14 pm |
|
|
Could be a 'voltage' problem ? 3.6 is a magical value just more than 5-1.5 ( 3.5) which is the comparators vmax in common mode. I can't test but perhaps the 3.6 battery is 'overloading' the comparator causing a latchup condition?
Have to ask is the PIC powered by the battery or from another source? If separate then you shouldn't have the battery attached before VDD is applied.
If possible post your schematic, via a 3rd party website, as it might help.
Jay |
|
|
pilar
Joined: 30 Jan 2008 Posts: 197
|
|
Posted: Fri Jan 08, 2016 12:46 pm |
|
|
Hi temtronic,
Here is the schematic...
|
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9225 Location: Greensville,Ontario
|
|
Posted: Fri Jan 08, 2016 1:39 pm |
|
|
Nice...
some comments
1) you need some kind of current limiting R in the battery connection. If you connect the battery without, you'll send a big 'spike' to the PIC...not a good idea...aka 'latchup' or 'blowup' is possible
2) battery needs a small load ,based on mAHr rating
3)battery should not be connected until after PIC has powered up.Potential 'backfeed' problems.
4)PIC should be configured then say a 100ms delay before reading battery. This 'configure' process should include disabling any peripherals on pins not used,clearing any interrupt bits,etc.,maybe flash OK LED 3 times to signal 'ready'.
Jay |
|
|
pilar
Joined: 30 Jan 2008 Posts: 197
|
|
Posted: Fri Jan 08, 2016 2:00 pm |
|
|
Hi temtronic,
I made a mistake, this is right schematic...
My question is, by reason the comparator does not detect changes if the voltage is below 3.6V after a reset. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19504
|
|
Posted: Fri Jan 08, 2016 2:44 pm |
|
|
The comparator interrupt is an interrupt on change, not an interrupt on level. At the start it won't trigger.
Your code needs to test whether the comparator is high or low before it all starts.
Think of it this way. The text in your high message should say the voltage has _changed_ high. While the voltage in the low interrupt message should say the voltage has _changed_ low.
To have the message show at reset you need to test the comparator output in code and have the code say whether it is high or low. |
|
|
pilar
Joined: 30 Jan 2008 Posts: 197
|
|
Posted: Fri Jan 08, 2016 3:45 pm |
|
|
Hi Ttelmah,
Please, coud you tell me how to test the comparator is high or low before it all starts, please give me a sample code... |
|
|
|