Embedded System & Microcontroller Application (4351102) - Summer 2024 Solution

Solution guide for Embedded System & Microcontroller Application (4351102) Summer 2024 exam

Question 1(a) [3 marks]

What is the definition of an embedded system? Provide an example of an embedded system.

Answer: An embedded system is a specialized computer system designed to perform specific tasks with dedicated functions. It combines hardware and software components that are integrated into a larger system.

Key Features:

  • Real-time operation: Responds to inputs within specified time limits
  • Dedicated function: Designed for specific applications
  • Resource constraints: Limited memory, power, and processing capabilities

Example: Washing machine controller that manages wash cycles, water temperature, and timing automatically.

Mnemonic: "SMART Embedded" - Specialized, Microprocessor-based, Application-specific, Real-time, Task-oriented


Question 1(b) [4 marks]

Define a Real-Time Operating System (RTOS) and list three characteristics of RTOS.

Answer: RTOS is an operating system designed to handle real-time applications where timing constraints are critical for system operation.

CharacteristicDescription
Deterministic ResponseGuaranteed response time for critical tasks
Priority-based SchedulingHigh-priority tasks execute before low-priority tasks
Multitasking SupportMultiple tasks can run concurrently

Additional Features:

  • Task management: Efficiently handles multiple concurrent processes
  • Interrupt handling: Quick response to external events
  • Memory management: Optimized for embedded applications

Mnemonic: "DPM RTOS" - Deterministic, Priority-based, Multitasking


Question 1(c) [7 marks]

a) Draw the general block diagram of Embedded System b) Explain the criteria for choosing a microcontroller for an embedded system.

Answer:

a) General Block Diagram:

b) Microcontroller Selection Criteria:

CriteriaConsiderations
Processing SpeedClock frequency, instruction execution time
Memory RequirementsFlash, RAM, EEPROM capacity
I/O CapabilitiesNumber of pins, special functions
Power ConsumptionBattery life, sleep modes
CostBudget constraints, volume pricing
Development ToolsCompiler, debugger availability

Key Factors:

  • Performance requirements: Processing speed and real-time constraints
  • Interface needs: ADC, PWM, communication protocols
  • Environmental conditions: Operating temperature, humidity

Mnemonic: "PMPICD Selection" - Performance, Memory, Power, Interface, Cost, Development tools


Question 1(c) OR [7 marks]

Explain the pin configuration of the ATmega32.

Answer: ATmega32 is a 40-pin microcontroller with four 8-bit I/O ports and various special function pins.

Port Configuration:

PortPinsFunctions
Port APA0-PA7ADC channels, general I/O
Port BPB0-PB7SPI, PWM, external interrupts
Port CPC0-PC7TWI, general I/O
Port DPD0-PD7USART, external interrupts, PWM

Special Pins:

  • VCC/GND: Power supply pins
  • AVCC/AGND: Analog power supply for ADC
  • XTAL1/XTAL2: Crystal oscillator connections
  • RESET: Active low reset input
  • AREF: ADC reference voltage

Pin Functions:

  • Dual-purpose pins: Most pins have alternate functions
  • Input/Output capability: All port pins are bidirectional
  • Internal pull-up: Software configurable for input pins

Mnemonic: "ABCD Ports" - ADC, Bus interfaces, Communication, Data transfer


Question 2(a) [3 marks]

Explain the data memory architecture of ATMEGA32.

Answer: ATmega32 data memory consists of three sections organized in a unified address space.

Memory Organization:

SectionAddress RangeSizePurpose
General Registers0x00-0x1F32 bytesWorking registers R0-R31
I/O Registers0x20-0x5F64 bytesControl and status registers
Internal SRAM0x60-0x45F2048 bytesData storage and stack

Key Features:

  • Unified addressing: All memory accessible through single address space
  • Register file: R0-R31 for arithmetic and logic operations
  • Stack pointer: Points to top of stack in SRAM

Mnemonic: "GIS Memory" - General registers, IO registers, SRAM


Question 2(b) [4 marks]

Explain the Program Status Word.

Answer: SREG (Status Register) contains flags that reflect the result of arithmetic and logic operations.

SREG Bit Configuration:

BitFlagDescription
Bit 7IGlobal Interrupt Enable
Bit 6TBit Copy Storage
Bit 5HHalf Carry Flag
Bit 4SSign Flag
Bit 3VOverflow Flag
Bit 2NNegative Flag
Bit 1ZZero Flag
Bit 0CCarry Flag

