You are on page 1of 29

ELEC 3300

Introduction to Embedded Systems

Topic 6
Timer and Counter
Prof. Tim Woo

ELEC 3300 : Spring 17/18 Tim Woo 1


Course Overview
Assembler

Instruction Set Architecture

Memory I/O System

Datapath & Control


Introduction to Basic Computer
Embedded Systems Structure
MCU Main Board

Digital and Analog


Interfacing USART, IEEE1394,
USB, I2C and SPI

Microcontroller Structure
A/D Port
Buffering and
Serial Port Direct Memory Access
CPU
(DMA)
External Memory Port
Memory,
Interfacing to Memory,
External Interrupt Port
Memory Timing
Interrupt and applications
External Timer Port Interfacing LCD
Organization Timer and
Counter Simple I/O Port
Motor Interfacing

In this course, STM32 is used as a driving vehicle for delivering the concepts.
To be covered In progress Done

ELEC 3300 : Spring 17/18 Tim Woo 2


Expected Outcomes
• On successful completion of this topic, you will be able to
– Summarize the importance of the timer in microprocessor
– Distinguish the features of timer and counter
– Demonstrate some applications of timers
– List the timing diagram of the general-purpose timers in STM32.

ELEC 3300 : Spring 17/18 Tim Woo 3


Timer-mode and counter-mode operation
• Two modes of operation : Timer-mode and counter-mode operation

Start as timer:
Switch
press one time

Periodic
Waveform

Start as counter:
Switch
press one time

ELEC 3300 : Spring 17/18 Tim Woo 4


Electronic circuits

Digital Pulse Counter

Digital Timer (range 0 – 99 sec)


ELEC 3300 : Spring 17/18 Tim Woo 5
Timer-Mode Operation
• An output pulse generated each time the counter reaches zero (or
preset value) provides a signal with a software-programmable
frequency.
Switch
press one time

Periodic
Waveform
• Applications:
– A common use for this output is as a baud rate generator for a serial
communication interface.
– generating waveforms for audible tones.
– Timing the length of event

ELEC 3300 : Spring 17/18 Tim Woo 6


Counter-Mode Operation
• It is useful for interfacing to other devices that produce streams of possibly
non-period pulses that must be counted。
Switch
press one time

• Applications:
– Count the number of times that a user has pressed a key.

ELEC 3300 : Spring 17/18 Tim Woo 7


Timer Structure
• Devices that uses a high speed clock input to provide a series of time or
count related events
• Building block of a timer

Auto-reload Register

Reload on preset value

Preset value timeout


or Interrupts

• Other important registers:


• Counter Register, Mode Register, Control Register, Status Register

ELEC 3300 : Spring 17/18 Tim Woo 8


Example 1 – Event Counting
Description
• Say, you want a microprocessor to know many times a user Abstract idea of project

has pressed a key. (Define the functionality of


the system)

• We can connect the keypad to the microprocessor pin line, Data format / representation
Programming Language
and then use Timer X to detect the key store which sends a Communication Protocol
pulse to pin. Physical connection (Pins
assignment)

Pseudo code: Hardware devices


(Microcontroller, Peripherals)
MCU
Timer initialization
Step 1: Initialization
Turn on Timer X Configure the type of Timer / Counter
Pin ……
……
while (the button is pressed) Step 2: Implementation
Run / Stop the timer,
Event counting Read the counter register
Device
Read counter register

ELEC 3300 : Spring 17/18 Tim Woo 9


Example 1 – Event Counting
Trigger the timer Trigger the timer Trigger the timer

Press and hold


the button

Increment
0003
Counter Register = 0
0001 0002 0004

Turn on the timer

MCU Number of counts


Values in Counter Register
Pin

ELEC 3300 : Spring 17/18 Tim Woo 10


Example 2 – Timing the Length of Events
Description
• Now, if we want to measure how long (time) a key is being pressed. Abstract idea of project
(Define the functionality of
• We can connect the keypad to a another pin. the system)

