0% found this document useful (0 votes)
9 views4 pages

Embedded Systems_cat_01

The document contains a series of practical C programming questions focused on embedded systems, specifically for RIMS. Each question includes a brief description of the task and a corresponding C code implementation. The tasks range from creating a binary counter, smart light control, door lock system, to more complex applications like a traffic light controller and car parking assistant.

Uploaded by

Gabriel Gitonga
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views4 pages

Embedded Systems_cat_01

The document contains a series of practical C programming questions focused on embedded systems, specifically for RIMS. Each question includes a brief description of the task and a corresponding C code implementation. The tasks range from creating a binary counter, smart light control, door lock system, to more complex applications like a traffic light controller and car parking assistant.

Uploaded by

Gabriel Gitonga
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

GABRIEL

GITONGA 21/08375

Embedded Systems

Cat 01

Practical C Programming Questions (RIMS-Based)

1. LED Binary Counter

Write a C program for RIMS that counts from 0 to 15 in binary using LEDs connected to
B3..B0 as outputs. The counter should increment every 500ms and restart after reaching 15.

#include <avr/io.h>
#include <util/delay.h>

int main() {
DDRB = 0x0F;
uint8_t count = 0;

while (1) {
PORTB = (PORTB & 0xF0) | (count & 0x0F);
_delay_ms(500);
count = (count + 1) % 16;
}
}

2. Smart Light Control System

Develop an embedded C program that simulates a smart light system. The system should:

 Turn on a light (B0 = 1) only if it is nighttime (A0=1) and motion is detected


(A1=1).
 Otherwise, the light should be off.

#include <avr/io.h>
int main() {
DDRB |= (1 << PB0);
DDRA &= ~((1 << PA0) | (1 << PA1));
while (1) {
if ((PINA & (1 << PA0)) && (PINA & (1 << PA1))) \
PORTB |= (1 << PB0); // Turn on light
else
PORTB &= ~(1 << PB0); // Turn off light
}
}
3. Door Lock System with Passcode
Implement an electronic door lock where:

 A four-button keypad (A3..A0) allows the user to enter a 4-bit passcode.


 If the correct passcode 1101 is entered, the door unlocks (B0=1).
 Otherwise, it remains locked (B0=0).

#include <avr/io.h>
int main() {
DDRB |= (1 << PB0);
DDRA &= 0xF0; // Set A3..A0 as inputs

while (1) {
if ((PINA & 0x0F) == 0x0D)
PORTB |= (1 << PB0);
else
PORTB &= ~(1 << PB0);
}
}

4. Overflow Handling in an 8-bit Counter

Write a program that implements an 8-bit counter using RIMS. The counter should:

 Increment B by 1 every 200ms.


 If B exceeds 255, it should reset to 0.
 Display the current value of B in hexadecimal.

#include <avr/io.h>
#include <util/delay.h>
int main() {
DDRB = 0xFF;
uint8_t counter = 0;
while (1) {
PORTB = counter;
_delay_ms(200);
counter = (counter + 1) % 256;
}
}

5. Traffic Light Controller

Simulate a traffic light system using B2..B0:

 B2 = Green, B1 = Yellow, B0 = Red.


 The sequence should be: Green (5s) → Yellow (2s) → Red (5s), then repeat.

#include <avr/io.h>
#include <util/delay.h>
int main() {
DDRB = 0x07; // B2..B0 as output
while (1) {
PORTB = 0x04; _delay_ms(5000); // Green
PORTB = 0x02; _delay_ms(2000); // Yellow
PORTB = 0x01; _delay_ms(5000);
}
6. 7-Segment Display for a Temperature Sensor

A temperature sensor is connected to A3..A0 and outputs values 0-9.

 Display the corresponding 7-segment pattern on B6..B0.


 Example: If A=3, set B to 01111001 (displaying the number 3).

#include <avr/io.h>
uint8_t seg_map[10] = {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F};
int main() {
DDRB = 0x7F;
DDRA &= 0xF0;
while (1) {
PORTB = seg_map[PINA & 0x0F];
}
}

7. Motion Sensor Alarm

Create an alarm system that:

 Triggers a buzzer (B0 = 1) if motion is detected (A0=1) for more than 3 seconds
continuously.
 Resets if no motion is detected for 2 seconds.

#include <avr/io.h>
#include <util/delay.h>
int main() {
DDRB |= (1 << PB0);
DDRA &= ~(1 << PA0);
uint8_t motionTime = 0;
while (1) {
if (PINA & (1 << PA0)) {
motionTime++;
if (motionTime >= 15) PORTB |= (1 << PB0);
} else {
motionTime = 0;
PORTB &= ~(1 << PB0);
}
_delay_ms(200);
}
}

8. Bitwise Masking - Extracting Specific Bits

Write a function that:

 Reads an 8-bit value from A.


 Extracts bits 3 to 0 and outputs them to B.
 All other bits in B should be set to 0.
#include <avr/io.h>
int main() {
DDRB = 0x0F;
DDRA = 0x00;
while (1) {
PORTB = PINA & 0x0F;
}
}

9. Car Parking Assistant

A parking lot has 8 parking spaces, each with a sensor connected to A7..A0:

 A 1 means a car is present; 0 means the space is empty.


 If all spaces are full, set B0 = 1 (Parking Full LED On).
 Display the number of available spaces in B7..B5.

#include <avr/io.h>
int main() {
DDRB = 0xE1;
DDRA = 0x00;
while (1) {
uint8_t occupied = PINA;
uint8_t free_spaces = 8 - __builtin_popcount(occupied);
PORTB = ((free_spaces & 0x07) << 5) | ((free_spaces == 0) ? 1 : 0);
}
}

10. Bit Manipulation – Swapping Nibbles

Write a function that:

 Reads an 8-bit value from A.


 Swaps the upper nibble (A7..A4) with the lower nibble (A3..A0).
 Outputs the swapped value to B.

#include <avr/io.h>
int main() {
DDRB = 0xFF;
DDRA = 0x00;
while (1) {
uint8_t value = PINA;
PORTB = (value << 4) | (value >> 4);
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy