View previous topic :: View next topic |
Author |
Message |
nikin78
Joined: 20 Sep 2007 Posts: 11
|
CCP2 Not Working |
Posted: Thu Oct 11, 2007 10:33 am |
|
|
I am using the following setup
setup_ccp1(CCP_CAPTURE_RE); // Configure CCP1 to capture rise
setup_timer_1(T1_INTERNAL|T1_DIV_BY_4); // Start timer 1
enable_interrupts(INT_CCP1); // Setup interrupt on falling edge
enable_interrupts(GLOBAL);
When I use this same code but change all of the ccp1 to ccp2, the pulse_width I get is random anybody have a clue why this happens. |
|
|
nikin78
Joined: 20 Sep 2007 Posts: 11
|
another question on same subject |
Posted: Thu Oct 11, 2007 11:26 am |
|
|
Can I use the same signal for CPP1 and CPP2 where CCP1 is set up for rising edge and CCP2 is setup for falling edge? I used the CCPMP.c example to do this but it didn't work. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Oct 11, 2007 11:30 am |
|
|
Describe the input signal in detail:
1. What device is producing the signal ?
2. Is it an analog or digital signal ?
3. Describe the waveform. Frequency, pulse lengths, etc.
4. What are the high and low voltage levels on the waveform ?
5. What is the overall purpose of reading this signal ? |
|
|
nikin78
Joined: 20 Sep 2007 Posts: 11
|
|
Posted: Thu Oct 11, 2007 11:32 am |
|
|
Describe the input signal in detail:
1. What device is producing the signal ? frequency generator
2. Is it an analog or digital signal ? 60hz square wave with 5Vpp
3. Describe the waveform. Frequency, pulse lengths, etc.
4. What are the high and low voltage levels on the waveform ?
5. What is the overall purpose of reading this signal ? Purpose is to make sure there is a 50% duty cycle coming out of the frequency generator. |
|
|
nikin78
Joined: 20 Sep 2007 Posts: 11
|
|
Posted: Thu Oct 11, 2007 11:35 am |
|
|
this is how my pic is setup
#include <18F4550.h>
#define ICD = TRUE
#fuses HSPLL,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV1,VREGEN
#use delay(clock=48000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
and this is how my timer and interupts are setup
setup_timer_1(T1_INTERNAL|T1_DIV_BY_8); // Start timer 1
enable_interrupts(INT_CCP1); // Setup interrupt on falling edge
enable_interrupts(GLOBAL); |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Oct 11, 2007 11:43 am |
|
|
Please don't start new threads on the same topic. It turns out that I
answered the question of using Ex_CCPMP.c with a 48 MHz clock in
this earlier thread.
http://www.ccsinfo.com/forum/viewtopic.php?t=32275
If it doesn't work, then tell us the results that you did get.
If you get random results, then post a list of the results.
And do it in this thread. |
|
|
nikin78
Joined: 20 Sep 2007 Posts: 11
|
|
Posted: Thu Oct 11, 2007 11:55 am |
|
|
so here is the thing I got everything working for the CCPMP.c example, but this week things started going wrong. so i tried changing the isr code to only using one capture line. the problem here is that sometimes when i use CCP1 everything works good. If I use CCP2 this code does not work. for the following code using CCP1 the pulse_width is a steady 49000us or so. this is correct. if i do the same code but substitute CPP1 for CPP2 I get random values. my question is why do I get random values for CPP2 and everything is good on CPP1?
/////////////////////////////////////////////////////////////////////////
//// EX_CCPMP2.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 <18F4550.h>
#define ICD = TRUE
#fuses HSPLL,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV1,VREGEN
#use delay(clock=48000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
//#define A_IO (0b00000000) // Used to define TRISA
//#define B_IO (0b00000000) // Used to define TRISB
//#define C_IO (0b00000110) // Used to define TRISC
//#define D_IO (0b00000100) // Used to define TRISD
#define LEDY PIN_B4
#define LEDR PIN_B5
#define LED_ON output_low
#define LED_OFF output_high
int flag;
long ACWave,GPSIPPS,rise,fall,pulse_width;
//pulse_width is calculated as follows.
//Clock is 48MHz. System clock is 48MHz/4 which is 12Mhz. Timer1 will not
//get the delay for a 50Hz or 60Hz signal it is too fast at 12MHz. So we use
//the prescalar for Timer1 and divide 12Mhz by 4. This makes Timer1 operate
//at 3,000,000 clocks/sec, which is good for getting a 50MHz and 60MHz signal.
#int_ccp1
void isr()
{
int16 current_ccp;
static int16 old_ccp = 0;
// Read the 16-bit hardware CCP1 register
current_ccp = CCP_1; // From 16F877.H file
// Calculate the time interval between the
// previous rising edge of the input waveform
// and the current rising edge. Put the result
// in a global variable, which can be read by
// code in main().
pulse_width = current_ccp - old_ccp;
// Save the current ccp value for the next pass.
old_ccp = current_ccp;
flag = 1;
// 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.
// flag = 1;
}
void main()
{
// set_tris_a(A_IO); // Set to the Value in A_IO
// set_tris_b(B_IO); // Set to the Value in B_IO
// set_tris_c(C_IO); // Set to the Value in C_IO
// set_tris_d(D_IO); // Set to the Value in D_IO
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
//Clock is 48MHz. System clock is 48MHz/4 which is 12Mhz. Timer1 will not
//get the delay for a 50Hz or 60Hz signal it is too fast at 12MHz. So we use
//the prescalar for Timer1 and divide 12Mhz by 4. This makes Timer1 operate
//at 3,000,000 clocks/sec, which is good for getting a 50MHz and 60MHz
//signal.
//To get Timer1 clock speed here is the equation:
//(48MHz/4)/4 = 3,000,000 clocks/second
setup_timer_1(T1_INTERNAL|T1_DIV_BY_4); // Start timer 1
enable_interrupts(INT_CCP1); // Setup interrupt on falling edge
enable_interrupts(GLOBAL);
while(TRUE) {
delay_ms(1000);
//Since Timer1 runs at 3,000,000 clock ticks/sec we divide by pulse_width
//by 3,000 to get milliseconds.
if(flag == 1)
{
printf("\r\n%lu us ", pulse_width );
flag = 0;
}
if(pulse_width >= 23500 && pulse_width <= 25000)
{
LED_ON(LEDY);
}
else
{
LED_OFF(LEDY);
}
if(pulse_width >= 29500 && pulse_width <= 31000)
{
LED_ON(LEDR);
}
else
{
LED_OFF(LEDR);
}
}
} |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Oct 11, 2007 12:03 pm |
|
|
You're using some tachometer code that I posted in an earlier thread.
That's not the same as the code to measure a pulse.
I don't have time right now, but later in the week I can duplicate your
hardware setup and test it with the Ex_ccpmp.c program. |
|
|
Guest
|
|
Posted: Thu Oct 11, 2007 1:45 pm |
|
|
well i have some things i would like to try in the mean time. if i get this thing working i will let you know. |
|
|
nikin78
Joined: 20 Sep 2007 Posts: 11
|
|
Posted: Thu Oct 11, 2007 2:41 pm |
|
|
so i took another look at my code and i realized the CCPMP.C never sets any tris bits probably because of the pic chips that they use dont need to worry about that. I use a 4550 which has two lines for CCP2. in order for me to get this correct, i need to set my tris bit for RC1 to make CCP2 multiplex to RC1. Thanks for the help, but i think i got it. |
|
|
|