0% found this document useful (0 votes)
36 views17 pages

Lecture 6

This document describes various timer functions of the MC9S12C128 microcontroller including the free running main timer, input capture, and overflow interrupt. It provides examples of using these functions to measure periods, pulse widths, duty cycles, and generate interrupts. Input capture allows recording the time an event occurs by latching the timer value. Overflow interrupt increments a counter each time the timer overflows.

Uploaded by

cecilio barría
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views17 pages

Lecture 6

This document describes various timer functions of the MC9S12C128 microcontroller including the free running main timer, input capture, and overflow interrupt. It provides examples of using these functions to measure periods, pulse widths, duty cycles, and generate interrupts. Input capture allows recording the time an event occurs by latching the timer value. Overflow interrupt increments a counter each time the timer overflows.

Uploaded by

cecilio barría
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 17

ESE 350

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

-Count $0000 to $FFFF

-Sets overflow flag when


count changes $FFFF
to $0000

-Can generate interrupt

2
Registers Related to Main Timer
Timer counter: TCNT

16-bit free running counter

Timer System Control Register: TSCR1

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

For example, given 2 MHz E clock, PR1=PRO=0:


T=1/2MHz=500 ns; At 16-bit there are 65536 cycles.
Overflow period=500 ns x 65536=32.77 ms
4
MC9S12C128 Input Capture
When you must know exactly when a particular signal is input to the microcontroller,
input capture is used. There are 8 pins that can be configured as either input capture
or output compare, which will be discussed shortly. The TIOS register regulates what
the pins are used for. Default, 0, indicates the channel is used as input capture.

TCTL3 and TCTL4 configure what input signals the corresponding channels act upon

5
MC9S12C128 Input Capture

These correspond to the following:

6
MC9S12C128 Input Capture
Now that the input capture system has been configured we need to enable it. The TIE
register controls this.

Whenever an input capture event occurs, a flag in theTFLG1 register is set.

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.

Rising edge Falling edge

or

The edge to be captured is selected by programming the registers denoted on the


previous page

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 Pulse width

(a) Capture two rising edges Rising edge Falling edge

one period

(b) Capture two falling edges

9
Input Capture Applications-continued
Duty Cycle Measurement

T
T T
duty cycle = * 100%
T

Phase Difference Measurement


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

Time of reference Time to activate


(set up by signal edge) output signal 11
(set up by output compare)
Measure Period: Polling
#include <hidef.h> /* common defines and macros */
#include <MC9S12C128.h> /* derivative information */
#include "termio.h“ //printf
#include "stdio.h“
one period
unsigned int edge1, period;

main ( )
{
TERMIO_Init();
(a) Capture two rising edges
EnableInterrupts;

TCTL4 = 0x03; //capture rising and falling edge


TIE = 0x01; //enable pin input capture
TFLG1 = 0x01; //clear IOC0 input capture flag

while (!(TFLG1 & 0x01)); /* wait for first rising edge */


edge1 = TC1L; /* save arrival time of first rising edge */
TFLG1 = 0x01; /* clear IOC0 flag , wait for next edge */
while (!(TFLG1 & 0x01)); /* wait for second rising edge */
period = TC1L - edge1;
printf("\n The period of the signal is %d E clock cycles. \r\n", period);
return 0;
}

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

(a) Capture two rising edges


13
Measure Period: Interrupt-continued
main ( )
{
TERMIO_Init();
EnableInterrupts; Step 2:
TFLG1 = 0x01; Initialize
TCTL4 = 0x03;
TIE = 0x01; interrupt

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

Rising edge Falling edge


15
Timer Overflow: Polling
#include <hidef.h> /* common defines and macros */
#include <MC9S12C128.h> /* derivative information */
#include "termio.h"
#include "stdio.h"
unsigned int overflow;

void TOFISR( )
{
TFLG2 = 0x80; /* Clear TOF flag */
overflow++;
}

16
Timer Overflow: Polling-continued
main ( )
{
TERMIO_Init();
EnableInterrupts;

TFLG2 = 0x80; /* Clear TOF flag */


TSCR2 = 0x80; /* Enable TOI-local interrupt */

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

You might also like