ETC PULSE

To all our customers
Regarding the change of names mentioned in the document, such as Mitsubishi
Electric and Mitsubishi XX, to Renesas Technology Corp.
The semiconductor operations of Hitachi and Mitsubishi Electric were transferred to Renesas
Technology Corporation on April 1st 2003. These operations include microcomputer, logic, analog
and discrete devices, and memory chips other than DRAMs (flash memory, SRAMs etc.)
Accordingly, although Mitsubishi Electric, Mitsubishi Electric Corporation, Mitsubishi
Semiconductors, and other Mitsubishi brand names are mentioned in the document, these names
have in fact all been changed to Renesas Technology Corp. Thank you for your understanding.
Except for our corporate trademark, logo and corporate statement, no changes whatsoever have been
made to the contents of the document, and these changes do not constitute any alteration to the
contents of the document itself.
Note : Mitsubishi Electric will continue the business operations of high frequency & optical devices
and power devices.
Renesas Technology Corp.
Customer Support Dept.
April 1, 2003
APPLICATION NOTE
M16C
Pulse Period/Width Measurement on the M16C/62
1. Abstract
Measuring the frequency (1/period) or the pulse width of an input signal is useful in applications such as
tachometers, DC motor control, power usage calculations, and so on. The following article describes how
to use timer B to measure the period and pulse width of an input waveform, referred to as ‘Pulse
Period/Pulse Width Measurement Mode’.
2. Introduction
The Mitsubishi M16C/62 is a 16-bit MCU, based on the M16C CPU core, with an impressive list of
features including 10-bit A/D, D/A, UARTS, timers, DMA, etc., and up to 256k bytes of user flash. The
MCU has 6 B timers and all 6 timers can operate in Pulse Period/Pulse Width Measurement Mode.
Timer B has the following additional modes of operation:
•
•
Timer Mode
Event Counter Mode
Figure 1 illustrates the operation of timer B. The remainder of this article focuses on setting up timer B0
to measure pulse width, and timer B1 to measure pulse period.
Figure 1. Block Diagram of Timer B
AN-DECE-MCU-6-A
May 2002
1
M16C
Pulse Period/Width Measurement on the M16C/62
3. Pulse Period/Pulse Width Measurement Mode Description
As illustrated in Figure 1, the timer TBi register consists of two parts, a counter and a reload register. In
Measurement Mode, when an effective edge appears on the TBiIN pin, the count value is transferred to
the reload register and the CPU can read this value by performing a read on the TBi register. The
measured time is the counter value (TBi) divided by the frequency of the clock source (Fi). Two period
measurement options are available that measure from falling edge to falling edge or rising edge to rising
edge. For width measurement, the measurement is taken at both edges and software determines if the
measured value is for the high width or low.
3.1 Pulse Period Measurement
In Period Measurement Mode (e.g. falling edge to falling edge), after the ‘start count flag’ is set, the
counter counts up using the selected clock source and every time a falling edge is detected on the TBiIN
pin, the value in the counter is transferred to the reload register, the counter is reset to zero, and then
continues counting. At the same time, the timer interrupt request bit is set and an interrupt is generated if
the timer interrupt priority level is set above the current CPU priority level (if the I flag in the CPU flag
register is cleared, the interrupt will not be serviced until the flag is set). If the timer’s counter overflows
within a period, it will also generate the interrupt and the MR3 bit in the TBiMR is set to distinguish
between the interrupt causes. Note that the measurement is free running and the reload register contains
the most recent measurement. The user has the option of polling the TBi register or reading it in an
interrupt service routine. Also note that the value of the counter immediately after the ‘start count flag’ is
set is indeterminate and an overflow could occur before the first falling edge. Figure 2 illustrates this.
Figure 2. Operation Timing When Measuring a Pulse Period
AN-DECE-MCU-6-A
May 2002
2
M16C
Pulse Period/Width Measurement on the M16C/62
3.2 Pulse Width Measurement
Pulse Width Measurement Mode operates in much the same way except the count register is transferred
to the reload register for every edge detected on the TBiIN pin, and the counter resets and resumes
counting, as shown in Figure 3. Again, note that the value of the counter immediately after the ‘start count
flag’ is set is indeterminate and an overflow could occur before the first falling edge. This measurement is
also free running but now the user must determine by software whether the measurement is for the high
or low width.
Figure 3. Operation Timing When Measuring a Pulse Width
4. Configuring Pulse Period/Pulse Width Measurement Mode
To configure a timer for Pulse Period/Pulse Width Measurement Mode:
1.
2.
Load the timer mode register, TAiMR.
−
Select Measurement Mode: bits TMOD0 = 0, TMOD1 = 1.
−
Set the MR0 and MR1 bits for period or width measurement.
−
Clear the MR2 bit for period or width measurement.
−
MR3 is the timer Bi overflow flag (can be cleared but not set).
−
Select the clock source (f1, f/8, f/32, or fc/32): bits TCK0,TCK1 register.
Set the timer ‘interrupt priority level’, TBiIC, to at least 1 if required.
AN-DECE-MCU-6-A
May 2002
3
M16C
Pulse Period/Width Measurement on the M16C/62
3.
Enable interrupts (CPU I flag set).
4.
Set the ‘start count flag’ bit, TBiS, in the ‘count start flag’ register, TABSR or TBSR.
It is not necessary to perform these steps in the order listed, but the mode register should be loaded
before the ‘start count’ flag is set. Also, the priority level should not be modified when there is a possibility
of an interrupt occurring.
The required registers are shown in Figure 4.
Figure 4. Timer Bi Mode Register in Pulse Period/Pulse Width Measurement Mode
AN-DECE-MCU-6-A
May 2002
4
M16C
Pulse Period/Width Measurement on the M16C/62
Figure 5. Timer Bi Register
Figure 6. Count Start Flag
Figure 7. Timer B3, 4, 5 Count Start Flag
AN-DECE-MCU-6-A
May 2002
5
M16C
Pulse Period/Width Measurement on the M16C/62
Figure 8. Interrupt Control Register
5. References
•
•
•
•
•
NC30 Ver. 4.0 User’s Manual, NC30UE.pdf
M16C/60 and M16C/20 C Language Programming Manual, 6020EC.pdf
M16C/62 datasheets, 62aeds.pdf
M16C/62 User’s Manual, 62eum.pdf
Application Note: Writing Interrupt Handlers in C for the M16C
AN-DECE-MCU-6-A
May 2002
6
M16C
Pulse Period/Width Measurement on the M16C/62
6. Software Code
The software supplied by Mitsubishi Electric & Electronics USA, Inc. is intended and supplied for use on Mitsubishi
Electric products. This software is owned by Mitsubishi Electric & Electronics USA, Inc. or Mitsubishi Electric
Corporation and is protected under applicable copyright laws. All rights are reserved.
THIS SOFTWARE IS PROVIDED “AS IS”. NO WARRANTIES, WHETHER EXPRESS, IMPLIED, OR STATUTORY,
INCLUDING BUT NOT LIMITED TO IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. MITSUBISHI ELECTRIC & ELECTRONICS USA, INC.
AND MITSUBISHI ELECTRIC CORPORATION RESERVE THE RIGHT, WITHOUT NOTICE, TO MAKE CHANGES
TO THIS SOFTWARE. NEITHER MITSUBISHI ELECTRIC & ELECTRONICS USA, INC. NOR MITSUBISHI
ELECTRIC CORPORATION SHALL, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR
CONSEQUENTIAL DAMAGES FOR ANY REASON WHATSOEVER ARISING OUT OF THE USE OR
APPLICATION OF THIS SOFTWARE.
Below is a program written for Mitsubishi’s NC30 compiler to illustrate how to configure Pulse
Period/Pulse Width Measurement Mode. The program can measure up to about a 16msec period and
runs on the MSV1632 Starter Kit Board. Using the KD30 debugger, the program can be ‘stopped’ and the
global variables ‘widthlow’, ‘width_hi’, and ‘period’ viewed from the global watch window.
To become familiar with this mode, try changing the clock source or even switch to a different timer (e.g.
TB2, TB3, etc.).
/*****************************************************************************
*
*
File Name: period_width.c
*
*
Content: Example program using Timer B in 'pulse width / period measurement
*
mode'. This program is written for the "Pulse Period/Width
*
Measurement" application note. Timer B0 is configured to measure
*
pulse width(TB0in pin),and timer B1,measures the period(TB1in pin).
*
Tested using a 250Hz square wave with a 0.5msec pulse width(high).
*
This program works with the MSV1632 starter kit board.
*
*
Compiled with NC30 ver. 3.20.00.
*
*
All timing based on 16 Mhz Xtal
*
*
Copyright,2001 MITSUBISHI ELECTRIC CORPORATION
*
AND MITSUBISHI SEMICONDUCTOR SYSTEM CORPORATION
*
and Mitsubishi Electric and Electronics USA
*=============================================================================
*
$Log:$
*===========================================================================*/
AN-DECE-MCU-6-A
May 2002
7
M16C
Pulse Period/Width Measurement on the M16C/62
#include "sfr62.h"
#define B1TIME_CONFIG 0x42
/*
01000010 value to load into timer B1 mode register
||||||||_ TMOD0,TMOD1: PULSE MEASUREMENT MODE
||||||____ MR0,MR1:
PULSE PERIOD MODE
||||_______MR2:
= 0 FOR PULSE MEASUREMENT
|||_______ MR3:
OVERFLOW FLAG
||________ TCK0,TCK1:
F DIVIDED BY 8 SELECTED */
#define B0TIME_CONFIG 0x4a
/*
01001010 value to load into timer B0 mode register
||||||||_ TMOD0,TMOD1: PULSE MEASUREMENT MODE
||||||____ MR0,MR1:
PULSE WIDTH MODE
||||_______MR2:
= 0 FOR PULSE MEASUREMENT
|||_______ MR3:
OVERFLOW FLAG
||________ TCK0,TCK1:
F DIVIDED BY 8 SELECTED */
#define
CNTR_IPL 0x03
// TB0
priority interrupt level
int period,widthlow,width_hi;
//prototypes
void init(void);
#pragma INTERRUPT /B TimerB0Int
void TimerB0Int(void);
/*****************************************************************************
Name:
TimerB0Int()
Parameters: none
Returns: nothing
Description: Timer B0 Interrupt Service Routine. The overflow flag is check
to determine if the TB0 register contains valid data. If so, the
input is tested to determine if the value in the TB0 register
is the high pulse width or low width and stored in the appropriate
variable.
**************************************************************************** */
void TimerB0Int(void)
{
if (mr3_tb0mr ==1)
// check for timer overflow
{ tb0mr = B0TIME_CONFIG;
// if so clear flag and
return;
// data invaild, so leave
}
if (p9_0== 1)
widthlow = tb0 ;
// if input now hi, just measured a low width
else
width_hi = tb0;
}
AN-DECE-MCU-6-A
May 2002
8
M16C
Pulse Period/Width Measurement on the M16C/62
/*****************************************************************************
Name:
main()
Parameters: none
Returns: nothing
Description: initializes variables. Then the variable 'period' is constantly
updated with the period count in timer TB1. This is to illustrate
that the period measurement is free running. Note that the first
few times TB1 is read, the data may not be valid.
*****************************************************************************/
void main (void)
{
init();
while (1)
{
period = tb1 ;
}
// period measured in polled mode
}
/*****************************************************************************
Name: initial()
Parameters: none
Returns: nothing
Description: Timer TB0 setup for pulse width interrupts and TB1 configured for
pulse period measurement (no interrupts).
**************************************************************************** */
void init()
{
/* the following procedure for writing an Interrupt Priority Level follows that as
described in the M16C
data sheets under 'Interrupts' */
_asm ("
fclr i") ;
tb0ic |= CNTR_IPL;
tb0mr = B0TIME_CONFIG;
_asm ("
fset i");
tb0s = 1;
// turn off interrupts before modifying IPL
// use read-modify-write instruction to write IPL
//start counting
tb1mr = B1TIME_CONFIG;
tb1s = 1; //start counting
}
AN-DECE-MCU-6-A
May 2002
9
M16C
Pulse Period/Width Measurement on the M16C/62
In order for this program to run properly, timer B0’s interrupt vector needs to point to the function. The
interrupt vector table is near the end of the startup file “sect30.inc”. Insert the function label “TimerB0Int”
into the interrupt vector table at vector 26 as shown below.
;*******************************************************************************
;
;
C Compiler for M16C/62
;
;
Copyright,2000 MITSUBISHI ELECTRIC CORPORATION
;
AND MITSUBISHI SEMICONDUCTOR SYSTEM CORPORATION
;
and Mitsubishi Electric and Electronics USA
;
All Rights Reserved.
;
;
Written by T.Aoyama
;
Modified for use on MSV1632 Starter Kit.
;
sect30.inc
: section definition
;
This program is applicable when using KD30 and the ROM Monitor.
;
;
$Id:
;
;******************************************************************************
:
:
:
:
:
:
.lword
.lword
.lword
.lword
.lword
.glb
.lword
.lword
.lword
.lword
.lword
.lword
dummy_int
dummy_int
dummy_int
dummy_int
dummy_int
_TimerB0Int
_TimerB0Int
dummy_int
dummy_int
dummy_int
dummy_int
dummy_int
;
;
;
;
;
timer
timer
timer
timer
timer
;
;
;
;
;
;
timer B0(for user)(vector 26)
timer B1(for user)(vector 27)
timer B2(for user)(vector 28)
int0 (for user)(vector 29)
int1 (for user)(vector 30)
int2 (for user)(vector 31)
.lword
.lword
.lword
.lword
.lword
.lword
dummy_int
dummy_int
dummy_int
dummy_int
dummy_int
dummy_int
;
;
;
;
;
;
:
:
:
vector
vector
vector
vector
vector
vector
AN-DECE-MCU-6-A
May 2002
A0(for
A1(for
A2(for
A3(for
A4(for
32
33
34
35
36
37
user)(vector
user)(vector
user)(vector
user)(vector
user)(vector
(for
(for
(for
(for
(for
(for
user
user
user
user
user
user
or
or
or
or
or
or
21)
22)
23)
24)
25)
MR30)
MR30)
MR30)
MR30)
MR30)
MR30)
10
Keep safety first in your circuit designs!
•
Mitsubishi Electric Corporation puts the maximum effort into making semiconductor products better and
more reliable, but there is always a possibility of trouble occurring. Trouble with semiconductors may
lead to personal injury, fire, or property damage. Remember to give due consideration to safety when
making your circuit designs, with appropriate measures such as (i) placement of backup, auxiliary
circuits, (ii) use of non-flammable material, or (iii) prevention against any malfunction or mishap.
Notes regarding this information
•
This information is intended as a reference to assist our consumers in the selection of the Mitsubishi
semiconductor product best suited to the customer’s application; it does not convey any license under
any intellectual property rights, or any other rights, belonging to Mitsubishi Electric Corporation or a third
party.
•
Mitsubishi Electric Corporation assumes no responsibility for any damage, or infringement of any thirdparty’s rights, originating in the use of any product data, diagrams, charts, programs, algorithms, or
circuit application examples contained in these materials.
•
All information contained in these materials, including product data, diagrams, charts, programs and
algorithms, represents information on products at the time of publication of these materials, and is
subject to change by Mitsubishi Electric Corporation without notice due to product improvements or
other reasons. It is therefore recommended that customers contact Mitsubishi Electric Corporation or an
authorized Mitsubishi Semiconductor product distributor for the latest product information before
purchasing a product listed herein.
•
The information described herein may contain technical inaccuracies or typographical errors. Mitsubishi
Electric Corporation assumes no responsibility for any damage, liability, or other loss arising from these
inaccuracies or errors.
•
Please also pay attention to information published by Mitsubishi Electric Corporation by various means,
including the Mitsubishi Semiconductor home page (http://www.mitsubishichips.com).
•
When using any or all of the information contained in these materials, including product, data, diagrams,
charts, programs, and algorithms, please be sure to evaluate all information as a total system before
making a final decision on the applicability of the information and products. Mitsubishi Electric
Corporation assumes no responsibility for any damage, liability, or other loss resulting from the
information contained herein.
•
Mitsubishi Electric Corporation semiconductors are not designed or manufactured for use in a device or
system that is used under circumstances in which human life is potentially at stake. Please contact
Mitsubishi Electric Corporation or an authorized Mitsubishi Semiconductor product distributor when
considering the use of a product contained herein for any specific purposes, such as apparatus or
systems for transportation, vehicular, medical, aerospace, nuclear, or undersea repeater use.
•
The prior written approval of Mitsubishi Electric Corporation is necessary to reprint or reproduce these
materials in whole or in part.
•
If these products or technologies are subject to Japanese export control restrictions, they must be
exported under a license from the Japanese government and cannot be imported into a country other
than the approved destination.
•
Any diversion or reexport contrary to the export control laws and regulations of Japan and/or the country
of destination is prohibited.
•
Please contact Mitsubishi Electric Corporation or an authorized Mitsubishi Semiconductor product
distributor for further details on these materials or the products contained therein.
Mitsubishi Electric & Electronics USA, Inc.
Electronic Device Group
USA Headquarters
Mitsubishi Electric &
Electronics USA, Inc.
Electronic Device Group
1050 East Arques Avenue
Sunnyvale, CA 94085-4601
Phone: 408-730-5900
FAX: 408-732-9382
Direct Sales Offices – USA
Northwest
Mitsubishi Electric &
Electronics USA, Inc.
Electronic Device Group
1050 East Arques Avenue
Sunnyvale, CA 94085-4601
Phone: 408-730-5900
FAX: 408-737-1129
Southwest
Mitsubishi Electric &
Electronics USA, Inc.
Electronic Device Group
20 Fairbanks, Suite 181
Irvine, CA 92618
Phone: 949-859-9453
FAX: 949-859-9450
Rocky Mountain
Mexico Inquiries
Northeast
Mitsubishi Electric &
Electronics USA, Inc.
Electronic Device Group
PO Box 273377
Fort Collins, CO 80527
Phone: 970-669-4068
FAX: 970-669-4129
Mitsubishi Electric &
Electronics USA, Inc.
Electronic Device Group
92 Montvale Avenue, Suite 2500
Stoneham, MA 02180
Phone: 781-245-6956
FAX: 781-245-4233
North Central
Western Mexico
Mitsubishi Electric &
Electronics USA, Inc.
Electronic Device Group
20 Fairbanks, Suite 181
Irvine, CA 92618
Phone: 949-859-9453
FAX: 949-859-9450
Mid-Atlantic
Mitsubishi Electric &
Electronics USA, Inc.
Electronic Device Group
3300 Edinborough Way, Suite 511
Edina, MN 55435
Phone: 952-837-9053
FAX: 952-837-9059
Mitsubishi Electric &
Electronics USA, Inc.
(Automotive Market Only)
50 West Big Beaver Rd., Suite 136
Troy, MI 48084
Phone: 248-526-9580
FAX: 248-526-9583
South Central
Mitsubishi Electric &
Electronics USA, Inc.
Electronic Device Group
8310 Capital of Texas Hwy. N.,
Suite 260
Austin, TX 78731
Phone: 512-346-4200
FAX: 512-346-4434
Mitsubishi Electric &
Electronics USA, Inc.
Electronic Device Group
92 Montvale Avenue, Suite 2500
Stoneham, MA 02180
Phone: 781-245-6956
FAX: 781-245-4233
Eastern Mexico
Mitsubishi Electric &
Electronics USA, Inc.
Electronic Device Group
8310 Capital of Texas Hwy. N.,
Suite 260
Austin, TX 78731
Phone: 512-346-4200
FAX: 512-346-4434
South Atlantic
Mitsubishi Electric &
Electronics USA, Inc.
Electronic Device Group
2625 Meridian Parkway, Suite 100
Durham, NC 27713
Phone: 919-767-7900
FAX: 919-767-7902
Puerto Rico Inquiries
Puerto Rico
Southeast
Mitsubishi Electric &
Electronics USA, Inc.
Electronic Device Group
2810 Premiere Parkway, Suite 400
Duluth, GA 30097
Phone: 678-258-4518
FAX: 678-258-4519
Mitsubishi Electric &
Electronics USA, Inc.
Electronic Device Group
2810 Premiere Parkway, Suite 400
Duluth, GA 30097
Phone: 678-258-4518
FAX: 678-258-4519
Mitsubishi Electric Sales Canada, Inc.
Direct Sales Offices – Canada
Central & Western Canada
Mitsubishi Electric Sales
Canada, Inc.
4299 14th Avenue
Markham, Ontario,
Canada L3R 0J2
Phone: 905-475-7728
FAX: 905-475-1918
Eastern Canada
Mitsubishi Electric Sales
Canada, Inc.
340 March Road, Suite 300
Kanata, Ontario,
Canada K2K 2E4
Phone: 613-591-3348
FAX: 613-591-3948
JQA-QS0161
Mitsubishi Electric MCU products sold into the
Automotive marketplace are QS-9000 certified.
© 2002 Mitsubishi Electric & Electronics USA, Inc., Electronic Device Group.
Mitsubishi Electric & Electronics USA, Inc. is a wholly owned subsidiary of Mitsubishi Electric Corporation. All trademarks are the property of their
respective owners. The information supplied by Mitsubishi Electric & Electronics USA, Inc. is believed to be accurate and reliable but in no event shall
Mitsubishi Electric & Electronics USA, Inc. be liable for any damages whatsoever arising out of the use or inability to use the information or any errors that
may appear in this publication.
THE INFORMATION IS PROVIDED AS IS WITHOUT ANY WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED. MITSUBISHI ELECTRIC &
ELECTRONICS USA, INC. RESERVES THE RIGHT, WITHOUT NOTICE, TO MAKE CHANGES TO THE INFORMATION OR TO THE DESIGN AND
SPECIFICATIONS OF ITS HARDWARE AND/OR SOFTWARE PRODUCTS. PRODUCTS SUBJECT TO AVAILABILITY.
Printed in U.S.A.
www.mitsubishichips.com
AN-DECE-MCU-6-A
May 2002
12