43
8051 Microcontroller and Interfacing Experiments Introduction to 8051 Microcontroller INTRODUCTION A microcontroller is a small computer on a single integrated circuit containing a processor core, memory, and programmable input/output peripherals. Microcontrollers are designed for embedded applications, in contrast to the microprocessors used in personal computers or other general purpose applications. But their presence is unnoticed at most of the places like At supermarkets in Cash Registers, Weighing Scales, etc. At home in Ovens, Washing Machines, Alarm Clocks, etc. At play in Toys, VCRs, Stereo Equipment, etc. At office in Typewriters, Photocopiers, Elevators, etc. In industry in Industrial Automation, safety systems, etc. On roads in Cars, Traffic Signals, etc. What inside them makes these machines smart? The answer is microcontroller. Creating applications for the microcontrollers is different than any other development job in electronics and computing. Before selecting a particular device for an application, it is important to understand what the different options and features are and what they can mean with regard to developing the application. By C. Chandra Mouli, Research Scholar, Department of Instrumentation, Sri Krishnadevaraya University, Anantapur. Mobile: +919963719606, email: [email protected] 1

8051 dev board guide

Embed Size (px)

Citation preview

Page 1: 8051 dev board guide

8051 Microcontroller and Interfacing Experiments

Introduction to 8051 MicrocontrollerINTRODUCTION

A microcontroller is a small computer on a single integrated circuit containing a

processor core, memory, and programmable input/output peripherals. Microcontrollers are

designed for embedded applications, in contrast to the microprocessors used in personal

computers or other general purpose applications. But their presence is unnoticed at most of the

places like

At supermarkets in Cash Registers, Weighing Scales, etc.

At home in Ovens, Washing Machines, Alarm Clocks, etc.

At play in Toys, VCRs, Stereo Equipment, etc.

At office in Typewriters, Photocopiers, Elevators, etc.

In industry in Industrial Automation, safety systems, etc.

On roads in Cars, Traffic Signals, etc.

What inside them makes these machines smart? The answer is microcontroller.

Creating applications for the microcontrollers is different than any other development job

in electronics and computing. Before selecting a particular device for an application, it is

important to understand what the different options and features are and what they can mean with

regard to developing the application.

The reminder of the book will go through different types of programming and interfacing

techniques of microcontroller, mainly 8051 based microcontrollers, in detail.

COMPARING MICROPROCESSOR AND MICROCONTROLLER

Microprocessor is a single chip CPU, microcontroller contains, a CPU and much of the

remaining circuitry of a complete microcomputer system in a single chip.

Microcontroller includes RAM, ROM, serial and parallel interface, timer, interrupt

schedule circuitry (in addition to CPU) in a single chip.

RAM is smaller than that of even an ordinary microcomputer, but enough for its

applications.

Interrupt system is an important feature, as microcontrollers have to respond to

control oriented devices in real time.

ByC. Chandra Mouli, Research Scholar, Department of Instrumentation, Sri Krishnadevaraya University, Anantapur.

Mobile: +919963719606, email: [email protected]

1

Page 2: 8051 dev board guide

8051 Microcontroller and Interfacing Experiments

Microprocessors are most commonly used as the CPU in microcomputer systems.

Microcontrollers are used in small, minimum component designs performing control-

oriented activities.

Microprocessor instruction sets are processing intensive, implying powerful addressing

modes with instructions catering to large volumes of data. Their instructions operate on

nibbles, bytes, etc. Microcontrollers have instruction sets catering to the control of inputs

and outputs. Their instructions operate also on a single bit. E.g., a motor may be turned

ON and OFF by a 1-bit output port.

FEATURES OF 8051 MICROCONTROLLER

The Intel 8051 microcontroller is one of the most popular general purpose

microcontrollers in use today. The microcontroller 8051 is 8-bit microcontroller which includes:

CPU (Central Processing Unit), RAM (Random Access Memory), EPROM/PROM/ROM

(Erasable programmable Read Only Memory), I/O (input/output) – serial and parallel, timers,

interrupt controllers.

Figure1: Block Diagram of 8051 Microcontroller

By only including the features specific to the task (control), cost is relatively low. A

typical microcontroller has bit manipulation instructions, easy and direct access to I/O

ByC. Chandra Mouli, Research Scholar, Department of Instrumentation, Sri Krishnadevaraya University, Anantapur.

Mobile: +919963719606, email: [email protected]

2

Page 3: 8051 dev board guide

8051 Microcontroller and Interfacing Experiments

(input/output), and quick and efficient interrupt processing. Figure1 shows the block diagram of

a typical microcontroller.

Basic features

8-bit CPU with register A and B.

