View previous topic :: View next topic |
Author |
Message |
irmanao
Joined: 08 Apr 2015 Posts: 77
|
Measuring time between two events (Solved) |
Posted: Tue May 01, 2018 7:13 am |
|
|
I wanted to test an example code for measuring time between two events for a future project, so i used this CCP code from the ccs manual. Code: | #int_ccp1
void isr()
{
rise=CCP_1; // CCP_1 is the time the pulse went high
fall=CCP2; // CCP_2 is the time the pulse went low
pulse_width=fall-rise; // pulse width
}
...
setup_ccp1(CCP_CAPTURE_RE); // Configure CCP1 to capture rise
setup_ccp2(CCP_CAPTURE_FE); // Configure CCP2 to capture fall
setup_timer_1(T1_INTERNAL); // Start timer 1 |
My code looks something like this (found from a different post from PCM Programmer):
Code: |
#include <16f1827.h>
#FUSES NOWDT
#FUSES XT
#FUSES PUT
#FUSES NOPROTECT
#FUSES NODEBUG
#FUSES NOLVP
#FUSES NOCPD
#use delay(internal=8000000)
#use rs232(baud=9600,xmit=PIN_B5,rcv=PIN_B2,bits=8,ERRORS)
long rise,fall,time;
#int_ccp2
void isr()
{
rise = CCP_1;
fall = CCP_2;
time = fall - rise;
}
void main()
{
setup_ccp1(CCP_CAPTURE_RE); // Configure CCP1 to capture rise
setup_ccp2(CCP_CAPTURE_FE); // Configure CCP2 to capture fall
setup_timer_1(T1_INTERNAL); // Start timer 1
enable_interrupts(INT_CCP2); // Setup interrupt on falling edge
enable_interrupts(GLOBAL);
while(TRUE) {
output_high(PIN_B4); // fed to CCP1 (pin_9)
delay_ms(10);
output_low(PIN_B4);
delay_ms(20);
output_high(PIN_A3); // fed to CCP2 (pin_16)
delay_ms(100);
output_low(PIN_A3);
delay_ms(1000);
printf("%lu us \n\r", time );
}
} |
The measured time should be 130ms, but i get: Quote: | 39624 us
39624 us
39624 us
39624 us
39624 us
39624 us
39624 us
39624 us
39624 us
39624 us
39624 us
39624 us |
What am i missing?
thanks
Last edited by irmanao on Thu May 03, 2018 6:58 am; edited 1 time in total |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9229 Location: Greensville,Ontario
|
|
Posted: Tue May 01, 2018 7:18 am |
|
|
The first program you should code, compile, run is a '1HZ LED' program.
Simply toggle and LED at a 1Hz rate. Confirm it's running at the correct speed. This way you KNOW the PIC is good, clock selection is correct and then build from there.
While your code may be OK (I haven't checked) you could have a hardware issue (wrong clock speed) or a compiler bug (wrong clock speed) that won't show up if we use your code on our test PICs.
Jay |
|
|
irmanao
Joined: 08 Apr 2015 Posts: 77
|
|
Posted: Tue May 01, 2018 7:20 am |
|
|
I am testing the output pins with an oscilloscope and it looks ok. |
|
|
newguy
Joined: 24 Jun 2004 Posts: 1908
|
|
Posted: Tue May 01, 2018 7:26 am |
|
|
Things to consider:
- At what rate is timer 1 counting? How many microseconds per tick? Then:
- How many counts are going to constitute 130ms? Can that number fit in a "long"? What is a "long"? Why not be explicit and declare your variables as int16 or int32? |
|
|
irmanao
Joined: 08 Apr 2015 Posts: 77
|
|
Posted: Tue May 01, 2018 7:46 am |
|
|
So, timer 1 is counting at 0.5us (8MHz/4) and it takes 260000 counts to reach 130ms so a int32 is needed. Right?
Now i get:
Quote: | 4292821246 us
4292821246 us
4292821246 us |
Did i connect the output pins to the wrong CCP pins? There are two CCP2 pins. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9229 Location: Greensville,Ontario
|
|
Posted: Tue May 01, 2018 10:07 am |
|
|
You best reread the datasheet section on timer1 . My recall is that timer 1 is a 16 bit timer, so 65535 counts max....... |
|
|
irmanao
Joined: 08 Apr 2015 Posts: 77
|
|
Posted: Tue May 01, 2018 11:10 am |
|
|
Yeap. So if i add Code: | SETUP_TIMER_1(T1_INTERNAL | T1_DIV_8 ); | then it should be ok, no? (8/4)/8=250KHz, 4us and 32500 counts for 130ms. I still get the same output. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue May 01, 2018 3:46 pm |
|
|
Quote: | Did i connect the output pins to the wrong CCP pins? |
What package are you using for your PIC ? The data sheet gives a table
of pin numbers, so we could check if your connections are correct, but
we need to know the package (DIP, SOIC, SSOP, or QFN ?). |
|
|
irmanao
Joined: 08 Apr 2015 Posts: 77
|
|
Posted: Wed May 02, 2018 3:12 am |
|
|
It's a DIP18. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9229 Location: Greensville,Ontario
|
|
Posted: Wed May 02, 2018 5:32 am |
|
|
I was curious, so I downloaded the datasheet
B4 is NOT the CCP1 pin, B3 is.
Also A3 is NOT the CCP2 pin.
I looked in section 12 ( IO ports) as well as the pinout selection
While both CCP1 and CCP2 can be 'remapped' or have alternate pins the ones you chose aren't valid.
CCP1 can be B3 or B0
CCP2 can be A7 or B6
At least that's what I read in my copy of the datasheet.
perhaps others will confirm/deny what I'm saying.
jay |
|
|
irmanao
Joined: 08 Apr 2015 Posts: 77
|
|
Posted: Wed May 02, 2018 6:41 am |
|
|
Code: | while(TRUE) {
output_high(PIN_B4); // fed to CCP1 (pin_9)
delay_ms(10);
output_low(PIN_B4);
delay_ms(20);
output_high(PIN_A3); // fed to CCP2 (pin_16)
delay_ms(100);
output_low(PIN_A3);
delay_ms(1000);
printf("%lu us \n\r", time );
}
} |
B4 is the output pin that is connected to pin_9(B3) and A3 is the output pin that is connected to pin_16(A7). |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9229 Location: Greensville,Ontario
|
|
Posted: Wed May 02, 2018 6:49 am |
|
|
OK THAT makes more sense... I misread what you're doing...got confused (easy I'm old...)after flipping through 100 pages to figure out how configure the PIC. |
|
|
pmuldoon
Joined: 26 Sep 2003 Posts: 218 Location: Northern Indiana
|
|
Posted: Wed May 02, 2018 1:05 pm |
|
|
I'm sure I'm missing something obvious, but what I would do is move the 1-sec delay after the printf().
Then add rise, fall and get_timer0() to the printf() and see if the numbers are making sense. You'll be reading them right after the interrupt event, if things are going as expected.
Either the timer isn't running or the interrupt isn't firing or the captures aren't happening, or I'm wrong. In any case it will become more obvious with more data. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed May 02, 2018 3:56 pm |
|
|
irmanao wrote: |
B4 is the output pin that is connected to pin_9(B3) and A3 is the output pin
that is connected to pin_16(A7).
|
There is a problem with this. Pin B6 is the default pin for CCP2. Your
program is using pin B6. I'm sure that's a major reason why it doesn't
work. This is shown in the table on page 6 of the 16F1827 data sheet.
http://ww1.microchip.com/downloads/en/DeviceDoc/41391D.pdf
Pin A7 is the alternate pin for CCP2. To select it, you need to use this line:
Code: | setup_ccp2(CCP_CAPTURE_FE | CCP2_A7); |
|
|
|
irmanao
Joined: 08 Apr 2015 Posts: 77
|
|
Posted: Thu May 03, 2018 3:32 am |
|
|
I connected the output to the default pin for CCP2 and now i get:
Quote: | 29605 us
27715 us
4294932551 us
28902 us
4294933805 us
30047 us
28002 us
4294932841 us
|
expecting 130000us from the original code. If i disconnect the CCP2 input i get the same result but when i disconnect the CCP1 input i get this: Quote: | ýø2321 us
2313 us
2304 us
2301 us
4294904084 us
2341 us
2327 us
2329 us
2335 us
2325 us
2314 us
2321 us |
|
|
|
|