News:

Let's find out together what makes a PIC Tick!

Main Menu

Playing with comparator

Started by keytapper, Aug 11, 2022, 04:10 PM

Previous topic - Next topic

keytapper

Hi forum,
I started to look at the comparator, because I run out of contiguous pin and I would like to catch a power drop, in order to save important data in EEPROM. Probably the triggering should be in a voltage window that will allow to save 4 bytes.
My experiments gave me a figure of 8 mSec per byte.

I never used this module, I still unsure how to make a good result.
My setting are as follow:
CVRCON = %10101111                  ' Setting Comparator for high level
CMCON =  %00000110                  ' multiplex op-amp
INTCON = %11000000                  ' GIE, PEIE interrupts enabled
'8<-------------'8<-------------'8<-------------'8<-------------'8<-------------
ISRoutine:
Context Save
  If  CMIF = TRUE Then              ' detecting a edge variation
    Set alarm
    Clear CMIE                      ' just to stop looping
    Clear CMIF
  End If
Context Restore
I'm testing by the simulation, but it seems that once the comparator has reached the low level, it won't get out from the ISR.
So when I want to re-enable the interrupt it will plunge to the ISR immediately.
Where's my mistake?
Ignorance comes with a cost

tumbleweed

The output of the comparator is latched. In order to clear the latch and allow you to clear CMIF, you must read or write to the CMCON register first before clearing CMIF.

Or, in your ISR you could just disable CMIE and use CMIF as the alarm flag instead of creating a new 'alarm' variable.

keytapper

I see, it's like the RBIE. Unfortunately the datasheet has little mentions how to clear the mismatch.
Ignorance comes with a cost

tumbleweed

I don't know what device you're using, but for example in the 18F4520 datasheet section 20.6 Comparator Interrupts it says

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

A mismatch condition will continue to set flag bit, CMIF.
Reading CMCON will end the mismatch condition and
allow flag bit, CMIF, to be cleared

I assume you're going to save the data inside the ISR. Otherwise, there's no reason to enable the interrupt in the first place. If you do save it that way make sure that you don't have the interrupt enabled when you read/write the EEPROM in your normal code.

keytapper

@tumbleweed
I got a museum piece  ;D of 16F877A to get rid of
Quote from: The datasheet12.6   Comparator Interrupts
The comparator interrupt flag is set whenever there is a change in the output value of either comparator.
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 occurred. The CMIF bit (PIR registers) is the Comparator Interrupt Flag. The CMIF bit must be reset by clearing it ('0'). Since it is also possible to write a '1' to this register, a simulated interrupt may be initiated.
For the current purpose it's not much important if the interrupt will block in ISR, that would be the last action to accomplish prior the power is lost. But I'm studying how the comparators will do for my know-how and future use.
Ignorance comes with a cost

Stephen Moss

It also goes on to say as tumbleweed mentioned that the CMCON register has the be read before the CMIF can be cleared.
Quote from: keytapper on Aug 11, 2022, 04:10 PMI started to look at the comparator, because I run out of contiguous pin and I would like to catch a power drop, in order to save important data in EEPROM. Probably the triggering should be in a voltage window that will allow to save 4 bytes.
My experiments gave me a figure of 8 mSec per byte.
If that is 8 milliseconds per byte (32mS) total that seems like quite a long time, it may be Ok in a brownout situation but in a power loss situation unless you can switch in a charged capacitor to keep the supply to the PIC running long enough for the save will you actually have time to save the data?

I am not sure that using the comparator is the best option here, as both the PIC supply and the input voltage are using the same power rail will not the voltage difference remain the same, dropping equally on both and therefore not trigger the interrupt as the power rail voltage can never fall below the refence voltage?
I think that is why someone previously posted a solution of using the ADC to monitor the voltage difference between the internal ADC reference voltage (not available on the 16F877a) and power rail as until the voltage rail = the reference voltage the stable reference voltage enables the relative change between voltages to be monitored.   

pjdenyer

I had this issue with one of my projects and used this circuit to give me time to save data to eprom.

https://www.codrey.com/arduino-projects/arduino-power-down-auto-save/

keytapper

Quote from: Stephen Moss on Aug 12, 2022, 07:56 AMI am not sure that using the comparator is the best option here, as both the PIC supply and the input voltage are using the same power rail will not the voltage difference remain the same, dropping equally on both and therefore not trigger the interrupt as the power rail voltage can never fall below the reference voltage?

As pjdenyer mentioning, there would be a capacitor tank (not as huge as 1 Farad ;D ) which has a diode blocking the discharge back to the non existing power. So for a length of few milliseconds the MCU is kept alive by the capacitor reservoir. The comparator will see the power falling and thus it will start to save the 4 bytes. I made such implementation with a 16F628A and I used 330 uF (with margin), but 220 uF could be enough.

For the previous project, I used a transistor to sense the power loss and then pulling down the IOC to initiate the EEPROM writing. For this experiment I'm expecting to set the Vref around 4 V. As soon as the power voltage will miss then the interrupt will trigger. I may consider to turn all PORT into input to prevent power drainage from the MCU. During the first hardware simulation I'll adjust the capacity and the Vref to a safe operating level.
Ignorance comes with a cost

RGV250

Hi,
Have you looked at supercapacitors, I know nothing about them apart from they were used in some of the PLC's at work and could hold the program for hours or longer.

Bob

keytapper

Yeah, I'm aware of them, but I don't have stock, neither intended to do so. I'm pretty sure that less than 2 mF I can get the work done. So for an instance the EEPROM can be written at 2.7V and the capacity should be up from 4V to 2.7V in few hundredth of a second.

There could be the rough figure that suppose to calculate the full MCU load and see how long a capacitor will be dropped of 2 Volts to determine how big it might be the capacitor.
Ignorance comes with a cost