SysTick, General-Purpose Timers, and PWM Output
What You'll Learn
- 1Configure SysTick for periodic interrupts or free running counts
- 2Explain prescaler, auto reload, and counter relationships
- 3Generate PWM with correct duty cycle resolution
- 4Use input capture to measure external signals
Timers are the most reused peripheral in any microcontroller design because time measurement underlies scheduling, delay, pulse generation, and event capture. The SysTick timer is a small core peripheral designed for operating system ticks, while the general purpose timer blocks provide prescaled counters, compare channels, and input capture. All of them work the same way at heart: a prescaler divides the clock, a counter runs up to an auto reload value, and compare channels flag or output when the count matches. PWM is simply that match event shaped into an output.
SysTick and the Time Base
SysTick is a 24 bit down counter with a reload value, a current value, and a control register that selects the clock source and enables the interrupt or free running mode. Setting reload to one less than the desired tick count gives an exact period, and writing the current value clears it so the next interval starts clean. It counts down to zero, sets the count flag, and optionally pends its interrupt.
Because it is part of the core, SysTick behaves identically on every Cortex-M part, which is why RTOS ports default to it. Free running mode without the interrupt gives a low overhead microsecond stopwatch that reads the current value to time short code sequences.
Prescaler, Auto Reload, and Counting Modes
A general purpose timer divides the timer clock by a 16 bit prescaler, then counts in up, down, or center aligned mode up to an auto reload value that is usually 16 bits wide. The update event that reloads the counter is also what latches new compare values when preload is enabled, which prevents duty cycle glitches mid period. The effective frequency is timer clock divided by prescaler plus one divided by auto reload plus one.
Center aligned counting is preferred for motor control because the counter direction produces symmetric PWM pulses and lets one trigger point sit at the period boundary for clean sampling. Up counting is cheaper and fine for simple PWM and delay work.
PWM, Capture, and Compare
In output compare mode the channel drives a pin when the counter matches the capture compare register, and the output mode selects active high, active low, or toggle. Edge aligned PWM sets the compare value as a fraction of the auto reload value to get duty cycle. Preloading both the auto reload and the compare registers means changes take effect only at the update event, giving glitch free updates.
Input capture latches the counter value on a selected edge, so software can subtract two captures to measure a period or measure a pulse width for infrared or sensor signals. Advanced timer blocks add complementary outputs with programmable dead time and a break input, which is what drives half bridge power stages safely.
Timer Register Chain
| Register | Meaning | Effect |
|---|---|---|
| Prescaler | Divides the timer clock | Sets the counting tick rate |
| Auto reload value | Top of the count | Sets the PWM period |
| Counter | Current count | Drives compare matches |
| Capture compare | Match threshold | Sets duty cycle or captures time |
| Control | Counting direction and mode | Up, down, or center aligned |
| Event generation | Software update | Forces a reload and latch |
Real-World Examples
- One millisecond RTOS tick â Reload SysTick to one less than the core clock divided by one thousand and enable its interrupt.
- Twenty kilohertz PWM â Prescale the timer clock, set the auto reload for the period, then set compare to a fraction of it for duty.
- Measure a servo pulse â Use input capture on both edges and subtract the two latched counts to read the pulse width.
- Glitch free duty update â Enable compare preload so new values latch only at the counter update event.
đĄCompute the Period From Both Registers
Common Mistakes
Forgetting the update event after changing the prescaler
Fix: New prescaler values only take effect when the counter reloads, so force an update event when needed.
Ignoring the timer clock source
Fix: Timers may run from a peripheral clock that is twice the core clock, which silently doubles every frequency.
Writing duty cycle without preload
Fix: Immediate compare writes can produce a shortened pulse and a current spike in a power stage.
Key Takeaways
- SysTick gives a portable time base for RTOS ticks and profiling.
- Prescaler and auto reload together set timer frequency and resolution.
- PWM duty is compare value over auto reload value at a fixed period.
- Input capture turns edges into timestamps without CPU polling.
Frequently Asked Questions
Why is my PWM frequency wrong?
Usually the timer clock is not the value you assumed, or the prescaler and auto reload were computed from different bases.
Can SysTick run without interrupts?
Yes, disable the interrupt and poll the count flag, which makes it a free running microsecond timer.
What is preload for?
It holds new compare or reload values until the next update event so outputs never change mid period.
Interview Questions
Q1: How do you get an exact one millisecond tick?
Reload equals the timer clock in kilohertz minus one, assuming the core clock is used as the SysTick source, and enable the interrupt.
Q2: Explain edge aligned versus center aligned PWM.
Edge aligned counts one direction and switches at the period boundary, while center aligned counts up and down, producing symmetric pulses favored by motor control.
Extended Learning: Beyond the Basics
Key Concepts Summary
This lesson covers fundamental concepts that serve as building blocks for more advanced topics. Master these basics before moving to intermediate and advanced material.
Prerequisite Knowledge
Before studying this topic in depth, ensure you understand:
- Basic definitions and terminology
- Core formulas and their derivations
- Common applications in real-world scenarios
- Typical pitfalls and misconceptions
Step-by-Step Walkthrough
Let us work through a complete example to solidify understanding:
Step 1: Identify the problem type. Determine which category this problem falls into and which approach is most appropriate.
Step 2: Apply the relevant formula. Substitute known values into the appropriate equation, paying attention to units and constraints.
Step 3: Simplify and solve. Work through the algebra step by step, checking each intermediate result.
Step 4: Verify the answer. Check that the solution satisfies the original conditions and makes logical sense.
Common Mistakes to Avoid
- Misidentifying the problem type â Always start by categorizing the problem
- Incorrect formula application â Verify the formula applies to your specific case
- Unit errors â Always check units are consistent throughout
- Sign errors â Pay careful attention to positive and negative values
- Rounding too early â Keep full precision until the final answer
- Forgetting constraints â Check that solutions satisfy all given conditions
Real-World Applications
Understanding this concept is essential for:
- Advanced problem solving in examinations
- Building intuition for more complex topics
- Developing analytical thinking skills
- Applying mathematical reasoning to practical situations
Practice Exercises
- Basic: Solve 5 problems of increasing difficulty using the core concept.
- Intermediate: Apply the concept to word problems with multiple steps.
- Advanced: Combine this concept with 2-3 other topics in a comprehensive problem.
- Challenge: Create your own problem that requires this concept plus additional knowledge.
Interview and Exam Tips
- Common question patterns: Identify which exam or interview formats test this topic
- Time management: Practice completing problems within time constraints
- Common traps: Know the typical mistakes examiners set as distractors
- Verification strategy: Develop a quick method to check your answer
Related Topics
- Previous Topic â Build on the foundation laid here
- Next Topic â See how this concept extends to more advanced material
- Practice Problems â Additional exercises with solutions
- Video Explanation â Visual walkthrough of key concepts
Quick Reference Card
| Concept | Formula/Rule | Key Insight | Common Use |
|---|---|---|---|
| Core Definition | See main text | Understand the intuition | Foundation for all applications |
| Key Formula | See main text | Memorize the derivation | Direct problem solving |
| Special Case | See main text | Recognize when it applies | Simplifies complex problems |
| Edge Case | See main text | Know the limitations | Avoids errors |
Further Reading
For students who want to go deeper:
- Standard textbooks on the subject
- Research papers applying this concept
- Online courses with video explanations
- Practice problem collections with detailed solutions