Вы находитесь на странице: 1из 45

EC 17L1 Embedded Systems Laboratory

Expt no 1

VISION COMPILER AND SIMULATOR


AIM:
To study the environment of Keil icrovision.

THEORY:
Vision Overview
The Vision IDE is a Windows-based software development platform that combines
a robust editor and project manager. Vision5 integrates all tools including C compiler,
macro assembler, linker/ locator and HEX file generator. Vision5 helps expedite the
development process of embedded applications.
Create Project File Folder and Specify Project Name
To create a new project file, from Vision5 menu, select Project/New/Vision
Project...The window appears as shown in Fig. 1.1. This opens a standard Windows dialog
that asks you for the new project file name. Use separate folder each project. To get a new
empty folder, use the icon Create New Folder in the dialog.
Select this folder and enter the file name for the new project, i.e., project1. Vision5
creates a new project file with the name PROJECT1.UV2 that contains a default target and
file group name. You can see these names in the Project Workspace-Files.

Fig 1.1 Window to create a project


ECE Department
Engineering, Trikaripur

College of

EC 17L1 Embedded Systems Laboratory


Select Microcontroller from Device Database
When you create a new project, Vision5 asks you to select a CPU for the project.
The Select Device dialog box shows the Vision5 device as shown in Fig.1.2. Select the
microcontroller, e.g., AT89S52.

Fig 1.2 Window to select target microcontroller

Add Source File to Project


Once the source file is created, add .asm or .c file to your project. Vision5 offers
several ways to add source file to project.
For example, you can select the file group in the Project Workspace and Files page
and click the right mouse key to open a local menu. The option Add Files open the
standard files dialog. Select the file MAIN.C just created.
Build Project
To build a project, go to Options/Target, translate all source files and link the
application with a click on the Build Target toolbar icon. When you build an application
with syntax error, Vision5 will display errors and warning messages in the Output
Window-Build page as shown in Fig.1.3. A double click on a message line opens the source
file on the correct location in a Vision5 editor window.
ECE Department
Engineering, Trikaripur

College of

EC 17L1 Embedded Systems Laboratory

Modify the existing source code or add new source files to the project. The Build Target
toolbar button translates only modified or new source files and generates the executable
file. Vision5 maintains a file dependency list and knows all the included files used
within a source file. Even the tool options are saved in the file dependency list, so that
Vision5 can rebuild files when it is needed. With the Rebuild Target command, all
source are translated, regardless of modifications.
Test programs with the Vision5 Debugger. The Vision5 Debugger offers two opening
modes simulator allows verifying applications on PC, or Target Debugging with an
Evaluation Board.
To program your application into Flash ROM, Vision5 integrates command-line
driven Flash Utilities ULINK USB JTAG. Create HEX file to use Flash programming
utilities.

Fig 1.3 Messages after compile/build of the program

Single Stepping
Vision5 supports various methods of single stepping through your application. The
various options are available on the menu as shown in Fig.1.4.

Fig 1.4 Single Stepping of the program

Click the Step Into button on the toolbar to execute a single instruction or line of code.
Step into all called functions.
Click the Step Over button on the toolbar to execute a single instruction or line of
code. Step Over executes all called functions without stepping into them.

ECE Department
Engineering, Trikaripur

College of

EC 17L1 Embedded Systems Laboratory

Click the Step Out button on the toolbar to step out of a function and return to the
caller.
Click the Run to Cursor button on the toolbar to begin executing your target program
until the current line is reached
Burning the Hex File to Program Memory
Click open the Bitsforge 8051 programmer V 2.0. A window as shown in Fig.1.5
appears on the screen. Select the Com1/Com2 port to which the target microcontroller is
connected. Select the target device, e.g., AT89S52.
Browse the target hex file (e.g. test.hex). Click auto icon on the window. Observe
the message window at the bottom. It erases and then program the flash memory. When
burning of the program is completed, the message finished is displayed. Then, the target
board is ready for the intended application.

Fig 1.5 Burning of hex files to the program memory

RESULT:
Familiarised the environment of Keil icrovision.

ECE Department
Engineering, Trikaripur

College of

EC 17L1 Embedded Systems Laboratory

SCHEMATIC DIAGRAM:

ECE Department
Engineering, Trikaripur

College of

EC 17L1 Embedded Systems Laboratory


Exp No: 2

TESTING 8051 I/O PORTS


AIM:
To test the 8051 system and its ports

ALGORITHM:
1.
2.
3.
4.

P2=0 which switches on the led.


Then Some Delay () Keeps Led in on state.
After Delay () port 2 is made P2=1 which switches off the led.
Then Some Delay () keeps led in switch off state.

PROGRAM:
/* LED BLINKING */

