Continue to Site

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.

How to measure pulse low period using CCP

Status
Not open for further replies.

engr.waqas

Full Member level 3
Joined
Jul 21, 2009
Messages
172
Helped
13
Reputation
26
Reaction score
10
Trophy points
1,298
Location
karachi,Pakistan
Activity points
2,342
Hi
I'm using CCS C compiler's example file to measure pulse width( high time). I want to change this example program to measure pulse low time. Can you please help me how can I do that ?
Below the example file which measure high time.

long rise,fall,pulse_width;

#int_ccp2
void isr()
{
rise = CCP_1;
fall = CCP_2;

pulse_width = fall - rise; // CCP_1 is the time the pulse went high
} // CCP_2 is the time the pulse went low
// pulse_width/(clock/4) is the time

// In order for this to work the ISR
// overhead must be less than the
// low time. For this program the
// overhead is 45 instructions. The
// low time must then be at least
// 9 us.

void main()
{
printf("\n\rHigh time (sampled every second):\n\r");
setup_ccp1(CCP_CAPTURE_RE); // Configure CCP1 to capture rise
setup_ccp2(CCP_CAPTURE_FE); // Configure CCP2 to capture fall
setup_timer_1(T1_INTERNAL); // Start timer 1

enable_interrupts(INT_CCP2); // Setup interrupt on falling edge
enable_interrupts(GLOBAL);

while(TRUE) {
delay_ms(1000);
printf("\r%lu us ", pulse_width/5 );
}
}
 

Read the value in interrupt and also use,
setup_ccp1(CCP_CAPTURE_RE); // Configure CCP1 to capture rise
setup_ccp2(CCP_CAPTURE_FE); // Configure CCP2 to capture fal
l
in interrupt.
 

Thanks for your reply.
I already solved the problem few mins ago
here is working code for other's help


#include <18F452.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)



long rise,fall,pulse_width;

#int_ccp1
void isr()
{
rise = CCP_1;
fall = CCP_2;

pulse_width = rise-fall; // CCP_1 is the time the pulse went high
} // CCP_2 is the time the pulse went low
// pulse_width/(clock/4) is the time

// In order for this to work the ISR
// overhead must be less than the
// low time. For this program the
// overhead is 45 instructions. The
// low time must then be at least
// 9 us.

void main()
{
printf("\n\rLow time (sampled every second):\n\r");

setup_ccp2(CCP_CAPTURE_FE); // Configure CCP2 to capture fall
setup_ccp1(CCP_CAPTURE_RE); // Configure CCP1 to capture rise
setup_timer_1(T1_INTERNAL); // Start timer 1

enable_interrupts(INT_CCP1); // Setup interrupt on rise edge
enable_interrupts(GLOBAL);

while(TRUE) {
delay_ms(1000);
printf("\r%lu us ", pulse_width/5 );
}
}
 

A hardware solution could be to invert the signal using a transistor or NOT gate and use the code (the first one). Since the signal is now inverted, you are actually reading the low time.
:smile:
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top