|
|
View previous topic :: View next topic |
Author |
Message |
nailuy
Joined: 21 Sep 2010 Posts: 159
|
capture time period with ccp3 |
Posted: Fri Jan 27, 2023 3:57 pm |
|
|
Dear guys.
I wan to use PIC16F1824 to capture time period (like IR receiver).
hardware setup is input IR to A2.
from data sheet on this pin is T0CLKY and CCP3.
T0CLKY will not help me.
CCP3 have interrupt function (page 96) CCP3IE
question is: how to use the CCP3 to capture time period.
I don’t know to use it, or how to write code for this. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19537
|
|
Posted: Sat Jan 28, 2023 5:02 am |
|
|
You don't tell us a lot about your setup (clock rate etc.), which makes it
hard to actually code...
The first thing to do always is read the chip data sheet. Now it tells you:
Quote: |
Capture mode makes use of the 16-bit Timer1
resource. When an event occurs on the CCPx pin, the
16-bit CCPRxH:CCPRxL register pair captures and
stores the 16-bit value of the TMR1H:TMR1L register
pair, respectively. An event is defined as one of the
following and is configured by the CCPxM<3:0> bits of
the CCPxCON register:
• Every falling edge
• Every rising edge
• Every 4th rising edge
• Every 16th rising edge
When a capture is made, the Interrupt Request Flag bit
CCPxIF of the PIRx register is set. The interrupt flag
must be cleared in software. If another capture occurs
before the value in the CCPRxH, CCPRxL register pair
is read, the old captured value is overwritten by the new
captured value.
|
Now this says that the capture has to use Timer1. It records the count
of this timer, when the 'event' happens, and then tells you what the
'events' potentially are.
So, let's assume you want to record on every rising edge of your pulse.
So then we look at the chip definitions for the setup_ccp3 function, and
we can see the definitions for these events.
CCP_CAPTURE_RE is to capture the rising edge.
Now in these definitions we will also see ones to set different timers, but
reading on in data sheet, we can see that this is only supported for PWM,
not capture, so we can ignore these.
So we have:
setup_ccp3(CCP_CAPTURE_RE);
as the setup for the CCP.
What the value this records 'is' will then depend on your clock rate, and
how timer1 is setup.
So you then need to look at Timer1 in the datasheet. Looking at the
diagram for this, we see that the timer has choices of being clocked from
it's own oscillator, the master oscillator, this /4, an external input, or the
CapSense oscillator. Again look at the definitions, and you can see that
these exactly match the options. T1_FOSC, says to run off the master
oscillator, so lets select this.
setup_timer_1(T1_FOSC|T1_DIV_BY_1);
The second bit there sets the prescaler option, where we have /1, /2, /4
and /8.
Now the initial datasheet paragraph says the CCP will interrupt whenever
the event occurs. So if we have:
Code: |
//Your processor setup here
//including a RS232 output.
int16 timer_val=0;
#INT_CCP3
void have_edge(void)
{
static int16 last_value, new_value;
new_value=CCP_3; //save the value
timer_val=last_value-new_value; //calculate delta from last value
last_value=new_value;
}
#define READ_VALUE() disable_interrupts(GLOBAL); local_timer_copy= \
timer_val; enable_interrupts(GLOBAL);
void main(void)
{
int16 local_timer_copy;
setup_ccp3(CCP_CAPTURE_RE);
setup_timer_1(T1_FOSC|T1_DIV_BY_1);
enable_interrupts(INT_CCP3);
enable_interrupts(GLOBAL);
while (timer_val==0)
; //wait till a value has changed from 0.
while (TRUE)
{
READ_VALUE();
printf("interval is %ld counts\n", local_timer_copy);
delay_ms(500);
}
}
|
Now two more things here:
The interval is the current value minus the last value.
Because this is a 16bit value and this is an 8bit processor, there is a
potential issue that the interrupt change the value half way through
doing something with it. So the value is copied from the global store
to a local variable, with the interrupts disabled. |
|
|
nailuy
Joined: 21 Sep 2010 Posts: 159
|
|
Posted: Sun Jan 29, 2023 2:36 pm |
|
|
Dear mr. Ttelmah your example code is perfect, is explained and now I understand how to build this project using CCPx, also many guys beginers like me will understand better using capture function of CCPx.
Thank you very much. And best day for you. |
|
|
|
|
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
|