Sie sind auf Seite 1von 72

List of Practicals

PRAC 1 Study of Hardware components

1. PRAC 2 WAP to blink an LED


2. PRAC 3 WAP to interface stepper motor controller
3. PRAC 4 WAP to serial data interface
4. PRAC 5 PRAC WAP for the keypad and LCD interface
5. PRAC 6 WAP to simulate Traffic Signal
6. PRAC 7 WAP to simulate Elevator Control

7. PRAC 8 WAP to interface ADC0808 with 8051

Embedded Systems Manual


Practical No .1
Aim : Study of the Embedded system Hardware Components
Description :

1. An embedded system is a combination of 3 things:


a. Hardware
b. Software
c. Mechanical Components

And it is supposed to do one specific task only.

The hardware could be a part of Input, output or both usually and includes
buttons, display, LED’s, sensors, actuators and other electronic circuitry.

2. Example : Washing Machine

A washing machine from an embedded systems point of view has:

a. Hardware: Buttons, Display & buzzer, electronic circuitry.


b. Software: It has a chip on the circuit that holds the software which
drives controls & monitors the various operations possible.
c. Mechanical Components: the internals of a washing machine which actually
wash the clothes control the input and output of water, the chassis
itself.
3. Example 2: Air Conditioner

An Air Conditioner from an embedded systems point of view has:

a. Hardware: Remote, Display & buzzer, Infrared Sensors, electronic


circuitry.
b. Software: It has a chip on the circuit that holds the software which
drives controls & monitors the various operations possible. The software
monitors the external temperature through the sensors and then releases
the coolant or suppresses it.
c. Mechanical Components: the internals of an air conditioner the motor,
the chassis, the outlet, etc
In this practical we review some hardware components that are used in embedded
systems.

Embedded Systems Manual


1. Microcontroller/Microprocessors

 A microprocessor is a silicon chip representing a central processing unit.


 A microprocessor is a dependent unit and it requires the combination of other hardware like
memory, timer unit, and interrupt controller, etc. for proper functioning. Ex. 8085
microprocessor
 A microcontroller is a highly integrated chip that contains a CPU, scratch pad RAM, special
and general purpose register arrays,on chip ROM/FLASH memory for program storage ,
timer and interrupt control units and dedicated I/O ports.
 Microcontrollers are designed for either general purpose application requirement or domain
specific application requirement. Ex. 8051 microcontroller

 The Intel 8051 microcontroller is one of the most popular general purpose microcontrollers
in use today. The success of the Intel 8051 spawned a number of clones (also called
derivatives) which are collectively referred to as the MCS-51 family of microcontrollers,
which includes chips from vendors such as Atmel, Philips, Infineon, and Texas Instruments.
 8051 Variants
• The 8051 has the widest range of variants of any embedded controller in the market.
• The smallest device is the Atmel 89c1051, a 20 pin flash variant with 2 timers, UART,
20mA.
• The fastest parts are from Dallas, with performance close to 10 MIPS.
• The most powerful chip is the Siemens 80C517A, with 32-bit ALU, 2 UARTs, 2K RAM,
PLCC84 package, 8×16 bit PWMs and other features.
 Intel 8051

ROM [ bytes] 4K

RAM [bytes] 128

Timers 2

I/O pins 32

Serial port 1

Interrupt sources 6

Embedded Systems Manual


 Basic features of 8051
The 8051 is the original member of the MCS-51 family, and is the core for all MCS-51 devices.
The features of the 8051 core are-
• 8-bit CPU optimized for control applications
• Sixteen bit program counter (PC) and data pointer (DPTR).
• Eight bit program status word (PSW).
• Eight bit stack pointer (SP).
• Extensive Boolean processing (single-bit logic) capabilities
• 64K program memory address space
• 64K data memory address space
• 4K bytes of on-chip program memory
• 128 bytes of on-chip data RAM
 Four register banks, each containing eight registers.
 Sixteen bytes, of bit addressable memory locations.
 Eighty bytes of general-purpose data memory.
• 32 bidirectional and individually addressable I/O lines(4 groups of eight)
• Two 16 bit timers/counters
• Full duplex 8-bit serial data transmitter/receiver (UART)
• Control registers: TCON, TMOD, SCON, PCON, IP and IE among SFRs
• Thirty two I/O pins arranged as four 8-bit ports P0, P1,P2 and P3 addressed as PX.0-
PX.7.
• Two external and three internal interrupt sources.
• 6- source/5-vector interrupt structure with two priority levels
• On-chip clock oscillator clock circuits

Embedded Systems Manual


A. Logic diagram of 8051 micro controller
B. Pin out diagram of 8051 micro
controller

Figure : Block diagram of 8051 micro


controller

Embedded Systems Manual


Figure : Internal RAM Memory

Embedded Systems Manual


Figure : Special Function Registers

Embedded Systems Manual


2. Resistors: (Color code and types)
o A resistor is a passive two-terminal electrical component that implements
electrical resistance as a circuit element. Resistors act to reduce current flow, and,
at the same time, act to lower voltage levels within circuits. In electronic circuits
resistors are used to limit current flow, to adjust signal levels, bias active
elements, terminate transmission lines among other uses.
o Resistors are common elements of electrical networks and electronic circuitsand
are ubiquitous in electronic equipment. Practical resistors as discrete components
can be composed of various compounds and forms. Resistors are also
implemented within integrated circuits.
o The electrical function of a resistor is specified by its resistance: common
commercial resistors are manufactured over a range of more than nine orders of
magnitude. The nominal value of the resistance will fall within a manufacturing
tolerance.

Resistor Color Codes

Embedded Systems Manual


3 Capacitors:
 A capacitor (originally known as a condenser) is a passive two-terminal electrical
component used to store energy electrostatically in an electric field. The forms of
practical capacitors vary widely, but all contain at least two electrical
conductors (plates) separated by adielectric (i.e. insulator).
 When there is a potential difference across the conductors (e.g., when a capacitor is
attached across a battery), an electric field develops across the dielectric, causing
positive charge +Q to collect on one plate and negative charge −Q to collect on the
other plate.
 Capacitors are widely used in electronic circuits for blocking direct current while