#include<reg52.h>
void delay(long g)
{
unsigned int i,j;
DELAY

//FUNCTION TO IMPLEMENT

for(i=0;i<g;i++){
for(j=0;j<255;j++);
}
}
void main()
{
P2=0;

//LEDs ARE CONNECTED TO PORT2

while(1) {
P2=0x00;
delay(700);
ECE Department
Engineering, Trikaripur

//LED BLINKING
6

College of

EC 17L1 Embedded Systems Laboratory


P2=0xFF;
delay(700);
}
}

/* LED SHIFTING*/
#include<reg52.h>
void delay(long g)
{
unsigned int i,j;
DELAY

//FUNCTION TO IMPLEMENT

for(i=0;i<g;i++){
for(j=0;j<255;j++);
}
}
void main()
{
P2=0X01;
while(P2!=0x80)
{
delay(700);
P2=P2<<1;

// FOR LED SHFTING

}
while(P2!=0x01)
{
delay(700);
P2=P2>>1;
ECE Department
Engineering, Trikaripur

College of

EC 17L1 Embedded Systems Laboratory


}
}

RESULT:
The program to test the 8051 system and its ports was written and executed
successfully.

ECE Department
Engineering, Trikaripur

College of

EC 17L1 Embedded Systems Laboratory

SCHEMATIC DIAGRAM

ECE Department
Engineering, Trikaripur

College of

EC 17L1 Embedded Systems Laboratory

ECE Department
Engineering, Trikaripur

10

College of

EC 17L1 Embedded Systems Laboratory

Exp No: 3

LCD INTERFACING
AIM:
To interface the LCD display with 8051 microcontroller and to verify data on LCD display.

THEORY:
Liquid Crystal Display (LCDs) provides a cost effective way to put a text output
unit for a microcontroller. This display has 2 lines and can display 16 characters on each
line. Nonetheless, when it is interfaced with the microcontroller, we can scroll the messages
with software to display information which is more than 16 characters in length.
LCD Internal Controller
The LCD is a simple device to use but the internal details are complex. Most of the
16x2 LCDs use a Hitachi HD44780 or a compatible controller. The Display Controller
takes commands and data from a external microcontroller and drivers the LCD panel
(LCDP). It takes an ASCII value as input and generates a pattern for the dot matrix.
HD44780 based LCD consists of two 8 bit registers. One is Instruction register
which consists of instruction given to LCD while another is called Data register which
temporarily stores data to be written to or read from DDRAM (Display Data RAM) or
CGRAM(character generation RAM).
Busy Flag
D7 pin of LCD can be used to read the status of busy flag. When busy flag is at logic HIGH
state, it means it is busy doing its internal operation and during this period neither an
instruction is send to LCD nor a data can be send to LCD. When this flag is at logic LOW
state it is free from its internal operation and this is the time when we send an instruction or
data which has to be displayed tin LCD.
DDRAM
DDRAM which stands for display data RAM stores the data which has to be displayed in
LCD. The first line of DDRAM begins with address 80h and second line begins with C0h
in 16*2 LCD.
Basic Commands
LCD can be interfaced in 8 bit mode or in 4 bit mode. The LCD controller uses RS and RW
lines along with E to operate the LCD.

Resister Select (RS): Determines whether a command (RS = 0) is sent (to set up the
display) or actual data (RS=1) is sent.

Read/Write RW=0; writes to the LCD. RW=1; Reads from the LCD.

ECE Department
Engineering, Trikaripur

11

College of

EC 17L1 Embedded Systems Laboratory

COMMAND

FUNCTION

0F

LCD ON, Cursor ON, Cursor blinking ON

01

Clear screen

02

Return home

04

Decrement cursor

06

Increment cursor

0E

Display ON ,Cursor blinking OFF

80

Force cursor to the beginning of 1st line

C0

Force cursor to the beginning of 2nd line

38

Use 2 lines and 57 matrix

83

Cursor line 1 position 3

3C

Activate second line

08

Display OFF, Cursor OFF

C1

Jump to second line, position1

OC

Display ON, Cursor OFF

C1

Jump to second line, position1

C2

Jump to second line, position2

PIN
NUMBER

FUNCTION

Ground

VCC

Contrast adjustment (VO)

Register Select (RS). RS=0:


Command, RS=1: Data

ECE Department
Engineering, Trikaripur

12

College of

EC 17L1 Embedded Systems Laboratory

Read/Write (R/W). R/W=0: Write,


R/W=1: Read

Clock (Enable). Falling edge triggered

Bit 0 (Not used in 4-bit operation)

Bit 1 (Not used in 4-bit operation)

Bit 2 (Not used in 4-bit operation)

10

Bit 3 (Not used in 4-bit operation)

