You are on page 1of 69

PIC-Timers

Some contents of these slides are taken from


Textbook 1 and 2 as well as the PIC manuals and URL.
Major editing and addition were done to have more detailed
in the slides. Programming examples and laboratory experiments were
developed and tested By Prof. Al-Ali, Mr. Hamam and Ms. Lalitha.
Mr. Ahmad Alnabalsi is working on the update date version.

Textbook 1: From: Reese/Bruce/Jones, “Microcontrollers: From Assembly to C with the PIC24 Family”
PIC24 Microcontroller: Timers
1. The PIC24F device family offers several
16-bit timers.

2. These timers are designated as Timer1,


Timer2, Timer3, ..., etc.

3. Each timer module is a 16-bit


timer/counter consisting of the
following readable/writable registers:

 TMRx: 16-Bit Timer Count register


 PRx: 16-Bit Timer Period register associated
with the timer
 TxCON: 16-Bit Timer Control register
associated with the timer

4. Each timer module also has the


associated bits for interrupt control:
 Interrupt Enable Control bit (TxIE)
 Interrupt Flag Status bit (TxIF)
 Interrupt Priority Control bits (TxIP <2:0>)
 With certain exceptions, all of the 16-bit
timers have the same functional circuitry.

5. The 16-bit timers are classified into


three types to account for their
functional differences:
 Type A time base
 Type B time base
 Type C time base
 Some 16-bit timers can be combined
to form a 32-bit timer.
With certain exceptions, all of the
16-bit timers have the same
functional circuitry. The 16-bit
timers are classified into three
types to account for their
functional differences:
 Type A time base is used for
time delay functions such as:
Delay (1000) …
Delay (0100)…
 Interrupt driven delay function
bit 15 TON: Timerx On bit bit 5-4 TCKPS<1:0>: Timerx Input Clock Prescale Select bits
1 = Starts the timer 11 = 1:256 prescale value
0 = Stops the timer 10 = 1:64 prescale value
bit 14 Unimplemented: Read as ‘0’ 01 = 1:8 prescale value
00 = 1:1 prescale value
bit 13 TSIDL: Stop in Idle Mode bit bit 3 Unimplemented: Read as ‘0’
1 = Discontinue timer operation when
device enters Idle mode bit 2 TSYNC: Timerx External Clock
0 = Continue timer operation in Idle mode Input Synchronization Select bit
When TCS = 1:
bit 12-7 Unimplemented: Read as ‘0’
1 = Synchronize external clock input
bit 6 TGATE: 0 = Don’t synchronize external clock input
Timerx Gated Time Accumulation Enable bit When TCS = 0:
When TCS = 1: This bit is ignored. Read as ‘0’. Timerx
This bit is ignored uses the internal clock when TCS = 0.
When TCS = 0: bit 1 TCS: Timerx Clock Source Select bit
1 = Gated time accumulation enabled 1 = External clock from TxCK pin
0 = Internal clock (FOSC/2)
0 = Gated time accumulation disabled bit 0 Unimplemented: Read as ‘0’
bit 15 TON: Timerx On bit  Internal Clock = Fosc/2 = 32MHz/2 = 16MHz
 TMR1 Max value = 0000-FFFF
1 = Starts the timer
 Total of 65536 counts (increments) or Cycles (Tcy)
0 = Stops the timer  Each one Cycle time Tcy = 1/ Fosc/2
= 2/32MHz = 0.0625 µsec
bit 5-4 TCKPS<1:0>: Timerx Input Clock  Total time to rollover = 65536*0.0625µsec =4096µsec
Prescale Select bits
11 = 1:256 pre scale value
10 = 1:64 prescale value Delay for one second = delay-one- half-sec
01 = 1:8 prescale value
TFLY = [(Tdelay *Fosc/2 )/Prescaler]
00 = 1:1 prescale value = [500msec *32MHz/2/256]
= [0.5sec *32000000Hz/2/256]
= 31250 cycles
Internal clock (FOSC/2)
Delay-one-sec = 62500
Delay-half-sec = 31250
T1CON = 0x8030; Delay-qtr-sec = 15625
 Internal Clock = Fosc/2
= 32MHz/2
= 16MHz
 TMR1 Max value = 0000-FFFF=65535
 Total of 65536 counts (increments) or Cycles (Tcy)
 Each one Cycle time = 1/ Fosc/2
= 2/32MHz
= 0.0625 µsec
 Total time to rollover = 65536*0.0625µsec
