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
Aragon



Joined: 19 Mar 2014
Posts: 20

View user's profile Send private message

PostPosted: Fri Oct 24, 2014 9:40 am     Reply with quote

thanks, I will check out the header file.
Aragon



Joined: 19 Mar 2014
Posts: 20

View user's profile Send private message

PostPosted: Fri Oct 24, 2014 1:05 pm     Reply with quote

I had a look at the header file and I made a small change to the code. But it still doesn't work. in the header file they mention

//#define CCP_USE_TIMER3 0x100 OBSOLETE, SEE TIMER-3
// OR in one of the following to use timer 3 with a CCP unit
#define T3_CCP2 0x8

in the void main program
Code:

setup_ccp2(CCP_CAPTURE_RE | T3_CCP2);
setup_timer_3(T3_INTERNAL | T3_DIV_BY_8);


I thought this would have setup timer3 and ccp2 correctly but no luck.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Oct 24, 2014 2:15 pm     Reply with quote

It's in the setup_timer_3() section. It says to OR it in with parameters
for that function, not with setup_ccp2().

It says:

Quote:
// Constants used for SETUP_TIMER_3() are:
Aragon



Joined: 19 Mar 2014
Posts: 20

View user's profile Send private message

PostPosted: Mon Oct 27, 2014 7:52 am     Reply with quote

Thanks PCM Programmer,
I took a look at the header file and I realised the answer was right infront of me. I got the Timer3 to finally work. Had to edit the header file to
Quote:
#define T3_INTERNAL 0x8D
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Oct 27, 2014 8:10 am     Reply with quote

Quote:
Had to edit the header file to Quote:
#define T3_INTERNAL 0x8D

I don't understand your thought processes. All you have to do is follow
the instructions in the 18F2620.h file. It says to OR the constants
together. You don't edit the file. You just do this:
Code:
setup_timer_3(T3_INTERNAL | T3_CCP2);


Oh..., I just figured it out. You took the instructions literally.
Don't do that. Let the compiler do the work, as shown above.
That's how CCS wants you to do it.
Aragon



Joined: 19 Mar 2014
Posts: 20

View user's profile Send private message

PostPosted: Mon Oct 27, 2014 9:18 am     Reply with quote

Thanks,
I changed the header file back to the original. I used
Quote:
setup_timer_3(T3_INTERNAL | T3_DIV_BY_8 | T3_CCP2 );

Timer 3 works good.
Ttelmah



Joined: 11 Mar 2010
Posts: 19217

View user's profile Send private message

PostPosted: Mon Oct 27, 2014 9:47 am     Reply with quote

This is one of those 'so obvious', but then you realise it isn't, things. Smile

The line in the header saying to 'or' together, is implicitly understood by everyone, who has at some point learnt to use the C 'or' instruction, or seen it used in the examples, but isn't immediately clear unless this is the case.

It's a bit like the old 'training' example, of the foreman coming on site, to find a man driving screws with a hammer. So he says "you don't do that, you use a screwdriver". Returning a while later, he finds the man hammering the screws in with the screwdriver.....

To anyone who knows how to use a screwdriver, it seems 'daft', but when you think about it, if you have never used a screwdriver, the extra explanation of how to use a screwdriver, was essential!.....
temtronic



Joined: 01 Jul 2010
Posts: 9100
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Mon Oct 27, 2014 10:39 am     Reply with quote

gee, and I thought screwdrivers were only for removing screws!

Jay
Aragon



Joined: 19 Mar 2014
Posts: 20

View user's profile Send private message

PostPosted: Thu Mar 05, 2015 12:52 pm     Reply with quote

I Apologise again for REVAMPING this post for the 3rd time. This has been a side project that I don't get to spend alot of time on. I have completed the hardware part and also completed the software. I have been trying to fix a bug for over one week and I thought it was time to ask for help. The code is below. The part i am stuck at is it appears the "Pulse Measurement" is not working. I tried different loops for that part of the program. I used mplab sim and was watching the ports as the program was executing. In the simulator i noticed that the measuring of the pulse was not working. Can someone please guide to the right direction.

Code:

#include <18F2620.h>
#fuses INTRC, NOWDT, NOPROTECT, PUT, BROWNOUT, NOLVP,
#use delay(clock=4000000)
//#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
SETUP_ADC_PORTS( ALL_DIGITAL );


int8 capture_rising_edge1;
int8 capture_rising_edge2;
int8 got_pulse_width1;
int8 got_pulse_width2;
int16 ccp_delta1;
int16 ccp_delta2;

#define MS_2 250
#define MS_3 375



#int_ccp1                     //Interrupt Name
  void isr_ccp1()               //Interrupt Function
  {

static int16 t1_rising_edge;

//If current interrupt is for rising edge.
if(capture_rising_edge1)
   {
   setup_ccp1(CCP_CAPTURE_FE);
   capture_rising_edge1 = FALSE;
   t1_rising_edge = CCP_1;
   }
else
   {
   setup_ccp1(CCP_CAPTURE_RE);
   capture_rising_edge1 = TRUE;
   ccp_delta1 = CCP_1 - t1_rising_edge;
   got_pulse_width1 = true;
   }

}

