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/62 Event Counter Mode on the M16C/62 1. Abstract Event counters are useful in automated packaging lines, tachometers, and mechanical equipment monitoring. Also, the event counters on the M16C/62 can be configured to interrupt on a single event, adding to the interrupt input pins. The following article describes how to configure the M16C/62 timers as event counters, referred to as “Event Counter 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 5 ‘A’ timers and 6 ‘B’ timers. All 11 timers can operate in “Event Counter Mode”. Timer A has the following additional modes of operation: • • • Timer Mode PWM Mode One-Shot Mode Timer B has the following additional modes of operation: • • Timer Mode Pulse Period/Pulse Width Measurement Mode Figure 1 illustrates the operation of timer A, and Figure 2, timer B. Note that there are some differences between the two timers but both operate similarly in Event Counter Mode. The remainder of this article focuses on setting up timer A0 in Event Counter Mode. AN-DECE-MCU-5-A February 2002 1 M16C/62 Event Counter Mode on the M16C/62 Figure 1. Block Diagram of Timer A Figure 2. Block Diagram of Timer B AN-DECE-MCU-5-A February 2002 2 M16C/62 Event Counter Mode on the M16C/62 3. Event Counter Mode Description In general, the Timer TAi or TBi register counts an input signal and, at any time, the count value can be read. When the timer overflows (for up-count) or underflows (down-count) 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 registers is cleared, the interrupt will not be serviced until the flag is set). If at any time during counting the count start flag is cleared, counting is suspended until set. This is illustrated in Figure 3. Figure 3. Operation Timing of Event Counter Mode, Reload Type Selected Besides having the option of counting up or down, Event Counter Mode has many other options such as count source (TAiIN or TBiIN input pin or another timer), reload or free running type, etc. and these options vary depending on which timer is used. The options and the timers they are associated with are summarized in Table 1, Table 2, and Table 3. AN-DECE-MCU-5-A February 2002 3 M16C/62 Event Counter Mode on the M16C/62 Table 1. Timer A Specifications in Event Counter Mode (Single Phase Mode Only) AN-DECE-MCU-5-A February 2002 4 M16C/62 Event Counter Mode on the M16C/62 Table 2. Timer Specifications in Event Counter Mode (when processing two-phase pulse signal with timers A2, A3, and A4) AN-DECE-MCU-5-A February 2002 5 M16C/62 Event Counter Mode on the M16C/62 Table 3. Timer B Specifications in Event Counter Mode 4. Configuring Event Counter Mode To configure a timer for Event Counter Mode: 1. Load the timer mode register, TAiMR. Select Event Counter Mode: bits TMOD0 = 1, TMOD1 = 0. Set the remaining bits (MR0, MR1, MR2, TCK0, TCK1) depending on required functions (see mode register diagrams below). 2. Load the TAi or TBi register with the count source. 3. Select the trigger via the TRGSR or ONSF register (N/A for Timer B). 4. Select up or down count via the UDF register (N/A for Timer B, Timer B counts down only). 5. Set the timer ‘interrupt priority level’, TAiIC or TBiIC, to at least 1 if required. 6. Enable interrupts (CPU I flag set). 7. Set the ‘start count’ flag bit, TAiS or 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 to Figure 7, Figure 9, Figure 10 and Figure 12. AN-DECE-MCU-5-A February 2002 6 M16C/62 Event Counter Mode on the M16C/62 Figure 4. Timer Ai Mode Register in Event Counter Mode Figure 5. Timer Ai Mode Register (When Not Using Two-Phase Pulse Signal Processing) AN-DECE-MCU-5-A February 2002 7 M16C/62 Event Counter Mode on the M16C/62 Figure 6. Timer Ai Mode Register (When Using Two-Phase Pulse Signal Processing) Figure 7. Timer Bi Register AN-DECE-MCU-5-A February 2002 8 M16C/62 Event Counter Mode on the M16C/62 Figure 8. One-Shot Start Flag Figure 9. Trigger Select Register AN-DECE-MCU-5-A February 2002 9 M16C/62 Event Counter Mode on the M16C/62 Figure 10. Timer Ai Register Figure 11. Count Start Flag AN-DECE-MCU-5-A February 2002 10 M16C/62 Event Counter Mode on the M16C/62 Figure 12. Interrupt Control Register Figure 13. Timer B3, 4, 5 Count Start Flag AN-DECE-MCU-5-A February 2002 11 M16C/62 Event Counter Mode on the M16C/62 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-5-A February 2002 12 M16C/62 Event Counter Mode 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 configuring Event Counter Mode. The program counts 100 falling edges on the P7.1 (TA0IN) pin then flashes D6 on the MDECE0620 Starter Kit Board. To get familiar with this mode, try changing to up-count, the count value or even switch to a different timer (e.g. TA1, TB0, etc). /***************************************************************************** * * File Name: event_mode.c * * Content: Example program using Timer A in "Event Counter Mode". This program * is written for the Event Counter Mode application note. This program * works with the MDECE0620 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:$ *===========================================================================*/ #include "sfr62.h" #define TIME_CONFIG 0x01 /* 00000001 value to load into timer mode register ||||||||_ TMOD0,TMOD1: EVENT COUNTER MODE ||||||____ MR0: NO PULSE OUTPUT |||||_____ MR1: COUNT FALLING EDGES ||||_______MR2: USE UP/DOWN FLAG |||_______ MR3: = 0 IN EVENT COUNTER MODE ||________ TCK0: RELOAD TYPE |__________TCK1: BIT NOT USED */ AN-DECE-MCU-5-A February 2002 13 M16C/62 Event Counter Mode on the M16C/62 #define #define #define #define CNTR_IPL 0x03 LED p7_7 LED_PORT_DIRECTION OUTPUT 1 pd7_7 // TA0 priority interrupt level //p6_0 LED port on MSV1632 board //pd6_0 LED port direction on MSV1632 board //prototypes void init(void); #pragma INTERRUPT /B TimerA0Int void TimerA0Int(void); /***************************************************************************** Name: TimerA0Int() Parameters: none Returns: nothing Description:Timer A0 Interrupt Service Routine. Interrupts every 100 falling edges on the TA0in pin. Flashes the LED and increments 'count'. **************************************************************************** */ void TimerA0Int(void) { int delaycntr; delaycntr = 0; count++; // e.g for an automated packaging line, counts # of cases LED = 1; while( delaycntr <0xffff) //software delay for flashing LED delaycntr++; LED = 0; } /***************************************************************************** Name: main() Parameters: none Returns: nothing Description: initializes variables and LED port. Then does nothing but wait for TA0 interrupts. **************************************************************************** */ void main (void) { int temp; count = 0; LED_PORT_DIRECTION = OUTPUT; init(); while (1); } AN-DECE-MCU-5-A February 2002 14 M16C/62 Event Counter Mode on the M16C/62 /***************************************************************************** Name: initial() Parameters: none Returns: nothing Description: Timer TA0 setup for 5msec interrupts. **************************************************************************** */ void init() { ta0 = 100; //e.g for an automated packaging line, 100 items per cases /* the following procedure for writing an Interrupt Priority Level follows that as described in the M16C data sheets under 'Interrupts' */ _asm (" fclr i") ; ta0ic |= CNTR_IPL; ta0mr = TIME_CONFIG; _asm (" fset i"); ta0s = 1; //turn off interrupts before modifying IPL // use read-modify-write instruction to write IPL //start counting } In order for this program to run properly, timer A0’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 “_TimerA0Int” into the interrupt vector table at vector 21 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. ;****************************************************************************** ;--------------------------------------------------------------: : : : : AN-DECE-MCU-5-A February 2002 15 M16C/62 Event Counter Mode on the M16C/62 .lword .lword .lword .lword .lword .lword .lword .glb .lword .lword .lword .lword .lword .lword .lword .lword .lword .lword .lword AN-DECE-MCU-5-A dummy_int dummy_int dummy_int dummy_int dummy_int 0fcb6bh 0fcb6bh _ TimerA0Int _TimerA0Int dummy_int dummy_int dummy_int dummy_int dummy_int dummy_int dummy_int dummy_int dummy_int dummy_int : : : : : ; ; ; ; ; ; ; A-D(for user)(vector 14) uart2 transmit(for user)(vector 15) uart2 receive(for user)(vector 16) uart0 transmit(for user)(vector 17) uart0 receive(for user)(vector 18) uart1 transmit(for user)(vector 19) uart1 receive(for user)(vector 20) ; ; ; ; ; ; ; ; ; ; ; timer A0(for user)(vector 21) timer A1(for user)(vector 22) timer A2(for user)(vector 23) timer A3(for user)(vector 24) timer A4(for user)(vector 25) 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) February 2002 16 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-5-A February 2002 18