allowing alternating current to pass. In analog filter networks, they smooth the output
of power supplies. In resonant circuits they tune radios to particular frequencies.
In electric power transmission systems, they stabilize voltage and power flow.

4. Inductors:
 An inductor, also called a coil or reactor, is a passive two-terminal electrical
component which resists changes in electric current passing through it. It consists of a
conductor such as a wire, usually wound into a coil. When a current flows through
it, energy is stored temporarily in a magnetic field in the coil.
 When the current flowing through an inductor changes, the time-varying magnetic
field induces a voltage in the conductor, according to Faraday’s law of
electromagnetic induction, which opposes the change in current that created it.

Embedded Systems Manual


5. LEDs/Displays

 A light-emitting diode (LED) is a two-lead semiconductorlight source. It is a basic pn-


junction diode, which emits light when activated.[7] When a suitable voltage is applied to
the leads, electrons are able to recombine with electron holes within the device, releasing
energy in the form of photons. This effect is called electroluminescence, and the color of
the light (corresponding to the energy of the photon) is determined by the energy band
gap of the semiconductor.
 An LED is often small in area (less than 1 mm2) and integrated optical components may
be used to shape its radiation pattern.

 Alphanumeric displays are available in seven-segment, starburst and dot-matrix format.


Seven-segment displays handle all numbers and a limited set of letters. Starburst displays
can display all letters. Dot-matrix displays typically use 5x7 pixels per character

A 7 segment LED Display A dot matrix display A 16 x 2 lcd display

Embedded Systems Manual


6. Diodes
 In electronics, a diode is a two-terminal electronic component with asymmetric
conductance; it has low (ideally zero) resistance to current in one direction, and high
(ideally infinite) resistance in the other. A semiconductor diode, the most common type
today, is a crystalline piece of semiconductor material with a p–n junction connected to
two electrical terminals.
 The most common function of a diode is to allow an electric current to pass in one