8-bit data bus.

16-bit address bus.

32 general purpose registers each of 8 bits.

16 bit timers (usually 2, but may have more, or less).

Bit as well as byte addressable RAM area of 16 bytes.

Four 8-bit ports, (short models have two 8-bit ports).

16-bit program counter and data pointer.

8-bit stack pointer and 8-bit PSW (Program Status Word).

Internal ROM (4kB) and RAM (128 Bytes).

Serial data communication.

Five Interrupt Sources (2 internal and 3 external interrupts).

Basic pins

Figure2 shows the pin diagram of 8051 micro controller.

PIN 9: PIN 9 is the reset pin which is used reset the microcontroller’s internal registers

and ports upon starting up. (Pin should be held high for 2 machine cycles.).

PINS 18 & 19: The 8051 has a built-in oscillator amplifier hence we need to only

connect a crystal at these pins to provide clock pulses to the circuit.

PIN 40 and 20: Pins 40 and 20 are VCC and ground respectively. The 8051 chip needs

+5V 500mA to function properly, although there are lower powered versions like the

Atmel 2051 which is a scaled down version of the 8051 which runs on +3V.

ByC. Chandra Mouli, Research Scholar, Department of Instrumentation, Sri Krishnadevaraya University, Anantapur.

Mobile: +919963719606, email: [email protected]

3

Page 4: 8051 dev board guide

8051 Microcontroller and Interfacing Experiments PINS 29, 30 & 31: As described in the features of the 8051, this chip contains a built-in

flash memory. In order to program this we need to supply a voltage of +12V at pin 31. If

external memory is connected then PIN 31, also called EA/VPP, should be connected to

ground to indicate the presence of external memory. PIN 30 is called ALE (address latch

enable), which is used when multiple memory chips are connected to the controller and

only one of them needs to be selected. We will deal with this in depth in the later

chapters. PIN 29 is called PSEN. This is "program select enable". In order to use the

external memory it is required to provide the low voltage (0) on both PSEN and EA pins.

ByC. Chandra Mouli, Research Scholar, Department of Instrumentation, Sri Krishnadevaraya University, Anantapur.

Mobile: +919963719606, email: [email protected]

4

Page 5: 8051 dev board guide

8051 Microcontroller and Interfacing Experiments

Figure2: Pin Diagram of 8051 microcontroller

Port Pins

There are four 8-bit ports: P0, P1, P2 and P3.

PORT P1 (Pins 1 to 8): The port P1 is a general purpose input/output port which can be

used for a variety of interfacing tasks. The other ports P0, P2 and P3 have dual roles or

additional functions associated with them based upon the context of their usage.

ByC. Chandra Mouli, Research Scholar, Department of Instrumentation, Sri Krishnadevaraya University, Anantapur.

Mobile: +919963719606, email: [email protected]

5

Page 6: 8051 dev board guide

8051 Microcontroller and Interfacing Experiments PORT P3 (Pins 10 to 17): PORT P3 acts as a normal IO port, but Port P3 has additional

functions such as, serial transmit and receive pins, 2 external interrupt pins, 2 external

counter inputs, read and write pins for memory access.

PORT P2 (pins 21 to 28): PORT P2 can also be used as a general purpose 8 bit port

when no external memory is present, but if external memory access is required then

PORT P2 will act as an address bus in conjunction with PORT P0 to access external

memory. PORT P2 acts as A8-A15, as can be seen from fig 1.1

PORT P0 (pins 32 to 39) PORT P0 can be used as a general purpose 8 bit port when no

external memory is present, but if external memory access is required then PORT P0 acts

as a multiplexed address and data bus that can be used to access external memory in

conjunction with PORT P2. P0 acts as AD0-AD7, as can be seen from Figure2.

Data and Program Memory

The 8051 Microprocessor can be programmed in PL/M, 8051 Assembly, C and a number

of other high-level languages. Many compilers even have support for compiling C++ for an

8051. Program memory in the 8051 is read-only, while the data memory is considered to be

read/write accessible. When stored on EEPROM or Flash, the program memory can be rewritten

when the microcontroller is in the special programmer circuit. Program memory in the 8051 is

read-only, while the data memory is considered to be read/write accessible. When stored on

EEPROM or Flash, the program memory can be rewritten when the microcontroller is in the

special programmer circuit.

Program Start Address

The 8051 starts executing program instructions from address 0x00 in the program

memory.

Direct Memory

ByC. Chandra Mouli, Research Scholar, Department of Instrumentation, Sri Krishnadevaraya University, Anantapur.

Mobile: +919963719606, email: [email protected]

6

Page 7: 8051 dev board guide