Flag Functions:

  • Arithmetic operations: C, Z, N, V, H flags updated automatically
  • Conditional branching: Flags used for decision making
  • Interrupt control: I flag enables/disables global interrupts

Mnemonic: "I THSVNZC" - Interrupt, Transfer, Half-carry, Sign, oVerflow, Negative, Zero, Carry


Question 2(c) [7 marks]

Draw and explain the architecture of ATMEGA32.

Answer:

ATmega32 Architecture:

Architecture Components:

ComponentDescription
Harvard ArchitectureSeparate program and data memory buses
RISC Core131 instructions, mostly single-cycle execution
ALU8-bit arithmetic and logic operations
Register File32 × 8-bit working registers

Memory System:

  • Program memory: 32KB Flash for storing instructions
  • Data memory: 2KB SRAM for variables and stack
  • EEPROM: 1KB non-volatile data storage

Peripheral Features:

  • Three timer/counters: 8-bit and 16-bit timers
  • 8-channel ADC: 10-bit resolution
  • Communication interfaces: USART, SPI, TWI

Mnemonic: "HRAM Micro" - Harvard architecture, RISC core, ALU, Memory system


Question 2 OR(a) [3 marks]

Explain Program Counter of ATMEGA32.

Answer: Program Counter (PC) is a 16-bit register that holds the address of the next instruction to be executed.

PC Characteristics:

FeatureDescription
Size16-bit (can address 64KB program memory)
Reset Value0x0000 (starts execution from beginning)
IncrementAutomatically incremented after instruction fetch
Jump/BranchModified by jump, branch, and call instructions

PC Operations:

  • Sequential execution: PC increments by 1 for most instructions
  • Branch instructions: PC loaded with target address
  • Interrupt handling: PC saved on stack, loaded with interrupt vector

Mnemonic: "SRIB PC" - Sequential, Reset, Increment, Branch


Question 2 OR(b) [4 marks]

Explain the role of clock and reset circuits in an AVR microcontroller.

Answer:

Clock System:

Clock SourceDescription
External CrystalHigh accuracy, 1-16 MHz typical
Internal RCBuilt-in 8 MHz oscillator
External ClockExternal clock signal input
Low-frequency Crystal32.768 kHz for RTC applications

Reset Circuit Functions:

  • Power-on Reset: Automatic reset when power is applied
  • Brown-out Reset: Reset when supply voltage drops
  • External Reset: Manual reset through RESET pin
  • Watchdog Reset: Reset from watchdog timer timeout

Key Features:

  • Clock distribution: System clock drives CPU and peripherals
  • Reset sequence: Initializes all registers to default values
  • Fuse bits: Configure clock source and reset options

Mnemonic: "CEIL Clock" - Crystal, External, Internal, Low-frequency


Question 2 OR(c) [7 marks]

Explain TCCRn and TIFR Timer Register

Answer:

TCCRn (Timer/Counter Control Register):

RegisterFunction
TCCR0Controls Timer0 operation mode
TCCR1A/BControls Timer1 (16-bit) operation
TCCR2Controls Timer2 operation mode

TCCR Bit Functions:

  • Clock Select (CS): Selects clock source and prescaler
  • Waveform Generation (WGM): Sets timer mode (Normal, CTC, PWM)
  • Compare Output Mode (COM): Controls output pin behavior

TIFR (Timer Interrupt Flag Register):

BitFlagDescription
TOVTimer OverflowSet when timer overflows
OCFOutput CompareSet when compare match occurs
ICFInput CaptureSet when input capture event occurs

Timer Operations:

  • Mode selection: Normal, CTC, Fast PWM, Phase Correct PWM
  • Interrupt generation: Flags trigger interrupts when enabled
  • Output generation: PWM signals for motor control, LED dimming

Mnemonic: "TCCR WGM" - Timer Control, Clock, Register, Waveform Generation Mode


Question 3(a) [3 marks]

Distinguish different data types for programming AVR in C.

Answer:

AVR C Data Types:

Data TypeSizeRangeUsage
char8-bit-128 to 127Characters, small integers
unsigned char8-bit0 to 255Port values, flags
int16-bit-32768 to 32767General integers
unsigned int16-bit0 to 65535Counters, addresses
long32-bit-2³¹ to 2³¹-1Large calculations
float32-bit±3.4×10³⁸Decimal calculations