direction (called the diode's forward direction), while blocking current in the opposite
direction (the reverse direction). Thus, the diode can be viewed as an electronic version
of a check valve. This unidirectional behavior is called rectification, and is used to
convert alternating current to direct current, including extraction of modulation from
radio signals in radio receivers—these diodes are forms of rectifiers.

7. Transistors
 A transistor is a semiconductor device used to amplify and switch electronic signals and
electrical power.
 It is composed of semiconductor material with at least three terminals for connection to
an external circuit.
 A voltage or current applied to one pair of the transistor's terminals changes the current
through another pair of terminals. Because the controlled (output) power can be higher
than the controlling (input) power, a transistor can amplify a signal.

Embedded Systems Manual


8. Sensors
o A sensor is a device that detects events or changes in quantities and provides a
corresponding output, generally as an electrical or optical signal; for example, a
thermocouple converts temperature to an output voltage.
o A sensor's sensitivity indicates how much the sensor's output changes when the input
quantity being measured changes.

o The sensitivity is then defined as the ratio between output signal and measured
property. For example, if a sensor measures temperature and has a voltage output, the
sensitivity is a constant with the unit [V/K]; this sensor is linear because the ratio is
constant at all points of measurement.

9. Actuators
o An actuator is a type of motor that is responsible for moving or controlling a
mechanism or system.
o It is operated by a source of energy, typically electric current, hydraulic
fluid pressure, or pneumatic pressure, and converts that energy into motion. An
actuator is the mechanism by which a control system acts upon an environment. The
control system can be simple (a fixed mechanical or electronic system), software-
based (e.g. a printer driver, robot control system), a human, or any other input.

Embedded Systems Manual


10.Crystal oscillator
o A crystal oscillator is an electronic oscillator circuit that uses the mechanical
resonance of a vibrating crystal of piezoelectric material to create an electrical signal
with a very precise frequency.
o The most common type of piezoelectric resonator used is the quartz crystal, so
oscillator circuits incorporating them became known as crystal oscillators, but other
piezoelectric materials including polycrystalline ceramics are used in similar circuits.
o A crystal is a solid in which the constituent atoms, molecules, or ions are packed in a
regularly ordered, repeating pattern extending in all three spatial dimensions.

11.Operational Amplifiers:
 An operational amplifier (op-amp) is a DC-coupled high-gain electronic
voltage amplifier with a differential input and, usually, a single-ended output.
 In this configuration, an op-amp produces an output potential (relative to circuit ground)
that is typically hundreds of thousands of times larger than the potential difference
between its input terminals.

Embedded Systems Manual


12.ADC, DAC
 An analog-to-digital converter (ADC) is a device that converts a continuous physical
quantity (usually voltage) to a digital number that represents the quantity's amplitude.
 An ADC is defined by its bandwidth and its signal to noise. The actual bandwidth of an
ADC is characterized primarily by its sampling rate, and to a lesser extent by how it
handles errors such as aliasing. The dynamic range of an ADC is influenced by many
factors, including the resolution, linearity and accuracy and jitter . The dynamic range of
an ADC is often summarized in terms of its effective number of bits (ENOB), the number
of bits of each measure it returns that are on average not noise.

 In electronics, a digital-to-analog converter (DAC) is a function that converts digital


data into an analog signal.DACs are commonly used in music players to convert digital
data streams into analog audio signals. They are also used in televisions and mobile
phones to convert digital video data into analog video signals which connect to the screen
drivers to display monochrome or color images. These two applications use DACs at
opposite ends of the speed/resolution trade-off. The audio DAC is a low speed high
resolution type while the video DAC is a high speed low to medium resolution type.

Conclusion: The practical of the study of the Embedded System hardware components
is successfully completed.

Embedded Systems Manual


PRACTICAL NO: 2

Aim: Write a program to blink an LED.

Description:

The Practical will be performed in 3 steps:

1. Design the circuit in a simulator

2. Write the code using and IDE and generate the hex file.

3. Give the hex file as input to the Designed circuit and execute.

 The circuit consists of 8 leds connected to Port 1 of the AT89C51


microcontroller.

 The algorithm is as follows:

1. Set Port 1 = FF; // all led’s ON


2. Delay(); // wait for some time
3. Set Port 1 = 00; // all led’s OFF
4. Delay(); // wait for some time
5. Repeat step 1

 During execution the LED’s are supposed to blink on and off


alternatively

Embedded Systems Manual


COMPONENTS
CRYSTAL Frequency : 11.059 MHz
LED-BIBY
RESISTOR R1- 10K,
R2 – R8 – 100R
CAPACITOR C1, C2- 33pf,
C3 – 1uf
MICROCONTROLLER AT89C51- FREQUENCY – 11.059MHz

Design:

Embedded Systems Manual


Code:
#include <reg51.h>
void delay();

void main()
{
P1=0;
while(1)
{
P1 = 0x00;
delay();
P1 = 0xFF;
delay();
}
}

void delay()
{
unsigned int i,j,k;
for(i=0; i < 6; i++ )
{
for(j=0;j<250;j++)
{
for(k=0;k<250;k++);
}
}
}

Embedded Systems Manual


STEPS FOR WRITING THE CODE AND GENERATING HEX FILE:

1. Go to Keil uVision4 and create a new project


( Create a new folder for this practical and save project by a name Blinking led prac2)

Embedded Systems Manual


2. Select Atmel and then AT89C51 and click OK

3. Create a new file by pressing Ctrl + N, type the code and save it with a name
blinkingled.c
Right click on the Source Group 1 and select “ Add Files to Group Source Group 1”.

Embedded Systems Manual


Select the file that has been created. “ blinking led.c”

4. Go the Project ->Options for Target Target 1. In the Target tab, change the
Xtal(mhz): 11.059

Click the Output tab on top and Select the “create HEX file” option

Embedded Systems Manual


EXECUTING THE PROJECT :
1. In the design page, right click on the micro controller and select “Edit properties”. In
the Edit Component window under the program file option, browse and add the hex file
of the code. “blinking led.hex”

2. Run the design.

Embedded Systems Manual


OUTPUT

Conclusion: The practical for blinking led is successfully completed.

Embedded Systems Manual


PRACTICAL NO : 3

Aim : Write a program to interface stepper motor

Description:

Stepper motor is one of the commonly used motors for precise angular movement.

The advantage of using a stepper motor is that the angular position of the motor

shaft can be controlled without any feedback mechanism. Stepper motors are

widely used in industrial and commercial applications. They are also commonly

used as in drive systems of autonomous robots.

The microcontroller is programmed to rotate the stepper in Full Step modes.

For a stepper motor, in the full step mode both the phases are energized

simultaneously. The coils A, B, C, D are energized in the following order:

Coil Coil Coil Coil Coil Coil Coil Coil


Step A B C D Step A B C D
1 H L L H 1 1 0 0 1 0x09
2 H H L L 2 1 1 0 0 0x0C
3 L H H L 3 0 1 1 0 0x06
4 L L H H 4 0 0 1 1 0x03

The Operating voltage of a stepper motor is in the range of 5V to 24V hence

ULN2003, a stepper motor driver IC which is available of the shelf is used to

interface the stepper motor to the Microcontroller.

Embedded Systems Manual


COMPONENTS :

 ULN2003A

 Stepper Motor – 3 pins

 AT89C51 – Frequency -11.059 MHz

 C1 & C2 – 33pF

 C3 – 1uF

 Crystal – 11.059 MHz

 R1 – 10k

Design :

Embedded Systems Manual


Code :

#include<reg51.h>
#include<stdio.h>

void delay(int);
void main()
{
while(1)
{
P2=0x09;
delay(500);
P2=0x0C;
delay(500);
P2=0x06;
delay(500);
P2=0x03;
delay(500);
}
}
void delay(int k)
{
int i,j;
for(i=0;i<k;i++)
{
for(j=0;j<100;j++)
{
}
}
}

Embedded Systems Manual


Output:

Conclusion: The practical for stepper motor interfacing is successfully completed.

Embedded Systems Manual


PRACTICAL NO : 4

Aim : WAP to serial data interface

Description:

 In this practical we simulate serial data transfer from Microcontroller


to external peripheral through serial port.
 For the output to be displayed we have setup another serial port and
established a virtual connection between these two virtual serial ports
using a third party software Virtual Serial Port driver. This enables to
send the data from our micro controller via serial port and receive
the same in the simulated environment to view the output

MAX232

 It is an IC, first that converts signals from an RS-232 serial port to

signals suitable for use in TTL compatible digital logic circuits. The

MAX232 is a dual driver/receiver and typically converts the RX, TX,

CTS and RTS signals.

 The drivers provide RS-232 voltage level outputs (approx. ± 7.5 V)

from a single + 5 V supply via on-chip charge pumps and external

capacitors. This makes it useful for implementing RS-232 in devices

that otherwise do not need any voltages outside the 0 V to + 5 V

range, as power supply design does not need to be made more

complicated just for driving the RS-232 in this case.

Embedded Systems Manual


 The receivers reduce RS-232 inputs (which may be as high as

± 25 V), to standard 5 V TTL levels. These receivers have a typical

threshold of 1.3 V, and a typical hysteresis of 0.5 V.

Virtual Serial Port Driver

 creates virtual serial ports and connects them in pairs via virtual null

modem cable. Applications on both ends of the pair will be able to

exchange data in such a way, that everything written to the first port

will appear in the second one and backwards.

 All virtual serial ports work and behave exactly like real ones,

emulating all their settings. We can create as many virtual port pairs

as you want, so there will be no serial ports shortage and no

additional hardware crowding your desk.

Embedded Systems Manual


COMPONENTS:

 MICROCONTROLLER - AT89C51: 11.059MHz

 CAPACITORS

o C1:33PF

o C2:33PF

o C3:1UF

o C4:10UF

o C5:10UF

o C6:10UF

o C7:10UF

o C8:10UF

o C9:10UF

 COMPIM1, COMPIM2

o Physical baud rate:9600

o Virtual baud rate:9600

 CRYSTAL:11.059MhZ

 MAX232

 RESISTORS: R1 – 10K

 VIRTUAL TERMINAL - Baud rate:9600

Embedded Systems Manual


DESIGN:

TRANSMITTER

RECEIVER

Embedded Systems Manual


CODE:

#include<REG51.H>
#include<stdio.h>

void serial_init(void);

void main(void)
{
serial_init();
while(1)
{
printf("hello world!! \n\r");
}
}

void serial_init(void)
{
SCON=0x50;
TMOD=0x20;
TH1=0xFD;
TR1=1;
TI=1;
}

Diagram for Virtual Serial Port Driver Configuration

Embedded Systems Manual


OUTPUT:

Conclusion: The practical for serial data transfer is successfully completed.

Embedded Systems Manual


Practical No 5

Aim : WAP for the keypad and LCD interface

Description:

In this practical we interface a 4 x 4 keypad and and LCD panel to a


microcontroller.
The keys pressed on the keypad have to be displayed on the LCD panel.
We have created a 4x4 keypad using push buttons, also we could use a 4x4 keypad
that is available under the components library.
We will use a 16 x 2 LCD panel for display purpose which is LM016L

The LCD can be used in 2 modes – 8 bit mode and 4 bit mode.
Before using the LCD for display purpose, LCD has to be initialized. The LCD is
initialized in the following sequence.
1. Send command 0x30 - Using 8-bit interface
2. Delay 20ms
3. Send command 0x30 - 8-bit interface
4. Delay 20ms
5. Send command 0x30 - 8-bit interface
6. Delay 20ms
7. Send Function set - see Table 4 for more information
8. Display Clear command
9. Set entry mode command
The first 3 commands are usually not required but are recommended when you
are using 4-bit interface

Following is a detailed description of the Pin Configuration, Commands, Memory


and Entry mode of LCD to be used

Embedded Systems Manual


Pin Configuration:

Pin
Function Name
No
1 Ground (0V) Vss
2 Supply voltage; 5V (4.7V – 5.3V) VDD
3 Contrast adjustment; through a variable resistor VEE
4 Selects command register when low; and data register when high Register
0 = Instruction input , 1 = Data input Select(RS)
5 Low to write to the register; High to read from the register Read/write (RW)
0 = Write to LCD Module, 1 = Read from LCD module
6 Sends data to data pins when a high to low pulse is given Enable (E)
7 DB0
8 DB1
9 DB2
10 DB3
8-bit data pins
11 DB4
12 DB5
13 DB6
14 DB7
15 Backlight VCC (5V) Led+
16 Backlight Ground (0V) Led

LCD Memory
LCD display contains three memory blocks:
 DDRAM Display Data RAM;
 CGRAM Character Generator RAM; and
 CGROM Character Generator ROM.

DDRAM Memory
 DDRAM memory is used for storing characters to be displayed. The size of this memory
is capable of storing 80 characters. Some memory locations are directly connected to the
characters on display.

Embedded Systems Manual


 Everything works quite simply: it is enough to configure the display to increment
addresses automatically (shift right) and set the starting address for the message to be
displayed (for example 00 hex).
 If the cursor is on, it appears at the currently addressed location. In other words, when a
character appears at the cursor position, it will automatically move to the next addressed
location.
 This is a sort of RAM memory so that data can be written to and read from it, but its
content is irretrievably lost when the power goes off.

LCD Basic Commands

All data transferred to an LCD through the outputs D0-D7 will be interpreted as a command or a
data, which depends on the RS pin logic state:
 RS = 1 - Bits D0 - D7 are addresses of the characters to be displayed. LCD processor
addresses one character from the character map and displays it. The DDRAM address
specifies location on which the character is to be displayed. This address is defined prior
to transferring character or the address of the previously transferred character is
automatically incremented.
 RS = 0 - Bits D0 - D7 are commands for setting the display mode.

Here is a list of commands recognized by the LCD:

EXECUTION
COMMAND RS RW D7 D6 D5 D4 D3 D2 D1 D0
TIME

Clear display 0 0 0 0 0 0 0 0 0 1 1.64mS

Cursor home 0 0 0 0 0 0 0 0 1 x 1.64mS

Entry mode set 0 0 0 0 0 0 0 1 I/D S 40uS

Display on/off control 0 0 0 0 0 0 1 D U B 40uS

Cursor/Display Shift 0 0 0 0 0 1 D/C R/L x x 40uS

Function set 0 0 0 0 1 DL N F x x 40uS

Set CGRAM address 0 0 0 1 CGRAM address 40uS

Set DDRAM address 0 0 1 DDRAM address 40uS

Read "BUSY" flag


0 1 BF DDRAM address -
(BF)

Write to CGRAM or
1 0 D7 D6 D5 D4 D3 D2 D1 D0 40uS
DDRAM

Read from CGRAM


1 1 D7 D6 D5 D4 D3 D2 D1 D0 40uS
or DDRAM

Embedded Systems Manual


I/D R/L
1 = Increment (by 1) 1 = Shift right
0 = Decrement (by 1) 0 = Shift left
S DL
1 = Display shift on 1 = 8-bit interface

0 = Display shift off 0 = 4-bit interface

D N
1 = Display on 1 = Display in two lines
0 = Display off 0 = Display in one line

U F
1 = Cursor on 1 = Character format 5x10 dots
0 = Cursor off 0 = Character format 5x7 dots

B D/C
1 = Cursor blink on 1 = Display shift
0 = Cursor blink off 0 = Cursor shift

THE BUSY FLAG: Compared to the microcontroller, the LCD is an extremely slow
component. For this reason, it is necessary to provide a signal which would, upon command
execution, indicate that the display is ready for the next piece of data. That signal, called the busy
flag, can be read from the line D7. The display is ready to receive new data when the voltage on
this line is 0V (BF=0).

LCD Entry mode


Two bits decide the entry mode for LCD, these bits are:
a) I/D – Increment/Decrement bit
b) S – Display shift.
With these two bits we get four combinations of entry mode which are 0x04,0x05,0x06,0x07
(see table 3 in LCD Command section). So we get different results with these different entry
modes. Normally entry mode 0x06 is used which is No shift and auto incremement. I
recommend you to try all the possible entry modes and see the results, I am sure you will be
surprised.