8051 Microcontroller and Interfacing ExperimentsThe 8051 has 256 bytes of internal addressable RAM, although only the first 128 bytes

are available for general use by the programmer. The first 128 bytes of RAM (from 0x00 to

0x7F) are called the Direct Memory, and can be used to store data.

Special Function Register (SFR)

The Special Function Register (SFR) is the upper area of addressable memory, from

address 0x80 to 0xFF. A, B, PSW, DPTR are called SFR. This area of memory cannot be used

for data or program storage, but is instead a series of memory-mapped ports and registers. All

port input and output can therefore be performed by memory mov operations on specified

addresses in the SFR. Also, different status registers are mapped into the SFR, for use in

checking the status of the 8051, and changing some operational parameters of the 8051.

General Purpose Registers

The 8051 has 4 selectable banks of 8 addressable 8-bit registers, R0 to R7. This means

that there are essentially 32 available general purpose registers, although only 8 (one bank) can

be directly accessed at a time. To access the other banks, we need to change the current bank

number in the flag status register.

A and B Registers

The A register is located in the SFR memory location 0xE0. The A register works in a

similar fashion to the AX register of x86 processors. The A register is called the accumulator,

and by default it receives the result of all arithmetic operations. The B register is used in a

similar manner, except that it can receive the extended answers from the multiply and divide

operations. When not being used for multiplication and Division, the B register is available as an

extra general-purpose register.

ByC. Chandra Mouli, Research Scholar, Department of Instrumentation, Sri Krishnadevaraya University, Anantapur.

Mobile: +919963719606, email: [email protected]

7

Page 8: 8051 dev board guide

8051 Microcontroller and Interfacing Experiments

Programming with P89V51RD2

INTRODUCTION

The P89V51RD2 is an 80C51 microcontroller with 64kB Flash and 1024 bytes of data

RAM. A key feature of the P89V51RD2 is its X2 mode option. The design engineer can choose

to run the application with the conventional 80C51 clock rate (12 clocks per machine cycle) or

select the X2 mode (6 clocks per machine cycle) to achieve twice the throughput at the same

clock frequency. Another way to benefit from this feature is to keep the same performance by

reducing the clock frequency by half, thus dramatically reducing the EMI.

The Flash program memory supports both parallel programming and in serial In-System

Programming (ISP). Parallel programming mode offers gang-programming at high speed,

reducing programming costs and time to market. ISP allows a device to be reprogrammed in the

end product under software control. The capability to field/update the application firmware

makes a wide range of applications possible.

The P89V51RD2 is also In-Application Programmable (IAP), allowing the Flash

program memory to be reconfigured even while the application is running. Figure3 shows the

photograph picture of 8051 development board.

Figure3: 8051 Development Board

The hardware required to develop projects is 8051 development board and it has the

following features:

ByC. Chandra Mouli, Research Scholar, Department of Instrumentation, Sri Krishnadevaraya University, Anantapur.

Mobile: +919963719606, email: [email protected]

8

Page 9: 8051 dev board guide

8051 Microcontroller and Interfacing Experiments P89V51RD2BN 40 Pin DIP.

Both Manual and Power on Reset.

16 x 2 Alphanumeric LCD and trimmer for contrast Control.

8channel analog input using ADC0809.

4 x 4 hexadecimal keyboard.

RS 232 serial interface.

On board EEPROM using 24C02.

On board RTC using DS1307.

Multiplexed four common anode seven segment displays.

8 Led indication panel for checking the port operation.

1channel relay drive.

8channel uln2803 drive that can be used to drive stepper motor or 8 relays.

Features of P89V51RD2

80C51 Central Processing Unit 5V Operating voltage from 0 to 40MHz.

64 KB of on-chip Flash program memory.

ISP (In-System Programming).

IAP (In-Application Programming).

Enhanced UART.

PCA (Programmable Counter Array) with PWM and Capture/Compare functions.

Four 8-bit I/O ports with three high-current Port 1 pins (16mA each).

Three 16-bit timers/counters Programmable Watchdog timer (WDT).

Eight interrupt sources with four priority levels Second DPTR register.

Low EMI mode (ALE inhibit).

TTL and CMOS compatible logic levels.

Software’s required to develop projects

ByC. Chandra Mouli, Research Scholar, Department of Instrumentation, Sri Krishnadevaraya University, Anantapur.

Mobile: +919963719606, email: [email protected]

9

Page 10: 8051 dev board guide

8051 Microcontroller and Interfacing Experiments1. Keil IDE(Integrated Development Environment)

An integrated development environment (IDE) also known as integrated design

environment or integrated debugging environment is a software application that provides

