void main (void)
{
unsigned int period;
TRISCbits.TRISC2 = 1; /* configure CCP1 pin for input */
T3CON = 0x81; /* use Timer1 as the time base for CCP1 capture */
PIE1bits.CCP1IE = 0; /* disable CCP1 capture interrupt */
PIR1bits.CCP1IF = 0; /* clear the CCP1IF flag */
T1CON = 0x81; /* enable 16-bit Timer1, prescaler set to 1 */
CCP1CON = 0x05; /* capture on every rising edge */
while (!(PIR1bits.CCP1IF)); /* wait for 1st rising edge */
PIR1bits.CCP1IF = 0;
period = CCPR1; /* save the first edge (CCPR1 is accessed as a 16-bit value) */
while (!(PIR1bits.CCP1IF)); /* wait for the 2nd rising edge */
CCP1CON = 0x00; /* disable CCP1 capture */
period = CCPR1 - period;
}
#include <p18f4620.h>
#include <usart.h>
#pragma config DEBUG=OFF
#pragma config OSC=INTIO7
#pragma config WDT=OFF
#pragma config LVP=OFF
#pragma config PWRT=OFF
#pragma config MCLRE=ON
void main (void)
{
unsigned int t1;
unsigned int t2;
unsigned int Gap;
char str[25];
//Clock Setup
OSCCON = 0b01110000; //select 8 MHz clock
OSCTUNE |= 0b01000000; //Fosc = 32 Mhz
TRISCbits.TRISC2 = 1 ; // Configure CCP1 pin as input
T3CON = 0x81; //User Timer 1 as base timer
PIE1bits.CCP1IE = 0; //Disable CCP1 capture interrupt
PIR1bits.CCP1IF = 0; //Clear CCP1IF flag
T1CON = 0x81;
TMR1H = 0x00;
TMR1L = 0x00;
// USART 9600 8-N-1
OpenUSART (USART_TX_INT_OFF & USART_RX_INT_OFF & USART_ASYNCH_MODE & USART_EIGHT_BIT & USART_CONT_RX & USART_BRGH_LOW, 51); // Baud Rate = 9600, 51
while(1) {
CCP1CON = 0b00000100 ; //Capture falling edge
while (!(PIR1bits.CCP1IF)); //Wait for 1st falling edge
PIR1bits.CCP1IF = 0;
t1 = CCPR1;
CCP1CON = 0b00000101 ; // Capture rising edge
while (!(PIR1bits.CCP1IF)); // Wait for rising edge
PIR1bits.CCP1IF = 0;
t2 = CCPR1;
CCP1CON = 0b00000100 ; //Capture falling edge
Gap = t2 - t1 ;
ultoa(Gap,str);
putsUSART(str);
putrsUSART("\n\r");
}
}
Allright since we started talking about CCP , Im back on it. This time I am trying to find the gap between two sqare waves ( pdf with an image attached).
Input : 12 V Square wave at RC2 ( CCP1 pin) of no set frequency
Methodology:
1) Set CCP to capture falling edge
2) Capture falling edge and save the value of timer 1 to t1
3) Set CCP to capture rising edge
4) Capture rising edge and save the value of timer 1 to t2
5) Gap = t1-t2
5) Gap = t1 - t2
5) Gap = t2 - t1
I do however have a question - how do I capture the Gap if it is greater than 2^16 times the Timer 1. So how do I account for the timer1 if it overflows while Im still measuring the gap. :\ I have set timer one as a 16 bit timer.
15.2.3 SOFTWARE INTERRUPT
When the Capture mode is changed, a false capture
interrupt may be generated. The user should keep the
CCPxIE interrupt enable bit clear to avoid false
interrupts. The interrupt flag bit, CCPxIF, should also be
cleared following any such change in operating mode.
while(1) {
CCP1CON = 0b00000100 ; //Capture falling edge
PIR1bits.CCP1IF = 0;
while (!(PIR1bits.CCP1IF)); //Wait for 1st falling edge
t1 = CCPR1;
CCP1CON = 0b00000101 ; // Capture rising edge
PIR1bits.CCP1IF = 0;
while (!(PIR1bits.CCP1IF)); // Wait for rising edge
t2 = CCPR1;
// CCP1CON = 0b00000100 ; //Capture falling edge // May Not Be Needed
Gap = t2 - t1 ;
ultoa(Gap,str);
putsUSART(str);
putrsUSART("\n\r");
}
I have an off-the-shelf sensor that gives me continuous data stream of 12v , 0v.
I tried debugging the code today and found that once PIR1bits.CCP1IF bit is set to 1, it doesnt really clear up afterwards. Even though Im clearing the value in the code after interrupt.
You can count overflows of timer 1 and combine that overflow count with the captured 16-bits of timer 1 to obtain an extended captured value, maybe 32 bits. But be very careful about how you handle the condition where an input capture event occurs at nearly the same time as an overflow event on timer 1. If you are not careful you might combine a captured timer 1 from just before the overflow with an overflow count that reflects just after the overflow, or vice versa. Here is what works for me:Gaps could be as long as 10-20 seconds [ sometimes even longer]. I looked up a technique to count the number of overflows timer 1 experience to measure longer gap. I will try to implement that once the code starts working for smaller gap.
I will try clearing the flag thingi as soon as I get back to my workplace.
If you manually set the variable Gap to a numerical value and then have the ultoa() routine perform the conversion what is the resulting string?
Can you successfully output a string stored in the character array str?
BigDog
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?