Escolar Documentos
Profissional Documentos
Cultura Documentos
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.
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
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
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
}
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
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 – 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)
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 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:
}
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
// The following code shows how to read the capture buffer when
// an interrupt is generated.
// Example code for Input Capture 1 ISR:
}
32-bits timers operations
Toggle LED RD0 using
Timer 1 Interrupt
Step 2: InitTimer1
Step 1:
Understand
the problem
T1CON = 0x8030;
Step 3: Write Interrupt Service Routine