11

Bit 4

12

Bit 5

13

Bit 6

14

Bit 7

15

Back-light Anode(+)

16

Back-Light Cathode(-)

ALGORITHM:
Writing to LCD
Data are written in Instruction Register (IR) and Data register (DR). Data written in
IR are used to send command to the LCD and data send to DR are used to display in
LCD. While writing data in IR of LCD, RS and RW pin should be at LOW logic level and
the logic state of Enable pin should change from HIGH to LOW logic state.
Reading from LCD
Reading data from DDRAM or CGRAM of LCD can be accomplished when RS
and RW are both at HIGH logic state and when EN changes its logic state from LOW to
HIGH.

PROGRAM:
#include<reg52.h>
sbit LCD_EN
OF PORT3

= P3^7;

sbit LCD_RS = P3^6;


6TH PIN OF PORT3

//ENABLE PIN OF LCD IS CONNECTED TO 7TH PIN


//REGISTER SELECT PIN OF LCD IS CONNECTED TO

char temp,nam[8]="HELLO";
int i,k,l;
void delay()
ECE Department
Engineering, Trikaripur

13

College of

EC 17L1 Embedded Systems Laboratory


{
for(k=0;k<2;k++)
{
for(l=0;l<255;l++);
}
}
void cmd(char temp)
{
LCD_RS = 0;

//Data/cmd reg

P0=temp;
LCD_EN =1;

//enable LCD

delay();
LCD_EN =0;
}
void dat(char temp)
{
LCD_RS = 1;
P0=temp;
LCD_EN =1;
delay();
LCD_EN =0;
}
void print(char *ptr)
{
while(*ptr!=0)
{
dat(*ptr);
ECE Department
Engineering, Trikaripur

14

College of

EC 17L1 Embedded Systems Laboratory


ptr++;
}
}
void main()
{
cmd(0X30);
delay();
delay();
delay();
delay();
cmd(0X30);
delay();
delay();
delay();
cmd(0X30);
delay();
delay();
delay();

cmd(0X38);
delay();
cmd(0X01);
//

delay();
cmd(0X0C);

//

delay();
cmd(0X06);

//

delay();

ECE Department
Engineering, Trikaripur

15

College of

EC 17L1 Embedded Systems Laboratory


cmd(0X83);
//

delay();

for(i=0;i<5;i++)
{
P0=nam[i];
dat(nam[i]);
}
cmd(0xc0);
print("CETKR");
while(1);
}

RESULT:
The program for interfacing LCD display with 8051 microcontroller was written
and executed successfully.

ECE Department
Engineering, Trikaripur

16

College of

EC 17L1 Embedded Systems Laboratory

SCHEMATIC DIAGRAM:

ECE Department
Engineering, Trikaripur

17

College of

EC 17L1 Embedded Systems Laboratory


Exp No: 4

INTERRUPT PROGRAMMING
AIM:
To program the external interrupt of 8051 microcontroller.

THEORY:
For the 8051 Microcontroller there are six interrupt sources as shown in the table below: Interrupt
Reset
External HW Interrupt 0 (INT0)
Timer 0 Interrupt (TF0)
External HW Interrupt 1 (INT1)
Timer 1 Interrupt (TF1)
Serial Comm. Interrupt (RI & TI)

ROM Location
0000
0003
000B
0013
001B
0023

Pin
9
P3.2(12)
P3.3(13)
-

Interrupt No. in C
--0
1
2
3
4

Enabling and Disabling the Interrupts


When the MCU is reset, all the interrupts are disabled. Hence in order to use them, we
should enable them. In 8051 Interrupt Enable (EA) Register is used to enable or disable the
interrupt. The register is shown below:
D7
EA

D6
-

D5
ET2

D4
ES

D3
ET1

D2
EX1

D1
ET0

D0
EX0

EA: 0; Disables all interrupts. 1; Enables all interrupts. It is kind of master control,
before enabling any of the interrupts this bit should be 1. For all the below
interrupts, setting (1) the bit enables the interrupt, 0 disables it.
ET2: Timer 2 Overflow interrupt(8052)
ES: Serial Port Interrupt
ET1:Timer 1 overflow interrupt
EX1:External Interrupt 1 on P3.3
ET0:Timer 0 overflow interrupt
EX0:External Interrupt 0 on P3.2

PROGRAM:
/*BUZZER IS CONNECTED TO 0TH PIN OF PORT1. A SWITCH IS CONNECTED
TO EXTERNAL INTERRUPT PIN OF CONTROLLER*/

ECE Department
Engineering, Trikaripur

18

College of

EC 17L1 Embedded Systems Laboratory


#include

<reg52.h>

sbit BUZ = P1^0;