Special Considerations:

  • Memory efficient: Use smallest suitable data type
  • Port operations: unsigned char for 8-bit ports
  • Timing calculations: unsigned int for timer values

Mnemonic: "CUIL Float" - Char, Unsigned, Int, Long, Float


Question 3(b) [4 marks]

Write a C program to toggle all the bits of Port C 200 times.

Answer:

C

Program Explanation:

  • DDRC = 0xFF: Configures all Port C pins as outputs
  • Toggle operation: Alternates between 0xFF and 0x00
  • Counter: Tracks number of toggle cycles
  • Delay: Provides visible timing for toggle operation

Mnemonic: "DTC Loop" - DDR setup, Toggle bits, Count iterations, Loop control


Question 3(c) [7 marks]

a) LED are connected to Pins of PORTB. Write an AVR programs to show the count from 0 to FFh on the LED b) Write an AVR C program to get a byte of data from Port C. If it is less than 100 send it to Port B; otherwise, send it to Port D.

Answer:

a) Binary Counter Display:

C

b) Conditional Data Transfer:

C

Key Programming Concepts:

  • Port direction: DDR registers configure input/output
  • Data reading: PIN registers read input values
  • Conditional logic: if-else statements for decision making

Mnemonic: "RCC Data" - Read input, Compare value, Conditional output


Question 3 OR(a) [3 marks]

Write AVR C program to send values of -3 to +3 Port B

Answer:

C

Program Features:

  • Signed data: Uses signed char for negative values
  • Array storage: Values stored in array for easy access
  • Cyclic operation: Continuously cycles through all values

Mnemonic: "SAC Values" - Signed char, Array storage, Cyclic operation


Question 3 OR(b) [4 marks]

Write AVR C program to send hex values for ASCII characters 0,1,2,3,4,5,A,B,C and D to port B.

Answer:

C

ASCII Values Table:

CharacterHex ValueBinary
'0'0x3000110000
'1'0x3100110001
'A'0x4101000001
'B'0x4201000010

Mnemonic: "HAC ASCII" - Hex values, Array storage, Cyclic transmission


Question 3 OR(c) [7 marks]

A door sensor is connected to bit 1 of Port B, and an LED is connected to bit 7 of Port C. Write an AVR C program to monitor the door sensor and, when it opens (PIN is HIGH), turn on the LED. Also draw Flow chart.

Answer:

C Program:

C

Flow Chart:

Bit Operations:

  • Input reading: PINB & 0x02 checks bit 1
  • LED control: PORTC |= 0x80 sets bit 7
  • LED off: PORTC &= 0x7F clears bit 7

Mnemonic: "BIC Door" - Bit manipulation, Input monitoring, Conditional LED control


Question 4(a) [3 marks]

Explain ADMUX ADC Register

Answer:

ADMUX (ADC Multiplexer Selection Register):

BitNameDescription
Bit 7-6REFS1:0Reference Selection
Bit 5ADLARADC Left Adjust Result
Bit 4-0MUX4:0Analog Channel Selection

Reference Selection (REFS1:0):

  • 00: AREF, Internal Vref turned off
  • 01: AVCC with external capacitor at AREF pin
  • 10: Reserved
  • 11: Internal 2.56V reference

Channel Selection (MUX4:0):

  • 00000-00111: ADC0-ADC7 (single-ended inputs)
  • Other combinations: Differential inputs with gain

Key Functions:

  • Voltage reference: Determines ADC measurement range
  • Channel multiplexing: Selects which analog input to convert
  • Result alignment: Left or right justified ADC result

Mnemonic: "RAM ADMUX" - Reference, Alignment, Multiplexer


Question 4(b) [4 marks]

Explain Different LCD Pins.

Answer:

16x2 LCD Pin Configuration:

PinSymbolFunction
1VSSGround (0V)
2VDDPower supply (+5V)
3V0Contrast adjustment
4RSRegister Select (Data/Command)
5R/WRead/Write select
6EEnable signal
7-14D0-D7Data bus (8-bit)
15ABacklight anode (+)
16KBacklight cathode (-)

Control Pin Functions:

  • RS = 0: Command register selected
  • RS = 1: Data register selected
  • R/W = 0: Write operation
  • R/W = 1: Read operation
  • E: Enable pulse triggers operation

Connection Modes:

  • 8-bit mode: All data pins D0-D7 connected
  • 4-bit mode: Only D4-D7 used (saves microcontroller pins)