comprehensive facilities to computer programmers for software development. An IDE

normally consists of:

Source code editor.

Compiler and/or an interpreter.

Build automation tools.

Debugger.

Installing keil software

Open My Computer

Within the my computer window, open the CD Drive, 8051

DEVELOPMENT BOARD CD\DATA\COMPILERS\keil

In this directory keil setup file contains, double click on the file and follow the

instructions.

After installation, if it prompts you to reboot the computer, do it.

2. Flash Magic Programmer

Flash Magic provides a Windows based user interface that is easy and intuitive,

which connects to the target via serial line. Flash Magic is a free, powerful, feature-rich

Windows application that allows easy programming of Philips FLASH Microcontrollers.

Installing flash magic software

Follow the same instructions as guided in the previous section, except locate

the setup file at 8051 DEVELOPMENT BOARD\DATA\PROGRAMMER

ByC. Chandra Mouli, Research Scholar, Department of Instrumentation, Sri Krishnadevaraya University, Anantapur.

Mobile: +919963719606, email: [email protected]

10

Page 11: 8051 dev board guide

8051 Microcontroller and Interfacing Experiments

ByC. Chandra Mouli, Research Scholar, Department of Instrumentation, Sri Krishnadevaraya University, Anantapur.

Mobile: +919963719606, email: [email protected]

11

Page 12: 8051 dev board guide

8051 Microcontroller and Interfacing Experiments

ByC. Chandra Mouli, Research Scholar, Department of Instrumentation, Sri Krishnadevaraya University, Anantapur.

Mobile: +919963719606, email: [email protected]

12

Page 13: 8051 dev board guide

8051 Microcontroller and Interfacing Experiments

ByC. Chandra Mouli, Research Scholar, Department of Instrumentation, Sri Krishnadevaraya University, Anantapur.

Mobile: +919963719606, email: [email protected]

13

Page 14: 8051 dev board guide

8051 Microcontroller and Interfacing Experiments

ByC. Chandra Mouli, Research Scholar, Department of Instrumentation, Sri Krishnadevaraya University, Anantapur.

Mobile: +919963719606, email: [email protected]

14

Page 15: 8051 dev board guide

8051 Microcontroller and Interfacing Experiments

ByC. Chandra Mouli, Research Scholar, Department of Instrumentation, Sri Krishnadevaraya University, Anantapur.

Mobile: +919963719606, email: [email protected]

15

Page 16: 8051 dev board guide

8051 Microcontroller and Interfacing Experiments

ByC. Chandra Mouli, Research Scholar, Department of Instrumentation, Sri Krishnadevaraya University, Anantapur.

Mobile: +919963719606, email: [email protected]

16

Page 17: 8051 dev board guide

8051 Microcontroller and Interfacing Experiments

Lab experiments on P89V51RD2

Experiment No. 1

Aim:To construct an 8-bit binary counter using P89V51RD2 and LEDs, that displays binary

count from 0 to 255.

Schematic Diagram:

Algorithm:1. Connect LEDs to the port1 as shown in the schematic diagram. In the

schematic it is shown that four LEDs are connected. But to display binary counter from 0 to 255 user should connect 8 LEDs.

2. Configure port1 as output port.3. Initialize a variable to 0, equate it to port1 and send it to port1.4. Increment the value and send it to port1 until it reaches 255.5. Write the program in Keil software by using embedded-C language.6. Generate the .hex file and transfer the .hex file from PC to the target board by

using flash magic.

Source Code:

ByC. Chandra Mouli, Research Scholar, Department of Instrumentation, Sri Krishnadevaraya University, Anantapur.

Mobile: +919963719606, email: [email protected]

17

Page 18: 8051 dev board guide

8051 Microcontroller and Interfacing Experiments

/* ----------------------------------------------------------------------- binarycounter.c : Demonstration of interfacing of LEDs Designed for 89c51 running at 11.0592Mhz LED binary counter at rate of 1Hz with crystal of 11.0592Mhz

compile the program in Keil uVision Compiler. Operation : when MCU power up LED binary counter runs at frequency of 1Hz. Feedback appreciated: researchermouli@gmail.com-----------------------------------------------------------------------------*/

//includes definition of 89c51

#include <at89x51.h>void Delay(unsigned int time);void main(void) {

int i;while(1) for(i=0;i<=255;i++)

{P1 = i; // Turn on ledsDelay(500); // delay for 500msec = 0.5 sec

} }

// program routinesvoid Delay(unsigned int time){

unsigned char pause; // Declare variable pause as unsigned charwhile( time > 0) // loop until time is greater than zero{

