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

Problems with a comparator after a reset

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



Joined: 30 Jan 2008
Posts: 172

View user's profile Send private message

Problems with a comparator after a reset
PostPosted: Fri Jan 08, 2016 9:15 am     Reply with quote

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: 14728

View user's profile Send private message

PostPosted: Fri Jan 08, 2016 9:29 am     Reply with quote

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: 172

View user's profile Send private message

PostPosted: Fri Jan 08, 2016 9:57 am     Reply with quote

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: 6846
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Fri Jan 08, 2016 12:14 pm     Reply with quote

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: 172

View user's profile Send private message

PostPosted: Fri Jan 08, 2016 12:46 pm     Reply with quote

Hi temtronic,

Here is the schematic...

temtronic



Joined: 01 Jul 2010
Posts: 6846
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Fri Jan 08, 2016 1:39 pm     Reply with quote

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: 172

View user's profile Send private message

PostPosted: Fri Jan 08, 2016 2:00 pm     Reply with quote

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: 14728

View user's profile Send private message

PostPosted: Fri Jan 08, 2016 2:44 pm     Reply with quote

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: 172

View user's profile Send private message

PostPosted: Fri Jan 08, 2016 3:45 pm     Reply with quote

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...
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