void ext_int(void)interrupt 0
{
BUZ ^= 1;
}
int main(void)
{
EA = 1;
EX0 = 1;
IT0 = 1;
BUZ=1;
while(1);
}

RESULT:
The program for external interrupt was written and executed successfully.

ECE Department
Engineering, Trikaripur

19

College of

EC 17L1 Embedded Systems Laboratory

CIRCUIT DIAGRAM:

ECE Department
Engineering, Trikaripur

20

College of

EC 17L1 Embedded Systems Laboratory

Exp No: 5

UART COMMUNICATION WITH 8051


AIM:
To transfer the data serially between 8051 microcontroller and PC using serial port (RS
232).

THEORY:
UART (Universal Asynchronous Receiver Transmitter), a hardware component
which are available in almost every microcontroller is used to transmit a byte of data
serially through a single wire. It converts a parallel sequence of data into serial data.
There are two ways to connect a microcontroller to a computer.
Using RS232 protocol to the legacy serial port.
Using a USB to serial convertor.
From microcontroller programming point of view, there is fundamentally no difference.
The communication between the computer and microcontroller is asynchronous and full
duplex.
Asynchronous: There is no fixed clock to synchronize transmission of data; rather a
fix bit rate is specified at both transmitter and receiver, termed as Baud Rate. In
simple words, it specifies number of bit received/transmitted in 1 second. With that
transmitter and receiver get to know time duration for each bit transfer.
Full Duplex: Both computer and microcontroller have the hardware capability to
transmit and receive at the same time
To perform communication between a PC and devices supporting UART, a line driver is
required which converts a line voltage (RS232 voltage level in serial port of PC) to TTL
level. One of most commonly used line driver is MAX232.
The protocol
RS232: It is a legacy standard for serial communication. The standard defines the electrical
characteristics and timing of signals, the meaning of signals, and the physical size and pinout of connectors.
RS232 Signals

ECE Department
Engineering, Trikaripur

21

College of

EC 17L1 Embedded Systems Laboratory


There are various signals for handshaking and communication in RS232 protocol. These are
hardly used with modern computers, here we use only Transmit (Tx) and Receive
(Rx) Signals.
Voltage Levels
Table below shows the voltage levels for RS232. They are significantly different from the
TTL high (5V) and Low (0V). Hence a line driver like the MAX232 is required.
Logic Level Voltages(V)
0

+3 to +25V

-3 to -25V

+3 to -3V

Serial Interface
AT89s52 provides synchronous and asynchronous communication modes. It
operates as a UART in three full-duplex modes (Mode1, Mode 2, and Mode3). Data
transfer rates known as Baud Rate in serial communication are measured in bps (bits per
second).Standard Baud Rate are multiple of 9600bps. Among 4 communication mode
(Mode0, Mode1, Mode2, Mode3), Mode 2 is mostly used.
The important registers for serial communication are:
SBUFF: Serial buffer register
SBUFF, it is a serial buffer for data. It holds 8 bit data that needs to be transmitted or the
data that is received. Since the serial port of 8051 is full duplex, the microcontroller
internally manages to transmit and receive data into the buffer register simultaneously.

SBUFF
D7

D6

D5

D4

D3

D2

D1

D0

SCON: Serial Control Register


SCON; Serial Control register SCON is also an 8 bit register used for configuring different
modes for serial communication as described below.

SCON
ECE Department
Engineering, Trikaripur

22

College of

EC 17L1 Embedded Systems Laboratory

D7

D6

D5

D4

D3

D2

D1

D0

SM0

SM1

SM2

REN

TB8

RB8

TI

RI

SM0 - Serial port mode bit 0 is used for serial port mode selection.

SM1 - Serial port mode bit 1.

SM
0

SM
1

Operatio
n

Description

Baud Rate Source

Mode 0

8 bit Shift Reg

1/12 the quartz frequency

Mode 1

8 bit UART

Determined by the timer 1

Mode 2

9 bit UART

1/32 the quartz frequency

Mode 3

9 bit UART

Determined by the timer 1

SM2 - Serial port mode 2 bit, also known as multiprocessor communication enable
bit. When set, it enables multiprocessor communication in mode 2 and 3, and
eventually mode 1. It should be cleared in mode 0.

REN - Reception Enable bit enables serial reception when set. When cleared, serial
reception is disabled.

TB8 - Transmitter bit 8. Since all registers are 8-bit wide, this bit solves the problem
of transmitting the 9th bit in modes 2 and 3. It is set to transmit a logic 1 in the 9th
bit.

RB8 - Receiver bit 8 or the 9th bit received in modes 2 and 3. Cleared by hardware
if 9th bit received is a logic 0. Set by hardware if 9th bit received is a logic 1.