Mnemonic: "VCR EDB LCD" - Vpower, Contrast, Register select, Enable, Data Bus


Question 4(c) [7 marks]

Write a Program to toggle all the bits of PORTB continually with 20µs delay. Use Timer0, normal mode and no Prescaler to generate delay

Answer:

C

Timer Calculation:

  • Clock frequency: 8 MHz (assumption)
  • Timer resolution: 1/8MHz = 0.125µs per count
  • Required counts: 20µs / 0.125µs = 160 counts

Timer0 Configuration:

SettingValueDescription
ModeNormalCounts from 0 to 255
Prescaler1No prescaling
Clock sourceSystem clock8 MHz

Program Flow:

  • Initialize: Set Port B as output
  • Toggle high: PORTB = 0xFF, wait 20µs
  • Toggle low: PORTB = 0x00, wait 20µs
  • Repeat: Continuous operation

Mnemonic: "TNP Timer" - Timer0, Normal mode, Prescaler none


Question 4 OR(a) [3 marks]

Short note Two wire Interface (TWI)

Answer:

TWI (Two Wire Interface) - I2C Protocol:

Key Features:

FeatureDescription
Two wiresSDA (data) and SCL (clock)
Multi-masterMultiple masters can control bus
Multi-slaveUp to 127 slave devices
Address-based7-bit or 10-bit device addressing
BidirectionalData flows in both directions

Bus Characteristics:

  • Open-drain: Requires pull-up resistors (4.7kΩ typical)
  • Synchronous: Clock provided by master
  • Start/Stop conditions: Special sequences for communication

Common Applications:

  • EEPROMs: Non-volatile memory storage
  • RTC modules: Real-time clock devices
  • Sensors: Temperature, pressure, accelerometer
  • Display controllers: OLED, LCD controllers

Mnemonic: "SDA SCL TWI" - Serial Data, Serial CLock, Two Wire Interface


Question 4 OR(b) [4 marks]

Explain ADCSRA ADC Register

Answer:

ADCSRA (ADC Control and Status Register A):

BitNameFunction
Bit 7ADENADC Enable
Bit 6ADSCADC Start Conversion
Bit 5ADATEADC Auto Trigger Enable
Bit 4ADIFADC Interrupt Flag
Bit 3ADIEADC Interrupt Enable
Bit 2-0ADPS2:0ADC Prescaler Select

Prescaler Settings (ADPS2:0):

BinaryDivision FactorADC Clock (8MHz)
00024 MHz
00124 MHz
01042 MHz
01181 MHz
10016500 kHz
10132250 kHz
11064125 kHz
11112862.5 kHz

Control Functions:

  • ADEN: Must be set to enable ADC operation
  • ADSC: Set to start conversion, cleared when complete
  • ADIF: Set when conversion completes
  • Prescaler: ADC clock should be 50-200 kHz for optimal accuracy

Mnemonic: "EASCID ADC" - Enable, Auto-trigger, Start, Conversion, Interrupt, Divider


Question 4 OR(c) [7 marks]

Write a Program to generate a square wave of 16 Khz frequency on pin PORTC.3. Assume Crystal Frequency 8 Mhz

Answer:

C

Frequency Calculation:

ParameterValueFormula
Target frequency16 kHzGiven
Period62.5 µs1/16000
Half period31.25 µsPeriod/2
Timer counts2508MHz × 31.25µs
OCR1A value249Counts - 1

Timer Configuration:

  • Mode: CTC (Clear Timer on Compare)
  • Prescaler: 1 (no prescaling)
  • Interrupt: Compare match toggles output pin

Mnemonic: "CTC Square" - CTC mode, Timer interrupt, Compare match


Question 5(a) [3 marks]

Difference between Polling and Interrupt

Answer:

Polling vs Interrupt Comparison:

AspectPollingInterrupt
CPU UsageContinuously checks statusCPU free until event occurs
Response TimeVariable, depends on polling frequencyFast, immediate response
Power ConsumptionHigher due to continuous checkingLower, CPU can sleep
ProgrammingSimple, sequential codeComplex, requires ISR
Real-timeNot suitable for critical timingExcellent for real-time systems

Key Differences:

  • Efficiency: Interrupts are more CPU efficient
  • Timing: Interrupts provide deterministic response
  • Complexity: Polling is easier to implement and debug