= 4096µsec

bit 15 TON: Timerx On bit bit 5-4 TCKPS<1:0>: Timerx Input Clock Prescaler Select Bits
1 = Starts the timer
0 = Stops the timer 0 0 1:1 65536*0.0625µsec = 4096µsec
= 4.096msec.
bit 5-4 TCKPS<1:0>: Timerx Input Clock
Prescale Select bits
0 1 1:8 4.096msec * 8 = 32.768msec
11 = 1:256 pre scale value
10 = 1:64 prescale value 1 0 1:64 4.096msec * 64 = 262.144msec
01 = 1:8 prescale value
00 = 1:1 prescale value 1 1 1:256 4.096msec * 256 = 1048.576msec
= 1.048576 sec

Internal clock (FOSC/2)


The Delay Function:

void Delay(int Tdelay) // Tdelay is time in msec


{
int TFLY;
T1CON = 0x0000; // Turn OFF Timer1
TMR1 = 0; // reset Timer count register
PR1 = 0xFFFF; // maximum period (rollover time) = PR1 * Prescaler * Tcy
TFLY = Tdelay*(62.5); // TFLY = Tdelay*[(Fosc/2)/Prescaler]=
// TFLY = Tdelay* (32MHz/2/256=62.50)= Tdaley*62.5
// where the oscillator clock is 32MHz
// For example: you can have this in the header file as follows
// defined Delay(500) = 500ms*(32Mhz/2)/256=31250 Cycles
T1CONbits.TSIDL = 0; // Continues module operation in Idle mode
T1CONbits.TGATE = 0; // Gated time accumulation is disabled bit 15 TON: Timerx On bit
T1CONbits.TCKPS = 0b11; // Prescaler = 1:256 1 = Starts the timer
T1CONbits.TSYNC = 0; // Does not synchronize external clock input 0 = Stops the timer
T1CONbits.TCS = 0; // Internal clock (FOSC/2)
bit 5-4 TCKPS<1:0>: Timerx
T1CONbits.TON = 1; // Timer 1 is ON 000 to FFFF
Input Clock Prescale Select
bits
// T1CON = 0x8030; // Timer 1 is ON, Prescalar 256, 11 = 1:256 pre scale value
//use System Clock Fosc/2 10 = 1:64 prescale value
while(TMR1<TFLY); // loop here until the timer 01 = 1:8 prescale value
// reaches TFLY (Tdelay) 00 = 1:1 prescale value
}
Internal clock (FOSC/2)
Example of the delay (x) function application:
Write a C program to turn ON the lower four LEDs connected to Port A (RA3-RA0)
with the higher four LEDs connected to Port A(RA7-RA4) alternately with a delay of
0.5 seconds.

T = 500ms +500ms =1000ms


P = 1/(1000ms) = 1Hz

9
Generate Square Wave of 1KHz using Timer1: 25% high and 75 % low:
Step 1: Understand the question and Calculate the number of cycles for
T-Low, T-High and Period using timer 1 for F = 1KHz keeping in mind the Prescaler

2.1 Assume Prescaler 1:1

2.2 One TCY = 2/Fosc=


= 2/32MHz = 62.5nsec

2.3 Timer Period in µsec


= 1/F = 1/1*103
= 1msec= 1000 µsec

2.3 T-high = 25% = 250µsec


T-low = 75% = 750µsec
Polling Method for generating square wave using Timer1

TRISDbits.TRISD6 = 1; // S3 as input; TRISDbits.TRISD0 = 0; // RD0 is IC1 must be Digital output

T-high=50% T-low50% T-high=25% T-low75%

Step 2: InitTimer1
Example: Square Wave Generator using time delay
Step 3: Write the Main
Step 3: Main Continue …..

T-high=50% T-
low50%

