Embedded Systems_cat_01
Embedded Systems_cat_01
GITONGA 21/08375
Embedded Systems
Cat 01
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;
}
}
Develop an embedded C program that simulates a smart light system. The system should:
#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:
#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);
}
}
Write a program that implements an 8-bit counter using RIMS. The counter should:
#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;
}
}
#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
#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];
}
}
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);
}
}
A parking lot has 8 parking spaces, each with a sensor connected to A7..A0:
#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);
}
}
#include <avr/io.h>
int main() {
DDRB = 0xFF;
DDRA = 0x00;
while (1) {
uint8_t value = PINA;
PORTB = (value << 4) | (value >> 4);
}
}