🎉 75% of content is free forever — Unlock Premium from $10/mo →
CW
NEWSLIVESearch All Content
đŸ’ŧ Servicesâ„šī¸ Aboutâœ‰ī¸ ContactView Pricing Plansfrom $10

SysTick, General-Purpose Timers, and PWM Output

Microcontrollers & ARMGPIO and TimersđŸŸĸ Free Lesson

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

RegisterMeaningEffect
PrescalerDivides the timer clockSets the counting tick rate
Auto reload valueTop of the countSets the PWM period
CounterCurrent countDrives compare matches
Capture compareMatch thresholdSets duty cycle or captures time
ControlCounting direction and modeUp, down, or center aligned
Event generationSoftware updateForces 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

Duty cycle is compare over auto reload, but the actual frequency also depends on the prescaler. Check both before measuring with a scope.

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

  1. Misidentifying the problem type — Always start by categorizing the problem
  2. Incorrect formula application — Verify the formula applies to your specific case
  3. Unit errors — Always check units are consistent throughout
  4. Sign errors — Pay careful attention to positive and negative values
  5. Rounding too early — Keep full precision until the final answer
  6. 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

  1. Basic: Solve 5 problems of increasing difficulty using the core concept.
  2. Intermediate: Apply the concept to word problems with multiple steps.
  3. Advanced: Combine this concept with 2-3 other topics in a comprehensive problem.
  4. 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

Quick Reference Card

ConceptFormula/RuleKey InsightCommon Use
Core DefinitionSee main textUnderstand the intuitionFoundation for all applications
Key FormulaSee main textMemorize the derivationDirect problem solving
Special CaseSee main textRecognize when it appliesSimplifies complex problems
Edge CaseSee main textKnow the limitationsAvoids 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
CW

ChatWhole Learn Editorial Team

Written by experienced data scientists, software engineers, and educators. All content is reviewed by industry professionals to ensure accuracy and relevance.

Last reviewed: 2026-11-14

—
☆☆☆☆☆
0 ratings

Rate & Feedback

Need Expert Microcontrollers & ARM Help?

Get personalized tutoring, project support, or professional consulting.

Advertisement