5.5.3 · D1 · HinglishEmbedded Systems & Real-Time Software

FoundationsTimers — PWM generation, input capture, output compare

2,354 words11 min read↑ Read in English

5.5.3 · D1 · Coding › Embedded Systems & Real-Time Software › Timers — PWM generation, input capture, output compare

Is page pe yeh assume kiya gaya hai ki tumne parent note ki koi bhi notation pehle nahi dekhi. Hum har letter, har subscript, aur har ko ground up se build karenge, usi order mein jis order mein woh ek doosre pe depend karte hain. Agar koi symbol appear karta hai, toh usse ek line pehle define kiya gaya hai. Upar se neeche padho.


0. Koi bhi symbol se pehle mental model

Ek clock imagine karo jisme ek hi haath ek dial pe ghoomta hai. Har tick par woh haath ek notch aage jump karta hai. Jab woh last notch tak pahunchta hai toh wapas start pe snap ho jaata hai aur phir se shuru ho jaata hai. Yahi poori machine hai. Hamara kaam hai iske parts ko naam dena.

Figure — Timers — PWM generation, input capture, output compare

Figure dekho: woh ghoomta hua haath counter hai, notches ticks hain, aur last notch (amber) ceiling hai. Yeh picture dimag mein rakho — neeche diye gaye har symbol is picture ke kisi ek part ka label hai.


1. Frequency aur period — metronome

YEH TOPIC ISKE BAARE MEIN KYUN CARE KARTA HAI: har timer symbol ya toh ek frequency (counter kitni tez feed ho raha hai) ya ek period (ek full sweep ya pulse kitni der chalti hai) se bana hota hai. aur ke bina kuch bhi nahi keh sakte.


2. The clock frequency — jahan ticks paida hote hain

Subscript "clk" sirf ek label hai jiska matlab hai clock. Yeh chip ke Clock Tree and Prescalers se aata hai. Abhi ke liye: ek raw, usually-bahut-fast heartbeat hai.

KYUN: 72 million ticks per second zyaadatar kamon ke liye bahut zyaada fast hai (ek 1 kHz LED blink ke liye counter ko 72,000 tak pahunchna padega — awkward). Toh hume ise slow karne ka koi tareeqa chahiye. Agle symbol mein yahi hai.


3. The prescaler — ek gearbox jo clock ko slow karta hai

Joh number hum program karte hain use kehte hain (short for prescaler). Lekin yahan ek crucial twist hai jo sabko confuse karta hai:

Figure — Timers — PWM generation, input capture, output compare

Figure dekho: left pe 72 white incoming ticks, right pe ek amber tick jo survive kar gayi — woh surviving-tick rate hi timer clock hai, haara agla symbol.


4. The counter — woh ghoomta hua haath

KYUN: har output event ki wajah hai aur har capture mein woh cheez hai jo copy hoti hai. Yeh woh dil hai jise baaki machine sunta hai.


5. The ceiling — jahan haath wapas snap karta hai

Figure — Timers — PWM generation, input capture, output compare

Figure dekho: ke liye dial pe 4 slots hain, kyunki chaar numbers hain, teen nahi. Yeh off-by-one timer code mein sabse common bug hai.


6. The threshold — dial pe "off line"

Figure — Timers — PWM generation, input capture, output compare

Figure dekho: amber "off line" sweep ko ek HIGH stretch (left, cyan) aur ek LOW stretch (right, dim) mein split kar rahi hai. Line ko right slide karo → zyaada HIGH → brighter LED / faster motor.


7. Do captures aur wrap — symbol

Jab input capture do snapshots leta hai, toh hume do counter values milti hain. Inhe (pehli edge) aur (doosri edge) bolte hain. Elapsed ticks hain . Lekin kya hua agar counter edges ke beech wrap (§5) ho gaya? Toh se chhota hoga aur raw subtraction negative ho jaayegi — clearly galat, kyunki time kabhi ulta nahi chalta.


8. Saare symbols ek diagram pe

Ab har symbol ka ek meaning bhi hai aur ek picture bhi. Yeh dikhaata hai ki woh topic ko kaise feed karte hain:

Frequency f and period T

Source clock f_clk

Prescaler PSC divide by PSC plus 1

Timer clock f_tim and tick time

Counter CNT counts up

Top value ARR wraps to zero

Period T equals ARR plus 1 times tick

Threshold CCR

PWM and duty D

Input capture copies CNT into CCR

Two captures use mod for wrap

Timers topic 5.5.3

Upar se neeche padho: frequency/period ideas clock ko feed karte hain, clock ko prescaler gear down karta hai, woh counter drive karta hai, counter plus uski ceiling period dete hain, counter plus ek threshold ya toh PWM (out) ya capture (in) deta hai, aur dono full topic mein flow karte hain.


9. Real peripherals mein har symbol kahan dikhta hai

  • chip ke Clock Tree and Prescalers se aata hai — tum ise invent nahi karte, inherit karte ho.
  • compare se drive hone wale output pin ko GPIO and Alternate Functions alternate function mode mein switch karna padta hai, taaki timer (tumhara code nahi) pin ka owner ho.
  • Overflow aur capture events Interrupts and NVIC raise karte hain — isi tarah software §7 ke wrap handling ke liye extra laps count karta hai.
  • PWM duty Motor Control and H-Bridges mein speed feed karta hai; pulse width (µs mein raw ) Servo and ESC Control ko feed karta hai.
  • Counting-both-directions cousin of capture Encoder Mode mein rehta hai.
  • Hindi walk-through prefer karte ho? Hinglish note dekho.

Upar sab kuch woh vocabulary hai jisme parent Timers note baat karta hai. Ab tumhare paas har letter hai.


Equipment checklist

Cover the right side and answer aloud. Agar koi bhi ruko, toh uska section dobara padho.

ka seedha matlab kya hai?
Frequency aur period ek hi baat ka ulta hai — beats-per-second vs seconds-per-beat.
kya hai?
Timer ko feed karne wali raw source clock frequency (jaise 72 MHz), usually on purpose bahut fast.
Prescaler ki jagah se kyun divide karta hai?
Taaki smallest register value se 1 se divide ho, divide-by-zero avoid ho sake.
72 MHz clock ko 1 MHz tak laane ke liye kya hoga?
(72 se divide karta hai).
kya hai aur yeh kaise bana hai?
Surviving tick ke seconds, .
kya hai?
Counter register — ek number jo tick karta hai, woh ghoomta hua clock haath.
Ek sweep mein ticks ki jagah ticks kyun hote hain?
Counting mein value bhi shamil hai, toh mein numbers hain.
PWM mode vs capture mode mein kya karta hai?
PWM: tum ise set karte ho, yeh HIGH-to-LOW threshold hai (Time→Pin). Capture: hardware ise likhta hai, pin edge pe snapshot karke (Pin→Time).
Duty cycle formula kya hai?
.
kya fix karta hai?
Do captures ke beech counter wrap, jo warna elapsed time ko negative bana deta.
First symbols se full period formula?
.