I want to use inbulit comparator in pic16f877a. I think it can be compare positive and negative voltage. if its positive voltage than output will high and its negative voltage then output will lowuse a zero crossing detector circuit(opamp comparator based).
start a timer at first pulse and end it after you detect the second pulse.
time interval is the timer value * period of your timer clock.
For precise timing measurements, CCP is the way to go, it's the only way to get it crystal accurate without e.g. interrupt latency effects. Internal comparator can't directly drive CCP, if you want it though, you need an external loopback connection from comparator output to CCP input.
I say, you can use the internal comparator with a loop back connection. Generally I want you to read the data sheet and think yourself.Exactly what do you mean ? Do i need to use external comparator?
Surely not. But it would be sufficient to clamp negative input voltage. I wonder if you even need a comparator. Most zero crossing solutions work without it.I assume the PIC and it´s comparator input can´t handle negative input voltages.
Okay I looked datasheet and some links and I tried to understand basic of Capture mode in PICHi,
I assume the PIC and it´s comparator input can´t handle negative input voltages. Read datasheet.
Also read about CCP in the datasheet.
Klaus
void main(void)
{
//Make all PORTD pins low
PORTA = 0;
PORTB = 0;
PORTC = 0;
PORTD = 0;
// Configured RB0 as Output
TRISB0 = 0; //RB0 as Output PIN
//Configured RC2/CCP1 pin as input
TRISC2 = 1;
T1CON = 0x01; // set Timer 1
CCP1CON = 0b00000101; // Set Module to capture on rising edge
INTCON = 0xc0; //Enabled Global interrupts
CCP1IE = 1; //Enabled CCP1 interrupt
while(1);
}
void interrupt CCP1_ISP()
{
// CCP1 Interrupt
if(CCP1IF == 1) // if the CCP1 Interrupt flag is set...
{
// Read capture timer and save the value//
}
}
I want to write program to measure time for every crossing.Hi,
basically correct.
Just the math is missing.
in case you expect bouncing signals:
* You know the expectable value. Maybe 10ms. So you know when the measured value is much smaller than the expected value .. you just may dismiss the captured value.
Klaus
#include <xc.h>
unsigned int value1, value2;
void main(void)
{
//Make all PORTD pins low
PORTA = 0;
PORTB = 0;
PORTC = 0;
PORTD = 0;
// Configured RB0 as Output
TRISB0 = 0; //RB0 as Output PIN
//Configured RC2/CCP1 pin as input
TRISC2 = 1;
T1CON = 0x01; // set Timer 1
CCP1CON = 0b00000101; // Set Module to capture on rising edge
INTCON = 0xc0; //Enabled Global interrupts
CCP1IE = 1; //Enabled CCP1 interrupt
while(1);
}
void interrupt CCP1_ISP()
{
// CCP1 Interrupt
if(CCP1IF == 1) // if the CCP1 Interrupt flag is set...
{
value1 = CCPR1H;
value2 = CCPR1L;
}
}
What´s unclear with the description of post#6?How to measure time for every crossing?
Yes I have somethings that I don't know how to implement in programHi,
What´s unclear with the description of post#6?
In short: CCP just safes the timestamp of the CCP event (zero cross)
You need to save this timestamp (t0) and wait for the next CCP event.
you may calculate delta_t = t_new - t0
then save t_new at the place of t0
do this in a loop
--> delta_t is the time difference in clock ticks from one zero_cross to the next zero_cross.
Klaus
void interrupt CCP1_ISP()
{
// CCP1 Interrupt
if(CCP1IF == 1) // if the CCP1 Interrupt flag is set...
{
value1 = CCPR1H;
value2 = CCPR1L;
}
}
* New_ccp = ccp1h, ccp1l
* Delta_t = new_ccp - t0
* t0 = new_ccp
Hi,
Since CCP1 represents a 16 bit value I recommend to use uint16.
For sure you may use a nother variable: new_ccp (just local in ISR)
And you need the variable: delta_t (global)
Then (pseudo code)Code:* New_ccp = ccp1h, ccp1l * Delta_t = new_ccp - t0 * t0 = new_ccp
No need for a loop
Klaus
This is a very basic job. Every tutorial will show how to do it. I even assume that the PIC datasheet tells you how to access 16 bit register values.Can you please provide a link or examples on how to store 16 bit data in 16 bit register example
I recommend to set counter frequency that it never counts more than 65536 with your lowest input signal frequency, then you don´t have to take care about timer overflow. Math will be correct.In any case, you should consider that timer 1 overflows at times. It may be necessary to track, possibly count it.
Hi,
This is a very basic job. Every tutorial will show how to do it. I even assume that the PIC datasheet tells you how to access 16 bit register values.
--> You urgently need to go through some basic tutorials. There will be even video tutorials.
Klaus
void main(void)
{
uint8_t T1;
}
So it's apparently not defined in the default include files. Which compiler are you using?When I wrote uint8_t in program it show error
#include < xc.h>
#include < stdint.h>
#include < stdlib.h>
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?