5.5.3 · D4 · HinglishEmbedded Systems & Real-Time Software

ExercisesTimers — PWM generation, input capture, output compare

2,452 words11 min read↑ Read in English

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

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

L1 — Recognition

Recall Solution 1.1

Divider hai, na ki PSC. 8 se divide karne ke liye: .

Recall Solution 1.2
  • Output Compare / PWM: Timer → Pin (count CCR tak pahunchta hai, hardware pin drive karta hai).
  • Input Capture: Pin → Timer (pin par edge aati hai aur CNT ko CCR mein copy karti hai). Mnemonic: Compare = Control the pin; Capture = Copy the time.
Recall Solution 1.3

ARR period set karta hai (poori saw-tooth length). CCR HIGH duration set karta hai (plum line kitni upar baithi hai). Frequency fixed rehti hai jab tum duty change karte ho — sirf orange split move hoti hai.


L2 — Application

Recall Solution 2.1

(a) → har tick ka hai. (b) Counter sweep karta hai, yaani ticks per period, toh . (c) .

Recall Solution 2.2

ticks rakho ⇒ . Period ticks. Kyunki sweep ticks leta hai, . Duty: .

Recall Solution 2.3

ticks ⇒ . Frame ticks . Servos pulse width directly read karte hain, toh ticks. → CCR=2000. (Dekho Servo and ESC Control kyun width, duty % nahi, woh hai jo servo decode karta hai.)


L3 — Analysis

Recall Solution 3.1

Scope sahi hai; student ne galat number se divide kiya. Duty hai , nahi. Counter ke paas per period distinct tick-slots hain (woh mein se har ek par ek tick spend karta hai), aur HIGH CCR=500 slots occupy karta hai → exactly half. (=999) ko denominator mein use karna ek phantom error invent karta hai jo hardware mein kabhi tha hi nahi.

Recall Solution 3.2

Counter dono edges ke beech ek baar wrap hua (woh 65535 hit kiya, 0 par reset hua, phir count karta raha). Raw subtraction negative jaati hai kyunki yeh wrap ignore karti hai. Yahan ka matlab hai " length ke poore counter cycles hatane ke baad non-negative remainder" — yeh ek negative difference mein ek full cycle wapas jodhta hai. Toh: /tick par: , toh .

Recall Solution 3.3

MHz, toh overflows har ticks par hote hain → overflow rate kHz. Lekin toggle har overflow par pin flip karta hai, aur ek poori wave ke liye do flips chahiye (HIGH→LOW→HIGH). Toh .


L4 — Synthesis

Recall Solution 4.1

ticks use karo → , MHz. Frame ticks . Idle pulse ticks ⇒ . Full pulse ticks ⇒ . Fit hoti hai? Longest pulse (2000) frame (2500) se kaafi kam hai → haan, comfortable margin. (Dekho Servo and ESC Control.)

Recall Solution 4.2

ke saath ( MHz), 1 Hz period ke liye chahiye ⇒ — 16-bit ceiling 65535 se bahut upar. Impossible. Divisor choose kaise karein: total division hai . Humein is product ko do factors mein split karna hai jo dono 16 bits mein fit hon (). Ek clean split ko round number banana hai: lo ⇒ (tick ), chhod ke () — dono comfortably 65536 se neeche. (Kai splits kaam karenge; yeh wala tick ko ek clean rakhta hai.) Period 1 Hz ticks ⇒ (fits!). Duty 10 %: . Check: . ✓


L5 — Mastery

Recall Solution 5.1

HIGH hold karta hai jab , ( slots) across.

  • 0 %: → condition kabhi true nahi → pin kabhi HIGH nahi.
  • 100 %: saare 1000 slots ke liye HIGH chahiye → . Ruko — kya CCR ARR se zyada ho sakta hai? Haan. Compare register ARR tak clamped nahi hai; yeh sirf ek 16-bit value hai jise hardware compare karta hai. Agar , counter kabhi sweep ke dauran CCR tak nahi pahunchta ya cross nahi karta, toh "go LOW" condition kabhi fire nahi hoti aur pin poore cycle ke liye HIGH rehta hai — exactly woh 100 % jo hum chahte hain. Tab har count ke liye true hai.
  • single slot LOW chhod deta hai → duty , 100 % nahi. Missing tick classic "almost-but-not-quite full brightness" bug hai.
Recall Solution 5.2

Maano = dono edges ke beech poore counter overflows ki sankhya; yahan . Har full cycle ticks ki hai, toh true gap raw difference mein poore cycles jodhta hai: /tick par: , toh . Single-wrap modulo ticks under-count karta — multi-cycle gaps ke liye tumhe overflow count zaroor track karna chahiye.

Recall Solution 5.3

Counter ek baar wrap hua. Yaad karo ka matlab hai "ek negative difference mein ek full cycle wapas jodho": /tick par HIGH pulse hai. (Bilkul aise hi H-bridge current-sense timing measure hoti hai.)

Recall Solution 5.4

Resolution = ek tick . Longest single-cycle span = ticks . Isse longer kuch bhi wrap karta hai aur overflow bookkeeping demand karta hai — trade-off hai fine resolution vs short unambiguous range.


Connections

  • Servo and ESC Control — L2/L4 pulse-width design problems.
  • Clock Tree and Prescalers — L4 balancing PSC vs ARR for out-of-range periods.
  • Interrupts and NVIC — L3/L5 overflow-counting for long captures.
  • Motor Control and H-Bridges — L5 dual-edge width sensing.
  • Encoder Mode — upar use ki gayi capture logic ka ek cousin.