الميدترم: أول 5 محاضرات (لحد آخر التايمرز) + شيت 1 و 2. مقالي كله ومن غير داتاشيت. الشرح بالعربي، والجمل اللي تتحفظ بالإنجليزي زي ما هتكتبها في الامتحان. علامة EXAM = جه في امتحان قبل كده.
MIND MAPLectures 1–5 at a glance
الخريطة الكاملة للي داخل في الميد. كل عنوان أزرق بيوديك لشرحه. ذاكر بيها عشان تعرف إنت فين وكل حاجة تابعة لإيه.
1.1 Embedded systems الأساسيات
Embedded systemhardware and softwarespecific functionlarger systemmobile phone
CPUMemoryRAM, ROM, FlashPeripheralsBus systemssystem bus
25 marks, all essay — no MCQ this year.كله مقالي؛ السنة اللي فاتت كانت 10 MCQ + 6 + 9 كود.
Scope: Lectures 1–5 (up to the end of Timers), Sheet 1, Sheet 2.INT0 و USART و ADC Vref برّه.
No datasheet: prescaler / mode values are given, but you must know the registers and their bits.الجداول بتتدّى، أسماء الريجسترز والبتات لأ — شوف Registers.
Every code answer starts with #define F_CPU + the #include lines (they carry marks), then the SET / CLEAR / TOGGLE / GET macros.الـ includes عليها درجة؛ والماكروز دي طريقة المعيدة. شوف Sheet 2 Ex 0.
Last midterm: power 4 · power mode 2 · timer code 9 · theory 10.الكود والحسابات أكتر من نص الدرجة.
Study plan
#
Lecture
Slides
Topic
Then solve
1
L1
S8–S36
ES, processors, MCU selection
Sheet 1 Q1–Q2 · Final 2026 Q1
2
L1
S37–S62
ATmega32 architecture numbers
Flashcards L1
3
L2
S2–S37
Power modes + power calculation
Sheet 1 Q3–Q6 · Mid 2026 Q2
4
L2
S38–S73
Firmware, data types, GPIO
Mid 2026 Q1 (3)–(4)
5
L3
S2–S39
Coding styles, software PWM
Expected Q: theory set
6
L3
S40–S69
Timers, prescaler, Timer0 registers
Registers: TCCR0 drill · Mid 2026 Q1 (5)–(6)
7
L4
S2–S25
Normal mode + ISR
Sheet 2 Ex 1a, 1b · Mid 2026 Q3
8
L4
S26–S65
CTC + Fast PWM
Sheet 2 Ex 2, 3 · Expected Q4
9
L4
S66–S84
Event counter
Sheet 2 Ex 4 · Expected Q2
10
L5
S1–S41
Timer1, input capture, TIMSK/TIFR
Registers drill (all) · Sheet 2 Ex 5, 6 · Final 2026 Q5
11
ALL
—
Past exams on a timer, newest first
Mid 2026 (1 hour) → Final 2026 midterm parts
12
ALL
—
Review
Flashcards (all) + Solvers presets
Differences from the slides / model answer
Preload: slides & model answer use 255 − ticks; mathematically it is 256 − ticks (Sheet 2 code uses 256).فرق tick واحد؛ اكتب القانون اللي بتستخدمه صريح.
المحاضرة دي نظري + مسألة واحدة مهمة جدًا: اختيار الـ MCU بالحساب (Sheet 1 Q1/Q2 وجت في الفاينال 15 درجة). النظري بييجي كـ "عرّف / قارن / اذكر" في سؤال مقالي. ركز على: تعريف الـ ES، أنواع الـ processors، Microprocessor vs Microcontroller، ومواصفات ATmega32 بالأرقام.
خريطة المحاضرة — العناوين وتابعة لإيه (دوس على أي عنوان يوديك له)
Microcontrolleron a single chipone task onlyburned into ROMany number of applications
1.2 MCU selection اختيار الكنترولر
CostEnvironmentPeripheral supportScalability
Available powerWeighted power≤ available powerPerformancebelow the required frequencyMemory
1.3 ATmega32 المعمارية
8-bit AVRinstructions and dataat the same timeRISC1–2 clock cycles32 general-purpose registers
What is an Embedded System?slides 8–13
Final 2026 Q3-18
Embedded system = a combination of computer hardware and software, either fixed in capability or programmable, designed for a specific function within a larger system.نظام هاردوير + سوفتوير معمول عشان يعمل وظيفة محددة، وهو جزء من حاجة أكبر.
Second definition: information-processing systems integrated into a larger product.تعريف تاني: نظام بيعالج بيانات بس مدفون جوه منتج أكبر.
Examples: car, train, electronic devices, mobile phone, smart house (lighting/heating).أمثلة: العربية، القطر، الموبايل، البيت الذكي.
We buy these products not for information processing itself but for driving, transportation, communication, convenience; the ES is an essential part of achieving that purpose.إنت مش بتشتري العربية عشان "تعالج بيانات"، بتشتريها عشان تسوق؛ الـ ES هو اللي بيخلي ده يحصل.
سؤال الفاينال: "Which is NOT a typical application of ES?" الإجابة Desktop word processing لأنه general-purpose computing مش وظيفة محددة.
General Types of Embedded Systemsslides 14–15
Type
Description
Examples
General Computing
like desktop apps but compact and integrated
video games, set-top boxes, wearables, ATMsزي الكمبيوتر بس صغير ومدمج.
Control Systems
closed-loop feedback for real-time processes
vehicle engines, chemical processes, nuclear plants, flight controlفيه feedback بيتحكم في عملية في real time.
Signal Processing
large data streams, computation-heavy
radar, sonar, video compressionداتا كتير وحسابات تقيلة.
Communication & Networking
switching, managing, transmitting information
telephone systems, Internetنقل وتوجيه المعلومات.
Components of an Embedded Systemslides 16–17, 20
CPU: executes instructions.بينفّذ الأوامر.
Memory: stores code and data (RAM, ROM, Flash).بتخزن البرنامج والبيانات.
Bus systems: communication pathways (data bus, address bus, control bus).الطرق اللي بتوصل المكونات ببعض.
Block diagram: Processor + mem on a system bus; Control (output) drives motor/light, Observe (input) reads a temperature sensor.الرسمة: البروسيسور بيقرا sensor (input) وبيحرك motor (output).
Slide 17 · CPU متوصل بـ RAM/ROM/Interface عن طريق address/data/control busSlide 20 · Embedded System Block Diagram: Observe (input) و Control (output)
Types of Embedded Processorsslides 18–19
Final 2026 Q3-19
Type
Key idea
Example
Microprocessor
general-purpose CPU for complex systems
ARM Cortex-ACPU بس، والباقي بيتركب برّه.
Microcontroller
CPU + memory + peripherals on a single chip
ARM, AVR, PICكله على شريحة واحدة.
DSP
specialized for signal processing (audio, video, digital data)
TI C2000متخصص في معالجة الإشارات.
ASIC
custom-designed circuit for a specific application, high efficiency
—دايرة متفصّلة لمهمة واحدة.
FPGA
reconfigurable hardware for custom tasks, flexibility
—هاردوير تقدر تعيد تشكيله.
Embedded processors (MCUs) vs PC processors: lower clock speeds, integrated memory, integrated I/O (A/D, D/A, PWM, CAN), higher environmental specs.الـ MCU سرعته أقل بس فيه الميموري والـ I/O جواه ومستحمل ظروف أصعب.
Microprocessor vs Microcontrollerslides 21–26
ESSAY
Microcontroller: microprocessor + memory + I/O lines on a single chip (VLSI).ميكروكنترولر = بروسيسور + ميموري + I/O على شريحة واحدة.
An embedded product does one task only; one application is burned into ROM.المنتج المدمج بيعمل حاجة واحدة وبرنامجه محروق في الـ ROM.
A computer runs any number of applications: RAM + OS loads programs into RAM.الكمبيوتر بيشغل أي برنامج عن طريق الـ OS.
General-purpose Microprocessor
Microcontroller
On-chip
no RAM, no ROM, no I/O ports
CPU + RAM + ROM + I/O + Timer + ADCالبروسيسور فاضي، الـ MCU فيه كل حاجة.
System
must add RAM/ROM/I/O/timers externally → large & expensive
fixed on-chip resources → ideal when cost & space are criticalالبروسيسور محتاج حاجات برّه فبيكبر ويغلى.
Advantage
flexible amount of RAM/ROM/I/O
low space, low power, low price per unitميزة الـ MCU: صغير ورخيص وموفر.
Slide 22 · فوق: Microprocessor والحاجات برّه على bus. تحت: كل ده جوه الـ MCU
MCU Selection Guideslides 27–29
SH1 Q1-Q2Final 2026 Q1
Criterion
Use case
Example MCU
Power consumption
battery devices (wearable health monitors)
MSP430 (ultra-low-power)لو على بطارية.
Processing speed
real-time audio/video
ARM Cortex-M4 (STM32F4) up to 180 MHzلو محتاج سرعة.
Step 0 — Available power = Total budget − (sensors + communication + display).الأول: الميزانية المتاحة للـ MCU = الكلي − باقي المكونات.
Step 1 — Weighted power P = P_active × active% + P_sleep × sleep%.احسب متوسط استهلاك كل MCU حسب نسبة الشغل والنوم.
Check every MCU is ≤ available power.اتأكد إن الكل جوه الميزانية.
Step 2 — Performance: reject any MCU with CPU clock below the required frequency.ارفض أي MCU سرعته أقل من المطلوب.
Step 3 — Memory: prefer more Flash/SRAM for data-heavy applications.بعدها قارن الميموري.
Final decision with a reason line (power + performance + memory).اختم بجملة قرار فيها السبب.
Lecture example (500 mW, 30% active)
P_weighted
Clock
Verdict
A: 16.5 / 0.9 mW
4.95 + 0.63 = 5.58 mW
110 MHz
OK but 64 KB SRAM
B: 10.2 / 0.3 mW
3.06 + 0.21 = 3.27 mW
48 MHz
✗ < 100 MHz
C: 9.9 / 1.4 mW
2.97 + 0.98 = 3.95 mW
150 MHz
✓ chosen (384 KB SRAM)
الدكتور بيحب الإجابة في 3 خطوات بعناوين: Power → Performance → Memory → Final Decision. اكتبها كده بالظبط.
ATmega32 Architecture — the numbersslides 37–62
Mid 2026 Q1-2Final 2026 Q3-20
8-bit AVR: processes 8 bits (1 byte) at a time.بيشتغل على بايت واحد كل مرة.
Modified Harvard architecture: separate paths for instructions and data → fetch both at the same time → faster.طريق للبرنامج وطريق للداتا، فبيجيب الاتنين مع بعض.
RISC: simple instructions, 1–2 clock cycles each.أوامر بسيطة بتاخد cycle أو اتنين.
32 general-purpose registers: fast "scratchpads" inside the CPU.32 مسجل جوه الـ CPU للحسابات السريعة.
Throughput 16 MIPS @ 16 MHz (~1 MIPS per MHz). With 8 MHz → ~8 MIPS.مليون أمر لكل ميجاهرتز؛ لو الكريستال 8 ميجا يبقى 8 MIPS.
Watchdog Timer = safety guard: resets the MCU automatically if the program hangs.لو البرنامج علّق، الـ watchdog بيعمل reset.
Special Function Registers (SFRs): memory addresses that control features (pin direction, timers, interrupts, ADC/UART) — "control switches".مسجلات تحكم: إنت بتكتب فيها عشان تشغّل وتظبط الطرفيات.
Packages: PDIP (40 pins, through-hole, breadboard, easy, bulkier) vs TQFP/MLF (square, surface-mount, compact, hard to hand-solder, better heat for MLF).PDIP للتجارب والطلبة، TQFP للمنتجات التجارية.
T0 8-bit · T1 16-bit · T2 8-bit async · ADC 8ch 10-bit · WDT resets on hang
LECTURE 2Power modes, firmware, data types, GPIO
0/0
المحاضرة دي فيها أكتر حاجتين بيجوا: (1) حساب الباور best/worst case (جه 4 درجات في الميد اللي فات) و (2) اختيار الـ power mode لحالة (جه 2 درجة). وفيها كمان GPIO (DDRx/PORTx/PINx + pull-up) اللي هتستخدمه في أي كود. في الامتحان مفيش داتاشيت، فالتيارات هتتدّيلك في السؤال.
خريطة المحاضرة — العناوين وتابعة لإيه (دوس على أي عنوان يوديك له)
what kind of valuehow much memoryPerformance & memoryPortabilityType safetyReadability & maintenance
2.3 GPIO الـ pins
A, B, C, DDDRx1 = output, 0 = inputPORTxPINxread-only
active-lowPA3
The 7 Power Management Modesslides 3–29
Mid 2026 Q2bSH1 Q5-Q6Final 2026 Q3-14/15
Mode
CPU
What keeps running
Wake-up / note
Active
ON
everything
highest power (~15–20 mA @5V,16MHz)كله شغال، أعلى استهلاك.
Idle
OFF
clocks + all peripherals (timers, UART, SPI, TWI, WDT, analog comparator, interrupts)
any interrupt; ~6–8 mAالـ CPU نايم بس الطرفيات شغالة.
ADC Noise Reduction
OFF
ADC clock + interrupts (I/O clock OFF)
ADC complete interrupt; ~1–2 mA @1MHzكل حاجة واقفة إلا الـ ADC عشان القراية تبقى نضيفة.
Power-down
OFF
only INT0/INT1/INT2, WDT, Reset (oscillator OFF)
lowest power (few µA), slowest wake-upأعمق نوم، بيصحى بـ external interrupt.
Power-save
OFF
like Power-down + async Timer2 (32.768 kHz crystal on TOSC1/2)
periodic wake-up by Timer2زي Power-down بس معاه منبّه (Timer2).
Standby
OFF
like Power-down + external oscillator running
fast wake-upالكريستال شغال فبيصحى بسرعة.
Extended Standby
OFF
Standby + async Timer2
fast wake-up + periodic timerStandby ومعاه Timer2.
Idle use cases: digital clock waiting for 1 s timer interrupt, serial waiting for data, smart sensors.Idle لما تستنى event والطرفيات لازم تفضل شغالة.
ADC Noise Reduction: digital circuits make noise → unstable ADC readings → stop them for precise analog measurement (LM35, gas sensor, medical). Used instead of Idle because it reduces digital noise interference.بيوقف الضوضاء الرقمية عشان القراية تبقى دقيقة.
Power-down: remote control asleep until a button, temperature logger, IoT node; can run months/years on a coin cell.للأجهزة اللي بتشتغل نادرًا.
Power-save: digital clock with 32.768 kHz crystal, data loggers, IoT nodes that wake periodically.لما تحتاج تصحى كل فترة لوحدك.
Extended Standby vs Standby: the only difference is that async Timer2 keeps running in Extended Standby.الفرق الوحيد إن Timer2 شغال.
اختيار الـ mode: لو فيه "every hour / periodic" → Power-save. لو فيه "respond immediately / fast response" → Standby (ومع timer → Extended Standby، ودي إجابة الموديل آنسر في الميد). لو "accurate ADC" → ADC Noise Reduction. لو "wake on button only" → Power-down.
Power Estimation from Electrical Characteristicsslides 30–35
Mid 2026 Q2a (4 marks)SH1 Q3-Q4
P = I × V.القدرة = التيار × الجهد.
Worst case → use maximum current; best case → use minimum (if min is empty, use typical).أسوأ حالة = أكبر تيار، أفضل حالة = أقل تيار.
P_total = V·I_active·active% + V·I_idle·idle%.اضرب كل باور في نسبته واجمع.
Case
Data
Work
Result
Worst, 8 MHz, 5 V, 60/40
I_act,max 15 mA · I_idle,max 8 mA
75×0.6 + 40×0.4
61 mW
Best, 1 MHz, 3 V, 20/80
I_act,typ 0.6 · I_idle,typ 0.2
1.8×0.2 + 0.6×0.8
0.84 mW
Mid 2026 best, 8 MHz, 5 V, 60/40
I_act,min 8 · I_idle,min 3
5×8×0.6 + 5×3×0.4
30 mW
Mid 2026 worst
15 / 8 mA
5×15×0.6 + 5×8×0.4
61 mW
Slide 31 · جدول I_CC من الداتاشيت (في الامتحان الأرقام بتتدّيلك)
اكتب القانون الأول بالرموز، وبعدين عوّض، وبعدين الناتج بالوحدة mW. ده اللي واخد الدرجة في الموديل آنسر.
Embedded Systems and Firmwareslides 38–45
ESSAY
Firmware: special software stored in non-volatile memory (Flash, EEPROM, ROM) that directly controls the hardware (not running on a PC OS).سوفتوير متخزن في ميموري مبتتمسحش وبيكلم الهاردوير مباشرة.
Four layers (bottom → top): Driver → HAL → Middleware → Application.4 طبقات من تحت لفوق.
Driver layer: talks directly to hardware registers and pins.بيكلم الريجسترز والـ pins.
HAL: bridge between hardware and software, hides hardware-specific details.بيخبّي تفاصيل الهاردوير.
Middleware: reusable services (communication protocols, data processing).خدمات جاهزة متكررة.
Application: the actual logic, what the product does (e.g. blinking an LED).منطق المنتج نفسه.
Advantages: Modularity, Portability (move code to other MCUs), Maintainability (fix one layer only).مميزات: تقسيم، نقل لكنترولر تاني، صيانة أسهل.
Disadvantage: too many layers slow real-time systems (extra function calls and data passing).عيب: طبقات كتير = بطء في الـ real time.
Slide 44 · الطبقات الأربعة
Programming Languages & Code → Hexslides 46–52
Selection criteria: Efficiency, Performance, Ease of use, Portability, Maintainability, Readability, Integration (IDE support).معايير اختيار اللغة.
هنا بتبدأ أهم حتة في الميد: التايمرز. أول جزء نظري (Flatten / Macro / Function و Software vs Hardware PWM) بييجي سؤال مقارنة. بعدين الـ Prescaler (جه في الميد والفاينال) ومسجلات Timer0 — الدكتور قال إنه هيديك جداول القيم (CS / WGM) بس لازم تكون عارف الريجسترز وأسماء البتات وفين كل حاجة.
خريطة المحاضرة — العناوين وتابعة لإيه (دوس على أي عنوان يوديك له)
S2–S39 ← Expected Q: theory set · S40–S69 ← Registers: TCCR0 drill · Mid 2026 Q1 (5)–(6)
TCNT0write a start valueOCR0TCNT0 == OCR0OC0 (PB3)TCCR0
Counter unitcountdirectionclearclkT0TOP
Flatten vs Macro vs Function-based codingslides 2–17
Final 2026 Q3-12ESSAY
Flatten: all code directly in main(), no modularization.كل الكود جوه main على طول.
Flatten: low abstraction, highest speed (no calls, no macro expansion), low maintainability, hard debugging/scalability.أسرع حاجة بس صعب تصلحه أو تكبّره.
Macro-based: #define macros replaced inline by the preprocessor → no call overhead → efficient.الماكرو بيتنسخ مكانه، فمفيش تكلفة استدعاء.
Function-based: functions for set/clear/toggle, pointers to register addresses (volatile uint8_t *var).دوال بتاخد عنوان الريجستر.
Function-based: best for large projects, slight performance penalty (call overhead), highly maintainable, easy debugging, fits HAL/drivers.أنضف وأسهل صيانة بس أبطأ سنة.
Aspect
Flatten
Macro
Function
Hex size (debug)
463 B
463 B
635 B
Speed
Fastest
Fast
Slightly slower
Readability / Maintainability
Poor
Moderate
Excellent / High
Reusability
None
Moderate
High
Debugging
Difficult
Harder
Easier
Best use
time-critical low-level routines
inline hardware control
modular systems
Function-based is larger because of stack usage, call frames and return instructions.الحجم الزيادة من الـ stack والاستدعاء والرجوع.
Modern compilers can inline small functions automatically → both speed and structure.الكومبايلر ممكن يعمل inline ويجمع المزايا.
سؤال الفاينال: ليه Flatten أسرع؟ لأنه avoids function calls and macro expansion.
PWM: software vs hardwareslides 18–39
ESSAY
PWM = control power by switching the signal ON/OFF very fast. Duty cycle = ON time ratio; average voltage ∝ duty.بنتحكم في القدرة بالتقطيع السريع؛ الـ duty هو نسبة الـ ON.
50% → half speed; 100% → full speed. Used for DC motor speed, servo angle, LED brightness; saves energy (switches power instead of reducing voltage).استخداماته وميزته إنه مش بيهدر طاقة.
Software PWM: code toggles a pin with delays. Cons: accuracy issues (delays not precise) + high CPU usage (CPU stuck in delay loops). Pros: easy.بالكود: سهل بس مش دقيق وبيستهلك الـ CPU.
Hardware PWM: timers generate it automatically → precise, stable, frees the CPU for sensors/communication. Needs timer knowledge.بالتايمر: دقيق والـ CPU فاضي لحاجات تانية.
Example (50 Hz, 16 MHz, PA6 out, switch PC4): period 20 ms; DELAY_H = 20·i/100, DELAY_L = 20 − DELAY_H; duty 90→0 step 10; each level repeated 100 times (100 × 20 ms = 2 s).المثال: كل مستوى duty بيتكرر 100 مرة = 2 ثانية.
Timers: concept and benefitsslides 40–48
ESSAY
A hardware timer is a counter that ticks from a fixed clock: measures time, schedules work, generates signals without busy-waiting.عداد هاردوير بيعدّ لوحده من غير ما يشغل الـ CPU.
Prescaler = frequency divider between F_CPU and the timer → slows the timer clock → longer intervals without changing CPU speed.مقسّم تردد بيبطّأ التايمر بس، مش الـ CPU.
Values for Timer0/1: 1, 8, 64, 256, 1024.القيم المتاحة.
f_timer = F_CPU / N · T_tick = N / F_CPU.سرعة التايمر وزمن التكة.
T_overflow = N × (MAX+1) / F_CPU (8-bit: ×256, 16-bit: ×65536).زمن الـ overflow.
Why important: without it the 8-bit timer at 1 MHz overflows every 256 µs (too short). With N = 64 → 16.384 ms (~61 overflows/s) → count overflows for seconds.من غيره التايمر بيخلص بسرعة جدًا.
Slide 58 · الـ prescaler بيطلع clk/8, /64, /256, /1024 و CS02:0 بتختار
سؤال Final 2026 Q4C "What is the importance of the prescaler?": اكتب التعريف + المثال 256µs → 16.384ms + "allows long, human-scale timing by counting overflows without changing CPU speed".
Timer0 registers: TCNT0, OCR0, TCCR0slides 54–63
Mid 2026 Q1-6كل كود
TCNT0: 8-bit counter holding the current count (0x00–0xFF); read it or write a start value; at 255→0 it overflows (TOV0).العداد نفسه؛ ممكن تكتب فيه قيمة بداية.
OCR0: compare value; when TCNT0 == OCR0 → compare match → toggle/set/clear OC0 (PB3), compare interrupt (OCIE0), reset timer (CTC), set duty (PWM).القيمة اللي التايمر بيتقارن بيها.
COM01:00 non-PWM: 00 disconnected · 01 toggle · 10 clear · 11 set OC0 on match. Fast PWM: 10 non-inverting (clear on match, set at BOTTOM) · 11 inverting.سلوك pin الـ OC0.
Slide 63 · جداول COM (non-PWM / Fast PWM / Phase Correct)
Inside Timer0: counter, compare unit, waveform generatorslides 64–69
Counter unit internal signals: count (inc/dec by 1), direction, clear, clkT0, TOP (reached 0xFF or OCR0), BOTTOM (reached 0x00).إشارات العداد الداخلية.
Output Compare Unit: continuously compares TCNT0 with OCR0 → on match sets OCF0 and (if OCIE0 enabled) raises a compare interrupt.بيقارن وبيرفع OCF0.
Waveform generator: uses the match signal to drive OC0 according to WGM01:0 and COM01:0.بيطلّع الموجة على الـ pin.
Slide 65 · 8-bit Timer/Counter block diagram
الخلاصة — Lecture 3أهم نقط المحاضرة في سطور؛ راجعها قبل ما تقفلها
WGM 00 Normal · 10 CTC · 11 Fast PWM · CS 101 = /1024 · 111 = T0 rising
LECTURE 4Normal, ISR, CTC, Fast PWM, event counter
0/0
دي قلب الميد. سؤال الكود اللي جه 9 درجات في الميد اللي فات كان Timer0 Normal mode + حساب prescaler و TCNT0. كل mode فيه: (1) فكرته (2) القانون (3) البتات في الريجسترز (4) الكود. احفظ الخطوات الثابتة لأي كود تايمر، والأرقام هتتحسب بالقانون.
خريطة المحاضرة — العناوين وتابعة لإيه (دوس على أي عنوان يوديك له)
Timer counts 0x00 (BOTTOM) → 0xFF (TOP) then overflows back to 0.بيعد لفوق لحد 255 ويرجع صفر.
On overflow TOVn is set automatically → poll it or use the overflow ISR TIMERn_OVF_vect (enable with TIMSK TOIEn).الـ flag بيترفع؛ يا تراقبه يا تستخدم interrupt.
Best for long delays / low-frequency square waves, especially when the delay exceeds one overflow N_MAX × 2ⁿ / F_CPU.مناسب للتأخيرات الطويلة.
ISR = special function executed when an interrupt occurs (e.g. timer overflow) → react automatically instead of polling.دالة بتشتغل لوحدها لما يحصل الحدث بدل ما تفضل تراقب.
Syntax: ISR(vector_name){ ... } (macro from avr-gcc, #include <avr/interrupt.h>).الصيغة.
Upon reset all interrupts are disabled (masked); SREG I-bit (bit 7) = 0.بعد الـ reset كل الـ interrupts مقفولة.
Two levels must be enabled: peripheral enable (e.g. TIMSK |= (1<<TOIE0)) and global enablesei() (sets I). cli() clears I.لازم الاتنين: بت الطرفية + sei.
Keep ISR entries infrequent: each call/return adds overhead → extra CPU delay.متخليش الـ ISR يتنده كتير.
Two ways to access a timer event: polling the flag in TIFR vs interrupt-driven (TIMSK enable + sei + ISR).طريقتين: polling أو interrupt.
الفاينال: sei جوه ISR → nested interrupts are allowed. ولو الـ I-bit مقفول وجه interrupt → الـ flag بيتسجل ويستنى لحد ما تعمل sei. (المتغير اللي بيتعدل في ISR اعمله volatile.)
CTC Mode (Clear Timer on Compare Match)slides 26–47
Mid 2026 Q1-7SH2 Ex2
Timer counts up; when TCNTn == OCRn: (1) timer resets to 0 (2) OCFn is set → ISR TIMERn_COMP_vect if OCIEn enabled.لما يساوي OCR بيصفّر ويرفع OCF.
TOVn is NOT set in CTC (counter never reaches 0xFF unless OCR = 0xFF).مفيش overflow هنا.
OCn pin can toggle / set / clear automatically on match (COM bits) → square waves without CPU.الـ pin ممكن يتقلب لوحده.
Uses: precise delays and square wave generation.استخدامين.
Delay = N × (OCRn + 1) / F_CPU → OCRn = Delay × F_CPU / N − 1.قانون التأخير.
Timer2 جدول الـ prescaler بتاعه مختلف عن Timer0/1: 100 = /64 و 110 = /256 و 111 = /1024 (وفيه /32 و /128). الدكتور هيديه، بس خد بالك إنك تستخدم جدول التايمر الصح.
Fast PWM Modeslides 48–65
SH2 Ex3
Timer counts 0 → TOP then instantly resets to 0 → fixed-period saw-tooth. 8-bit TOP = 255; 16-bit up to 65535.بيعد لفوق ويرجع صفر فورًا.
OCn changes automatically at TCNTn == OCRn; duty set by OCRn.الـ OCR بيحدد الـ duty.
Non-inverting (COMn1:0 = 10): clear OCn on match, set at BOTTOM → duty = OCR/255. Inverting (11): opposite.non-inverting: عالي في الأول وبيقع عند الـ match.
Flags: OCFn on match, TOVn at rollover.الاتنين بيترفعوا.
لاحظ تناقض في المثال: نص السؤال بيقول السويتش على PA0 والكود على PA2. في الامتحان استخدم الـ pin اللي في نص السؤال.
Event Counter (external clock on T0/T1)slides 66–84
Mid 2026 Q1-8SH2 Ex4
Timer counts external events (edges on a pin) instead of clock time; e.g. heartbeats.بيعد أحداث خارجية بدل الوقت.
External clock pins: T0 = PB0 (Timer0), T1 = PB1 (Timer1). Each edge → TCNTn + 1.الـ pins الخاصة بالعد.
The pin is sampled once every system clock (synchronization) because the external signal is not synchronized with the CPU clock; e.g. 8 MHz → every 125 ns.الـ pin بيتقري كل clock عشان يتزامن.
Edge detector generates one count per edge (even if the signal stays high). CSn2:0 = 110 (6) → falling, 111 (7) → rising.كل edge = عدّة واحدة بس.
آخر محاضرة في الميد. فيها Timer1 (16-bit) بالـ TCCR1A / TCCR1B، والـ Input Capture (جه سؤالين في الميد اللي فات)، وTIMSK / TIFR اللي بيخدموا التلات تايمرز — دول لازم تحفظ بتاتهم لأنك هتكتبها في الكود. وفي مثالين كود: delay بـ Timer1، وعداد اهتزازات بـ Timer0 + Timer1 مع بعض.
خريطة المحاضرة — العناوين وتابعة لإيه (دوس على أي عنوان يوديك له)
S1–S41 ← Registers drill (all) · Sheet 2 Ex 5, 6 · Final 2026 Q5
8-bit counts to 255 (2⁸−1); 16-bit to 65,535 (2¹⁶−1).حدود العد.
Best practice: use the smallest timer that meets your needs — low-bit timers save power and simplify the program; a 16-bit timer when unnecessary wastes power and adds complexity.استخدم أصغر تايمر يكفّي.
Analogy: small stopwatch (8-bit) for short timings, big stopwatch (16-bit) for long/precise timings.ساعة صغيرة وساعة كبيرة.
Measures the exact time of an external edge; source: ICP1 pin (PD6) or Analog Comparator output (ACO).بيسجل وقت حدوث الـ edge بالظبط.
Step 1 — Edge select with ICES1 in TCCR1B: 1 = rising, 0 = falling.اختار الحافة.
Step 2 — On the edge, TCNT1 is copied into ICR1 (16-bit) at the exact clock cycle.قيمة العداد بتتنسخ في ICR1.
Step 3 — ICF1 in TIFR is set; if TICIE1 in TIMSK = 1 → input capture interrupt (TIMER1_CAPT_vect).الـ flag والـ interrupt.
Step 4 — ICF1 cleared automatically when the ISR executes, or by writing 1 to it.مسحه.
Step 5 — Read ICR1L first (latches high byte in TEMP), then ICR1H.اقرا الـ low الأول.
ICNC1 (Noise Canceler, TCCR1B bit 7): small digital filter → increases response time (delay) but improves edge-detection stability.فلتر بيأخر شوية بس بيثبّت الكشف.
Use: pulse width and frequency measurement without software delay (ultrasonic, tachometer).قياس عرض النبضة والتردد.
السلايد كاتب PORTC = TCNT1 * (1024/4000); — في C ده integer division = 0 فالنتيجة دايمًا صفر. اكتب TCNT1 * 1024UL / 4000 أو TCNT1 * 256UL / 1000. ذكر الملاحظة دي في الامتحان بيبيّن إنك فاهم.
الخلاصة — Lecture 5أهم نقط المحاضرة في سطور؛ راجعها قبل ما تقفلها
الدكتور قال: الأرقام (قيم الـ prescaler والـ WGM) هيديهالك، لكن الريجسترز لازم تكون عارفها: اسم كل ريجستر، بيعمل إيه، وكل بت اسمه إيه وفين. وتحت فيه تدريب تكتب البتات من دماغك.
Given in the exam vs. memorize
Item
Status
ملاحظة
Prescaler / clock-select values (CS bit tables)
Given
زي الجدول اللي في آخر صفحة الموديل آنسر
WGM / COM numeric tables (which combination = which mode)
Given
الدكتور قال: "الأرقام أنا بديها ليكم"
Supply currents for power questions
Given
زي I_ACTIVE,min/max في الميد
Register names and what each controls (TCCR0, TCNT0, OCR0, TIMSK, TIFR, TCCR1A/B …)
Memorize
مش هيتدّى — لازم تعرف فين كل بت
Bit names inside each register (WGM01, CS02, TOIE0, OCIE2, ICES1 …)
Memorize
عشان تكتب (1<<BIT) صح
ISR vector names, sei()/cli(), flag cleared by writing 1
while (GET(TIFR, TOV0) == 0); // wait
SET(TIFR, TOV0); // write 1 to clear
(12)Stop a timer
الإجابة
TCCR0 = 0; // CS = 000 → no clock
TCCR1B = 0;
SHEET 1 · LECTURES 1–2MCU Selection, Power & Power Modes
0/0
الشيت ده 3 أنواع أسئلة وبيتكرروا بالحرف: اختيار MCU (جه في الفاينال 15 درجة)، حساب الباور (جه في الميد 4 درجات)، واختيار power mode (جه في الميد 2 درجة). ذاكر Lecture 1 سلايد 27–36 و Lecture 2 كلها الأول.
Question 1 — Smart Agriculture Monitoring
SH1 Q1
السؤال الأصلي
Budget 400 mW · sensors 80, communication 150, display 120 mW · 80 MHz · active 40% / idle 60%.
أول حاجة دايمًا: الميزانية المتاحة للـ MCU = الكلي − باقي المكونات. بعدها P لكل MCU = Pa×active + Ps×idle.
(a)Find the power available for the microcontroller.
Question 5 — Environmental monitoring (reading every hour)
SH1 Q5L2 Ex3
السؤال الأصلي
الكلمة المفتاحية: periodic / every hour ← محتاج منبّه داخلي ← Timer2 async.
(a)Which power management mode maximizes battery life with periodic readings?
الإجابة
Power-save mode: stops the CPU and most peripherals; the asynchronous Timer2 keeps running and wakes the MCU periodically (timer interrupt) to take the readings → very low power + periodic wake-up.
يعني: Power-save = Power-down + منبّه.
Question 6 — Wireless security camera (fast response)
SH1 Q6= Mid 2026 Q2b
السؤال الأصلي
الكلمة المفتاحية: immediately / fast response ← الأوسيلاتور لازم يفضل شغال.
(a)Which mode minimizes power while responding quickly?
الإجابة
Extended Standby mode: low-power operation with the external oscillator running → fast wake-up (no oscillator start-up delay); async Timer2 can keep running for timing/housekeeping → immediate reaction to motion.
يعني: Standby عشان السرعة، و Extended عشان Timer2 كمان شغال.
SHEET 2 · LECTURES 4–5Timer Code Examples
0/0
شيت 2 هو أكواد أمثلة المحاضرات بطريقة المعيدة: تعرّف SET / CLEAR / TOGGLE / GET فوق وتستخدمهم. كل كود ماشي على نفس الـ 8 خطوات. في الامتحان اكتب الحساب الأول (a)، وبعدين الكود (b). السؤال الأصلي هنا هو سلايد المثال. (مثال 7 — INT0 — من محاضرة 6 فـ برّه الميد.)
Ex 0 — Code skeleton (the includes carry marks)
ALL CODE
أول 10 سطور في أي كود ثابتين: F_CPU ← includes ← الماكروز الأربعة ← أسماء الـ pins.
(a)Which header lines do you write, and when?
الإجابة
Line
Why
When
#define F_CPU 8000000UL
CPU frequency for delay calculations
always, first line (before util/delay.h)
#include <avr/io.h>
register & bit names (DDRB, TCCR0, CS02 …)
always
#include <avr/interrupt.h>
ISR(), sei(), cli()
any interrupt / ISR
#include <util/delay.h>
_delay_ms(), _delay_us()
when you use delays
#include <stdint.h>
uint8_t, uint16_t, uint32_t
when you use these types
يعني: الـ includes عليها درجة، اكتبها دايمًا.
(b)Write the program template with the bit macros.
الإجابة
#define F_CPU 8000000UL#include <avr/io.h>#include <avr/interrupt.h>#include <util/delay.h>#include <stdint.h>
/* bit-operation macros (not part of the AVR headers) */
#define SET(reg, bit) ((reg) |= (1U << (bit)))#define CLEAR(reg, bit) ((reg) &= ~(1U << (bit)))#define TOGGLE(reg, bit) ((reg) ^= (1U << (bit)))#define GET(reg, bit) (((reg) >> (bit)) & 1U)
#define LED_PIN 0 /* name every pin you use */
void TimerX_Init(void) /* Steps 1–7 of the flowchart */
{
}
ISR(TIMER0_OVF_vect) /* only if you use interrupts */
{
}
int main(void)
{
/* I/O directions + initial values */
/* TimerX_Init(); */
/* sei(); (Step 8) */
while (1)
{
}
return 0;
}
Use: SET(TCCR0, CS02); instead of TCCR0 |= (1<<CS02); · CLEAR(DDRB, SW_PIN); · TOGGLE(PORTB, LED_PIN); · if (GET(PINA, SW_PIN)).
The slide writes TCNT1 * (1024/4000): in C this integer division is 0. Multiply first: TCNT1 * 256UL / 1000.
يعني: الفكرة كلها في الـ preload 246 اللي بيخلي الـ overflow = النبضة العاشرة.
MIDTERM · NOV 8, 2025 · TERM 2025/2026 · 25 MARKS · 1 HOURMid 2026 — solved
0/0
ده امتحان السنة اللي فاتت وأهم حاجة تحلها. كان فيه 10 أسئلة MCQ، و6 درجات للباور والـ mode، و9 درجات لكود تايمر. السنة دي كله مقالي، فالـ MCQ هنا متحوّلة لـ "جاوب واشرح ليه". Q2 هو شيت 1 بالحرف، و Q3 بنفس أسلوب شيت 2.
Question 1 — 10 points (was MCQ: answer + reason)
Mid 2026 Q1
السؤال الأصلي
في المقالي ممكن ييجي نفس السؤال "Explain…" أو "What happens if…". احفظ السبب مش الحرف.
(1)What is the main purpose of the Watchdog Timer in ATmega32? = L1 S61✓ key
الإجابة
It resets the MCU automatically if the program hangs (safety guard).
يعني: لو البرنامج علّق، الـ watchdog بيعمل reset.
(2)ATmega32 achieves 16 MIPS @ 16 MHz. What happens with an 8 MHz crystal? = L1 S48✓ key
الإجابة
Throughput halves to ≈ 8 MIPS, because instruction execution depends on the clock frequency (≈ 1 MIPS per MHz).
(3)Configure PORTB.2 as input with the internal pull-up. = L2 S68✓ key
(b)Battery wireless security camera: detect motion continuously, record immediately. Which mode? [2 marks] ✓ key
الإجابة
Extended Standby: the external oscillator keeps running → fast wake-up; Timer2 can keep running for timing → immediate reaction while staying low-power.
يعني: 6 درجات سهلة لو حافظ الطريقة.
Question 3 — 9 points: LED on PD7, 1 s ON / 3 s OFF
Mid 2026 Q3like SH2 Ex1
السؤال الأصلي
Active-low LED on PD7. CPU 32 kHz. Timer0 Normal mode. Calculate the prescaler and TCNT0 for each delay, then write the program.
لكل delay: ticks = Delay × F / N. جرّب N من الأصغر لحد ما ticks ≤ 256، و TCNT0 = 256 − ticks.
(a)Calculate the prescaler and TCNT0 for 1 s and for 3 s. ≠ key: 255 − ticks
الإجابة
Delay
Try
ticks
TCNT0 (256 − ticks)
Model answer (255 − ticks)
1 s
N = 256
32000 / 256 = 125
131
130
3 s
N = 256 → 375 ✗, N = 1024
96000 / 1024 = 93.75 ≈ 94
162
161
The model answer uses 255 − ticks (one tick off). Write the formula you use explicitly.
(b)Write the program. ≠ key: timer restarted
الإجابة
استخدم overflow interrupt. في الـ ISR اقلب الـ LED وغيّر الـ prescaler والـ preload للمرحلة الجاية وأعد تشغيل التايمر. active-low: ON = LOW.
#define F_CPU 32000UL#include <avr/io.h>#include <avr/interrupt.h>#include <stdint.h>
/* bit-operation macros (not part of the AVR headers) */
#define SET(reg, bit) ((reg) |= (1U << (bit)))#define CLEAR(reg, bit) ((reg) &= ~(1U << (bit)))#define TOGGLE(reg, bit) ((reg) ^= (1U << (bit)))#define GET(reg, bit) (((reg) >> (bit)) & 1U)
#define LED_PIN 7 /* PD7, active-low */
volatile uint8_t phase = 0; /* 0: ON for 1 s · 1: OFF for 3 s */
void Timer0_Start(uint8_t start, uint16_t scale)
{
TCCR0 = 0; /* Step 1: stop */
CLEAR(TCCR0, WGM00); /* Step 2: Normal */
CLEAR(TCCR0, WGM01);
TCNT0 = start; /* Step 3: preload */
if (scale == 1024) /* Step 7: prescaler → starts */
{
SET(TCCR0, CS02); CLEAR(TCCR0, CS01); SET(TCCR0, CS00);
}
else /* 256 */
{
SET(TCCR0, CS02); CLEAR(TCCR0, CS01); CLEAR(TCCR0, CS00);
}
}
ISR(TIMER0_OVF_vect)
{
TOGGLE(PORTD, LED_PIN);
if (phase == 0) { phase = 1; Timer0_Start(162, 1024); } /* next: 3 s OFF */
else { phase = 0; Timer0_Start(131, 256); } /* next: 1 s ON */
}
int main(void)
{
SET(DDRD, LED_PIN);
CLEAR(PORTD, LED_PIN); /* active-low → LED ON first */
SET(TIMSK, TOIE0); /* Step 6 */
sei(); /* Step 8 */
Timer0_Start(131, 256); /* 1 s first */
while (1)
{
}
return 0;
}
The official model code stops the timer in the ISR (TCCR0 = 0) and never restarts it, so it runs only once. Restarting it inside the ISR (as above) fixes that.
يعني: ده أقرب شكل لسؤال الكود الجاي: حساب + كود بالـ ISR.
FINAL · JAN 4, 2026 · TERM 2025/2026 · 80 MARKS · 3 HOURSFinal 2026 — midterm parts only
0/0
من الفاينال خدت بس اللي من أول 5 محاضرات. Q1 هو شيت 1 Q2 بالحرف (15 درجة). Q2 (USART) و Q4-A (ADC Vref) و الـ MCQ بتاعة الـ ADC والـ USART برّه الميد. Q5 فيه external interrupt pin (محاضرة 6)، فحليته بالـ polling عشان يفضل جوه المنهج. ونسخة الامتحان دي من أحدث للأقدم: Mid 2026 ثم Final 2026.
Question 1 — 15 points: choose the MCU
Final 2026 Q1= SH1 Q2
السؤال الأصلي
(a)Smart Home Energy Monitoring: 60 MHz, 300 mW, components 70/100/90 mW, D/E/F, 50% active.
الإجابة
Available = 40 mW · D = 4.85 · E = 3.70 · F = 4.50 mW
F (90 MHz, 64 KB SRAM, acceptable 4.5 mW); E as the low-power alternative; D rejected (highest power, least SRAM).
يعني: لو ذاكرت الشيت، الـ 15 درجة مضمونين. الحل المفصل في Sheet 1 Question 2.
Question 3 — MCQ items inside the midterm scope
Final 2026 Q3
السؤال الأصلي
الأسئلة 5–11 و 16–17 (ADC و USART و I2C) برّه الميد.
(1)Which instruction restores the I-bit in SREG after an interrupt?
الإجابة
reti (return from interrupt) — the ISR() macro ends with it.
(2)sei() is called inside an ISR: consequence?
الإجابة
Nested interrupts are allowed (the I-bit is set again inside the ISR).
(4)Global I-bit disabled and an external interrupt occurs?
الإجابة
The interrupt flag is set and waits until I is enabled (edge-triggered).
The photo shows "the interrupt is lost" circled; by AVR behaviour the flag stays pending, so "flag is set and waits" is the safer answer.
(12)Why is Flatten architecture usually the fastest?
(B)In most microcontroller timers, how can the interrupt be accessed?
الإجابة
طريقتين: polling للـ flag أو interrupt-driven.
Polling: the program keeps checking the flag in TIFR (e.g. TOV0), then clears it by writing 1 → simple but wastes CPU time.
Interrupt-driven: enable the source in TIMSK (TOIE0, OCIE0 …) + global sei(); on the event the CPU jumps to ISR(vector) automatically → the CPU is free for other tasks; keep ISRs short and infrequent.
يعني: يا تراقب الـ flag بنفسك، يا الهاردوير يناديلك الـ ISR.
(C)What is the importance of the prescaler in microcontroller timers?
الإجابة
The prescaler divides F_CPU before it reaches the timer → slower timer clock → longer intervals without changing the CPU speed and without large counters (f_timer = F_CPU / N).
Example: 1 MHz, 8-bit → overflow every 256 µs; with N = 64 → 16.384 ms, so seconds are reached by counting overflows.
يعني: مقسّم تردد بيطوّل الوقت اللي التايمر يقدر يقيسه.
Question 5 — 20 points: coffee machine, 8 s with Timer1
Final 2026 Q5
السؤال الأصلي
Active-high push button → dispense coffee for 8 s, then stop. The dispensing mechanism is driven by a low-level signal on PD7. CPU 4 MHz, use Timer1 for the 8 s.
ticks = 8 × 4×10⁶ / N؛ أول N يدي ≤ 65536 هو 1024. السؤال بيقول الزرار على external interrupt pin (محاضرة 6) من غير ما يحدد أنهي واحد؛ هنا بالـ polling على PD2.
يعني: دايمًا ابدأ بأصغر N واطلع لحد ما الرقم يدخل في التايمر.
Problem 6 — Theory set (2 marks each)
L1–L3
(a)Watchdog timer.
الإجابة
A safety timer that resets the MCU if the program hangs.
(b)Firmware and its layers.
الإجابة
Software in non-volatile memory that directly controls hardware; layers Driver → HAL → Middleware → Application.
(c)Microprocessor vs microcontroller.
الإجابة
uP: no on-chip RAM/ROM/I/O (added externally → large, expensive). MCU: CPU, RAM, ROM, I/O, timers, ADC on one chip (cheap, small, low power).
(d)Modified Harvard architecture.
الإجابة
Separate instruction and data paths → fetched simultaneously → faster.
(e)Idle vs Power-down.
الإجابة
Idle stops only the CPU (peripherals run, any interrupt wakes it). Power-down stops the oscillator and almost everything (wake only on INT0/1/2, WDT, reset) → lowest power.
(f)Software vs hardware PWM.
الإجابة
Software: delays, inaccurate, high CPU usage. Hardware: timer-generated, precise, CPU free.
(g)Flatten vs function-based coding.
الإجابة
Flatten: fastest, smallest (463 B) but poorly maintainable. Functions: readable and reusable but larger (635 B) due to stack and call overhead.
يعني: جملة تعريف + فرق واحد واضح = درجة كاملة.
PRACTICESolvers
استخدمهم تراجع حلك بعد ما تحل على الورقة. كل أداة فيها أمثلة الامتحانات جاهزة.
Timer delay — preload / overflows / CTC OCR
Fast PWM — prescaler and OCR
CTC square wave on OCn (toggle)
Event counter limit
Power estimate (best / worst)
MCU selection
MCU
Active mW
Sleep mW
MHz
SRAM KB
PRACTICEFlashcards
اقرا السؤال، جاوب في دماغك، اقلب الكارت. "Not yet" بيرجعلك في الآخر.