CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Measuring pulse width using CCP
Goto page Previous  1, 2, 3
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Mar 05, 2015 1:15 pm     Reply with quote

Before we look at your code, describe the pulse. Post the voltage levels,
rise time, shape, positive or negative-going, pulse duration, and repetition rate.

Also describe the connections between your pulse generator and the PIC.

Post the Vdd voltage of your PIC.

And post your CCS compiler version.
Aragon



Joined: 19 Mar 2014
Posts: 20

View user's profile Send private message

PostPosted: Thu Mar 05, 2015 3:56 pm     Reply with quote

-The Pulse is a square wave (High = 2.643mS & Low = 7.033mS). duration = 9.676ms
-its a positive raising edge, I am getting a 5VD going in the CCp1 and CCP2, Instead of the pulse signal. The signal generator is controlled by the RB0 pin on the PIC18 to output the signal. The signal is there for the first 3 seconds and then turns off. RB0 is 0.5vdc instead of being 5 vdc.

-The pulse generator is on the same circuit board as PIC18F2620. The Pulse generator is PIC12F675. The output pulse from the PIC12 goes to CCP1 on the PIC18.

-the VDD voltage of the PIC is 4.91V
-The CCS Compiler is V8.70
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Mar 05, 2015 6:16 pm     Reply with quote

Your program doesn't work because you took a bunch of code from
various programs and dumped it all into one program without thinking
about how the CCP works in capture mode. I've written a program
that shows how to do it. I hope you can understand it.

Using a test setup similar to yours, with a 2nd PIC board providing the
test signal, I get the following output on TeraTerm from the CCP capture
board. The pulse duration is a little low, probably because the internal
oscillator is used on the capture board, and it's not precisely 4.000 MHz.
Quote:

2.633 ms
2.633 ms
2.633 ms
2.632 ms
2.632 ms
2.632 ms
2.633 ms
2.633 ms
2.632 ms


Test program:
Code:

#include <18F4520.h>
#fuses INTRC_IO, BROWNOUT, PUT, NOWDT
#use delay(clock=4M)
#use rs232(baud=9600, UART1, ERRORS)

int8 capture_rising_edge;
int8 got_pulse_width;
int16  ccp_delta;

#int_ccp1
void ccp1_isr(void)
{
static int16 t1_rising_edge;

// If current interrupt is for rising edge.
if(capture_rising_edge)
  {
   setup_ccp1(CCP_CAPTURE_FE);
   capture_rising_edge = FALSE;
   t1_rising_edge = CCP_1;
  }
else
  {
   setup_ccp1(CCP_CAPTURE_RE);
   capture_rising_edge = TRUE;
   ccp_delta = CCP_1 - t1_rising_edge;
   got_pulse_width = TRUE;
  }

}

//====================================
void main()
{
float pulse_width_ms;
int16 local_ccp_delta;


got_pulse_width = FALSE;
capture_rising_edge = TRUE;

setup_ccp1(CCP_CAPTURE_RE);
setup_timer_1(T1_INTERNAL | T1_DIV_BY_1);
set_timer1(0);
clear_interrupt(INT_CCP1);
enable_interrupts(INT_CCP1);
enable_interrupts(GLOBAL);


while(TRUE)
  {
     
   // Check if we captured the pulse width. 
   // If so, display the pulse duration.
   if(got_pulse_width)
     {
      disable_interrupts(GLOBAL);
      local_ccp_delta = ccp_delta; 
      enable_interrupts(GLOBAL);

      pulse_width_ms = local_ccp_delta / 1000.0;
      printf("%7.3f ms \n\r", pulse_width_ms);
      got_pulse_width = FALSE;
     }

   delay_ms(500);  // Slow down update rate on TeraTerm
  }



Notice how short this program is, compared to your program. More code
does not equal success. A short program that is completely understood,
equals success.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page Previous  1, 2, 3
Page 3 of 3

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group