Embedded System Lab Ii
Embedded System Lab Ii
#include <LPC213x.h>
void delay(int time)
{
int i,j;
for(i=0;i<time;i++)
{
for(j=0;j<i;j++);
}
}
int main()
{
int i, a[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x07,0x7F};
PINSEL0=0x00000000;
IO0DIR=0xFFFFFFFF;
while(1)
{
for(i=0;i<10;i++)
{
IO0SET=a[i];
delay(500);
IO0CLR=a[i];
}
}
return 0;
OUTPUT:
2) LCD INTERFACE USING ARM PROCESSOR(LPC 2138)
PROGRAM:
#include <LPC213x.h>
void initLCD(void);
void LCD_Write(unsigned int c);
void LCD_Cmd(unsigned int LCD_Cmd);
void delay(void);
int main(void)
{
unsigned char ch[]="EST_LAB";
unsigned char ch1[]="2019212003";
unsigned int i,j,k,t;
initLCD();
for(i=0;ch[i]!='\0';i++)
{
LCD_Write(ch[i]);
}
LCD_Cmd(0xc3);
for(j=0;ch1[j]!='\0';j++)
{
LCD_Write(ch1[j]);
}
while(1)
{
for(k=0;k<16;k++)
{
LCD_Cmd(0x1c);
for(t=0;t<30000;t++);
}
}
}
void initLCD(void)
{
IO0DIR = 0x0FFFF00;
delay();
LCD_Cmd(0x38);
LCD_Cmd(0x01);
LCD_Cmd(0x0c);
LCD_Cmd(0x83);
LCD_Cmd(0x06);
}
void LCD_Write(unsigned int c)
{
IO0PIN = (c<<16)|(1<<10);
delay();
}
void LCD_Cmd(unsigned int LCD_Cmd)
{
IO0PIN = (LCD_Cmd<<16)|(0<<10);
delay();
}
void delay(void)
{
int i=0,x=0;
IO0PIN|=(1<<13);
for(i=0; i<15000; i++)
{
x++;
}
IO0PIN&=~(1<<13);
}
OUTPUT:
3) INTERFACE PROGRAM TO CONTROL A DC MOTOR USING ARDUINO
( CONTROLLER USED: L293D)
PROGRAM:
#define button 8
#define pot 0
#define pwm1 9
#define pwm2 10
boolean motor_dir = 0;
int motor_speed;
void setup ()
peripheral_setup();
pinMode(button, INPUT_PULLUP);
pinMode(pwm1, OUTPUT);
pinMode(pwm2, OUTPUT);
}
void loop()
{
peripheral_loop();
motor_speed = analogRead(pot) / 4;
if(motor_dir)
analogWrite(pwm1, motor_speed);
else
analogWrite(pwm2, motor_speed);
if(!digitalRead(button))
{
while(!digitalRead(button));
motor_dir = !motor_dir;
if(motor_dir)
digitalWrite(pwm2, 0);
else
digitalWrite(pwm1, 0);
}
}
OUTPUT:
4) INTERFACE PROGRAM TO COLOR DETECTOR USING ARDUINO
PROGRAM:
const int s0 = 8;
const int s1 = 9;
const int s2 = 12;
const int s3 = 11;
const int out = 10;
int redLed = 2;
int greenLed = 3;
int blueLed = 4;
int red = 0;
int green = 0;
int blue = 0;
void setup()
{
Serial.begin(9600);
pinMode(s0, OUTPUT);
pinMode(s1, OUTPUT);
pinMode(s2, OUTPUT);
pinMode(s3, OUTPUT);
pinMode(out, INPUT);
pinMode(redLed, OUTPUT);
pinMode(greenLed, OUTPUT);
pinMode(blueLed, OUTPUT);
digitalWrite(s0, HIGH);
digitalWrite(s1, HIGH);
}
void loop()
{
color();
Serial.print("R Intensity:");
Serial.print(red, DEC);
Serial.print(" G Intensity: ");
Serial.print(green, DEC);
Serial.print(" B Intensity : ");
Serial.print(blue, DEC);
if (red < blue && red < green && red < 20)
{
Serial.println(" - (Red Color)");
digitalWrite(redLed, HIGH); // Turn RED LED ON
digitalWrite(greenLed, LOW);
digitalWrite(blueLed, LOW);
}
void color()
{
digitalWrite(s2, LOW);
digitalWrite(s3, LOW);
red = pulseIn(out, digitalRead(out) == HIGH ? LOW : HIGH);
digitalWrite(s3, HIGH);
blue = pulseIn(out, digitalRead(out) == HIGH ? LOW : HIGH);
digitalWrite(s2, HIGH);
green = pulseIn(out, digitalRead(out) == HIGH ? LOW : HIGH);
}
OUTPUT:
S2 S3 PHOTODIODE TYPE
LOW LOW RED
LOW HIGH BLUE
HIGH LOW WHITE
HIGH HIGH GREEn
PROGRAM:
import time
import math
import RPi.GPIO as GPIO
import signal
import atexit
import tmp102
timeout = 0.020
gpio_pin = 7
num_pings = 40
fudge_factor = 2
tank_width = 68
tank_height = 111.5
tank_length = 151
num_tanks = 2
oil_burn_rate = 1
daily_runtime = 5
def cleanup():
print "Cleaning up GPIO."
GPIO.cleanup()
def termhandler(signum, frame):
print "Terminating script."
global shutdown_flag
shutdown_flag = True
atexit.register(cleanup)
def add(x,y):
return x+y
def init_sensor():
GPIO.setmode(GPIO.BOARD)
GPIO.setup(gpio_pin, GPIO.OUT)
GPIO.output(gpio_pin, 0)
time.sleep(0.000002)
def read_distance():
distances = []
for ping_number in range(1, num_pings):
successful=False
while not successful:
GPIO.setup(gpio_pin, GPIO.OUT)
GPIO.output(gpio_pin, 0)
time.sleep(0.000002)
GPIO.output(gpio_pin, 1)
time.sleep(0.000005)
GPIO.output(gpio_pin, 0)
GPIO.setup(gpio_pin, GPIO.IN)
goodread=True
watchtime=time.time()
starttime=0
endtime=0
while GPIO.input(gpio_pin)==0 and goodread:
starttime=time.time()
if (starttime-watchtime > timeout):
goodread=False
if goodread:
watchtime=time.time()
while GPIO.input(gpio_pin)==1 and goodread:
endtime=time.time()
if (endtime-watchtime > timeout):
goodread=False
else:
continue
if not goodread:
continue
if goodread:
air_temp=tmp102.read()
c_air_m_s=331.5 + (0.6 * air_temp)
duration=endtime-starttime
distance=((duration * c_air_m_s )/2)*100
distances.append(distance)
successful=True
time.sleep(0.05)
distances.sort
onethird=int(len(distances)/3)
twothirds=onethird * 2
mid_distances=distances[onethird:twothirds]
tank_air_space=(reduce(add, mid_distances)) / len(mid_distances)
tank_air_space=round(tank_air_space)
tank_air_space = tank_air_space - fudge_factor
print "Adjusted air space in tank is ", tank_air_space, " cm"
return tank_air_space
def get_fill_height():
air_space = read_distance()
oil_level = tank_height - air_space
print "Oil level calculated as ", oil_level, " cm"
return oil_level
def get_tank_volume():
tank_radius=tank_width/2
tank_square_height=tank_height-tank_width
tank_end_area=math.pi*(tank_radius**2)+(2*tank_radius*tank_square_height)
=tank_end_area*tank_length
return tank_volume
def get_oil_quantity():
oil_level=get_fill_height()
tank_radius=tank_width/2
tank_square_height=tank_height-tank_width
if oil_level < tank_radius:
tank_m=tank_radius-oil_level
theta=2*math.acos(tank_m/tank_radius)
oil_volume=0.5 * tank_radius**2 * (theta - math.sin(theta)) *
tank_length
elif (oil_level > tank_radius) and (oil_level < (tank_radius +
tank_square_height)):
vol_circle=math.pi * (tank_radius**2) * tank_length
height_in_square = oil_level - tank_radius
vol_cube=height_in_square * tank_width * tank_height
oil_volume=(vol_circle/2) + vol_cube
else:
vol_tank=get_tank_volume()
air_space=tank_height-oil_level
tank_m=tank_radius-air_space
air_volume=0.5 * tank_radius**2 * (theta - math.sin(theta)) *
tank_length
oil_volume = vol_tank - air_volume
return oil_volume
def cc_to_gallons(cubic_cm):
return cubic_cm * 0.000264172052358
init_sensor()
signal.signal(signal.SIGTERM, termhandler)
signal.signal(signal.SIGINT, termhandler)
oil_remaining_cc_1tank=get_oil_quantity()
oil_remaining_cc=oil_remaining_cc_1tank * num_tanks
oil_capacity_1tank=get_tank_volume()
oil_capacity=oil_capacity_1tank * num_tanks
oil_remaining_gal=cc_to_gallons(oil_remaining_cc)oil_capacity_gal=cc_to_gallons(oil_capacity)
orp=(oil_remaining_cc / oil_capacity) * 100
print "%.1f of %.1f gallons remaining ( %3d%% )" % (oil_remaining_gal,
oil_capacity_gal, orp)
hours_left=oil_remaining_gal / oil_burn_rate
print "%d hours of runtime remaining at %.2f gal/hr" % (hours_left, oil_burn_rate)
days_left=hours_left / daily_runtime
print "%d days of operation at %.2f hours per day average" % (days_left,
daily_runtime)
OUTPUT:
PROGRAM:
SERVER SIDE CODE:
import socket
import numpy as np
import encodings
HOST = ‘192.168.56.160’
PORT = 65432
def random_data():
x1 = np.random.randint(0, 55, None)
y1 = np.random.randint(0, 45, None)
my_sensor = "{},{}".format(x1,y1)
return my_sensor
def my_server():
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
print("Server Started waiting for client to connect ")
s.bind((HOST, PORT))
s.listen(5)
conn, addr = s.accept()
with conn:
print('Connected by', addr)
while True:
data = conn.recv(1024).decode('utf-8')
if str(data) == "Data":
print("Ok Sending data ")
my_data = random_data()
x_encoded_data = my_data.encode('utf-8')
conn.sendall(x_encoded_data)
elif str(data) == "Quit":
print("shutting down server ")
break
if not data:
break
else:
pass
if __name__ == '__main__':
while 1:
my_server()
CLIENT SIDE CODE:
import socket
import threading
import time
HOST = '192.168.56.1'
PORT = 65432
def process_data_from_server(x):
x1, y1 = x.split(",")
return x1,y1
def my_client():
threading.Timer(11, my_client).start()
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((HOST, PORT))
my = input("Enter command ")
my_inp = my.encode('utf-8')
s.sendall(my_inp)
data = s.recv(1024).decode('utf-8')
x_temperature,y_humidity = process_data_from_server(data)
print("Temperature {}".format(x_temperature))
print("Humidity {}".format(y_humidity))
s.close()
time.sleep(5)
if __name__ == "__main__":
while 1:
my_client()
OUTPUT:
Enter command
Temperature: 26.2000deg Celsius
Humidity: 27.3999%
7) PWM CONTROL USING ARDUINO
PROGRAM:
This relies on a highway speed checker device that senses speeding on highways and
notifies traffic authorities if it notes a vehicle that violates the defined highway speed limits.
Vehicle tracking
Vehicle trackers work by determining the exact location of a vehicle with the help of
GPS. They are used to curb theft and possible criminal activities. The GPS modem sends a
message to a specific mobile that stores data. The location information in terms of longitude
and latitude values are displayed using an LCD display. Its microcontroller checks the GPS
modem regularly.
Almost all modern cars (and motor cycles as well) are equipped with a safety feature
called Anti – lock Braking System or ABS when we apply brakes on a car, the brake pressure
will be applied on all the wheels irrespective of the orientation of the wheel and speed of the car,
causing the wheels to lock. This will result in skidding of the vehicle and loss of traction (there
is separate Traction control system). ABS (Anti-locking Barking System) plays an important
role in such situations. An ABS system typically comprises of three parts: Sensors, Control Unit
and Hydraulic Valves. Each wheel has its own sensor, which helps in continuously monitoring
the speed of rotation of the wheel. The data from the sensor is constantly monitored by the
Electronic Control Unit (ECU). If the rotational speed of a wheel is less than that of the others,
the wheel is susceptible to locking when brakes are applied. The ECU will then activate the
hydraulic valves according to the rotational speed of the wheel i.e. less hydraulic pressure to the
wheel rotating at slower speed than others. The reduction in hydraulic pressure to the brake will
reduce the braking force on that particular wheel and thus all the wheels will be synchronized.
The other case i.e. if the rotational speed of a wheel is higher than the others, then also ABS
comes in to picture.
Heart Pacemaker:
The need for secure real-time operating system and embedded computing security software
for use in military embedded system continues to rise due to increasing security concerns
and threats. The IT sector has responded by designing numerous security innovations that are
used to deliver protection at system and component levels. Connected security systems must
integrate numerous capabilities to achieve absolute security, especially in hostile network
environments. This may entail the use of layered security components that are integrated
with capabilities that keep the components updated to ensure continuous service and
application availability while retaining top security capabilities.
Aerospace and avionics system have strict requirements for software and
require the utmost security. Some of the most efficient aerospace systems deployed today
utilize embedded virtualization technology that creates advanced partitioning strategies.
Partitioning creates room for integration of several applications with the help of several
operating systems within shared compute platforms. It also triggers support for legacy
applications, facilitates the use of enterprise OS on traditional RTOS platform, offers a basis
for obsolescence management while systems evolve through their life cycle, and decreases
SWaP (size, weight and power) for next-gen system designs through platform sharing.
Support for modern hardware is also important for the Aerospace and avionics sector.
Applications with zero execution time: This is where a task has to be performed at a specific instant that
is minimized. For instance, in a digital control theory, taking measurement, calculating a control action, and
sending it to the peripheral device occurs instantly.
Quality of service: An RTOS can be used when a task must have a fixed service amount per time unit. In
this case, service implies CPU time. However, it can be network bandwidth, disk access bandwidth or
memory pages. This is crucial for applications like network services and video and audio streaming.
Applications that involve competition for resources: In some applications, tasks compete for resources
like network, processors, and memory. This competition is stiffer in real-time operating systems than in
general purpose operating systems. Therefore, a real-time operating system ensures that each task is allotted
the necessary resources promptly.
Applications with worst-case scenarios
If there are numerous tasks that need a service, there will be a time when they will all need the service at
once. Real-time operating systems are used in such applications to ensure that worst-case scenarios are given
priority.
PROGRAM:
int a[][]={{1,3,4},{2,4,3},{3,4,5}};
int b[][]={{1,3,4},{2,4,3},{1,2,4}};
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
{
c[i][j]=a[i][j]+b[i][j];
System.out.print(c[i][j]+" ");
System.out.println();
OUTPUT:
PROGRAM:
import java.util.Scanner;
int number;
number = scanner.nextInt();
scanner.close();
long fact = 1;
int i = 1;
while(i<=number)
fact = fact * i;
i++;
OUTPUT: