Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] To solve Histeresis problem in PIC16F628A when using comparators

Status
Not open for further replies.

speedEC

Full Member level 6
Joined
Apr 1, 2011
Messages
337
Helped
23
Reputation
46
Reaction score
21
Trophy points
1,298
Activity points
4,326
Dear All,

I previously used LM358 IC to monitor battery voltage of my 6v, 4.2Ah sealed lead acid battery. It consumes more current even device is in idle status, since it has connected directly to battery. So, I have decided to use on chip analog comparators provided on PIC16F628A to extend the battery life double. If so, I can monitor the battery at the desired interval, say for example everyday morning. I have designed the circuit as follows and it works well. But, I need to design hysteresis circuit for this comparator. Because, LED is lit lightly in small variations. I have to stop the LED and ON the LED only when the comparator output reaches exact value. Can anyone help me to design hysteresis circuit?

View attachment COMPARATOR_PIC16F628A.bmp

Thanks
pmk
 

cent24.3

Member level 2
Joined
Jul 14, 2004
Messages
46
Helped
4
Reputation
8
Reaction score
3
Trophy points
1,288
Activity points
304
Hi speedEC,

Can you explain what is your voltage reference for the comparator ?
If I'm right, internal voltage ref of the pic16f628a is relative to vdd...
 

speedEC

Full Member level 6
Joined
Apr 1, 2011
Messages
337
Helped
23
Reputation
46
Reaction score
21
Trophy points
1,298
Activity points
4,326
If I'm right, internal voltage ref of the pic16f628a is relative to vdd...

Yes. I set 1.93v as Vref on AN1 pin.

thanks
pmk
 

cent24.3

Member level 2
Joined
Jul 14, 2004
Messages
46
Helped
4
Reputation
8
Reaction score
3
Trophy points
1,288
Activity points
304
Yes. I set 1.93v as Vref on AN1 pin.

So you are trying to detect a voltage variation of the battery voltage but the Vdd of your pic is varying according to the battery voltage (so do the Vref of the pic).
According to microchip data sheet:
The equations used to calculate the output of the
Voltage Reference are as follows:
if VRR = 1: VREF = (VR<3:0>/24) x VDD
if VRR = 0: VREF = (VDD x 1/4) + (VR<3:0>/32) x VDD

To make it work better you have to consider a fixed voltage ref.
 

alexxx

Advanced Member level 4
Joined
Apr 17, 2011
Messages
1,013
Helped
273
Reputation
552
Reaction score
270
Trophy points
1,383
Location
Greece
Activity points
7,938
The below image is from LM358 datasheet. It explains how can you achieve your goal, but have in mind that R2>>R1.

comparator_hysteresis.JPG


Hope that helped.
 

speedEC

Full Member level 6
Joined
Apr 1, 2011
Messages
337
Helped
23
Reputation
46
Reaction score
21
Trophy points
1,298
Activity points
4,326
alexxx, Already I have tried with this. But, wrong result I get. That is, I connected RA1(through voltage divider) to RB3 pin (Output - LED Connected to this pin). If I am wrong pl update me.

thanks
pmk

---------- Post added at 17:37 ---------- Previous post was at 17:36 ----------

I have tried with 22k, 47K. RA1 to RB3. But I receive wrong result.
 

alexxx

Advanced Member level 4
Joined
Apr 17, 2011
Messages
1,013
Helped
273
Reputation
552
Reaction score
270
Trophy points
1,383
Location
Greece
Activity points
7,938
speedEC said:
I have tried with 22k, 47K. RA1 to RB3. But I receive wrong result.


If R2 is not very much bigger than R1, the possitive feedback effects will become stronger. Ideally to have a comparator you need an open switch feedback. So try something like R2=470K, R1=1K, make sure that it is working as comparator first, and then experiment with resistor values to achieve the threshold you need.
 

cent24.3

Member level 2
Joined
Jul 14, 2004
Messages
46
Helped
4
Reputation
8
Reaction score
3
Trophy points
1,288
Activity points
304
I don't know if you've understood what I was trying to point out. :-?

No matter the hysteresis, no matter the circuit you are going to use, the most important is the reference voltage your are comparing the battery voltage with. It has to be a fixed one (not varying with the battery voltage).

In brief it's hard to compare two things that are varying the same way. Your can use a voltage reference like a **broken link removed** (an Ultra-Low-Power Precision Series Voltage Reference) or what you want else.
 

speedEC

Full Member level 6
Joined
Apr 1, 2011
Messages
337
Helped
23
Reputation
46
Reaction score
21
Trophy points
1,298
Activity points
4,326
Hi cent24.3, I understand what you are saying. Thanks for your response to my query. But, I try to eliminate the componenets as much as possible to reduce the circuit size, cost and also power consumption. Because, I am going to run this circuit only using 3.6v, 1500 mAh NiMH Battery. So, power consumption is most critical. Thats why, I preferred to use on-chip comparator provided on PIC16F628A IC. Previously (also still), I have used LM358IC for this purpose. It takes high current.

The following is the code I written to compare voltage of battery with Vref (Internal). But, I have calculated the Vref as follows and compare the input on AN1 (using Voltage divider).

View attachment COMPARATOR_PIC16F628A_EdaBoard.bmp

Code:
#include <htc.h>

__CONFIG(XT & WDTDIS & PWRTDIS & BORDIS & LVPDIS & UNPROTECT & MCLRDIS);

#define BATT_STATUS_HIGH C2OUT
#define BATT_LOW_LED RB3
void delay_ms(unsigned int duration){ // delay in miliseconds for 4.0MHZ crystal
    unsigned int i;
    for(;duration!=0;duration--)
    {
        for(i=0;i<=50;i++)
        {
            _asm
                nop
                nop
                nop
            _endasm
        }
        _asm
            nop
            nop
        _endasm
    }
}