Mnemonic: "PIE Method" - Polling inefficient, Interrupt efficient, Event-driven


Question 5(b) [4 marks]

Explain LM35 Interface with AVR ATmega32.

Answer:

LM35 Temperature Sensor Interface:

goat

LM35 Characteristics:

ParameterValueDescription
Output10mV/°CLinear temperature coefficient
Range0°C to 100°COperating temperature range
Supply4V to 30VPower supply range
Accuracy±0.5°CTemperature accuracy

Interface Code:

C

Calculation:

  • ADC Resolution: 10-bit (0-1023)
  • Reference Voltage: 5V
  • LM35 Scale: 10mV/°C
  • Formula: Temperature = (ADC_Value × 5000mV) / (1024 × 10mV/°C)

Mnemonic: "LAC Temperature" - LM35 sensor, ADC conversion, Calculation formula


Question 5(c) [7 marks]

Write a program to interface DC Motor with AVR ATmega32.

Answer:

DC Motor Interface Circuit:

goat

Motor Control Program:

C

L293D Truth Table:

ENIN1IN2Motor Action
0XXStop
100Stop
101Reverse
110Forward
111Stop

Key Components:

  • L293D: Dual H-bridge motor driver IC
  • Enable pin: Controls motor power
  • Direction pins: IN1, IN2 control rotation direction
  • Protection: Built-in diodes for back EMF protection

Mnemonic: "LED Motor" - L293D driver, Enable control, Direction pins


Question 5 OR(a) [3 marks]

Explain basic block diagram of GSM based security system.

Answer:

GSM Security System Block Diagram:

System Components:

ComponentFunction
SensorsPIR, door/window sensors, smoke detector
MicrocontrollerProcess sensor data, control system
GSM ModuleSend SMS alerts, make calls
DisplayShow system status
AlarmLocal audio/visual alert

Working Principle:

  • Sensor monitoring: Continuous surveillance of security zones
  • Event detection: Triggered when unauthorized access detected
  • Alert generation: SMS sent to predefined numbers
  • Local alarm: Immediate audio/visual warning

Key Features:

  • Remote monitoring: Real-time alerts via SMS
  • Multiple sensors: Various intrusion detection methods
  • Backup power: Battery backup for power failures

Mnemonic: "SGMA Security" - Sensors, GSM module, Microcontroller, Alerts


Question 5 OR(b) [4 marks]

Explain Relay Interface with AVR ATmega32.

Answer:

Relay Interface Circuit:

goat

Relay Interface Code:

C

ULN2803 Features:

FeatureDescription
8 ChannelsEight Darlington pair drivers
High CurrentUp to 500mA per channel
ProtectionBuilt-in flyback diodes
Input Voltage5V TTL compatible
Output VoltageUp to 50V

Applications:

  • Home automation: Light, fan control
  • Industrial control: Motor, valve operation
  • Security systems: Door locks, alarms

Mnemonic: "ULN Relay" - ULN2803 driver, Load control, Non-contact switching


Question 5 OR(c) [7 marks]

Draw and Explain Automatic Juice vending machine

Answer:

Automatic Juice Vending Machine Block Diagram:

System Components:

| Component | Function | |---|---|---| | Coin Sensor | Detects and validates inserted coins | | Keypad | User selection interface (4x4 matrix) | | LCD Display | Shows menu, price, status messages | | Pump Motors | Dispense selected juice | | Solenoid Valves | Control juice flow | | Level Sensors | Monitor juice container levels | | Coin Return | Returns excess money |

System Operation:

  1. Initialization: Display welcome message and juice menu
  2. Coin Input: User inserts coins, system validates amount
  3. Selection: User presses keypad to select juice type
  4. Validation: Check if enough money and juice available
  5. Dispensing: Activate pump and valve for selected juice
  6. Completion: Return change if any, display thank you message

Control Logic:

C

Key Features:

  • Multiple juice types: 4-6 different flavors
  • Automatic dispensing: Precise volume control
  • Change return: Calculates and returns exact change
  • Inventory tracking: Monitors juice levels
  • Error handling: Handles various fault conditions

Safety Features:

  • Over-dispensing protection: Timer-based pump control
  • Coin validation: Prevents fake coin acceptance
  • Level monitoring: Prevents dry running of pumps
  • Emergency stop: Manual override capability

Mnemonic: "CLPDV Juice" - Coin sensor, LCD display, Pump motors, Dispensing unit, Valve control