Lecture 6
Lecture 6
Embedded Systems/Microcontroller
Laboratory
Lecture 6
Timer & Input Capture Functions
Professor Rahul Mangharam
Electrical and Systems Engineering
University of Pennsylvania
Free Running Main Timer (TCNT)
-16-bit non-stop timer
2
Registers Related to Main Timer
Timer counter: TCNT
Write a 1 to the TEN bit, TSCR1 |= 0x80, to enable the free running counter
3
Prescale Factor
The prescale factor for the main timer is selected by bits 2, 1, and 0 of the Timer
System Control Register 2: TSCR2
TCTL3 and TCTL4 configure what input signals the corresponding channels act upon
5
MC9S12C128 Input Capture
6
MC9S12C128 Input Capture
Now that the input capture system has been configured we need to enable it. The TIE
register controls this.
To clear one of the bits, write a 1 to the corresponding place in the register.
The value of the free running counter at the time of the event is stored in the TCxH
and TCxL registers, where x is the corresponding bit number 0-7.
7
Edge Detection
The occurrence of an event is represented by a signal edge (rising or falling edge).
The time when an event occurs can be recorded by latching the count of the main
timer when a signal arrives.
or
8
Applications of Input Capture
- Event arrival time recording (to compare for different events)
- Period measurement: the input capture function captures the main
timer values corresponding to two consecutive rising or falling edges
- Pulse width measurement: capture the rising and falling edges
one period
9
Input Capture Applications-continued
Duty Cycle Measurement
T
T T
duty cycle = * 100%
T
signal S1
T
T phase difference = * 360 o
T
signal S2
10
Input Capture Applications-continued
- Interrupt generation: three input capture functions can be used as three edge-sensitive
interrupt sources.
- Event counting: by counting the number of signal edges arrived during a period
e1 e2 e3 e4 ei ej
... ...
Start of End of
interval interval
- Time reference: often used in combination with an output compare function
Time t 0
Time t 0
+ delay
main ( )
{
TERMIO_Init();
(a) Capture two rising edges
EnableInterrupts;
12
Measure Period: Interrupt
#include <hidef.h> /* common defines and macros */
#include <MC9S12C128.h> /* derivative information */
#include "termio.h"
#include "stdio.h"
int edge_cnt
unsigned int edge1, period;
void IC1ISR ( )
{
Interrupt
TFLG1 = 0x01; /* clear IOC0 flag */ Programming
if (edge_cnt == 2)
edge1 = TC1L; /* save the first edge */ Step 1: Write
-- edge_cnt;
} ISR
one period
while (edge_cnt);
period = TC1L - edge1;
printf("\n The period is %d E clock cycles. \r\n", period);return 0;
}
Step 3:
Run
14
Measure Pulse Width: Polling
C Subroutine:
unsigned int pul_width( )
{
unsigned temp;
TCTL3 = 0x01; /* prepare to capture the rising edge */
TFLG1 = 0x01; /* clear IOC0 flag */
while (!(TFLG1 & 0x01)); /* wait for rising edge */
TFLG1 = 0x01; /* clear IOC0 for falling edge */
temp = TC1L; /* store timer value */
TCTL4 &= 0xFE; /* prepare to capture the falling edge */
while (!(TFLG1 & 0x01)); /* wait for falling edge */
return (TC1L-temp);
}
Pulse width
void TOFISR( )
{
TFLG2 = 0x80; /* Clear TOF flag */
overflow++;
}
16
Timer Overflow: Polling-continued
main ( )
{
TERMIO_Init();
EnableInterrupts;
while(1){
while (!(PORTA & 0x04)); /* wait for key press; not using IC1 */
overflow=0;
printf("Key pressed");
while (PORTA & 0x04); /* wait for key release, not using IC1 */
printf("Key released:%u \r\n", overflow);
}
return 0;
}
17