void main(void){

	CMCON = 0b00100101; //select mode 7 (101)
    VRCON = 0b11101101; // setting Voltage refernce for 1.93v
                        // to select 13
	delay_ms(1000); // 1 sec delay
	
    TRISA = 0b11111111;
    TRISB = 0b00000000;
    PEIE = 1; // Enable Pheriperal Interrupts
	GIE = 1; // setting GLOBAL Interrupts
    ei(); // enable all Interrupts
    
	while (1){
	       BATT_LOW_LED = !BATT_STATUS_HIGH;
		   delay_ms(1000); // 1 sec delay
	       CMCON;    //Read CMCON to clear mismatch condition
	       CMIF = 0; // clear CMCON Flag
           CMIE = 1; //Enable Comparator Interrupt
      SLEEP();
	}
}
 

speedEC

Full Member level 6
Joined
Apr 1, 2011
Messages
337
Helped
23
Reputation
46
Reaction score
21
Trophy points
1,298
Activity points
4,326
Everything seems working fine except LED lit lightly when voltages not equal (It should be OFF). Any help would be highly appreciated and thank full.

Thanks
pmk

---------- Post added at 14:06 ---------- Previous post was at 13:51 ----------

If R2 is not very much bigger than R1, the possitive feedback effects will become stronger. Ideally to have a comparator you need an open switch feedback. So try something like R2=470K, R1=1K, make sure that it is working as comparator first, and then experiment with resistor values to achieve the threshold you need.

I have read Microchip document and it says in order to solve hysteresis problem it recommends 39K (If 10K and 8K2 combination voltage divider used).

from the datasheet:
Example:
• A VDD = 5.0V, VH = 3.0V and VL = 2.5V
• VAVG = 2.77V
• R = 8.2k and R2 = 10k, gives a VAVG = 2.75V
• REQ = 4.5k
• DR = .1
• R3 = 39k (40.5 calculated)
• VHACT = 2.98V
• VLACT = 2.46V

But I have tried both as per datasheet and your suggestion. Both didn't work. Can you please help.

For your information, I have used solder board. May that could be a problem?

thanks
pmk
 
Last edited:

cent24.3

Member level 2
Joined
Jul 14, 2004
Messages
46
Helped
4
Reputation
8
Reaction score
3
Trophy points
1,288
Activity points
304
Hi speedEC, in your source code in comment it's written :
VRCON = 0b11101101; // setting Voltage refernce for 1.93v

It's not 1.93v, it's 13/24 of Vdd.
If Vdd is 3.6v then Vref is 1.95v, if Vdd is decreasing to 3.3v (for example) then Vref is 1.79v.

If I understand what you are trying to do, you want to compare this voltage with the battery voltage via a voltage divider made of resistors (like the schematic). If I'm right you are going to compare two voltage that are varying EXACTLY the same way...

That's what I was trying to point out.

And for the power consideration the MAX6029 needs only 5.25μA (max), it's far less than the pic.

Any way else, to create an hysteresis with the pic you just have to change the VR value when the comparator triggers.

ps: in your program you are enabling interrupt but you have no interrupt routine.
 

speedEC

Full Member level 6
Joined
Apr 1, 2011
Messages
337
Helped
23
Reputation
46
Reaction score
21
Trophy points
1,298
Activity points
4,326
Thanks cent24.3,

Any way else, to create an hysteresis with the pic you just have to change the VR value when the comparator triggers.

Can you pl explain this?

ps: in your program you are enabling interrupt but you have no interrupt routine.

Yes. I have not used Interrupt routine. because I have to use loops (to tell the user about the LOW BATTERY Level thro' beep sound with specified interval using timer0) in my next step and to avoid loops inside the ISR i have not used ISR in my code.

Thanks
pmk
 

cent24.3

Member level 2
Joined
Jul 14, 2004
Messages
46
Helped
4
Reputation
8
Reaction score
3
Trophy points
1,288
Activity points
304
So, even if I think what you are trying to do is nonsense. I can explain these points.

1)
Create an hysteresis by changing VR

In your case you have set de Vref to 13/24 of vdd.
If the external voltage on AN1 decreases under this value the comparator detect that transition A (Vin- < Vin+).
In your program, when the comparator detect a transition A, it changes the Vref value to 14/24 of Vdd (for exemple).
Then after that, if the voltage on AN1 increase, it has to rise 14/24 of Vdd to be detected as a transition B (Vin- > Vin+) by the comparator.
In your program, when the comparator detect a transition B, it changes back the Vref value to 13/24 of Vdd (the initial value).
Like this you've created an hysteresis of 1/24 of Vdd.

2)
You must not enable interrupt if you don't have an ISR (that's probably what leads the strange behaviour of your program) .
I think, in your case you just have to poll the C2OUT bit.
But with that I think you are not very familiar with pic programming, I suggest you to read carefully the datasheet.
 
Last edited:

speedEC

Full Member level 6
Joined
Apr 1, 2011
Messages
337
Helped
23
Reputation
46
Reaction score
21
Trophy points
1,298
Activity points
4,326
I have solved the problem. I have used feedback resistor of 22 K between RA1 and RA2 pins. Everything works fine when I tried on PCB. Before, I used solder board. That might be a problem I think.

---------- Post added at 01:29 ---------- Previous post was at 01:26 ----------

Also as cent24.3 suggested, I have used LM2941T IC for supplying power to PIC MCU for constant power supply of +5v. Previously, I have not used this IC for testing on Solder Board. This could also be a problem.

thanks to all who helped to solve the issue.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top