pause = 150; // Initialize pause to 150 (decimal value)while(pause--); // Decrement pause until it becomes zerotime--; // Decrement time and loop back until time

} // value becomes zero} // End of Delay function

ByC. Chandra Mouli, Research Scholar, Department of Instrumentation, Sri Krishnadevaraya University, Anantapur.

Mobile: +919963719606, email: [email protected]

18

Page 19: 8051 dev board guide

8051 Microcontroller and Interfacing Experiments

Experiment No. 2

Aim:To interface a seven segment display (SSD) with P89V51RD2, that displays hexadecimal

numbers from 0 to F.

Seven segment display:

This version is a common anode version.  That means that the positive leg of each LED is connected to a common point which is pin 3 in this case.  Each LED has a negative leg that is connected to one of the pins of the device.  To make it work you need to connect pin 3 to 5 volts.  Then to make each segment light up, connect the ground pin for that led to ground.   A resistor is required to limit the current.  Rather than using a resistor from each LED to ground, you can just use one resistor from Vcc to pin 3 to limit the current. The following table shows how to form the numbers 0 to 9 and the letters A, b, C, d, E, and F.  '0' means that pin is connected to ground. '1' means that pin is connected to Vcc. 

To Display a (P0.1) b (P0.0) c (P0.6) d (P0.5) e (P0.4) f (P0.2) g (P0.3)

0 0 0 0 0 0 0 1

1 1 0 0 1 1 1 1

2 0 0 1 0 0 1 0

3 0 0 0 0 1 1 0

4 1 0 0 1 1 0 0

5 0 1 0 0 1 0 0

6 0 1 0 0 0 0 0

7 0 0 0 1 1 1 1

ByC. Chandra Mouli, Research Scholar, Department of Instrumentation, Sri Krishnadevaraya University, Anantapur.

Mobile: +919963719606, email: [email protected]

19

Page 20: 8051 dev board guide

8051 Microcontroller and Interfacing Experiments

8 0 0 0 0 0 0 0

9 0 0 0 1 1 0 0

A 0 0 0 1 0 0 0

b 1 1 0 0 0 0 0

C 0 1 1 0 0 0 1

d 1 0 0 0 0 1 0

E 0 1 1 0 0 0 0

F 0 1 1 1 0 0 0

Schematic Diagram of the experiment:

Algorithm:1. Connect SSD to the port0 as shown in the schematic diagram.2. Configure port0 as output port.3. Send the codes shown in table to port0 one by one.4. Write the program in Keil software by using embedded-C language.5. Generate the .hex file and transfer the .hex file from computer to the target

board by using flash magic.

ByC. Chandra Mouli, Research Scholar, Department of Instrumentation, Sri Krishnadevaraya University, Anantapur.

Mobile: +919963719606, email: [email protected]

20

Page 21: 8051 dev board guide

8051 Microcontroller and Interfacing Experiments

Source Code:

/* ----------------------------------------------------------------------- ssd.c : Demonstration of interfacing of SSD Designed for microcontroller running at 11.0592Mhz SSD displays at rate of 1Hz with crystal of 11.0592Mhz

compile the program in Keil uVision Compiler. Operation : when MCU power up SSD runs at frequency of 1Hz. Feedback appreciated: researchermouli@gmail.com-----------------------------------------------------------------------------*/ //includes definition of 89c51

#include<at89x51.h>

void Delay(unsigned int);

void main(void){ unsigned int r ,codes[]= {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x83,0xf8,

0x80,0x98,0x88,0x80,0xc6, 0xc0, 0x86, 0x8e }; P0=0;

while(1){for(r=0; r<=15; r++)

{ P0 = codes[r]; Delay(500);

}

}}

// program routinesvoid Delay(unsigned int time){

unsigned char pause; // Declare variable pause as unsigned charwhile( time > 0) // loop until time is greater than zero{

pause = 150; // Initialize pause to 150 (decimal value)while(pause--); // Decrement pause until it becomes zerotime--; // Decrement time and loop back until time

} // value becomes zero} // End of Delay function

ByC. Chandra Mouli, Research Scholar, Department of Instrumentation, Sri Krishnadevaraya University, Anantapur.

Mobile: +919963719606, email: [email protected]

21

Page 22: 8051 dev board guide

8051 Microcontroller and Interfacing Experiments

Experiment No. 3

Aim:To establish serial communication between P89V51RD2 and PC by programming the

serial port of microcontroller.

Schematic Diagram:

Description of MAX232:

The P89V51RD2 has two pins that are used specifically for transferring and receiving data serially. These two pins are called TxD and RxD and are part of the port3 group (P3.0 and P3.1). Pin 11 of the controller i.e. P3.1 is assigned to TxD and pin 10 i.e. P3.0 is designated as RxD. These pins are TTL compatible; therefore, they require a line driver to make them RS232 compatible. One such line driver is MAX232 chip