• In this way, the timer X will run when pulse is high (or a key is held Data format / representation

down). When pulse is low (or a key is released), the timer X will be Programming Language
Communication Protocol
stopped.
Physical connection (Pins
assignment)
Pseudo code: Hardware devices
(Microcontroller, Peripherals)
MCU Timer initialization Step 1: Initialization
Turn on Timer X Configure the type of Timer / Counter
……
……
Pin If the pulse is high, trigger the timer X Step 2: Implementation
If the pulse is low, stop the timer X Run / Stop the timer,
Read the counter register
Read counter register

• From the current counter value and the frequency of the input clock, we can
calculate the time elapsed for the event.
ELEC 3300 : Spring 17/18 Tim Woo 11
Example 2 – Timing the Length of Events
Start the timer Stop the timer

Press and hold


the button

Increment
Counter Register = 0

MCU

Values in Counter Register


Pin

ELEC 3300 : Spring 17/18 Tim Woo 12


Example 2 – Timing the Length of Events
Start the timer Stop the timer
Press and hold the button

FFFF 0000 FFFF


Increment
Counter Register = 0
… … …

Interrupt pulse

Number of completed cycles


Values in Counter Register

ELEC 3300 : Spring 17/18 Tim Woo 13


Program flow
• Timer can be used as a delay function call.

Code Program
Start
Trigger Timer

background
Is the
No Timer interrupt
signal enabled? Time Out
Interrupt
Yes

ELEC 3300 : Spring 17/18 Tim Woo 14


Program flow
– Interrupt I/O allows the clock to interrupt the CPU announcing that the
device requires attention. This allows CPU to ignore devices unless they
request servicing via interrupt

Code Program
Start
Trigger Timer

Code background
Program
Interrupt still running
Service Time Out
Routine Interrupt

ELEC 3300 : Spring 17/18 Tim Woo 15


STM32 : Structure of general-purpose timer (TIM2 -TIM5)

• The high-density STM32F103xx performance line devices include up to


two advanced control timers, up to four general-purpose timers, two
basic timers, two watchdog timers and a SysTick timer.
– TIM1 / TIM8 – advanced control timers
– TIM2 / TIM3 / TIM4 / TIM5 – general purpose timers
– TIM6 / TIM7 – basic timers

ELEC 3300 : Spring 17/18 Tim Woo 16


STM32 : Structure of general-purpose timer (TIM2 -TIM5)

External clock mode2:


external trigger input
(ETR)

Internal trigger input


(ITRx): using one timers
as prescaler for another
timer
Timer Clock
(CK_CNT)

16-bit counter
External clock mode1:
external input pin (TIx)

Input Capture
Output Compare
PWM generation
One-pulse mode input

ELEC 3300 : Spring 17/18 Tim Woo 17


STM32 : Structure of general-purpose timer (TIM2 -TIM5)

Generate the Timer Clock

* Upcounting configuration is employed.

ELEC 3300 : Spring 17/18 Tim Woo 18


STM32 : Structure of general-purpose timer (TIM2 -TIM5)
With auto-reload features, says TIMx_ARR = 0X36h

* Upcounting configuration is employed.

ELEC 3300 : Spring 17/18 Tim Woo 19


STM32 : Structure of general-purpose timer (TIM2 -TIM5)

PWM generation

* Upcounting configuration is employed.

ELEC 3300 : Spring 17/18 Tim Woo 20


STM32 : Structure of general-purpose timer (TIM2 -TIM5)

• In the initialization of timer, we need to configure


– The parameters of input clock rate
• TIMx Timebase includes
– prescalar register (TIMx_PSC), auto-reload register (TIMx_ARR), etc
– The parameters of output waveform
• TIMx Output Compare, TIMx_CCRx Register

• For the implementation stage, we just need to enable the timer / counter
pin
– The result will be stored in the Counter Register (TIMx_CNT), Output Compare
Register (OCxREF), etc

ELEC 3300 : Spring 17/18 Tim Woo 21


STM32 : Structure of general-purpose timer (TIM2 -TIM5)