http://lib.myilibrary.com.ezproxy.aus.edu/Open.aspx?id=334823
PIC-Timers
Some contents of these slides are taken from
Textbook 1 and 2 as well as the PIC manuals and URL.
Major editing and addition were done
to have more detailed in the slides.
Programming examples and laboratory
experiments were developed and tested
By Prof. Al-Ali, Mr. Hamam and Ms. Lalitha.
PIC24 Microcontroller: Timers
bit 15 TON: Timerx On bit bit 5-4 TCKPS<1:0>: Timerx Input Clock Prescale
1 = Starts the timer Select bits
0 = Stops the timer 11 = 1:256 prescale value
bit 14 Unimplemented: Read as ‘0’ 10 = 1:64 prescale value
01 = 1:8 prescale value
bit 13 TSIDL: Stop in Idle Mode bit 00 = 1:1 prescale value
1 = Discontinue timer operation when bit 3 Unimplemented: Read as ‘0’
device enters Idle mode bit 2 TSYNC: Timerx External Clock Input
0 = Continue timer operation in Idle mode Synchronization Select bit
When TCS = 1:
bit 12-7 Unimplemented: Read as ‘0’ 1 = Synchronize external clock input
0 = Do not synchronize external clock input
bit 6 TGATE: When TCS = 0:
Timerx Gated Time Accumulation Enable bit This bit is ignored. Read as ‘0’. Timerx uses the internal
When TCS = 1: clock when TCS = 0.
This bit is ignored. bit 1 TCS: Timerx Clock Source Select bit
When TCS = 0: 1 = External clock from TxCK pin
1 = Gated time accumulation enabled 0 = Internal clock (FOSC/2)
0 = Gated time accumulation disabled bit 0 Unimplemented: Read as ‘0’
bit 15 TON: Timerx On bit  Internal Clock = Fosc/2 = 32MHz/2 = 16MHz
 TMR1 Max value =0000-FFFF
1 = Starts the timer
 Total of 65536 counts (increments) or Cycles (Tcy)
0 = Stops the timer  Each one Cycle time = 1/ Fosc/2
= 2/32MHz = 0.0625µsec
bit 5-4 TCKPS<1:0>: Timerx Input Clock Delay for one second = delay-o-sec
Prescale Select bits
11 = 1:256 pre scale value TFLY = [(Tdelay *Fosc/2 )/Prescaler]
10 = 1:64 prescale value = [500msec *32MHz/2/256]
= [0.5sec *32000000H/2/256]
01 = 1:8 prescale value
= 31250 cycles
00 = 1:1 prescale value
Delay-one-sec = 62500
Delay-half-sec = 31250
Internal clock (FOSC/2) Delay-qtr-sec = 15625

T1CON = 0x8030;
OC/IC Ports Mapping in PIC24F
Output Compare Timers

http://ww1.microchip.com/downloads/en/DeviceDoc/39706a.pdf
OUTPUT COMPARE REGISTERS

Each output compare channel has


the following registers:
OCxCON: the control register for
the output compare channel

OCxR: a data register for the


output compare channel

OCxRS: a secondary data register


for the output compare channel

 The control registers for the 5