Since RS232 is not compatible with today’s microprocessors and microcontrollers, we need a line driver (voltage converter) to convert RS232’s signals to TTL voltage levels that will be acceptable to the controller TxD and RxD pins. One example of such a converter is MAX232 chip that converts RS232 voltage levels to TTL voltage levels, and vice versa. The circuit diagram of interfacing P89V51RD2 and MAX 232 is shown in Schematic diagram.

Algorithm:

1. Initialize TMOD = 0X20 for Timer 1, 8-bit auto reload mode.2. Store TH1 = 0xFD to set 9600 baud rate.3. Load SCON = 0X50 and TR1 = 1 to start timer.4. Transfer the data serially using SBUF register.5. Write the program in Keil software by using embedded-C language.

ByC. Chandra Mouli, Research Scholar, Department of Instrumentation, Sri Krishnadevaraya University, Anantapur.

Mobile: +919963719606, email: [email protected]

22

Page 23: 8051 dev board guide

8051 Microcontroller and Interfacing Experiments6. Generate the .hex file and transfer the .hex file from computer to the target

board by using flash magic.

Source Code:

/*----------------------------------------------------------------------------- serial coomunication.c : Demonstration of serial communication - A ECHO program Designed for P89V51RD2 running at 11.0592Mhz communication between PC and microcontroller. RS232 serial specification 9600 baud rate 8-bit 1- start bit 1-stop bit parity none

compile the program in Keil uVision Compiler. Note: Hyper Terminal / Terminal v1.9b software for communication operation: receives data from PC (or any serial device) and transmitt

same data back to PC- A ECHO program feedback appreciated: researchermouli@gmail.com------------------------------------------------------------------------------*/

#include<at89x51.h> //Includes definition of 89c51

//DEFINE CONSTANT#define Baud_rate 0xFD // BAUD RATE 9600

//DEFINE PROTOTYPESvoid main(void); void SerialInitialize(void);void SendByteSerially(unsigned char ascii);unsigned char ReceiveByteSerially(void);

//FUNCTIONS

void main(void) {

unsigned char serialdata;SerialInitialize(); // CALL ROUTINE TO INITIALIZE SERIAL PORT

while(1) // INFINITE LOOP{serialdata = ReceiveByteSerially();//RECEIVE DATA FROM SERIAL PORT (PC)

SendByteSerially(serialdata); // SEND DATA BACK TO PC}

ByC. Chandra Mouli, Research Scholar, Department of Instrumentation, Sri Krishnadevaraya University, Anantapur.

Mobile: +919963719606, email: [email protected]

23

Page 24: 8051 dev board guide

8051 Microcontroller and Interfacing Experiments} // END OF MAIN FUNCTION

void SerialInitialize(void) // INITIALIZE SERIAL PORT{

TMOD = 0X20; // Timer 1 IN MODE 2 -AUTO RELOAD RATESCON = 0x50; // SERIAL MODE 1, 8-DATA BIT 1-START BIT, 1-STOP BIT

TH1 = Baud_rate; // LOAD BAUDRATE TO TIMER REGISTER

TR1 = 1; // START TIMER}

void SendByteSerially(unsigned char serialdata){

SBUF = serialdata; // LOAD DATA TO SERIAL BUFFER REGISTERwhile(TI == 0); // WAIT UNTIL TRANSMISSION TO COMPLETETI = 0; // CLEAR TRANSMISSION INTERRUPT FLAG

}

unsigned char ReceiveByteSerially(void){

while(RI == 0); // WAIT UNTIL DATA IS RECEIVED RI = 0; // CLEAR FLAGreturn SBUF; // RETURN SERIAL DATA

} // END OF PROGRAM

ByC. Chandra Mouli, Research Scholar, Department of Instrumentation, Sri Krishnadevaraya University, Anantapur.

Mobile: +919963719606, email: [email protected]

24

Page 25: 8051 dev board guide

8051 Microcontroller and Interfacing Experiments

Experiment No. 4

Aim:To interface a 16X2 Liquid Crystal Display (LCD) with P89V51RD2.

Schematic Diagram:

 Pin  Symbol Function

 1  Vss  Ground

 2  Vdd  Supply Voltage

 3  Vo  Contrast Setting

 4  RS  Register Select

 5  R/W  Read/Write Select

 6  En  Chip Enable Signal

 7-14  DB0-DB7  Data Lines

 15  A/Vee  Gnd for the backlight

 16  K  Vcc for backlight

