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

Lab Important Question1

The document contains a series of programming tasks and examples related to embedded systems, specifically using Embedded C, assembly language for the 8051 microcontroller, and Python for Raspberry Pi and Arduino. It covers arithmetic operations, data transfer between registers and memory, logical operations, comparisons, and interfacing with sensors and cloud platforms. Each task includes procedures, code snippets, and expected outputs for clarity.

Uploaded by

Mano
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)
9 views13 pages

Lab Important Question1

The document contains a series of programming tasks and examples related to embedded systems, specifically using Embedded C, assembly language for the 8051 microcontroller, and Python for Raspberry Pi and Arduino. It covers arithmetic operations, data transfer between registers and memory, logical operations, comparisons, and interfacing with sensors and cloud platforms. Each task includes procedures, code snippets, and expected outputs for clarity.

Uploaded by

Mano
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
You are on page 1/ 13

LAB IMPORTANT QUESTIONS-1 (Batch 1)

Detailed Answers with Procedure, Code, and Output

1. Write addition and multiplication operation using Embedded C.

Procedure:

• Include the header file <reg51.h> for 8051.

• Declare two integers and initialize values.

• Perform addition and multiplication.

• Store results and output via ports for verification.

Code:

#include <reg51.h>

void main() {

unsigned int num1 = 15, num2 = 25;

unsigned int sum, product;

sum = num1 + num2;

product = num1 * num2;

P1 = sum; // Output sum on Port 1

P2 = product; // Output product on Port 2

while(1); // Infinite loop

Expected Output:

• P1 = 40

• P2 = 375
2. Write a program to transfer the data between two registers

Procedure:

• Load initial value into Accumulator A.

• Transfer this value to register R0.

• Use a loop to increment R0 until it reaches a limit (35 decimal).

• Output each value on Port 1.

Code (Assembly):

MOV A, #30H ; Load 30H into Accumulator (48 decimal)

MOV R0, A ; Move A to R0

LOOP:

MOV A, R0 ; Move R0 to A

MOV P1, A ; Output value to Port 1

INC R0 ; Increment R0

CJNE R0, #36H, LOOP ; Loop until R0 == 36H (54 decimal)

HERE: SJMP HERE ; Stop execution

Expected Output:
Port 1 will sequentially output values from 0x30 to 0x35.

3. Write a program to transfer data between memory and register

Procedure:

• Load accumulator with initial value.

• Store in internal RAM (address 40H).

• Read from memory into register R1 in a loop.

• Increment memory value after each read.

Code (Assembly):

MOV A, #30H ; Load 30H into A


MOV 40H, A ; Store A to RAM location 40H

LOOP:

MOV A, 40H ; Read value from memory

MOV R1, A ; Move it to R1

MOV P2, A ; Output current value on Port 2

INC A ; Increment value

MOV 40H, A ; Store back to memory

CJNE A, #36H, LOOP ; Repeat until 36H reached

HERE: SJMP HERE ; Stop execution

Expected Output:
Port 2 outputs values from 0x30 to 0x35 sequentially.

4. Write subtraction and division operation using Embedded C.

Procedure:

• Initialize variables for operands.

• Perform subtraction and division manually (since 8051 lacks division instruction, use
loop-based division).

• Output results on ports.

Code:

#include <reg51.h>

void main() {

unsigned int num1 = 50, num2 = 15;

unsigned int difference, quotient = 0;

unsigned int temp = num1;

// Subtraction
difference = num1 - num2;

// Division by repeated subtraction

while(temp >= num2) {

temp = temp - num2;

quotient++;

P1 = difference; // Output difference on Port 1

P2 = quotient; // Output quotient on Port 2

while(1);

Expected Output:

• Port 1 = 35 (50 - 15)

• Port 2 = 3 (50 / 15 approximately)

5. Write a program to perform ALU operations using Embedded C.

Procedure:

• Declare variables for operands and results.

• Perform add, subtract, AND, OR, XOR.

• Output results on different ports.

Code:

#include <reg51.h>

void main() {

unsigned char a = 0x55; // 01010101

unsigned char b = 0x0F; // 00001111


unsigned char add_res = a + b;

unsigned char sub_res = a - b;

unsigned char and_res = a & b;

unsigned char or_res = a | b;

unsigned char xor_res = a ^ b;

P1 = add_res; // Port 1: Addition result

P2 = sub_res; // Port 2: Subtraction result

P3 = and_res; // Port 3: AND result

P0 = or_res; // Port 0: OR result

// XOR could be sent to LED or other output if available

while(1);

Expected Output:

• P1 = 0x64 (0x55 + 0x0F)

• P2 = 0x46 (0x55 - 0x0F)

• P3 = 0x05 (0x55 & 0x0F)

• P0 = 0x5F (0x55 | 0x0F)

6. Write a program to perform arithmetic operations in 8051 using simulator.

Procedure:

• Write assembly to perform add, subtract, multiply.

• Use registers for inputs and outputs.

• Use simulator debugger to verify results.

Code (Assembly):

MOV A, #20H ; Load 20H (32 decimal)


MOV R0, #0AH ; Load 0AH (10 decimal)

ADD A, R0 ; A = A + R0 = 0x2A (42 decimal)

MOV R1, A ; Store result in R1

SUBB A, R0 ; A = A - R0 (with borrow)

MOV R2, A ; Store result in R2

MUL AB ; Multiply A and B (B must be loaded)

MOV A, #05H

MOV B, #03H

MUL AB

MOV R3, A ; Lower byte of product

MOV R4, B ; Higher byte of product

HERE: SJMP HERE

Expected Output:

• R1 = 0x2A (42)

• R2 = 0x20 (32) after subtraction

• R3 and R4 contain product bytes (5 * 3 = 15 decimal)

7. Write a program to perform logical operations using Embedded C.

Procedure:

• Define two variables.

• Perform AND, OR, NOT, XOR operations.

• Output results on ports.

Code:

#include <reg51.h>
void main() {

unsigned char a = 0xA5; // 10100101

unsigned char b = 0x3C; // 00111100

unsigned char and_op = a & b;

unsigned char or_op = a | b;

unsigned char xor_op = a ^ b;

unsigned char not_a = ~a;

P1 = and_op; // AND result

P2 = or_op; // OR result

P3 = xor_op; // XOR result

P0 = not_a; // NOT result

while(1);

Expected Output:

• P1 = 0x24

• P2 = 0xBD

• P3 = 0x99

• P0 = 0x5A

8. Write a program to compare two numbers in 8051 using simulator.

Procedure:

• Load two values into registers.

• Compare using CJNE instruction.

• Set a flag or output based on comparison.


Code (Assembly):

MOV A, #0x2A ; Load 42 decimal

MOV R0, #0x30 ; Load 48 decimal

CJNE A, R0, NOT_EQUAL ; Compare A and R0

EQUAL:

MOV P1, #01H ; Set Port 1 = 1 if equal

SJMP END

NOT_EQUAL:

MOV P1, #00H ; Set Port 1 = 0 if not equal

END:

SJMP END ; Infinite loop

Expected Output:

• P1 = 0 (because 42 != 48)

9. Write a program to perform comparison operations using Embedded C.

Procedure:

• Take two numbers.

• Compare with if-else.

• Output result to port.

Code:

#include <reg51.h>

void main() {

unsigned char num1 = 50;


unsigned char num2 = 40;

if(num1 > num2) {

P1 = 1; // num1 greater

} else if(num1 < num2) {

P1 = 2; // num2 greater

} else {

P1 = 0; // Equal

while(1);

Expected Output:

• P1 = 1 (since 50 > 40)

10. Explain Arduino platform and programming with sample programs.

Explanation:
Arduino is an open-source microcontroller platform easy for beginners. It uses simplified C++
with libraries and IDE for quick prototyping. The code has two main functions: setup() runs
once, and loop() runs repeatedly.

Sample Program: Blink LED

void setup() {

pinMode(13, OUTPUT); // Set pin 13 as output

void loop() {

digitalWrite(13, HIGH); // Turn LED on

delay(1000); // Wait 1 second

digitalWrite(13, LOW); // Turn LED off


delay(1000); // Wait 1 second

11. Write a program to interface any one sensor with Raspberry Pi.

Example: Interfacing DHT11 Temperature Sensor using Python

import Adafruit_DHT

sensor = Adafruit_DHT.DHT11

pin = 4 # GPIO pin number

humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)

if humidity is not None and temperature is not None:

print(f'Temp={temperature:0.1f}*C Humidity={humidity:0.1f}%')

else:

print('Failed to get reading.')

12. Write a program to setup cloud platform to log the data.

Example: Using MQTT to publish data to cloud (e.g., AWS IoT or HiveMQ)

import paho.mqtt.client as mqtt

broker = "broker.hivemq.com"

port = 1883

topic = "sensor/data"

client = mqtt.Client()

client.connect(broker, port)
data = '{"temperature": 25, "humidity": 60}'

client.publish(topic, data)

client.disconnect()

13. Design the smart city application based on IoT.

Procedure:

• Use sensors to monitor air quality, traffic, lighting, and waste management.

• Collect data via microcontrollers/Raspberry Pi.

• Upload data to cloud for real-time monitoring.

• Use mobile/web apps for citizen access.

• Implement automated controls (e.g., smart traffic lights).

14. How to log the data in Raspberry Pi and upload the data in cloud platform.

Procedure:

• Read sensor data using Python.

• Store data in local CSV or database.

• Use MQTT or REST API to send data to cloud.

• Schedule scripts using cron for periodic logging.

Sample code snippet:

import time

import csv

import requests

def read_sensor():

# Dummy sensor read

return 25, 60

with open('data_log.csv', 'a') as file:


writer = csv.writer(file)

while True:

temp, hum = read_sensor()

writer.writerow([time.strftime('%Y-%m-%d %H:%M:%S'), temp, hum])

requests.post('https://api.cloudservice.com/upload', json={'temp': temp, 'hum': hum})

time.sleep(60) # Wait 1 min

15. Write a python programming to perform data transfer operation in Arduino.

Procedure:

• Use PySerial library to send/receive data via serial port.

• Write Arduino sketch to receive data.

• Python script to send data and read response.

Arduino Code:

void setup() {

Serial.begin(9600);

void loop() {

if(Serial.available() > 0) {

int val = Serial.read();

val = val + 1; // Process data

Serial.write(val); // Send back

Python Code:

import serial

import time
arduino = serial.Serial(port='COM3', baudrate=9600, timeout=1)

time.sleep(2) # Wait for connection

data_to_send = 10

arduino.write(bytes([data_to_send]))

response = arduino.read()

print(f'Received from Arduino: {int.from_bytes(response, "big")}')

arduino.close()

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