output compare channels are
named OC1CON through
OC5CON. All 5 control registers
have identical bit definitions.
They are represented by a
common register definition
below. The ‘x’ in OCxCON
represents the output compare
channel number. http://lib.myilibrary.com.ezproxy.aus.edu/Open.aspx?id=334823
Output Compare Control Register (OCxCON
Output Compare Timers are used to generate various pulses and square waves with variable duty cycle

bit 3 OCTSEL: Output Compare x Timer Select bit(1)


1 = Timer3 is the clock source for Output Compare x
0 = Timer2 is the clock source for Output Compare x

bit 2-0 OCM<2:0>: Output Compare x Mode Select bits


111 = PWM mode on OCx, Fault pin enabled
110 = PWM mode on OCx, Fault pin disabled
101 = Initialize OCx pin low, generate continuous output pulses on OCx pin
100 = Initialize OCx pin low, generate single output pulse on OCx pin
011 = Compare event toggles OCx pin
010 = Initialize OCx pin high, compare event forces OCx pin low
001 = Initialize OCx pin low, compare event forces OCx pin high
000 = Output compare channel is disabled
Note 1: Refer to the device data sheet for specific time bases
Generate Square Wave 50%-50, 8.192 msec period using Toggle Mode
Step 1: Understand the mode and calculate the period and 50%-50% time and cycles

2*( 65535+1) *0.0625µsec * 1 = 8.192msc


Step 2: Timer3 Reg initialize function
void initTimer3(void)
{
T3CON = 0x0000; // Turn OFF Timer3
TMR3=0x0000; // reset Timer count register
PR3=0xFFFF; // maximum period (rollover time)
// = (PR3+1) * Prescaler * Tcy
T3CONbits.TSIDL = 0; // Continues module operation in Idle mode
T3CONbits.TGATE = 0; // Gated time accumulation is disabled
T3CONbits.TCKPS = 0b00;// Prescaler = 1:1
T3CONbits.T32 = 0; // TMR3 form separate 16-bit timer
T3CONbits.TCS = 0; // Internal clock (Fosc/2)
}

void initOC3(void) Step 3: initialize output compare Reg 3


{
OC3R = 19660-1;//Set high after 19660*Tcy*Pres = 1.229 ms
OC3CON = 0x0000;
OC3CONbits.OCSIDL = 0; // Output compare x will continue
// to operate in CPU Idle mode
OC3CONbits.OCTSEL = 1; // Timer3 is the clock source for Output Compare x
OC3CONbits.OCM = 0b011; // Compare event toggles OCx pin
// Output pulse period
// =2*(PR3+1) * Tcy * Prescaler = 8.192 ms
// Duty Cycle is fixed 50 %
// Output is from pin RD2
}
Step 4: Main Program
int main(void) {
TRISDbits.TRISD2 = 0; // RD2 is digital output compare RD2
// (OC1=RD0, OC2=RD1, OC3=RD2, OC4=RD3, OC5=RD4)
initOC3(); // Toggle (50% DutyCycle)
// Timer initialization
initTimer3();
T3CONbits.TON = 1; // Start Timer3 with prescaler settings at 1:1
//and clock source sets to the internal
//instruction cycle
while (1)
{

//You may do any programming


//here like read ADC, Temp,
//Humid, Digital Inputs and
//Outputs, Display ….. 50% 50%

}
return (0);
}// main
Generate Square Wave of Duty Cycle =30% of 4.096 msec period using PWM Mode
Step 1: Understand the mode and calculate the period and 30%-70% time and cycles

// = (PR1+1) * Prescaler * Tcy =


// = 65536 * 62.5nsc= 4.096msec
// 30% duty cycle high of 4.096msec= 1.23 msec and the low = 4.096msec -1.23msec = 2.87 msec
// High Cycles = 1.23msec /62.5nsec = 19660, Low Cycles = 2.87ms/62.5nsec = 45875
// High Cycles = 0.3 * 65536 = 19660.8 Low Cycles = 0.7 * 65536 = 45875.2
// = 19661 = 45875
// Adding the number high cycles and low cycles= 19661+45875 = 65356. If this number came out more
that the PR value, it should be attributed to the rounding up or down. In suhc case you should correct it
accordingly. So that that total should not be more the PR value. It would be better to reduce T-High
// 30% duty cycle high of 4.096msec= 1.23 msec
and the low = 4.096msec -1.23msec = 2.87 msec

T-H T-L

PR
///////////: Pulse With Modulation “PWM” 10////////////////
#include <p24fj128ga010.h>
#include <config.h>
/* Step 2: Initialize the reference timer for the output
compare function. You may select any one of the five timers.
In this example T3 is selected */

5 - 16-bit Timers 90% H + 10% L

5 – OC/PWM Timers
5 Input Capture Timers
void initTimer3(void)
{
T3CON = 0x0000; // Turn OFF Timer3
TMR3 = 0x0000; // Reset Timer count register
PR3 = 0xFFFF; // maximum period (rollover time)
// = (PR3+1) * Prescaler * Tcy
// = 65536 * 62.5nsc= 4.096msec
T3CONbits.TSIDL = 0; // Continues module operation in Idle mode
T3CONbits.TGATE = 0; // Gated time accumulation is disabled
T3CONbits.TCKPS = 0b00; // Prescaler = 1:1
T3CONbits.TCS = 0; // Internal clock (Fosc/2)
}
/* Step 3: Initialize the output compare register. You may
select any one of the five output compare register,
In this example OC3 is selected */

void initOC3()
{
OC3RS = 0; // init. the output compare
// secondary register
OC3R = 0; // init. the output compare Reg.
OC3CON = 0x0000; // Init. Output compare control Register.
OC3CONbits.OCSIDL = 0; // Output compare x will continue
// to operate in CPU Idle mode
OC3CONbits.OCTSEL = 1; // Timer3 is the clock source for
// Output Compare x
OC3CONbits.OCM= 0b110; // PWM mode on OC3, Fault pin disabled
// Output is from pin RD2
}
/* Step 4: The following program calculates the values that should be loaded in the output compare
registers in order to generate a square wave with predefined Duty Cycle.*/

int main(void) {
float dutyCycle = 0.30; //Duty Cycle is 30% of the PR.
float T_High;
TRISDbits.TRISD2 = 0; // RD2 is digital output
// You may not need the above statement
// because by default the OC timers are
// mapped and connected as
// (OC1=RD0, OC2=RD1, OC3=RD2, OC4=RD3, OC5=RD4)

initTimer3(); // Timer initialization


initOC3(); // PWM mode on OCx, Fault pin disabled.

T3CONbits.TON = 1; // Start Timer3 with


//prescaler settings at 1:1.
// clock source set to the internal
// instruction cycle .
while (1)
{
T_High = dutyCycle*((float)PR3+1);
OC3RS = (int) T_High;
} // = (PR3+1) * Prescaler * Tcy
return (0); // = 65536 * 62.5nsc= 4.096msec
}// main // 30% duty cycle high of 4.096msec= 1.23 msec high
and the low = 4.096msec -1.23msec = 2.87 msec low
http://lib.myilibrary.com.ezproxy.aus.edu/Open.aspx?id=334823#
Input Capture Timers
PIC24 Microcontroller: Timers
What can you do with the Input Capture timers
Timer 2 is used as reference (base) timer.

bit 15 TON: Timerx On bit bit 5-4 TCKPS<1:0>: Timerx Input Clock Prescale
1 = Starts the timer Select bits
0 = Stops the timer 11 = 1:256 prescale value
bit 14 Unimplemented: Read as ‘0’ 10 = 1:64 prescale value
01 = 1:8 prescale value
bit 13 TSIDL: Stop in Idle Mode bit 00 = 1:1 prescale value
1 = Discontinue timer operation when bit 3 Unimplemented: Read as ‘0’
device enters Idle mode bit 2 TSYNC: Timerx External Clock Input
0 = Continue timer operation in Idle mode Synchronization Select bit
When TCS = 1:
bit 12-7 Unimplemented: Read as ‘0’ 1 = Synchronize external clock input
0 = Do not synchronize external clock input
bit 6 TGATE: When TCS = 0:
Timerx Gated Time Accumulation Enable bit This bit is ignored. Read as ‘0’. Timerx uses the internal
When TCS = 1: clock when TCS = 0.
This bit is ignored. bit 1 TCS: Timerx Clock Source Select bit
When TCS = 0: 1 = External clock from TxCK pin
1 = Gated time accumulation enabled 0 = Internal clock (FOSC/2)
0 = Gated time accumulation disabled bit 0 Unimplemented: Read as ‘0’
External Input
Signal or event is Fix prescalar
connected here.

http://www.microchip.com.tw/Data_CD/Reference%20Manuals/16-
Bits%20Family%20Reference%20Manual/PIC24F%20FRM%20Section%2015.%20Input%20Capture%20(DS39701A).pdf
From: Reese/Bruce/Jones, “Microcontrollers: From Assembly to C with the PIC24 Family”
http://courses.ece.msstate.edu/ece3724/main_pic24/videos/lectures/ch9_part1/
Simple Capture Events
The input capture module can capture a timer count value (TMR2 or
TMR3) based on the selected edge (rising or falling defined by mode) of
the input applied to the ICx pin. These modes are specified by setting
the ICM<2:0> (ICxCON<2:0>) bits to ‘011’ or ‘010’, respectively. In these
modes, the prescaler counter is not used. See Figure 15-2 and Figure
15-3 for timing diagrams of a simple capture event.
The input capture logic detects and synchronizes the rising or falling
edge of the capture pin signal on the internal phase clocks. If the
rising/falling edge has occurred, the capture module logic will write the
current time base value to the capture buffer and signal the interrupt
generation logic. When the number of elapsed capture events matches
the number specified by the ICI<1:0> control bits, the respective Input
Capture Interrupt Flag, ICxIF, is asserted two instruction cycles after the
capture buffer write event.
If the capture time base increments every instruction cycle, the
captured count value will be the value that was present one or two
instruction cycles past the time of the event on the ICx pin. This time
delay is a function of the actual ICx edge event related to the instruction
cycle clock and delay associated with the input capture logic. If the
input clock to the capture time base is prescaled, then the delay in the
captured value can be eliminated. See Figure 15-2 and Figure 15-3 for
details.
INTRODUCTION
This section describes the input capture module and its associated operational
modes. The input capture module is used to capture a timer value from one of
two selectable time bases upon an event on an input pin. The input capture
features are quite useful in applications requiring frequency (Time Period) and
pulse measurement. Figure 15-1 depicts a simplified block diagram of the input
capture module.
Refer to the specific device data sheet for further information on the number of
channels available in a particular device. All input capture channels are
functionally identical. In this section, an ‘x’ in the pin name or register name is a
generic reference to an input capture channel in place of a specific input
capture channel number.
The input capture module has multiple operating modes which are selected via
the ICxCON register. The operating modes include:
• Capture timer value on every falling edge of input applied at the ICx pin
• Capture timer value on every rising edge of input applied at the ICx pin
• Capture timer value on every 4th rising edge of input applied at the ICx pin
• Capture timer value on every 16th rising edge of input applied at the ICx pin
• Capture timer value on every rising and every falling edge of input applied at
the ICx pin
• Device wake-up from capture pin during CPU Sleep and Idle modes.
The input capture module has a four-level FIFO buffer. The number of capture
events required to generate a CPU interrupt can be selected by the user.1
1000 1001/1002
Signal Period and Frequency measurement
Step 1: Understand the question and get R2 and R1
then calculate the period and frequency accordingly

Example 1: with PRES 1:1


PR = (R2-R1) PRES*Tcy
R1 = 1000 Tcy, R2 = 11629 Tcy
PR = (11629- 1000) *1 *62.5nsec
PR = 66.43µsec

Example 2: with PRES 1:256


PR = (R2-R1) PRES*Tcy
R1 = 1000 Tcy, R2 = 11629 Tcy
PR = (11629- 1000) *256 *62.5nsec
PR = 17006 µsec
PR = 17.006 msec

Example 3: Max capture is with PRES 1:256 and R1=0 and R2 =65536
PR = (R2-R1) PRES*Tcy
R1 = 0 Tcy and R2 = 65534 Tcy
PR = (65536 – 00000) *256 *62.5nsec
PR = 1048.857 msec
PR = 1.04857 sec
Step 4: Develop the Interrupt Service Routine ISR (IC).

// The following code shows how to read the capture buffer when
// an interrupt is generated.
// Example code for Input Capture 1 ISR:

int risingl, rising2;


int diff;
float period, freq;

void _ISR _IC1Interrupt(void)

risingl = IC1BUF; // Read and save off first rising edge

rising2 = IC1BUF; // Read and save off send rising edge

IFSObits.IC1IF = 0; // Reset respective interrupt flag

}
Step 5: Develop the main

