0% found this document useful (0 votes)
4 views12 pages

Solution Eiot

The document contains a series of embedded C and Arduino programs for various operations including arithmetic, logical, and control tasks. It includes examples for addition, multiplication, subtraction, division, and comparisons, as well as interfacing with sensors like LM35 and DHT11. Additionally, it covers LED control, object detection with IR sensors, and implementing counters using Raspberry Pi and Arduino.

Uploaded by

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

Solution Eiot

The document contains a series of embedded C and Arduino programs for various operations including arithmetic, logical, and control tasks. It includes examples for addition, multiplication, subtraction, division, and comparisons, as well as interfacing with sensors like LM35 and DHT11. Additionally, it covers LED control, object detection with IR sensors, and implementing counters using Raspberry Pi and Arduino.

Uploaded by

landocleatusmsec
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1) Write addition and Multiplication operations using Embedded C.

#include<reg51.h>

#include<stdio.h>
void serial_init(){
SCON=0X50;
TMOD=0X20;
TH1=0XFD;
TR1=1;
TI=1;
}
unsigned char a,b,c;
unsigned int d;
void main(){
serial_init();
a=3;
b=4;
c=a+b;
d=(unsigned int)a*(unsigned int)b;
printf("Sum =%X\n",(unsigned int)c);
printf("MUL =%X\n",d);
while(1);
}
2) Write a program to transfer the data between two registers
ORG 0000H
MOV R1,#30H
MOV A,R1
MOV R2,A
HERE : SJMP HERE
END
3) Write a program to transfer data between memory and register
ORG 0000H
MOV 30H,#30H
MOV R1,30H
HERE : SJMP HERE
END
4) Write Subtraction and Division operation using embedded c.
#include<reg51.h>
#include<stdio.h>
void serial_init(){
SCON=0X50;
TMOD=0X20;
TH1=0XFD;
TR1=1;
TI=1;
}
unsigned char a,b,diff,quo,rem;
void main(){
serial_init();
a=3;
b=4;
diff=a-b;
if(PSW&0X80)
printf("result is negative :%X\n",(unsigned int)diff);
else
printf("result sub is positive: %X\n",(unsigned int)diff);
quo=a/b;
rem=a%b;
printf("Quotient : %X\n",(unsigned int)quo);
printf("Reminder : %X\n",(unsigned int)rem);
while(1);
}
5) Write a program to perform ALU operations using embedded C
#include<reg51.h>
#include<stdio.h>
void serial_init(){
SCON=0X50;
TMOD=0X20;
TH1=0XFD;
TR1=1;
TI=1;
}
unsigned char a,b,sum,diff,quo,rem,an,o,xo;
unsigned int mul;
void main(){
serial_init();
a=3;
b=4;
sum=a+b;
printf("sum: %X\n",(unsigned int)sum);
mul=a*b;
printf("mul : %X\n",mul);
diff=a-b;
if(PSW&0X80)
printf("result is negative :%X\n",(unsigned int)diff);
else
printf("result sub is positive: %X\n",(unsigned int)diff);
quo=a/b;
printf("Quotient : %X\n",(unsigned int)quo);
rem=a%b;
printf("Reminder : %X\n",(unsigned int)rem);
an=a&b;
printf("and : %X\n",(unsigned int)an);
o=a|b;
printf("or : %X\n",(unsigned int)o);
xo=a^b;
printf("xor : %X\n",(unsigned int)xo);
while(1);
}
6) Write a program to perform arithmetic operations in 8051 using a simulator.
ORG 0000H
MOV A,#03H
MOV R1,#03H
ADD A,R1
JNC NO_CARRY
MOV 31H,#01H
SJMP STORE_SUM
NO_CARRY :
MOV 31H,#00H
STORE_SUM :
MOV 30H,A
HERE : SJMP HERE
END

ORG 0000H
MOV A,#0AH
MOV R1,#03H
SUBB A,R1
JNC NO_BORROW
MOV 33H,#01H
SJMP STORE_DIFF
NO_BORROW :
MOV 33H,#00H
STORE_DIFF :
MOV 32H,A
HERE : SJMP HERE
END