COMPONENTS:
 AT89C51:11.059MHz
 BUTTON
 CAPACITOR
 C1:33pf
 C2:33pf
 C3:1uf
 CRYSTAL:11.059MHz
 LED-BLUE
 LM016L

Embedded Systems Manual


DESIGN

Embedded Systems Manual


CODE:
int main(void)
{
char key; // key char for keeping record of pressed key
while(1)
{
key = get_key(); // Get pressed key
writecmd(0x01); // Clear screen
writedata(key); // Echo the key pressed to LCD
}
}

char get_key(void) //get key from user


{
char key = 'n'; //assume no key pressed
while(key=='n') //wait untill a key is pressed
key = READ_SWITCHES(); //scan the keys again and again
return key; //when key pressed then return its value
}

char READ_SWITCHES(void)
{
Return ‘A’; // or the other characters
}

Embedded Systems Manual


#include<reg51.h>

void cct_init(void);
void delay(int);
void lcdinit(void);
void writecmd(int);
void writedata(char);
void Return(void);
char READ_SWITCHES(void);
char get_key(void);

sbit RowA = P1^0; //RowA


sbit RowB = P1^1; //RowB
sbit RowC = P1^2; //RowC
sbit RowD = P1^3; //RowD

sbit C1 = P1^4; //Column1


