![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
hamid9543
Joined: 31 Jan 2013 Posts: 63
|
measurement pulse width |
Posted: Sun Sep 01, 2019 1:41 pm |
|
|
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 |
|
![](templates/subSilver/images/spacer.gif) |
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Sun Sep 01, 2019 2:55 pm |
|
|
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? |
|
![](templates/subSilver/images/spacer.gif) |
hamid9543
Joined: 31 Jan 2013 Posts: 63
|
|
Posted: Sun Sep 01, 2019 10:13 pm |
|
|
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,......} |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Sep 02, 2019 1:56 am |
|
|
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 ? |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19368
|
|
Posted: Mon Sep 02, 2019 2:23 am |
|
|
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. |
|
![](templates/subSilver/images/spacer.gif) |
hamid9543
Joined: 31 Jan 2013 Posts: 63
|
|
Posted: Fri Sep 06, 2019 5:02 am |
|
|
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 |
|
![](templates/subSilver/images/spacer.gif) |
|
|
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
|