The LCD requires 3 control lines (RS, R/W & EN) & 8 (or 4) data lines. The number on data lines depends on the mode of operation. If operated in 8-bit mode then 8 data lines + 3 control lines i.e. total 11 lines are required. And if operated in 4-bit mode then 4 data lines + 3 control lines i.e. 7 lines are required. How do we decide which mode to use? It’s simple if you have sufficient data lines you can go for 8 bit mode & if there is a time constrain i.e. display

ByC. Chandra Mouli, Research Scholar, Department of Instrumentation, Sri Krishnadevaraya University, Anantapur.

Mobile: +919963719606, email: [email protected]

25

Page 26: 8051 dev board guide

8051 Microcontroller and Interfacing Experimentsshould be faster then we have to use 8-bit mode because basically 4-bit mode takes twice

as more time as compared to 8-bit mode.

When RS is low (0), the data is to be treated as a command. When RS is high (1), the data being sent is considered as text data which should be displayed on the screen.

When R/W is low (0), the information on the data bus is being written to the LCD. When RW is high (1), the program is effectively reading from the LCD. Most of the times there is no need to read from the LCD so this line can directly be connected to Gnd thus saving one controller line.

The ENABLE pin is used to latch the data present on the data pins. A HIGH - LOW signal is required to latch the data. The LCD interprets and executes our command at the instant the EN line is brought low. If you never bring EN low, your instruction will never be executed.

Display Data Ram (DDRAM) stores the display data. So when we have to display a character on LCD we basically write it into DDRAM. For a 2x16 LCD the DDRAM address for first line is from 80h to 8fh & for second line is 0c0h to 0cfh. So if we want to display 'H' on the 7th position of the first line then we will write it at location 87h.

Now as you have noticed two types of data is given to the LCD data to be displayed, command or special instruction.

Source Code:

#include <AT89X51.H>#define LCD P1sbit sw=P2^7;sbit rs=P2^0;sbit en=P2^1;void lcdcmd(unsigned char);void lcd_data(unsigned char);void lcd_data_string(unsigned char *);void init_lcd();//void ser_str(unsigned char* );//void ser_data(unsigned char )void delay(unsigned int );void main(){init_lcd();sw=1;while(1){

lcd_data_string(" ESSENCE TRAINING PROGRAM FOR SKU ");//ser_str("\n anticlockwise \r ");

delay(900);}}void init_lcd(void)

ByC. Chandra Mouli, Research Scholar, Department of Instrumentation, Sri Krishnadevaraya University, Anantapur.

Mobile: +919963719606, email: [email protected]

26

Page 27: 8051 dev board guide

8051 Microcontroller and Interfacing Experiments