sbit C2 = P1^5; //Column2
sbit C3 = P1^6; //Column3
sbit C4 = P1^7; //Column4

sbit E = P3^6; //E pin for LCD


sbit RS = P3^7; //RS pin for LCD

int main(void)
{
char key; // key char for keeping record of pressed key
cct_init(); // Make input and output pins as required
lcdinit(); // Initilize LCD

while(1)
{
key = get_key(); // Get pressed key
writecmd(0x01); // Clear screen
writedata(key); // Echo the key pressed to LCD
}
}

void cct_init(void)
{
P0 = 0x00; //not used
P1 = 0xf0; //used for generating outputs and taking inputs from Keypad
P2 = 0x00; //used as data port for LCD
P3 = 0x00; //used for RS and E
}

Embedded Systems Manual


void delay(int a)
{
int i;
for(i=0;i<a;i++); //null statement
}

void writedata(char t)
{
RS = 1; // This is data mode
P2 = t; //Data transfer
E = 1; // => E = 1
delay(150);
E = 0; // => E = 0
delay(150);
}

void writecmd(int z)
{
RS = 0; // This is command mode
P2 = z; //Data transfer
E = 1; // => E = 1
delay(150);
E = 0; // => E = 0
delay(150);
}

void lcdinit(void)
{
delay(15000);
writecmd(0x30);
delay(4500);
writecmd(0x30);
delay(300);
writecmd(0x30);
delay(650);

writecmd(0x38); //function set


writecmd(0x0c); //display on,cursor off,blink off
writecmd(0x01); //clear display
writecmd(0x06); //entry mode, set increment
}

void Return(void) //Return to 0 location on LCD


{
writecmd(0x02);
delay(1500);
}

Embedded Systems Manual


