Renesas | M16C/26A | M16C/26 APPLICATION NOTE Using the M16C/26 Timers in

APPLICATION NOTE
M16C/26
Using the M16C/26 Timers in Timer Mode
1.0 Abstract
The following article describes how to use timers A and B as basic timers, referred to as Timer Mode. Timers are
useful for updating multiplexed display, scanning inputs, real time clocks, hardware watchdogs, etc.
2.0 Introduction
The Renesas M30262 is a 16-bit MCU based on the M16C/60 series CPU core. The MCU features include up to
64K bytes of Flash ROM, 2K bytes of RAM, and 4K bytes of Virtual EEPROM. The peripheral set includes 10-bit
A/D, UARTS, Timers, DMA, and GPIO. The MCU has eight timers that consists of five Timer A’s and three Timer
B’s. All eight timers can operate in Timer Mode.
Timer A also has the following additional modes of operation:
• Event Counter Mode
• PWM Mode
• One-Shot Mode
Timer B has the following additional modes of operation:
• Event Counter Mode
• Pulse Width Measurement Mode
Figure 1 and Figure 2 show the block diagrams for timers A and B. Note that there are some differences between
the two timers but both operate similar in Timer Mode. The remainder of this document will focus on setting up
timer A0 in Timer Mode.
REU05B0046-0100Z
June 2003
Page 1 of 8
M16C/26
Using the M16C/26 Timers in Timer Mode
Figure 1 Bock Diagram of Timer A
Figure 2 Bock Diagram of Timer A
REU05B0046-0100Z
June 2003
Page 2 of 8
M16C/26
Using the M16C/26 Timers in Timer Mode
3.0 Timer Mode Description
In Timer Mode, the counter register counts down using the selected clock source until the counter underflows
(0000 to FFFFh). At this point, the value in the reload register is copied into the counter and countdown continues.
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 and the I flag is set. If at any time during countdown the count
start flag is cleared, counting is suspended until the start flag is set. Figure 3 illustrates this operation.
Figure 3 Operation Timing of Timer Mode
4.0 Configuring Timer Mode
The steps to configure a timer A for Timer Mode are shown below. The steps are similar when setting timer B.
1. Load the TAi register (which also loads the reload register) with the count source
2. Load the timer mode register, TAiMR
• Select timer mode: bits TMOD0, TMOD1 = 0.
• Select the clock source (f1, f/8, f/32, or fc/32): bits TCK0, TCK1
3. Load the timer ‘interrupt priority level’, TAiIC with a value of at least 1
4. Ensure interrupts are enabled (CPU I flag set)
5. Set the ‘start count’ flag bit, TAiS in the ‘count start flag’ register, TABSR
It is not necessary to perform these steps in the order listed, however, the count 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.
REU05B0046-0100Z
June 2003
Page 3 of 8
M16C/26
Using the M16C/26 Timers in Timer Mode
Figure 4 to Figure 7 show the Timer A related registers for Timer mode.
Figure 4 Timer Ai mode register
Figure 5 Timer Ai mode register
REU05B0046-0100Z
June 2003
Page 4 of 8
M16C/26
Using the M16C/26 Timers in Timer Mode
Figure 6 Count start flag register
Figure 7 Interrupt control register
REU05B0046-0100Z
June 2003
Page 5 of 8
M16C/26
Using the M16C/26 Timers in Timer Mode
5.0 Reference
Renesas Technology Corporation Semiconductor Home Page
http://www.renesas.com
E-mail Support
techsupport.rta@renesas.com
Data Sheets
• M16C/26 datasheet
User’s Manual
• KNC30 Users Manual, KNC30UE.PDF
• M16C/60 and M16C/20 C Language Programming Manual, 6020EC.PDF
• Writing interrupt handlers in C for the M16C Application Note
• MSV30262-SKP or MSV-Mini26-SKP Quick start guide
• MSV30262-SKP or MSV-Mini26-SKP Users Manual
• MDECE30262 or MSV-Mini26-SKP Schematic
6.0 Software Code
A sample program written in C and compiled using the KNC30 compiler to illustrate how to set up a timer mode
on timer A0 is listed below. This program runs on the MSV30262 Starter Kit Board and flashes the green LED
(D5) at 1 Hz rate.
To become familiar with the timer, try changing the flash rate, the clock source, or even switch to a different timer
(e.g. TA1, TB0, etc.).
/*****************************************************************************
*
File Name:
timer_mode.c
*
*
Content:
Example program using Timer A in "Timer Mode" .This program
*
is written for the Timer Mode application note. This program
*
works with the MSV30262 starter kit board and flashes
*
D5 connected to P7.2.
*
*
Compiled with KNC30.
*
*
All timing based on 20 MHz Xtal
*
*
Copyright 2003 Renesas Technology America, Inc.
*
All Rights Reserved.
*
REU05B0046-0100Z
June 2003
Page 6 of 8
M16C/26
Using the M16C/26 Timers in Timer Mode
*=============================================================================
*
$Log:$
*===========================================================================*/
#include "sfr26.h"
#define
TIME_CONFIG 0x40
#define
#define
CNTR_IPL 0x03
LED p7_2
int time_cnt;
int count;
/*
01000000 value to load into timer mode register
||||||||_ TMOD0, TMOD1: TIMER MODE SELECTED
||||||____ MR0:
NO PULSE OUTPUT
|||||_____ MR1, MR2:
GATE FUNCTION NOT SELECTED
|||_______ MR3:
SET TO 0 IN TIMER MODE
||________ TCK0, TCK1: F8 SELECTED */
// TA0 priority interrupt level
// LED4 is connected to p7_2 on the MSV30262 board
//Global count value, incremented every second
//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 1ms,
toggles LED every second, and increments global'count'
**************************************************************************** */
void TimerA0Int(void)
{
if ((time_cnt++) > (1000))
// = 1 second
{ LED ^= 1;
// toggle LED
count++;
// example 1 second "clock"
time_cnt = 0;
}
}
/*****************************************************************************
Name:
main()
Parameters:
none
Returns:
nothing
Description: initializes variables and LED port. Then does nothing but
wait for TA0 interrupts.
**************************************************************************** */
void main (void)
{
time_cnt = 0;
count = 0;
pd7_2 = 1;
init();
while (1);
}
REU05B0046-0100Z
//LED flashing is interrupt driven
June 2003
Page 7 of 8
M16C/26
Using the M16C/26 Timers in Timer Mode
/*****************************************************************************
Name:
init()
Parameters:
none
Returns:
nothing
Description: Timer TA0 is setup to interrupt every 1ms.
**************************************************************************** */
void init()
{
ta0 = 2500;
// 20MHz xtal divide by 8 at 1ms intervals = 2,500 counts
/* the following procedure for writing an Interrupt Priority Level follows what
is described in the M16C datasheets 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 TimerA0Int 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.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
;
File Name: sect30.inc
;
;
Content:
Section include file for MSV30262-SKP.
;
;
Copyright 2003 Renesas Technology America, Inc.
;
All rights reserved
;
;=============================================================================
;
$Log:$
;=============================================================================
:
:
.lword dummy_int
; uart1 transmit(for user)(vector 19)
.lword dummy_int
; uart1 receive(for user)(vector 20)
.glb
_TimerA0Int
.lword _TimerA0Int
; timer A0(for user)(vector 21)
.lword dummy_int
; timer A1(for user)(vector 22)
.lword dummy_int
; timer A2(for user)(vector 23)
:
:
REU05B0046-0100Z
June 2003
Page 8 of 8
Keep safety first in your circuit designs!
• Renesas Technology Corporation puts the maximum effort into making semiconductor products
better and more reliable, but there is always the possibility that trouble may occur with them. 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 substitutive, auxiliary circuits, (ii) use of nonflammable material or
(iii) prevention against any malfunction or mishap.
Notes regarding these materials
• These materials are intended as a reference to assist our customers in the selection of the Renesas
•
•
•
•
•
•
•
Technology Corporation product best suited to the customer's application; they do not convey any
license under any intellectual property rights, or any other rights, belonging to Renesas Technology
Corporation or a third party.
Renesas Technology Corporation assumes no responsibility for any damage, or infringement of any
third-party'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 are
subject to change by Renesas Technology Corporation without notice due to product improvements
or other reasons. It is therefore recommended that customers contact Renesas Technology
Corporation or an authorized Renesas Technology Corporation product distributor for the latest
product information before purchasing a product listed herein.
The information described here may contain technical inaccuracies or typographical errors.
Renesas Technology Corporation assumes no responsibility for any damage, liability, or other loss
rising from these inaccuracies or errors.
Please also pay attention to information published by Renesas Technology Corporation by various
means, including the Renesas Technology Corporation Semiconductor home page
(http://www.renesas.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. Renesas
Technology Corporation assumes no responsibility for any damage, liability or other loss resulting
from the information contained herein.
Renesas Technology 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 Renesas Technology Corporation or an authorized Renesas Technology Corporation 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 Renesas Technology Corporation is necessary to reprint or reproduce in
whole or in part these materials.
If these products or technologies are subject to the 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 Renesas Technology Corporation for further details on these materials or the
products contained therein.
Download PDF