TI - Transmit Interrupt flag is automatically set at the moment the last bit of one
byte is sent. It's a signal to the processor that the line is available for a new byte
transmit. It must be cleared from within the software.

RI - Receive Interrupt flag is automatically set upon one byte receive. It signals that
byte is received and should be read quickly prior to being replaced by a new data.
This bit is also cleared from within the software.

Generating Baud Rate


Both transmitter and receiver should agree upon a fixed data transfer rate known as baud
rate. For 8051 the Timer 1 is used to generate the baud rate in Auto reload mode.

ECE Department
Engineering, Trikaripur

23

College of

EC 17L1 Embedded Systems Laboratory

Fig: Baud rate at 11.0592 MHz Crystal

The crystal frequency Fclk is divided by 12 internally which is used to execute instructions
also known as Machine Clock, Mclk. The timer again divides the Mclk by 32 and uses it as
timer frequency, say Tclk.
Usually an 11.0592 MHz crystal oscillator is used to provide clock to 8051.
To generate Baud Rate for example 9600bps, we first divide Fclk freq by 12. The obtained
value is again divided by 32. Now, the obtained value is divided by desired baud rate. The
negation of obtained result is then placed in TH1 register for generation of Baud Rate.

11.0592MHZ / 12 = 921600HZ
921600HZ / 32 = 28800HZ
28800HZ / 9600 = 3
Thus, -3 (decimal value) is to be loaded in TH1 register. To obtain Hexa Decimal value add
decimal value to FFh and again add 1.
eg:- FFh 3 + 1 = FDh
The standard baud rates used for serial communication are shown in the table below. All the
baud rates are factors or multiples of the Tclk (28.8 KHz).

ECE Department
Engineering, Trikaripur

Baud Rate

Multiple (or Factor)

2400

Tclk/12

4800

Tclk/6

9600

Tclk/3

115200

Tclk*4

14400

Tclk*5
24

College of

EC 17L1 Embedded Systems Laboratory

ALGORITHM:
To serially transmit a byte of data:1. First, TMOD reg is loaded with value of 20h (Timer 1in Mode 2 to set Baud Rate).
2. Then, TH1 is loaded with required baud rate value as calculated above.
3. SCON reg is loaded with 50h (Mode 1 serial communication).
4. TR1 is set to start timer.
5. TI is cleared.
6. The data to be transferred is placed in SBUF reg.
7. Then TI flag is monitored. If TI flag is set, then a byte of data is transmitted
successfully.

To serially receive a byte of data:1.


2.
3.
4.
5.
6.
7.

First, TMOD reg is loaded with value of 20h (Timer 1in Mode 2 to set Baud Rate).
Then, TH1 is loaded with required baud rate value as calculated above.
SCON reg is loaded with 50h (Mode 1 serial communication).
TR1 is set to start timer.
RI is cleared.
Then RI flag is monitored. If RI flag is set, then a byte of data is received serially.
The received byte is stored in SBUF reg and needs to be transferred to another
register or location to receive another byte of data.

PROGRAM:
#include<reg52.h>
void UART_Init()
{
TMOD |=0x20; //Timer1 in Mode2.
TH1=-3; // 9600 Baud rate at 11.0592MHz
SCON=0x50; // Asynchronous mode 8-bit data and 1-stop bit
TR1=1; //Turn ON the timer.
}
char UART_RxChar()
{
while(RI==0);
// Wait till the data is received
ECE Department
Engineering, Trikaripur

25

College of

EC 17L1 Embedded Systems Laboratory


RI=0;
// Clear Receive Interrupt Flag for next cycle
return(SBUF);
// return the received char
}
void UART_TxChar(char ch)
{
SBUF=ch;
// Load the data to be transmitted
while(TI==0); // Wait till the data is trasmitted
TI=0;
//Clear the flag for next cycle.
}
void main()
{
char msg[50];
UART_Init();

// function to initialzie UART

while(1)
{
UART_TxString("\n\n\rEnter a new String: ");
UART_RxString(msg);
UART_TxString("\n\rEntered String: ");
UART_TxString(msg); //Transmit the received string
}
}

RESULT:
The program for transferring data serially between 8051 microcontroller and PC was
written and executed successfully.

ECE Department
Engineering, Trikaripur

26

College of

EC 17L1 Embedded Systems Laboratory

SCHEMATIC DIAGRAM:

ECE Department
Engineering, Trikaripur

27

College of

EC 17L1 Embedded Systems Laboratory

Exp No: 6

I2C COMMUNICATION WITH 8051


AIM:
To write a program to read, write and validate data from a serial EPROM.

