View previous topic :: View next topic |
Author |
Message |
Panos
Joined: 09 Jun 2010 Posts: 8
|
Calculate the period of a pulse using ccp |
Posted: Fri Jun 25, 2010 12:11 pm |
|
|
Hello !
I try to use this simple program:
Code: |
/////////////////////////////////////////////////////////////////////////
//// EX_CCPMP.C ////
//// ////
//// This program will show how to use the built in CCP to ////
//// measure a pulse width. ////
//// ////
//// Configure the CCS prototype card as follows: ////
//// Connect a pulse generator to pin C2 and pin C1 ////
//// ////
//// Jumpers: ////
//// PCM,PCH pin C7 to RS232 RX, pin C6 to RS232 TX ////
//// ////
//// This example will work with the PCM and PCH compilers. The ////
//// following conditional compilation lines are used to include a ////
//// valid device for each compiler. Change the device, clock and ////
//// RS232 pins for your hardware if needed. ////
/////////////////////////////////////////////////////////////////////////
//// (C) Copyright 1996,2003 Custom Computer Services ////
//// This source code may only be used by licensed users of the CCS ////
//// C compiler. This source code may only be distributed to other ////
//// licensed users of the CCS C compiler. No other use, ////
//// reproduction or distribution is permitted without written ////
//// permission. Derivative programs created using this software ////
//// in object code form are not restricted in any way. ////
/////////////////////////////////////////////////////////////////////////
#include <16F877.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_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 );
}
} |
which is included in ccs compiler as:EX_CCPMP.C. I Connect a pulse generator to pin C2 and pin C1 as it wants and i use 20000000Mz but the result is this message:
High time (sampled every second):
0us
0us
0us
...
I've tried many frequencies (of the pulse generator): 10Hz,1Hz,1KHz,3KHz, 10KHz,100KHz,1MHz,10MHz but the result is always 0us!
Why is this happening? Am I doing something wrong?
Also I would like to have a 4000000 crystal not 20000000 but when I change
the #use delay(clock=20000000) to #use delay(clock=4000000) there is no message to the rs232 panel!!!
What I should do to use 4MHz crystal? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Jun 25, 2010 12:23 pm |
|
|
The #use delay() value should match the crystal frequency.
What are the voltage levels of the signal on the CCP pins ?
If the PIC is running at 5 volts, the signals need to go from 0v to 4v
at least, and preferably from 0v to at least 4.5v. |
|
|
Panos
Joined: 09 Jun 2010 Posts: 8
|
|
Posted: Fri Jun 25, 2010 1:41 pm |
|
|
The voltage in ccp pins is 5v. And the crystal, when I use the command #use delay(clock=20000000) is 20000000! When I change this command to #use delay(clock=4000000) I change the crystal too to 4000000.
(I'm sorry I was late to answer ) |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Jun 25, 2010 2:03 pm |
|
|
I copied and pasted your program into MPLAB and compiled it with
compiler vs. 4.109. I programmed it into a 16F877 with a 20 MHz
crystal on my PicDem2-Plus board. I added a "\n" to the start
of the printf() statement, so it would put each reading on a new line.
I attached a B&K function generator to pins C1 and C2 on the PIC.
I set it for a 1 KHz square wave with 50% duty cycle, going from 0 to 5v.
It works. I got the following output on the terminal window in my PC:
Quote: |
High time (sampled every second):
498 us
498 us
497 us
498 us
497 us
498 us
498 us
497 us
|
|
|
|
Panos
Joined: 09 Jun 2010 Posts: 8
|
|
Posted: Fri Jun 25, 2010 2:14 pm |
|
|
So the program seems to work right! I have to search in which detail of the hardware I'm wrong !
Thank you very much PCM !!! |
|
|
|