You are on page 1of 15

NuMicro® Timer

A Leading MCU Platform Provider

June 2016
Agenda
• Timer Feature
• Timer Block Diagram
• Function Mode
- Timer Counting Mode
- Event Counting Mode
- Timer Counter Capture/Reset
- External Capture Mode
- External Reset Counter Mode
• Functions and Sample Code in BSP
• Sample Code 2
Timer Feature (I)
• Timer mode
- 4 sets of 32-bit timers with 24-bit up counter and 8-bit pre-scale counter
- 6 selections of clock source
- Timeout period =
(Period of timer clock input) * (8-bit Pre-scale + 1) * (24-bit CMPDAT)
- Four operation modes of counting
 One-shot, Periodic, Toggle and Continuous Counting mode
- Support event counting function and external capture pin event --
- Support wake-up CPU from power-down mode

3
Timer Block Diagram -Revised
Wake up

TMRx Clock Counter Logic Interrupt


& Event Input

External
Capture

4
Timer Clock Source for timer mode

5
Timer Function Mode Lists
• Timer Counting Mode
• Event Counting Mode
• External Capture Mode
- Capture CNT value to CAPDAT value while edge transition detected on
Tx_EXT pin (x= 0~3)
• External Reset Counter Mode
- Reset counter while edge transition detected on Tx_EXT pin (x= 0~3)

6
Timer Function Mode: Register Setting -Revised

Counter Input
Register
TMRx clock Input Event Pin Input *1
TEXCON
RSTCAPSEL TCSR

TEXEN

CEN

MODE
CTB = 0 CTB = 1

[4] [3] [30] [28:27] [24] [24]

x 0 1 0,1,2,3 Timer Counting Mode Event Counting Mode


0 1 1 x Timer Counter Capture Function*2

1 1 1 0,1,2,3 Timer Counter Reset Function

*1 If using “event pin input”, Timer cannot set to toggle mode (MODE_SEL=2)
*2 If using “Timer Counter Capture Function”, the MODE will be ignored.

7
Four Operation Modes of Counting

Ex: TCMPR=100
Interrupt
0 100
One-shot

Reset counter

Periodic 0 100
Toggle Output :
Reset counter
TM0→PB8, TM1→PB9
Toggle 0 100
TM2→PB10, TM3→PB11
Overflow
0 100 101 224-1
Continuous Counting

8
External Pin Event
• External Capture Mode
TIMERx_CTL 5 6 7 8 9 10 11 12 13 14 15

TMx_EXT
(CAPEDGE=0x02)
Clear by software

CAPIF

TIMERx_CAP XX 6 10 13

• External Reset Counter Mode


TIMERx_CTL 5 6 0 1 2 3 0 1 2 3 0

TMx_EXT
(CAPEDGE=0x02)
Clear by software

CAPIF

9
Sample Code Path

NUC029xEEBSPv3.00.001

NuMicro M029 Series


Document Driver Reference Guide

SampleCode

StdDriver

TIMER_PeriodicINT

TIMER_PeriodicINT.uvproj
KEIL

10
Functions and Sample Code in BSP
• \SampleCode\StdDriver
- TIMER_Capture
- TIMER_Counter
- TIMER_PeriodicINT
- TIMER_Powerdown

11
Functions and Sample Code in BSP
• timer.c
- uint32_t TIMER_Open(TIMER_T *timer, uint32_t u32Mode, uint32_t u32Freq)
 Open Timer with Operate Mode and Frequency
- void TIMER_Close(TIMER_T *timer)
 Stop Timer Counting
- void TIMER_Delay(TIMER_T *timer, uint32_t u32Usec)
 Create a specify Delay Time

12
Timer Sample Code
int main (void){
/* Init System, peripheral clock and multi-function I/O */
SYS_Init();
……
/* Open Timer0 in periodic mode, enable interrupt and 1 interrupt tick per second */
TIMER_Open(TIMER0, TIMER_PERIODIC_MODE, 1);
TIMER_EnableInt(TIMER0);
NVIC_EnableIRQ(TMR0_IRQn);
……
/* Start Timer0 ~ Timer2 counting */
TIMER_Start(TIMER0);
……
while(u32InitCount < 20) {
/* Check Timer0 ~ Timer3 interrupt counts */
printf("TMR0:%3d TMR1:%3d TMR2:%3d TMR3:%3d\n",
g_au32TMRINTCount[0], g_au32TMRINTCount[1], g_au32TMRINTCount[2], g_au32TMRINTCount[3]);
}
13
Timer Sample Code
void SYS_Init(void) void TMR0_IRQHandler(void)
{ {
…… if(TIMER_GetIntFlag(TIMER0) == 1) {
/* Enable peripheral clock */ /* Clear Timer0 time-out interrupt flag
*/
CLK_EnableModuleClock(TMR0_MODULE);
TIMER_ClearIntFlag(TIMER0);
/* Peripheral clock source */
/* Toggle LED */
CLK_SetModuleClock(TMR0_MODULE, CLK_CLKSEL1_TMR0_S_HXT, NULL);
PB4 ^= 1;
……
g_au32TMRINTCount[0]++;
}
}
}

14
Thank you!

You might also like