If I'm right, internal voltage ref of the pic16f628a is relative to vdd...
Yes. I set 1.93v as Vref on AN1 pin.
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
speedEC said:I have tried with 22k, 47K. RA1 to RB3. But I receive wrong result.
#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();
}
}
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.
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
VRCON = 0b11101101; // setting Voltage refernce for 1.93v
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.
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?