{lcdcmd(0X38);delay(500);lcdcmd(0X0E);delay(500);lcdcmd(0X01);delay(500);lcdcmd(0X06);delay(500);/*lcdcmd(0X80);delay(500);*/}void lcdcmd(unsigned char a){LCD=a;rs=0;en=1;delay(500);en=0;}void lcd_data(unsigned char a){LCD=a;rs=1;en=1;delay(500);en=0;//return(0);}void delay(unsigned int j){unsigned int i;for(i=0;i<j;i++);}void lcd_data_string(unsigned char *p){ while(*p!='\0') { lcd_data(*p++); }}

ByC. Chandra Mouli, Research Scholar, Department of Instrumentation, Sri Krishnadevaraya University, Anantapur.

Mobile: +919963719606, email: [email protected]

27

Page 28: 8051 dev board guide

8051 Microcontroller and Interfacing ExperimentsExperiment No. 5

Aim:Interface 12V DC Electro Mechanical Relay (EMR) with P89V51RD2 using 8051

development board.

Electro Mechanical Relay (EMR):

A relay is an electrically controllable switch widely used in industrial controls, automobiles and appliances. It allows the isolation of two separate sections of a system with two different sources. One such relay is called Electro Mechanical Relay or Electro Magnetic Relay. The EMRs have three components: the coil, spring and contacts. When current flows through the coil, a magnetic field is created around the coil (the coil is energized), which causes the armature to be attracted to the coil. The armature contact acts like a switch and closes or opens the circuit.

The contacts normally open (NO) or normally close (NC). In the NC type, the contacts are closed when the coil is not energized. In the NO, the contacts are open when the coil is not energized. There can one or more contacts. For example, we can have SPST (Single pole, single through), SPDT (Single pole, double through) and DPDT (Double pole, double through) relays. Relay diagram shows all types of relay internal diagram.

Relay Diagrams:

ByC. Chandra Mouli, Research Scholar, Department of Instrumentation, Sri Krishnadevaraya University, Anantapur.

Mobile: +919963719606, email: [email protected]

28

Page 29: 8051 dev board guide

8051 Microcontroller and Interfacing ExperimentsSchematic Diagram:

Algorithm:1. Connect the relay to microcontroller as shown in the schematic.

a. Relay coil needs around 10mA to be energized; the microcontroller pin can provide a maximum of 1-2mA current.

b. For this reason we place a driver such as a power transistor or ULN2803 between microcontroller and relay.

2. Control the P1.0 pin by switching it with some delay.3. Write the program in Keil software by using embedded-C language.4. Generate .hex file and transfer the .hex file from computer to the target

board by using flash magic.

Source Code:

/* ----------------------------------------------------------------------- relay.c : Demonstration of interfacing an Relay Designed for 89c51 running at 11.0592Mhz Controlling a port pin (turn on and off) at rate of 1Hz with crystal of 11.0592Mhz

compile the program in Keil uVision Compiler. Operation : when MCU power up port pin on/off continuously at frequency of 1Hz. Feedback appreciated: researchermouli@gmail.com-----------------------------------------------------------------------------*/

ByC. Chandra Mouli, Research Scholar, Department of Instrumentation, Sri Krishnadevaraya University, Anantapur.

Mobile: +919963719606, email: [email protected]

29

Page 30: 8051 dev board guide

8051 Microcontroller and Interfacing Experiments //INCLUDES DEFINITION OF 89C51 #include<at89x51.h> //CONSTANT DEFINES #define INPUT_PIN 1 // define 1 as INPUT_PIN #define LOW 0#define HIGH 1

//PINS DEFINE #define RELAY_PIN P1_0 // define port P1_0 as RELAY i.e connect RELAY to P1_0

//DEFINE MACROS#define RELAYOn() RELAY_PIN = LOW#define RELAYOff() RELAY_PIN = HIGH

//FUNCTIONS PROTOYPESvoid Delay(unsigned int time);

//main function beginsvoid main(void) {

RELAY_PIN = INPUT_PIN; while(1) // loop forever{

RELAYOn(); // Turn on relayDelay(500); // delay for 500msec = 0.5 secRELAYOff(); // Turn off relayDelay(500); // delay for 500msec = 0.5 sec

} // end of while loop} // end of main function

// PROGRAM ROUTINESvoid Delay(unsigned int time){

unsigned char pause; // Declare variable pause as unsigned charwhile( time > 0) // loop until time is greater than zero{

pause = 150; // Initialize pause to 150 (decimal value)while(pause--); // Decrement pause until it becomes zerotime--; // Decrement time and loop back until time

} // value becomes zero} // End of Delay function

ByC. Chandra Mouli, Research Scholar, Department of Instrumentation, Sri Krishnadevaraya University, Anantapur.

Mobile: +919963719606, email: [email protected]

30

Page 31: 8051 dev board guide

8051 Microcontroller and Interfacing Experiments

Experiment No. 6

Aim:To interface stepper motor with P89V51RD2 using 8051 development board.

Schematic Diagram:

1. Stepper motor unlike DC motor rotates in steps.2. Stepper motor has 4 coils which forms the stator and a central rotor.3. Rotation depends on excitation of stator coils.

step coil A coil B coil C coil D

1 0 0 0 1

2 1 0 0 0

3 0 1 0 0

4 0 0 0 1

Anyone of these values forms the initial value. To get 360o revolution 48 steps are required.

ByC. Chandra Mouli, Research Scholar, Department of Instrumentation, Sri Krishnadevaraya University, Anantapur.

Mobile: +919963719606, email: [email protected]

31

Page 32: 8051 dev board guide

8051 Microcontroller and Interfacing ExperimentsStep angle= 360o /48 = 7.5o.

Algorithm:

1. Configure P1 as output.2. Apply the initial excitation of 11 to motor coils through P1.3. For clockwise motion -Rotate right once the excitation and repeat step 2.4. For anticlockwise motion -Rotate left once the excitation and repeat step 2.

Source Code:

#include<reg51.h>

void Delay(unsigned int);void main(void){ unsigned int r, rotation, codes[4]= {0x11,0x44,0x22,0x88};

while(1){for(rotation=0; rotation<5; rotation++)

{for(r=0; r<=3; r++){

P2 = codes[r]; Delay(500); }

}}

}

void Delay(unsigned int time){

unsigned char pause;while( time > 0){

pause = 52;while(pause--);time--;

}}

ByC. Chandra Mouli, Research Scholar, Department of Instrumentation, Sri Krishnadevaraya University, Anantapur.

Mobile: +919963719606, email: [email protected]

32

Page 33: 8051 dev board guide