Professional Documents
Culture Documents
Topic 6
Timer and Counter
Prof. Tim Woo
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
Start as timer:
Switch
press one time
Periodic
Waveform
Start as counter:
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
• Applications:
– Count the number of times that a user has pressed a key.
Auto-reload Register
• 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)
Increment
0003
Counter Register = 0
0001 0002 0004
• 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
Increment
Counter Register = 0
…
MCU
Interrupt pulse
Code Program
Start
Trigger Timer
background
Is the
No Timer interrupt
signal enabled? Time Out
Interrupt
Yes
Code Program
Start
Trigger Timer
Code background
Program
Interrupt still running
Service Time Out
Routine Interrupt
16-bit counter
External clock mode1:
external input pin (TIx)
Input Capture
Output Compare
PWM generation
One-pulse mode input
PWM generation
• 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
Clock
Direction
division
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
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
• 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.
Code Program
Start
Trigger Timer
Code background
Program
Interrupt still running
Service Time Out
Routine Interrupt