THEORY:
The term I2C is an inter integrated circuit and it is called as I squared C. I2C is a
serial computer bus, which is invented by NXP semiconductors previously it is named as
Philips semiconductors. The I2C bus is used to attach low speed peripheral integrated
circuits to microcontrollers and processors.
I2c bus uses two bidirectional open-drain lines such as SDA (serial data line) and
SCl (serial clock line) and these are pulled up with resistors. I2C bus permits a master
device to start communication with a slave device. Data is interchanged between these two
devices. Typical voltages used are +3.3V or +5V although systems with extra voltages are
allowed.

ECE Department
Engineering, Trikaripur

28

College of

EC 17L1 Embedded Systems Laboratory

Interfacing I2C EEPROM


I2C is a master- slave protocol, which includes the data along with clock pulse.
Typically, the master device switch the clock line, SCL. This line orders the data timing which
transfers on the I2C bus. Unless the clock is operated, no data will be transferred. All slaves are
controlled by the same clock, SCL. I2C bus supports various devices where each device is
identified by a unique address whether it is an LCD driver, memory card, microcontroller
or interfacing of keyboard which can operate as Tx or Rx depends on the functioning of the
device. The controller is designed to control the EEPROM device via I2C protocol. Here, he
I2C protocol works as a master device and regulates EEPROM and it works as a slave. The
R/W operations are proficient by transferring a set of control signals comprising the address
AND/ OR data bus. These signals should be attended with suitable clock signals.
In order to Read, Write and Erase EEPROM by using I2C bus in 8051 board. The
operation of this interfacing is to send a signal like WRITE, followed by data and address bus.
In this operation, the EEPROM is used to store the data. In 8051 kit, two numbers of EEPROM
lines are regulated by I2C supported drivers. The SCL and SDA are connected to the I2C based
serial EEPROM IC. By using SDA and SCL I2C lines, the read and write operations of
EEPROM are done in 8051 Kit.