while (1)
{
// Using the LEDs will not interfere with the Input Capture
// Module operation. You do other job like read ADC_
if (!PORTDbits.RD6) // If S3 is pressed,
PORTA = Ox000F; // turn ON the lower 4 LEDs

if (!PORTDbits.RD13) // If S4 is pressed,
PORTA = Ox00F0; // turn ON the upper 4 LEDs

if (!PORTDbits.RD13 & !PORTDbits.RD6)


PORTA = Ox0OFF; // if both are pressed ON all LEDs

diff = rising2 - risingl;


period = (diff*62.5)/1000; // in µsec
freq = (1000.0/period); // in KHz
// 1st Line of LCD
homeLCD();
putsLCD("P ="); IC connected as
putfLCD(period); IC1=RD8
putsLCD(" usec "); IC2=RD9
// 2nd Line of LCD
setLCDC(LINE2); IC3=RD10
putsLCD("Freq = "); IC4=RD11
putfLCD(freq); IC5=RD12
putsLCD(" KHz ");
}
return (0);
}
Step 4: Develop the Interrupt Service Routine ISR (IC).

// The following code shows how to read the capture buffer when
// an interrupt is generated.
// Example code for Input Capture 1 ISR:

int risingl, rising2;


int diff;
float period, freq;

void _ISR _IC1Interrupt(void)

risingl = IC1BUF; // Read and save off first rising edge

rising2 = IC1BUF; // Read and save off send rising edge

IFSObits.IC1IF = 0; // Reset respective interrupt flag

}
32-bits timers operations
Toggle LED RD0 using
Timer 1 Interrupt
Step 2: InitTimer1
Step 1:
Understand
the problem

bit 15 TON: Timerx On bit


1 = Starts the timer
0 = Stops the timer

bit 5-4 TCKPS<1:0>: Timerx


Input Clock Prescale Select bits
11 = 1:256 pre scale value
10 = 1:64 prescale value
01 = 1:8 prescale value
00 = 1:1 prescale value

Internal clock (FOSC/2)

T1CON = 0x8030;
Step 3: Write Interrupt Service Routine

Step 4: Write the main

You might also like