ORG 0000H
MOV A,#0AH
MOV B,#03H
MUL AB
MOV 34H,A
MOV 35H,B
HERE : SJMP HERE
END

ORG 0000H
MOV A,#0AH
MOV B,#03H
DIV AB
MOV 34H,A
MOV 35H,B
HERE : SJMP HERE
END
7) Write a program to perform logical operations using Embedded C.
#include<reg51.h>
#include<stdio.h>
void serial_init(){
SCON=0X50;
TMOD=0X20;
TH1=0XFD;
TR1=1;
TI=1;
}
unsigned char a,b, an,o,xo;
void main(){
serial_init();
a=3;
b=4;
an=a&b;
printf("and : %X\n",(unsigned int)an);
o=a|b;
printf("or : %X\n",(unsigned int)o);
xo=a^b;
printf("xor : %X\n",(unsigned int)xo);
while(1);
}
9) Write a program to perform comparison operations using Embedded C.
#include<reg51.h>
#include<stdio.h>
void serial_init(){
SCON=0X50;
TMOD=0X20;
TH1=0XFD;
TR1=1;
TI=1;
}
unsigned char a,b;
void main(){
serial_init();
a=3;
b=4;
if(a==b)
printf("Equal");
else if(a>b)
printf("a greater than b");
else if(a<b)
printf("b is greater than c");
while(1);
}
10) Write a program to control LED blinking speed based on temperature using LM35 sensor in
Arduino.
const int lm35Pin = A0; // LM35 output pin
const int ledPin = 9; // LED pin
void setup() {
pinMode(ledPin, OUTPUT);
[Link](9600);
}
void loop() {
int sensorValue = analogRead(lm35Pin);
// Convert to voltage
float voltage = sensorValue * (5.0 / 1023.0);
// Convert voltage to temperature (LM35: 10mV per °C)
float temperature = voltage * 100;
[Link]("Temperature: ");
[Link](temperature);
[Link](" °C");
// Map temperature to delay (higher temp → faster blinking)
int delayTime = map(temperature, 20, 50, 1000, 100);
delayTime = constrain(delayTime, 100, 1000);
digitalWrite(ledPin, HIGH);
delay(delayTime);
digitalWrite(ledPin, LOW);
delay(delayTime);
}
11) Write a program to interface LED and IR sensor for object detection using Arduino.
const int irPin = 2; // IR sensor output
const int ledPin = 9; // LED pin
void setup() {
pinMode(irPin, INPUT);
pinMode(ledPin, OUTPUT);
[Link](9600);
}
void loop() {
int sensorState = digitalRead(irPin);
if (sensorState == LOW) {
// Object detected (most IR sensors give LOW when object is present)
digitalWrite(ledPin, HIGH);
[Link]("Object Detected");
} else {
// No object
digitalWrite(ledPin, LOW);
[Link]("No Object");
}
delay(200);
}
12) Write a program to interfacing any one sensor with Raspberry PI.
import [Link] as GPIO
import time
# GPIO setup
[Link]([Link])
ir_pin = 17
led_pin = 27
[Link](ir_pin, [Link])
[Link](led_pin, [Link])
try:
while True:
sensor_state = [Link](ir_pin)
if sensor_state == 0: # Object detected
print("Object Detected")
[Link](led_pin, [Link])
else: # No object
print("No Object")
[Link](led_pin, [Link])
[Link](0.5)
except KeyboardInterrupt:
print("Program stopped")
finally:
[Link]()
13) Write a program to interface LM35 temperature sensor with Arduino and display the
temperature.
// LM35 Temperature Sensor with Arduino
// Display Temperature in Celsius and Fahrenheit
int sensorPin = A0;
float voltage;
float tempC;
float tempF;
void setup()
{
[Link](9600);
}
void loop()
{
int sensorValue = analogRead(sensorPin);
// Convert analog reading to voltage
voltage = sensorValue * (5.0 / 1023.0);
// Convert voltage to Celsius
tempC = voltage * 100;
// Convert Celsius to Fahrenheit
tempF = (tempC * 9.0 / 5.0) + 32.0;
// Display Celsius
[Link]("Temperature: ");
[Link](tempC);
[Link](" °C");
// Display Fahrenheit
[Link](" | ");
[Link](tempF);
[Link](" °F");
delay(1000);
}
14) Write a program to interface DHT11 sensor with Arduino to measure temperature and
humidity.
#include <DHT.h>
#define DHTPIN 2
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
float temperatureC;
float temperatureF;
float humidity;
void setup()
{
[Link](9600);
[Link]();
}
void loop()
{
humidity = [Link]();
// Read temperature in Celsius
temperatureC = [Link]();
// Read temperature in Fahrenheit
temperatureF = [Link](true);
// Check if reading failed
if (isnan(humidity) || isnan(temperatureC) || isnan(temperatureF))
{
[Link]("Failed to read from DHT11 sensor!");
return;
}
[Link]("Humidity: ");
[Link](humidity);
[Link](" %");
[Link](" | Temperature: ");
[Link](temperatureC);
[Link](" °C");
[Link](" / ");
[Link](temperatureF);
[Link](" °F");
delay(2000);
}
15) Write a program to control LED brightness using PWM in Arduino.
const int ledPin = 9; // PWM pin
void setup() {
pinMode(ledPin, OUTPUT);
}
void loop() {
// Increase brightness
for (int i = 0; i <= 255; i++) {
analogWrite(ledPin, i); // set brightness
delay(10);
}
// Decrease brightness
for (int i = 255; i >= 0; i--) {
analogWrite(ledPin, i);
delay(10);
}
}
16) Write a program to read analog values from LM35 sensor and control LED based on
temperature threshold in arduino.
const int lm35Pin = A0; // LM35 output
const int ledPin = 9; // LED pin
float threshold = 30.0; // Temperature threshold (°C)
void setup() {
pinMode(ledPin, OUTPUT);
[Link](9600);
}
void loop() {
int sensorValue = analogRead(lm35Pin);
// Convert analog value to voltage
float voltage = sensorValue * (5.0 / 1023.0);
// Convert voltage to temperature (LM35: 10mV per °C)
float temperature = voltage * 100;
[Link]("Temperature: ");
[Link](temperature);
[Link](" °C");
// Check threshold
if (temperature >= threshold) {
digitalWrite(ledPin, HIGH); // LED ON
} else {
digitalWrite(ledPin, LOW); // LED OFF
}
delay(500);
}
17) Write a program to implement a 2-bit binary counter using LEDs with Raspberry Pi GPIO.
import [Link] as GPIO
import time
# GPIO setup
[Link]([Link])
led1 = 17 # LSB
led2 = 27 # MSB
[Link](led1, [Link])
[Link](led2, [Link])
try:
while True:
for i in range(4): # 0 to 3 (2-bit counter)
# Extract bits
bit0 = i & 1 # LSB
bit1 = (i >> 1) & 1 # MSB
[Link](led1, bit0)
[Link](led2, bit1)
print(f"Count: {i:02b}") # Binary display
[Link](1)
except KeyboardInterrupt:
print("Program stopped")
finally:
[Link]()
18) Write a program to implement a digital counter using LEDs in Arduino.
int leds[] = {2, 3, 4, 5}; // LED pins
void setup() {
for (int i = 0; i < 4; i++) {
pinMode(leds[i], OUTPUT);
}
}
void loop() {
// Count from 0 to 15
for (int count = 0; count < 16; count++) {
// Display binary value on LEDs
for (int bit = 0; bit < 4; bit++) {
int state = (count >> bit) & 1;
digitalWrite(leds[bit], state);
}
delay(1000);
}
}
19) Write a program to blink multiple LEDs in sequence using Arduino.
int leds[] = {8, 9, 10}; // LED pins
int numLeds = 3;
void setup() {
for (int i = 0; i < numLeds; i++) {
pinMode(leds[i], OUTPUT);
}
}
void loop() {
// Turn ON LEDs one by one
for (int i = 0; i < numLeds; i++) {
digitalWrite(leds[i], HIGH);
delay(500);
digitalWrite(leds[i], LOW);
}
}

You might also like