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

measurement pulse width

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
hamid9543



Joined: 31 Jan 2013
Posts: 63

View user's profile Send private message

measurement pulse width
PostPosted: Sun Sep 01, 2019 1:41 pm     Reply with quote

I want to measure pulse width on RB0, RB1.
Frequency=1.2 khz and pulse width on Rb0=27us and Rb1=320us (Which is applied manually).
But the value applied does not match the measured value.

Code:

#include <18F66K80.h>
#FUSES NOWDT,NOMCLR          //Fuse
#use delay(clock=64MHz,crystal=16MHz)
#USE TIMER(TIMER=1,TICK=1us,BITS=16,NOISR)
#use rs232(baud=19200, xmit=PIN_G3,rcv=PIN_G0)


void main()
{

   printf("hello!\n\r");
   unsigned int16 pulseWidth[SENSOR];
   int8 deltaPulseWidth=1;
   int16 Rcj_Pulse=833;
   //Enable_Interrupts(GLOBAL);
pulseWidth[0]=0;pulseWidth[1]=0;
   while(TRUE)
   {

        set_ticks(0);
        unsigned int16 startTime=get_ticks();

       do {
     
          if(input(pin_b0)==1)  pulseWidth[0] +=2;
          if(input(pin_b1)==1)  pulseWidth[1] +=2;
          } while (get_ticks()<833);

            printf("0=%lu ,1=%lu \r\n",pulseWidth[0],pulseWidth[1]);
            pulseWidth[0]=0;pulseWidth[1]=0;

   }

}


Last edited by hamid9543 on Sun Sep 01, 2019 10:05 pm; edited 1 time in total
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Sun Sep 01, 2019 2:55 pm     Reply with quote

What sort of errors are you getting?

Are they random?
Always too low / high?
Similar range for both Rb0 and Rb1?

Mike

EDIT Where in your code have you initialised the counts for pulseWidth(0) and pulseWidth(1) before the first time round the loop?
hamid9543



Joined: 31 Jan 2013
Posts: 63

View user's profile Send private message

PostPosted: Sun Sep 01, 2019 10:13 pm     Reply with quote

Mike Walne wrote:
What sort of errors are you getting?

Are they random?
Always too low / high?
Similar range for both Rb0 and Rb1?

Mike

EDIT Where in your code have you initialised the counts for pulseWidth(0) and pulseWidth(1) before the first time round the loop?


for Rb0=27us --->> 22us
for Rb1=310us --->>between 264 , 270us {264,266,270,268,......}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Sep 02, 2019 1:56 am     Reply with quote

Try this method. See if it works better for the Pin B0 pulse:
Code:
#include <18F66K80.h>
#FUSES NOWDT,NOMCLR          //Fuse
#use delay(clock=64MHz,crystal=16MHz)
#USE TIMER(TIMER=1,TICK=1us,BITS=16,NOISR)
#use rs232(baud=19200, xmit=PIN_G3,rcv=PIN_G0)

//=================================
void main()
{
unsigned int16 pulseWidth_B0;
 
while(TRUE)
  {
   while(!input(PIN_B0)); // Wait while low (Find rising edge)
   set_ticks(0);   
   while(input(PIN_B0)); // Wait while high (Find falling edge)
   pulseWidth_B0 = get_ticks() * 2;

   printf("0=%lu \r\n", pulseWidth_B0);
   }

}


A question: Why not use the CCP in capture mode ?
Ttelmah



Joined: 11 Mar 2010
Posts: 19215

View user's profile Send private message

PostPosted: Mon Sep 02, 2019 2:23 am     Reply with quote

It's not very surprising. There is nothing in the code to actually make the
time even remotely 'right'.

All you are doing is waiting for 833uSec (so one cycle of the incoming
waveform), and looping, adding 2 to each count, while each signal is
high. Problem is that the loop times will be different if the signal
is high or low. So each of the counts will increment at different rates
when the other signal is high....
Then there is nothing that makes the loop time actually 2uSec, which
is what you are assuming. I doubt if it is.

Use your tick.

Wait while B0 is low.
As soon as it goes high, clear the tick.
Wait while B1 is high.
As soon as it drops, read the tick.

Repeat for B1.

Result, you have the time each signal was high, in 'ticks'.

Still won't be really accurate, because of the delays between detecting
the edge and resetting/reading the count. For real accuracy, use inputs
that are supported by CCP channels, and program this to count in
oscillator cycles, between the edges. Result value accurate to a single
cycle of the master oscillator.

I see PCM_programmer posted exactly the same idea and comments, while
I was typing.
hamid9543



Joined: 31 Jan 2013
Posts: 63

View user's profile Send private message

PostPosted: Fri Sep 06, 2019 5:02 am     Reply with quote

PCM programmer wrote:
Try this method. See if it works better for the Pin B0 pulse:
Code:
#include <18F66K80.h>
#FUSES NOWDT,NOMCLR          //Fuse
#use delay(clock=64MHz,crystal=16MHz)
#USE TIMER(TIMER=1,TICK=1us,BITS=16,NOISR)
#use rs232(baud=19200, xmit=PIN_G3,rcv=PIN_G0)

//=================================
void main()
{
unsigned int16 pulseWidth_B0;
 
while(TRUE)
  {
   while(!input(PIN_B0)); // Wait while low (Find rising edge)
   set_ticks(0);   
   while(input(PIN_B0)); // Wait while high (Find falling edge)
   pulseWidth_B0 = get_ticks() * 2;

   printf("0=%lu \r\n", pulseWidth_B0);
   }

}


A question: Why not use the CCP in capture mode ?


The 15 signals are connected to the micro which must measure their pulse width.
Is this possible with 2 modules of CCP?

The measurement must be done concurrently so that the order cannot be used while for any signal
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
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