char READ_SWITCHES(void)
{
RowA = 0; RowB = 1; RowC = 1; RowD = 1; //Test Row A

if (C1 == 0) { delay(10000); while (C1==0); return '7'; }


if (C2 == 0) { delay(10000); while (C2==0); return '8'; }
if (C3 == 0) { delay(10000); while (C3==0); return '9'; }
if (C4 == 0) { delay(10000); while (C4==0); return '÷'; }

RowA = 1; RowB = 0; RowC = 1; RowD = 1; //Test Row B

if (C1 == 0) { delay(10000); while (C1==0); return '4'; }


if (C2 == 0) { delay(10000); while (C2==0); return '5'; }
if (C3 == 0) { delay(10000); while (C3==0); return '6'; }
if (C4 == 0) { delay(10000); while (C4==0); return 'X'; }

RowA = 1; RowB = 1; RowC = 0; RowD = 1; //Test Row C

if (C1 == 0) { delay(10000); while (C1==0); return '1'; }


if (C2 == 0) { delay(10000); while (C2==0); return '2'; }
if (C3 == 0) { delay(10000); while (C3==0); return 'I3; }
if (C4 == 0) { delay(10000); while (C4==0); return '-'; }

RowA = 1; RowB = 1; RowC = 1; RowD = 0; //Test Row D

if (C1 == 0) { delay(10000); while (C1==0); return 'E'; }


if (C2 == 0) { delay(10000); while (C2==0); return '0'; }
if (C3 == 0) { delay(10000); while (C3==0); return '='; }
if (C4 == 0) { delay(10000); while (C4==0); return '+'; }

return 'n'; // Means no key has been pressed


}

char get_key(void) //get key from user


{
char key = 'n'; //assume no key pressed
while(key=='n') //wait untill a key is pressed
key = READ_SWITCHES(); //scan the keys again and again
return key; //when key pressed then return its value
}

Embedded Systems Manual


Output:

Conclusion: The practical for LCD & Keypad interfacing is successfully completed.

Embedded Systems Manual


PRACTICAL NO 6

Aim : Write a program to simulate traffic signals

Description:

Traffic Lights are signaling devices positioned at road intersections, pedestrian

crossings and other locations to control competing flows of traffic

For this practical we will use LED of red, green, yellow and blue color and are

going to implement the code logic according to the following diagram.

The above diagram illustrates the traffic flow layout of the four way road

The traffic flow can be classified in to four phases in the above diagram and we
have considered the North as starting point of this traffic flow. And in the above
scheme vehicles are allowed to make a free right turn so we need to consider only
two directions straight and left. So the green signal was classified into two types
one for G for permitting vehicle to proceed forward and GL for permitting vehicles
to left.
PHASE I-
 Initially Vehicle from A needs to travel to F and from E to B roads.
 So in the first Phase forward green signal in A and E permits vehicles to
pass through while East and west roads are stopped by red signal.
PHASE II-

Embedded Systems Manual


 Phase II permits the vehicle to pass from G to D and from C to H roads.
 Traffic flow from rest of the two roads North and south was stopped by
means of Red signal.
PHASE III-
 Phase three permits traffic flow in the left directions from A to D and from
E to H.
 Traffic flow in East and west are stopped by means of red signal.
PHASE IV-
 Phase four permits traffic flow from C to F and from G to B.
 Traffic flow in the North and south are stopped by means of red signal.
 The cycle repeats again from Phase I to Phase IV and thus the traffic is
regulated.

Embedded Systems Manual


COMPONENT :

 AT89C51 – 11.059 MHz


 C1 & C2 – 33pF
 C3 – 1uF
 Crystal – 11.059 MHz
 R1 – 10k
 R2 – R17 – 100
 LED – 10mA
 Traffic Lights

DESIGN :

Embedded Systems Manual


Embedded Systems Manual
Code :
#include <REG51.H>
void delay();
void count1();
sbit NR = P0^0;
sbit NY = P0^1;
sbit NG = P0^2;
sbit NL = P0^3;

sbit SR = P0^4;
sbit SY = P0^5;
sbit SG = P0^6;
sbit SL = P0^7;

sbit ER = P1^0;
sbit EY = P1^1;
sbit EG = P1^2;
sbit EL = P1^3;

sbit WRR = P1^4;


sbit WY = P1^5;
sbit WG = P1^6;
sbit WL = P1^7;

void main(void)
{

while(1){
/*
NR = 1; //allow north to south AND WEST
NY = 1;
NG = 0;
NL = 1;

SR = 1; //allow south to north AND EAST


SY = 1;
SG = 0;
SL = 1;

Embedded Systems Manual


ER = 0; //BLOCK EAST TO WEST
EY = 1;
EG = 1;
EL = 1;

WRR = 0; //BLOCK WEST TO EAST


WY = 1;
WG = 1;
WL = 1;

count1();
NR = 0;
NY = 1;
NG = 1;
NL = 1;

SR = 0;
SY = 1;
SG = 1;
SL = 1;

ER = 1; //BLOCK EAST TO WEST


EY = 1;
EG = 0;
EL = 1;

WRR = 1; //BLOCK WEST TO EAST


WY = 1;
WG = 0;
WL = 1;
count1();

NG=0;
SG=0;
ER=0;
WRR=0;
count1();
EY=0;
WY=0;
ER=1;
Embedded Systems Manual
WRR=1;
count1();
EY=1;
WY=1;
NG=1;
SG=1;
NR=0;
SR=0;
ER=1;
WRR=1;
EG=0;
WG=0; */

//1ST 7SEC, NORTH TO SOUTH AND SOUTH TO NORTH REST BLOCKED


NR = 1; //allow north to south AND WEST
NY = 1;
NG = 0;
NL = 1;

SR = 1; //allow south to north AND EAST


SY = 1;
SG = 0;
SL = 1;

ER = 0; //BLOCK EAST TO WEST


EY = 1;
EG = 1;
EL = 1;

WRR = 0; //BLOACK WEST TO EAST


WY = 1;
WG = 1;
WL = 1;
count1();

//2ND 7SEC, NORTH TO SOUTH AND yELLOW FOR EAST TO WEST N WEST TO EAST
NR = 1; //allow north to south AND WEST
NY = 1;
NG = 0;
NL = 1;
Embedded Systems Manual
SR = 1; //allow south to north AND EAST
SY = 1;
SG = 0;
SL = 1;

ER = 1; //BLOCK EAST TO WEST, yellow on


EY = 0;
EG = 1;
EL = 1;

WRR = 1; //BLOACK WEST TO EAST, yellow on


WY = 0;
WG = 1;
WL = 1;
count1();

//3RD 7 SEC, EAST TO WEST AND WEST TO EAST REST BLOCKED


NR = 0; //allow north to south AND WEST
NY = 1;
NG = 1;
NL = 1;

SR = 0; //allow south to north AND EAST


SY = 1;
SG = 1;
SL = 1;

ER = 1; //BLOCK EAST TO WEST, yellow on


EY = 1;
EG = 0;
EL = 1;

WRR = 1; //BLOACK WEST TO EAST, yello on


WY = 1;
WG = 0;
WL = 1;
count1();

Embedded Systems Manual


//4TH 7 SEC, SOUTH TO WEST AND NORTH TO EAST REST ALL BLOCKED
NR = 0;
NY = 1;
NG = 1;
NL = 0;

SR = 0;
SY = 1;
SG = 1;
SL = 0;

ER = 0;
EY = 1;
EG = 1;
EL = 1;

WRR = 0;
WY = 1;
WG = 1;
WL = 1;
count1();

//5TH 7 SEC, EAST TO SOUTH AND WEST TO NORTH REST ALL BLOCKED
NR = 1;
NY = 0;
NG = 1;
NL = 1;

SR = 1;
SY = 0;
SG = 1;
SL = 1;

ER = 1; //BLOCK EAST TO WEST


EY = 1;
EG = 1;
EL = 0;

WRR = 1; //BLOACK WEST TO EAST


Embedded Systems Manual
WY = 1;
WG = 1;
WL = 0;
count1();
}
}

void count1()
{
unsigned int i,j;
for(j=0;j<=2;)
{
for(i=0;i<=3;)
{
i++;
delay();
}
j++;
delay();
}
}

void delay(void)
{
unsigned int s,k;
for(s=0;s<=230;s++)
{
for(k=0;k<238;k++);
}
}
OUTPUT:

Embedded Systems Manual


Conclusion: The practical for implementing traffic signal is successfully completed.

Embedded Systems Manual


PRACTICAL NO 7

Aim: WAP to implement elevator control.


Description:
This practical is based on the concept of elevator control. Here the elevator

is simulated for a Ground + 3 Floors structures.

A button is made available on every floor to call the lift.

An LED is used to indicate the availability of the lift. RED indicates. BUSY

and GREEN Indicates available.

A 7 segment display is used to indicate the current position of the lift. The

seven segment LED is used in the Common Cathode configuration.

The following table displays the how decimal values 0-9 are displayed on

the 7 segment display

Embedded Systems Manual


COMPONENTS

 AT89C51 – 11.059 MHz

 C1 & C2 – 33pF

 C3 – 1uF

 Crystal – 11.059 MHz

 R1 – 10k

 7SEG-MPX1-CC (7 SEGMENT DISPLAY -COMMON CATHOD)

DESIGN:

Embedded Systems Manual


Code:

#include<reg51.h>
void delayd();

char key; // key char for keeping record of pressed key


//2. PUSH BUTTON SWITCHES TO CALL LIFT
sbit TF = P1^0;
sbit SF = P1^1;
sbit FF = P1^2;
sbit GF = P1^3;
sbit led = P1^4;

char READ_SWITCHES(void);
char get_key(void);

void main()
{
P2 = 0x3f;
P1 = 0x00;
while(1)
{

key = get_key(); // Get pressed key


P1 = 0x00;
led=0;
}
}

char get_key(void) //get key from user


{
char key = 'n'; //assume no key pressed

while(key=='n') //wait untill a key is pressed


key = READ_SWITCHES(); //scan the keys again and again

return key; //when key pressed then return its value


}

char READ_SWITCHES(void)
{
//ground floor
Embedded Systems Manual
if (GF == 1)
{

led=1;
// third to ground
if(GF==1 && key=='T')
{
delayd();
P2 = 0x5b;
delayd();
P2 = 0x06;
delayd();
P2 = 0x3f;
}
// second to ground
if(GF==1 && key=='S')
{
delayd();
P2 = 0x06;
delayd();
P2 = 0x3f;
}
// first to ground
if(GF==1 && key=='F')
{
delayd();
P2 = 0x3f;
}
return 'G';
}

//first floor
if (FF == 1)
{
led=1;
//third floor to first floor
if(FF==1 && key=='T')
{
delayd();
P2 = 0x5b;
delayd();
Embedded Systems Manual
P2 = 0x06;
}
//second floor to first floor
if(FF==1 && key=='S')
{
delayd();
P2 = 0x06;
}
//ground floor to first floor
if(FF==1 && key=='G')
{
delayd();
P2 = 0x06;
}
delayd();
return 'F';
}
//second floor
if (SF == 1)
{
led=1;
//third floor to second floor
if(SF==1 && key=='T')
{
delayd();
P2 = 0x5b;
}
//first floor to second floor
if(SF==1 && key=='F')
{
delayd();
P2 = 0x5b;
}
//ground floor to second floor
if(FF==1 && key=='G')
{
delayd();
P2 = 0x06;
delayd();
P2 = 0x5b;
}
Embedded Systems Manual
delayd();
return 'S';
}
//third floor
if (TF == 1)
{
led=1;
if(TF==1 && key='G') //ground floor to third floor
{
delayd();
P2 = 0x06;
delayd();
P2 = 0x5b;
delayd();
P2 = 0x4f;
}
if(TF==1 && key='F') //first floor to third floor
{
delayd();
P2 = 0x5b;
delayd();
P2 = 0x4f;
}

if(TF==1 && key='S') //second floor to third floor


{
delayd();
P2 = 0x4f;
}
delayd();
return 'T';
}
}

void delayd ( )
{
unsigned int s,k,i;
for(i=0;i<3;i++)
{
for(s=0;s<=230;s++)
{
Embedded Systems Manual
for(k=0;k<238;k++);
}
}
}

Embedded Systems Manual


Output:

Conclusion: The practical to implement elevator control is successfully completed.

Embedded Systems Manual


PRACTICAL NO : 8

Aim : WAP to implement ADC0808 with 8051 microcontroller

Description:

In this practical we aim to study analog to digital conversion. IN the

simulated environment we use a variable resistor to indicate the analog

input of a sensor. The variable resistor is interfaced to ADC0808 chip which

is interfaced to the microcontroller.

The output is displayed on a LCD panel.

ADC0808 is an 8 bit analog to digital converter with eight input analog

channels, i.e., it can take eight different analog inputs. The input which is to

be converted to digital form can be selected by using three address lines.

The voltage reference can be set using the Vref+ and Vref- pins. The step

size is decided based on set reference value. Step size is the change in analog

input to cause a unit change in the output of ADC. The default step size is

19.53mV corresponding to 5V reference voltage. ADC0808 needs an

external clock to operate unlike ADC0804 which has an internal clock. The

ADC needs some specific control signals for its operations like start

conversion and bring data to output pins. When the conversion is complete

the EOC pins goes low to indicate the end of conversion and data ready to

be picked up.
Embedded Systems Manual
Components:
 ADC0808
 AT89C51:11.059MHz
 CAP
 C1:33pf
 C2:33pf
 C3:1uf
 CAP-ELEC
 CRYSTAL
 LM016L
 POT-HG
 RES

Embedded Systems Manual


Figure: Pin Description of ADC0808

Embedded Systems Manual


DESIGN

Embedded Systems Manual


Code:

#include<reg51.h>
//Pin description
/*
P1.4 to P1.7 is data bus
P1.1 is RS
P1.0 is E
*/
//********************

// Defines Pins
sbit RS = P1^1;
sbit E = P1^0;

sbit Add_A = P2^0; // Address Pin A


sbit Add_B = P2^1; // Address Pin B
sbit Add_C = P2^2; // Address Pin C
sbit ALE = P2^3; // Address Latch Enable
sbit EOC = P2^4; // End Of Conversion
sbit OE = P2^5; // Output Enable
sbit START = P2^6; // Start Conversion
sbit CLK = P2^7; // Clock for AD0808

#define E_Delay 250 // Constants


#define AN1 1 // Define ADC Channels

#define Data_Bus P0 // Define Data Bus


#define HalfCycleDelay 10 // usecs

// Function Declarations
void delay_sec(unsigned int);
void __delay_us(unsigned int);
void InitLCD(void);
void WriteCommandToLCD(int);
void WriteDataToLCD(char);
void WriteStringToLCD(const char*);
void ClearLCDScreen(void);
void InitADC(void);
unsigned char ReadADC(unsigned char);

void main()
{
unsigned char ADC_Value = 0; // To capture ADC value
unsigned char Digit[3] = { 0,0,0 }; // To make digits to display on LCD

InitADC(); // Initialize ADC


Embedded Systems Manual
InitLCD(); // Initialize LCD display
WriteStringToLCD("Starting..."); // Show Welcome Message
delay_sec(1); // 1 Sec delay
ClearLCDScreen(); // Clear LCD

while(1)
{
ADC_Value = ReadADC(AN1); // Read ADC value from Channel 1

Digit[2] = (unsigned char)( ADC_Value/100);


// Find out first digit
Digit[1] = (unsigned char)( ADC_Value/10) - Digit[2]*10; // Find out second digit
Digit[0] = ADC_Value - Digit[2]*100 - Digit[1]*10; // Find out third
digit

ClearLCDScreen();
// Clear LCD
WriteStringToLCD("ADC value = ");
// Display string
WriteDataToLCD(Digit[2]+0x30);
// Display first digit
WriteDataToLCD(Digit[1]+0x30);
// Display second digit
WriteDataToLCD(Digit[0]+0x30);
// Display third digit

delay_sec(1);
// Delay of one second
}
}

void InitADC(void)
{
Add_A = 0; // Make output
Add_B = 0; // Make output
Add_C = 0; // Make output
ALE = 0; // Make output
EOC = 1; // Make input
OE = 0; // Make output
START = 0; // Make output
CLK = 0; // Make output

Data_Bus = 0xFF; // Make Inputs


}

Embedded Systems Manual


unsigned char ReadADC(unsigned char Channel)
{
unsigned int i = 0;
unsigned int ADC_value = 0;

Add_C = 0; Add_B = 0; Add_A = 1;

__delay_us(HalfCycleDelay); // 250kHz Frequency


ALE = 1; // Enable Address Latch
CLK = 1; // Make CLK High
__delay_us(HalfCycleDelay); // 250kHz Frequency
CLK = 0; // Make CLK Low
START = 1; // Start ADC Conversion
__delay_us(HalfCycleDelay); // 250kHz Frequency
CLK = 1; // Make CLK High
ALE = 0; // Disable Address Latch
__delay_us(HalfCycleDelay); // 250kHz Frequency
CLK = 0; // Make CLK Low
START = 0; // Complete the start pulse

for(i=0;i<2000;i++)
{
CLK = !CLK; // Toggle Clock
__delay_us(HalfCycleDelay); // 250kHz Frequency

if(!EOC) // Wait for EOC to be low


break;
}

for(i=0;i<2000;i++)
{
CLK = !CLK; // Toggle Clock
__delay_us(HalfCycleDelay); // 250kHz Frequency

if(EOC) // Wait for EOC to be High


break;
}

CLK = 0; // Make CLK Low


OE = 1; // Enable Output
__delay_us(HalfCycleDelay); // 250kHz Frequency
CLK = 1; // Make CLK High
__delay_us(HalfCycleDelay); // 250kHz Frequency
CLK = 0; // Make CLK Low
__delay_us(HalfCycleDelay); // 250kHz Frequency
CLK = 1; // Make CLK High

ADC_value = Data_Bus; // Read value


Embedded Systems Manual
__delay_us(HalfCycleDelay); // 250kHz Frequency
OE = 0; // Disable Output
CLK = 0; // Make CLK Low
__delay_us(HalfCycleDelay); // 250kHz Frequency

return ADC_value; // Return ADC value


}

void delay_sec(unsigned int d)


{
unsigned int i;

for(i=0;i<(d*20);i++)
__delay_us(50000);
}

// Function Purpose: Produce approximate delay in given uSecs.


void __delay_us(unsigned int d)
{
unsigned int i, limit;
limit = d/15;

for(i=0;i<limit;i++);
}

void ToggleEpinOfLCD(void)
{
E = 1; // Give a pulse on E pin
__delay_us(E_Delay); // so that LCD can latch the
E = 0; // data from data bus
__delay_us(E_Delay);
}

void WriteDataToLCD(char t)
{
RS = 1; // This is data

P1 &= 0x0F; // Make P1.4 to P1.7 zero


P1 |= (t&0xF0); // Write Upper nibble of data
ToggleEpinOfLCD(); // Toggle E pin to send data

P1 &= 0x0F; // Make P1.4 to P1.7 zero


P1 |= ((t<<4)&0xF0);// Write Lower nibble of data
ToggleEpinOfLCD(); // Toggle E pin to send data
}
void WriteCommandToLCD(int z)
{
Embedded Systems Manual
RS = 0; // This is command

P1 &= 0x0F; // Make P1.4 to P1.7 zero


P1 |= (z&0xF0); // Write Upper nibble of data
ToggleEpinOfLCD(); // Toggle E pin to send data

P1 &= 0x0F; // Make P1.4 to P1.7 zero


P1 |= ((z<<4)&0xF0);// Write Lower nibble of data
ToggleEpinOfLCD(); // Toggle E pin to send data
}

void InitLCD(void)
{
RS = 0; // Make pin zero
E = 0; // Make Pin zero

///////////// Reset process from datasheet /////////


__delay_us(15000);

P1 = 0x0F; // Make P1.4 to P1.7 zero


P1 = 0x30; // Write 0x3
ToggleEpinOfLCD(); // Toggle E pin to send data

__delay_us(4500);

P1 &= 0x0F; // Make P1.4 to P1.7 zero


P1 |= 0x30; // Write 0x3
ToggleEpinOfLCD(); // Toggle E pin to send data

__delay_us(300);

P1 &= 0x0F; // Make P1.4 to P1.7 zero


P1 |= 0x30; // Write 0x3
ToggleEpinOfLCD(); // Toggle E pin to send data

__delay_us(650);

P1 &= 0x0F; // Make P1.4 to P1.7 zero


P1 |= 0x20; // Write 0x2
ToggleEpinOfLCD(); // Toggle E pin to send data

__delay_us(650);

/////////////////////////////////////////////////////
WriteCommandToLCD(0x28); //function set
WriteCommandToLCD(0x0c); //display on,cursor off,blink off
WriteCommandToLCD(0x01); //clear display
WriteCommandToLCD(0x06); //entry mode, set increment
}
Embedded Systems Manual
void WriteStringToLCD(const char *s)
{
while(*s)
WriteDataToLCD(*s++); // print first character on LCD
}

void ClearLCDScreen(void) // Clear the Screen and return cursor to zero position
{
WriteCommandToLCD(0x01); // Clear the screen
__delay_us(2000); // Delay for cursor to return at zero position
}
Output:

Conclusion: The practical for ADC0808 interfacing is successfully completed.

Embedded Systems Manual


Embedded Systems Manual

Das könnte Ihnen auch gefallen