#int_ccp2
   void isr_ccp2()
{

static int16 t3_rising_edge;
if(capture_rising_edge2)
   {
   setup_ccp2(CCP_CAPTURE_FE);
   capture_rising_edge2 = FALSE;
   t3_rising_edge = CCP_2;
   }
else
   {
   setup_ccp2(CCP_CAPTURE_RE);
   capture_rising_edge2 = TRUE;
   ccp_delta2 = CCP_2 - t3_rising_edge;
   got_pulse_width2 = true;
   }

}     
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++



void main()   ///
{

int16 local_ccp_delta1;
int16 local_ccp_delta2;
int16 pulse_width_ms1;
int16 pulse_width_ms2;

got_pulse_width1 = FALSE;
got_pulse_width2 = FALSE;
capture_rising_edge1 = TRUE;
capture_rising_edge2 = TRUE;

setup_ccp1(CCP_CAPTURE_RE);
setup_timer_1(T1_INTERNAL | T1_DIV_BY_8);
enable_interrupts(INT_CCP1);
enable_interrupts(GLOBAL);

setup_ccp2(CCP_CAPTURE_RE);
setup_timer_3(T3_INTERNAL | T3_DIV_BY_8 | T3_CCP2);
enable_interrupts(INT_CCP2);
enable_interrupts(GLOBAL);


output_low(PIN_B0);               //set RB0 to LOW
output_low(PIN_B2);               //set RB2 to LOW
output_low(PIN_B3);               //set RB3 to LOW
output_high(PIN_B1);            //set RB1 to HIGH
output_high(PIN_B4);            //set RB4 to HIGH
output_low(PIN_C3);               //set RC3 to Low
output_low(PIN_C4);               //set RC4 to Low
input(PIN_A2);                  //set RA2 as INPUT
input(PIN_A3);                  //set RA3 as INPUT
input(PIN_C1);                  //set RC1 as INPUT
input(PIN_C2);                  //set RC2 as INPUT
   
if (input (PIN_A2) == 1) {                //Check, is RA2 high?
   {
   output_high(PIN_B0);            //set output RB0 to HIGH   
   }

if (input (PIN_A3) != 0)
   {
   if (input (PIN_A3) != 0)
      {
      output_high(PIN_B2);
      }
   }
   else
   {
   output_high(PIN_B3);
   output_low(PIN_B4);
   }

do {
   if(got_pulse_width1)
   {
   disable_interrupts(GLOBAL);
   local_ccp_delta1 = ccp_delta1;
   enable_interrupts(GLOBAL);

   pulse_width_ms1 = local_ccp_delta1/125;
    //printf("%lu ms \n\r", pulse_width_ms);
   got_pulse_width1 = FALSE;
   }
if(local_ccp_delta1>MS_2 && local_ccp_delta1<MS_3)
{
   output_high(PIN_C3);
}
 else
  {
   output_low(PIN_C3);
 }
}
while (local_ccp_delta1>MS_2 && local_ccp_delta1<MS_3);



do {
   if (PIN_C3 != 1)
   {
   output_high(PIN_B2);
   output_high(PIN_B4);
   output_low(PIN_B0);
   break;
   }
   
   if (input (PIN_A3) != 0)
   {
   output_high(PIN_B2);
   output_high(PIN_B4);
   output_low(PIN_B0);
   break;
   }
}
while (true);


}

else
{
output_low(PIN_B0);

if (input (PIN_A3) != 1)
   {
   if (input (PIN_A3) != 1)
      {
      output_high(PIN_B2);
      }
}
else
   {
   output_high(PIN_B3);
   }

do
    {
   if(got_pulse_width2)
   {
   disable_interrupts(GLOBAL);
   local_ccp_delta2 = ccp_delta2;
   enable_interrupts(GLOBAL);

   pulse_width_ms2 = local_ccp_delta2/125;
//   printf("%lu ms \n\r", pulse_width_ms2);
   got_pulse_width2 = FALSE;
   }

if (local_ccp_delta2>MS_2 && local_ccp_delta2<MS_3)
   {
   output_high(PIN_C4);
   }
    else
    {
    output_low(PIN_C4);
    }

   }
while (local_ccp_delta2>MS_2 && local_ccp_delta2<MS_3);



do {
    if (PIN_C4 != 1)
   {
   output_high(PIN_B0);
   output_LOW(PIN_B4);
   break;
   }

    if (input (PIN_A3) != 1)
   {
   output_high(PIN_B0);
   output_low(PIN_B4);
   break;
   }
   }
while (true);

do
{
  if (PIN_C3 != 1)
        {
      output_high(PIN_B2);
        output_high(PIN_B4);
        output_low(PIN_B0);
      output_low(PIN_B1);
      break;
         }
   if(input (PIN_A3) != 0)
        {
      output_high(PIN_B2);
        output_high(PIN_B4);
        output_low(PIN_B0);
      output_low(PIN_B1);
       break;
       }
     
}
while(true);
}
}
 



   



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