• Example: You can set up the Timebase structure as below.

TIM_TimeBaseStructure.TIM_Period = 36; <- TIMx_ARR register


TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);

CKD: Clock division


This bit-field indicates the division ratio between the timer
clock (CK_INT) frequency and sampling clock used by the
digital filters (ETR, TIx),
00: tDTS = tCK_INT
01: tDTS = 2 × tCK_INT
10: tDTS = 4 × tCK_INT
11: Reserved

ELEC 3300 : Spring 17/18 Tim Woo 22


STM32 : Structure of general-purpose timer (TIM2 -TIM5)

• Example: You can set up the Timebase structure as below.

TIM_TimeBaseStructure.TIM_Period = 36; <- TIMx_ARR register


TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);

Refer to page 388 of Reference Manual

Equivalently, the TIMx control register 1 (TIMx_CR1) is set as

Clock

Direction
division

ELEC 3300 : Spring 17/18 Tim Woo 23


Course Overview
Assembler

Instruction Set Architecture

Memory I/O System

Datapath & Control


Introduction to Basic Computer
Embedded Systems Structure
MCU Main Board

Digital and Analog


Interfacing USART, IEEE1394,
USB, I2C and SPI

Microcontroller Structure
A/D Port
Buffering and
Serial Port Direct Memory Access
CPU
(DMA)
External Memory Port
Memory,
Interfacing to Memory,
External Interrupt Port
Memory Timing
Interrupt and applications
External Timer Port Interfacing LCD
Organization Timer and
Counter Simple I/O Port
Motor Interfacing

In this course, STM32 is used as a driving vehicle for delivering the concepts.
To be covered In progress Done

ELEC 3300 : Spring 17/18 Tim Woo 24


Reflection (Self-evaluation)
• Do you …
– List some applications for timer / counter operation ?
– State the important registers in the timer / counter operation?
– Write the sub-routines for event counting or timing the length of event?
– Describe the counter timing diagram in the count-down operation with
different parameters (Clock frequency, up/down; auto-reload values)?

ELEC 3300 : Spring 17/18 Tim Woo 25


Reflection (Self-evaluation)
• Challenges about laboratory experiment 4
– A periodic square wave can be implemented either by timer or while-
loop in main program.
• Can they be implemented either by polling or interrupt driven?
• Does the period change when the program optimization is applied?

– A sound is produced by inducing an electrical periodic square wave to


the loud speaker.
• Is it a monotone signal or multi-tone signal with its harmonics?
• How can we generate different pitches of sound?
• How can we adjust the volume of sound?

– Which pins of STM 32 are already configured?

ELEC 3300 : Spring 17/18 Tim Woo 26


Reflection (Self-evaluation)
Adjustment of volume
Fourier Series Coefficients
0.5
Duty cycle = 0.2
Periodic square wave 0.45 Duty cycle = 0.5

0.4

0.35

0.3

Amplitude
0.25

0.2

0.15

0.1

T T 0.05

0
0 1 2 3 4 5 6 7 8 9 10
The n-th of fundamental frequency fo, fo = 1/T

The Fundamental frequency, at 1/T

ELEC 3300 : Spring 17/18 Tim Woo 27


Timer Structure
• Devices that uses a high speed clock input to provide a series of time or
count related events

• Applications include
– timing references for control sequences,
– providing system ticks for OS,
– generating waveforms for serial port baud rate generations and
– generating waveforms for audible tones.

• Two modes of operation : Timer-mode and counter-mode operation

ELEC 3300 : Spring 17/18 Tim Woo 28


Program flow
– Interrupt I/O allows the clock to interrupt the CPU announcing that the
device requires attentionThis allows CPU to ignore devices unless they
request servicing via interrupt
– Software does not know when an interrupt will occur, and this makes it more
difficult to write code

Code Program
Start
Trigger Timer

Code background
Program
Interrupt still running
Service Time Out
Routine Interrupt

ELEC 3300 : Spring 17/18 Tim Woo 29

You might also like