PROGRAM:
#include <reg52.h>
#include "i2c.h"
#include
"lcd.h"
int main(void){
lcd_init();
printlcd(" HAI ");
lcd_command(0xc0);
printlcd(" BFORGE

");

ECE Department
Engineering, Trikaripur

29

College of

EC 17L1 Embedded Systems Laboratory

write(1,'A');
//DelayMs(10);
write(0,'B');
//DelayMs(10);
write(2,'C');
//DelayMs(10);
lcd_command(0xc0);
lcd_data(read(0));
lcd_data(read(1));
lcd_data(read(2));
while(1);
}
#include"i2c.h"
void start(void){
SDA = 1;
SCL = 1;
SDA = 0;
SCL = 0;
}
char out(char s_data){
char s_cnt=0;
while(s_cnt++ < 8){
if((s_data & 0x80)==0)SDA = 0;
else SDA = 1;
SCL = 1;
SCL = 0;
s_data <<=1;
}
SDA = 1;
SCL = 1;
SCL = 0;
return SDA;
}
char in(void){
char in_data=0;
char s_cnt=0;
SDA = 1;
while(s_cnt++ < 8){
ECE Department
Engineering, Trikaripur

30

College of

EC 17L1 Embedded Systems Laboratory


SCL = 0;
SCL = 1;
in_data <<=1;
in_data |= SDA;
}
SCL = 0;
return in_data;
}
void stop(void){
SDA = 0;
SCL = 1;
SDA = 1;
}
void write(char w_loc,char w_data){
while(!ext_eeprom_ready());
start();
out(DEV_W);
out(w_loc);
out(w_data);
stop();
}
char ext_eeprom_ready() {
char ack;
start();
// If the write command is acknowledged,
ack = out(DEV_W); // then the device is ready.
stop();
return !ack;
}
char read(char r_loc){
char r_data=0;
start();
out(DEV_W);
out(r_loc);
stop();
start();
out(DEV_R);
r_data = in();
stop();
return r_data;
ECE Department
Engineering, Trikaripur

31

College of

EC 17L1 Embedded Systems Laboratory


}
#include
#include

<reg52.h>
"lcd.h"

#define

LCD_STROBE()

#define

lcd_goto(pos) lcd_write(0x80+(pos))

LCD_EN = 1;LCD_EN=0

void DelayMs(char del){


char ms=120;
while(del--){
while(ms--);
}
}
void
lcd_data(unsigned char c)
{
LCD_RS = 1;
//
LCD_RW = 0;
P0 = c;
LCD_STROBE();
DelayMs(3);
}
/* write a string of chars to the LCD */
void printlcd(const char * s){
while(*s)
lcd_data(*s++);
}
/* write one character to the LCD */
void lcd_command(unsigned char cmd){
LCD_RS = 0;
//
LCD_RW = 0;
P0 = cmd;
LCD_STROBE();
DelayMs(3);
}
ECE Department
Engineering, Trikaripur

32

College of

EC 17L1 Embedded Systems Laboratory

/* initialise the LCD - put into 4 bit mode */


void lcd_init(void){
LCD_RS = 0; // write control bytes
//
LCD_RW = 1; // write control bytes
DelayMs(15); // power on delay
P0 = 0x30;
// attention!
LCD_STROBE();
DelayMs(5);
LCD_STROBE();
DelayMs(1);
LCD_STROBE();
DelayMs(1);
lcd_command(0x38); // 4 bit mode, 1/16 duty, 5x8 font
//

lcd_command(0x0F); // display on, blink curson on


lcd_command(0x0C); // display on, blink curson on
lcd_command(0x06); // entry mode
lcd_command(0x80); // entry mode
lcd_command(0x01); // display clear
}

RESULT:
The program to read, write and validate data from a serial EEPROM was written and
executed successfully.

CIRCUIT DIAGRAM:

ECE Department
Engineering, Trikaripur

33

College of

EC 17L1 Embedded Systems Laboratory

ECE Department
Engineering, Trikaripur

34

College of

EC 17L1 Embedded Systems Laboratory


Exp No: 7

ADC INTERFACING
AIM:
To interface an ADC0804 to the 8051 microcontroller.

THEORY:
ADC0804 is an 8-bit successive approximation ADC which works with +5V dc
voltage and has a resolution of 8 bits. The device is fed with differential analog input
voltage which in turn provides output in form of digital signal which can be processed by
any processor. The conversion time of ADC0804 cannot be faster than 110 microseconds
and it has on-chip clock generator which can be used to provide clock pulse required for the
operation of this device.
The pin layout is shown in figure below and the description of each pins are then
described.

Pin Layout of ADC0804

CS: - This is an active low input pin. When this pin is at LOW logic level (i.e. logic 0),
ADC0804 is ready to operate.
WR:-This is an active low input pin. A LOW-to-HIGH pulse at this pin tells the ADC to
start the conversion of applied analog input signal.
INTR: - This is an active low output pin to indicate the end of conversion.
RD: - This is an active low input pin. After the end of conversion the converted digital
signal is stored in the internal register of ADC0804 chip. Before the data is lost it is
required to read the digital data from those internal register. By sending a HIGH-to-LOW
pulse at this pin, we can read the digital data through 8-bit parallel pins ofADC0804.

ECE Department
Engineering, Trikaripur

35

College of

EC 17L1 Embedded Systems Laboratory


CLK IN: - This is an input pin to provide a clock source to ADC0804 required for
conversion when an external clock source is used.
CLK R: - To use on chip clock generator of ADC0804, the CLK IN and CLK R pins needs
to be connected with RC circuit as shown in figure below.

On chip clock generator circuitry

The suitable value of R and C used in the circuitry is 10k and 150pF respectively. But its
not any hard rule to use the same value of R and C, any value of R and C can be used which
can generate approximately 110us of conversion time.
Vin: - These (Vin+ and Vin-) are the pin where you apply differential analog input from any
sensor.
Vref/2:- This is the reference voltage pin. If this pin is left not connected then the reference
voltage will automatically become 5V. But for application where you require reference
voltage to be less then 5V, an external source of appropriate voltage should be connected to
this pin. A voltage divider circuit can be used to obtain the required reference voltage by
utilizing the same power source which microcontroller and ADC0804 are using.
AGND: - This is analog ground pin where a ground of analog input source is connected.
Similarly, in DGND which is referred to as digital ground, the ground of working voltage
source of the MCU or ADC is connected.
D0-D7:- These are 8-bit parallel data pins of ADC0804.

ALGORITHM:
1.
2.
3.
4.

Send LOW level signal (LOGIC 0) to CS pin.


Send a LOW-to-HIGH pulse to WR pin to initiate the conversion.
Wait until INTR pin is at logic 0.
When INTR pin is at logic 0, send HIGH-to-LOW pulse to RD pin and then read the
data from internal registers of ADC0804 using D0-D7 data pins.

PROGRAM:
ECE Department
Engineering, Trikaripur

36

College of

EC 17L1 Embedded Systems Laboratory


/*NO INBUILT ADC IN 8051 SO OUTPUT PINS OF ADC0804 IS CONNECTED
TO PORT2*/
#include<reg52.h>
#include"delay.h"

sbit LCD_EN = P3^7;


CONNECTED TO 7TH PIN OF PORT3

//ENABLE PIN OF LCD IS

sbit LCD_RS = P3^6;


OF
LCD IS CONNECTED TO 6TH PIN OF PORT3

//REGISTER SELECT PIN

char ar[5];
int i,k,l,val=0;

void delay()
{
for(k=0;k<2;k++)
for(l=0;l<255;l++);
}

void dtoa(unsigned long value,char *buf,char n){


buf[n] = 0;
while(n--){
buf[n] = (value%10)+'0';
value /= 10;
}
}

void cmd(char temp)


{
LCD_RS = 0;
ECE Department
Engineering, Trikaripur

//Data/cmd reg
37

College of

EC 17L1 Embedded Systems Laboratory


P0=temp;
LCD_EN =1;

//enable LCD

delay();
LCD_EN =0;
}
void dat(char temp)
{
LCD_RS = 1;
P0=temp;
LCD_EN =1;
delay();
LCD_EN =0;
}

void print(char *ptr)


{
while(*ptr!=0)
{
dat(*ptr);
ptr++;
}
}

void lcd_init()
{
cmd(0X30);
delay();
delay();
delay();
ECE Department
Engineering, Trikaripur

38

College of

EC 17L1 Embedded Systems Laboratory


delay();
cmd(0X30);
delay();
delay();
delay();
cmd(0X30);
delay();
delay();
delay();

cmd(0X38);
delay();
cmd(0X01);
//

delay();
cmd(0X0C);

//

delay();
cmd(0X06);

//

delay();
cmd(0X83);

//

delay();

void main()
{
lcd_init();
cmd(0x80);
print("ADC");
//RC3 = 0;
while(1)
ECE Department
Engineering, Trikaripur

39

College of

EC 17L1 Embedded Systems Laboratory


{
val=P2;
delay();
dtoa(val,ar,4);
cmd(0xc0);
print(ar);

}
}

RESULT:
The program to interface an ADC0804 to the 8051 microcontroller was written and
executed successfully.

ECE Department
Engineering, Trikaripur

40

College of

EC 17L1 Embedded Systems Laboratory

SCHEMATIC DIAGRAM

Unipolar Stepper Motor Winding

ECE Department
Engineering, Trikaripur

41

College of

EC 17L1 Embedded Systems Laboratory


Exp No: 8

STEPPER MOTOR INTERFACING


AIM:
To interface a stepper motor to 8051.

THEORY:
A Stepper Motor is a brushless, synchronous DC Motor. It has many applications in
the field of robotics and mechatronics. The total rotation of the motor is divided into steps.
The angle of a single step is known as the stepper angle of the motor. There are two types
of stepper motors Unipolar and Bipolar. Due to the ease of operation unipolar stepper
motor is commonly used. Stepper Motors can be easily interfaced with a microcontroller
using driver ICs such as L293D or ULN2003.Unipolar stepper motors can be used in three
modes namely the Wave Drive, Full Drive and Half Drive mode.
Wave Drive
In this mode only one electromagnet is energized at a time. Generated torque will be
less when compared to full drive in which two electromagnets are energized at a time but
power consumption is reduced. It has same number of steps as in the full drive. This drive
is preferred when power consumption is more important than torque. It is rarely used.
Wave Drive Stepping Sequence
Step

Full Drive
In this mode two electromagnets are energized at a time, so the torque generated
will be larger when compared to Wave Drive. This drive is commonly used than others.
Power consumption will be higher than other modes.
Full Drive Stepping Sequence
Step

ECE Department
Engineering, Trikaripur

42

College of

EC 17L1 Embedded Systems Laboratory

Half Drive
In this mode alternatively one and two electromagnets are energized, so it is a
combination of Wave and Full drives. This mode is commonly used to increase the angular
resolution of the motor but the torque will be less, about 70% at its half step position. We
can see that the angular resolution doubles when using Half Drive.
Half Drive Stepping Sequence
Step

PROGRAM:
#include<reg52.h>
int k;
void DelayMs(long d)
{
int i,j;
for(i=0;i<d;i++)
{
ECE Department
Engineering, Trikaripur

43

College of

EC 17L1 Embedded Systems Laboratory


for(j=0;j<255;j++);
}
}
void main()
{
while(1){
P1=0XFD;
DelayMs(3);
P1=0XF7;
DelayMs(3);
P1=0XFB;
DelayMs(3);
P1=0XEF;
DelayMs(3);
}
}
//

P1=0XEF;

//

DelayMs(3);

//

P1=0XFB;

//

DelayMs(3);

//

P1=0XF7;

//

DelayMs(3);

//

P1=0XFD;

//

DelayMs(10);

//
//

//order for anti_clockwise rotation

}
}

RESULT:
ECE Department
Engineering, Trikaripur

44

College of

EC 17L1 Embedded Systems Laboratory


The program to interface a stepper motor to the 8051 was written and executed
successfully.

ECE Department
Engineering, Trikaripur

45

College of

Вам также может понравиться