final report

86
QUAID-E-AWAM UNIVERSITY OF ENGINEERING SCIENCE AND TECHNOLOGY NAWABSHAH DEPARTMENT OF ELECTRONIC ENGINEERING Submitted in the fulfillment of partial requirement for the degree of Bachelor of Electronic Engineering Microcontroller 8051 based PC Remote Control via Serial Port Project Supervisor ENGR. Abdul Rafay Khatri BY ENGR. WAHAB UDDIN NAZAR (GROUP LEADER) (06ES56) ENGR. SAMI CHANNA (ASST: GROUP LEADER) (06ES50) 1

Upload: wahab-uddin-nazar

Post on 17-Nov-2014

11 views

Category:

Documents


8 download

TRANSCRIPT

Page 1: Final Report

QUAID-E-AWAM UNIVERSITY OF ENGINEERING

SCIENCE AND TECHNOLOGY NAWABSHAH

DEPARTMENT OF ELECTRONIC ENGINEERING

Submitted in the fulfillment of partial requirement for the degree of

Bachelor of Electronic Engineering

Microcontroller 8051 based PC Remote

Control via Serial Port

Project Supervisor

ENGR. Abdul Rafay Khatri

BY

ENGR. WAHAB UDDIN NAZAR (GROUP LEADER) (06ES56)

ENGR. SAMI CHANNA (ASST: GROUP LEADER) (06ES50)

MEMBERS

ENGR. WASIM ABBAS (06ES51)

ENGR.LATEEF GUL (06ES20)

ENGR. SHERYAR JAMALI (06ES11)

1

Page 2: Final Report

CERTIFICATE

This is certify that Mr.___________________________________

S/O________________________ Roll No.________ Final Year

student of Bachelor of Electronic Engineering has completed the

compulsory requirement of Project/Thesis during session; 2008

2009.The title thesis is “Microcontroller 8051 Based Pc Remote

Control via Serial Port”. It is submitted to the Quaid-e-Awam

University of Engineering science & Technology Nawabshah for

award of the Degree of Bachelor of Electronic Engineering.

ENGR. ABDUL RAFAY KHATRI

SUPERVISOR

PROF: MUEEN DIN MEMEON

CHAIRMAN DEPARTMENT OF ELECTRONIC

ENGINEERING

QUAID-E-AWAM UNIVERSITY OF ENGINEERING

SCIENCE AND TECHNOLOGY NAWABSHAH

2

Page 3: Final Report

ACKNOWLEDGEMENT

Bismillahirrahmaanirrahim,

In the name of Allah, the most compassionate and the merciful.

Thank to God is Almighty because of we can finish our project

with successful. We would like to express my sincere gratitude to my

project supervisor, Mr. Abdul Rafay Khatri lecturer, for his useful

information, help, guidance and constructive comments towards the

completion of this project. With his supported we can do this project

and finish it.

And we would like to say thank you to our family because of

their support like, money, time and spiritual. Without their support,

it’s very hard to us to run and finished this final project.

Lastly we thanks to all our friends because of with their help

like teaching us, give borrow their transport to us for buy the

component and equipment, and their morale supported.

3

Page 4: Final Report

ABSTRACT

This final report is established to fulfill the Final year Project/Thesis requirement for

Engineering for final semester student, a standard guideline is prepared to standardize

various documentations and this report is done mainly to list down all the important

aspect of the project or the electrical and electronic device that designed.

We introduce a method of how electronics devices and machines can be controlled

remotely from an operator/user. As a practical model we design a microcontroller 8051

based receiver circuit may connected to Personal computer. This project is an

implementation of RC5-remote reception on an 8051 microcontroller. The received code

is decoded and sent to the PC IR remote software developed using high level graphical

programming language (Visual Basic). The cursor position is moved according to the

keys pressed.

The project report focuses on the system which is designed by using the Microcontroller

AT89c2051. The infrared receiver have three pins in which two pins are for +5v supply

and ground while the third pin is for data output. The Infrared Receiver is designed for

demodulating the frequency of 30 kHz to 40 kHz, for example, TSOP1738 is designed

for demodulating frequency of 38 kHz which is used in our project.

The IR receiver module receives the data sent by remote handset, amplifies,

demodulates and converts it to MCU compatible voltage format and outputs it on its data

output pin. The microcontroller decodes the infrared signal data and the microcontroller

will sent the infrared Key code to the PC through the Serial port.

We use Microsoft Visual Basic 6.0 to receive the Key code through the Component

called MSCOMM which is a component for the Com port control. Through this

component we get the key code and do the appropriate functions.

4

Page 5: Final Report

After getting this key code of the Remote control the program compare the key code

with the code present in the program and if they are equal then the corresponding

function was done.

CHAPTER 1

MICROCONTROLLER BASED PC REMOTE CONTROL

1-1 INTRODUCTION ……………………………………………………….……………10

1-2 DESCRIPTION OF THE PROJECT…………………………….………………………10

1-3 HISTORY OF REMOTE CONTROLS………………………………………..…..……11

1-3-1 THE FIRST REMOTE CONTROL……………………………………………...……11

1-3-2 MILLITARY USES OF REMOTE CONTROL IN WWI & WWII…………..………11

1-3-3 INFRARED DEVICES REPLACED THE ULTRASONIC RC………………………11

1-4 HOW A SIMPLE REMOTE CONTROL WORKS………………………………………12

1-4-1 INFRARED REMOTE CONTROL: THE PROCESS……………………….…………12

1-5 EMBEDED SYSTEM DESIGN CHALLENGES………………………….……14

1-5-1 SHORTER DEVELOPMENT TIME……………………………………………………14

1-5-2 LOWER MANUFACTURING COST…………………………………………..………14

1-5-3 LOWER POWER CONSUMPTION………………………………………….…………15

1-6 OPPURTUNITIES OF THIS PROJECT IN MARKET……………………………………15

1-6-1 HOUSE HOLD PRODUCTS……………………………………………………….……15

1-6-2 ON INDUSTRIAL BASIS……………………………………………………….………15

1-7 WHY USING SERIAL PORT INSTEAD OF PARALLEL PORT…………….…………15

1-8 THE FUTURE OF REMOTE CONTROL…………………………………………….……16

5

Page 6: Final Report

CHAPTER 2

COMPONENTS AND PARTS USED IN THE PROJECT

2-1 MICRO-CONTROLLER…………………………………………………….…………18

2-1-1 MICRO-CONTROLLER: WHY? WHAT? HOW?.....................................................18

2-1-2 CHOSSING A MICRO-CONTROLLER………………………………….…………18

2-1-3 HISTORY OF MICRO-CONTROLLER………………………………….…………19

2-1-4 MICRO-CONTROLLER 8051(AT892051)…………………………………………19

2-1-5 PIN DESCRIPTION OF AT892051…………………………………………………20

2-1-6 FUNDAMENTAL OPERATION OF AN 8051

MICRO-CONTROLLER BASED SYSTEM………………………………………………21

2-1-7 THE 8051 SYSTEM ARRCHITECTURE……………………………………..……21

2-2 RS232 SERIAL PORT ……………………………………………………..……….…22

2-2-1 INTRODUCTION TO RS232……………………………………………….………23

2-2-2 PIN DESCRIPTION…………………………………………………………………23

2-2-3 SPEED……………………………………………………………………….………23

2-2-4 RS232 SPECIFICATIONS………………………………………………………….25

2-3 MAX232…………………………………………………………………………….…27

2-3-1 FEATURES…………………………………………………………….……………27

2-4 TSOP1738………………………………………………………………………..……27

2-4-1 PIN CONFIGURATION……………………………………………………………27

6

Page 7: Final Report

2-5 POWER SUPPLY……………………………………………………………..………27

2-5-1 PIN CONGIGURATION……………………………………………………………27

2-6 COMPONENT LISTING…………………………………………………..…………28

CHAPTER 3

PROGRAMMING AND INTERFACING OF MICROCONTROLLER

8051

3-1 PROGRAMS THE 8051 MICROCONTROLLER…………………………………………30

3-1-1 PRE-REQUEST FOR PROGRAMMING…………………………………………..……30

3-1-2 INTRODUCTION TO ASSEMBLY LANGUAGE…………………………...…………30

3-1-3 ASSEMBLY LANGUAGE AND OTHER COMPUTER LANGUAGES………………30

3-1-4 STRUCTURE OF ASSEMBLY PROGRAMMING……………………………..………31

3-1-5 ASSEMBLY CODE OVERVEIW……………………………………………….………31

3-1-6ARITHEMATIC AND LOGIC INSTRUCTIONS……………………………….………31

3-1-7 ADDRESSING MODES………………………………………………………………….32

3-1-8 ASSEMBLING AND RUNNING AN 8051 PROGRAM:……………………………….32

3-1-9 I/O PORT PROGRAMMING………………………………………………….…………32

3-1-10 INTERRUPTS PROGRAMMING………………………………………………………34

3-2 MICROCONTROLLER 8051 INTERFACING TECHNIQUES…………………..………36

3-2-1INTERFACING WITH RS232 AND MAX232……………………………………..……38

3-2-2 SENSORS…………………………………………………………………………………40

3-2-3NTERFACING TO INFRA RED SENSOR………………………………………..……..40

CHAPTER 4

SERIAL COMMUNICATION AND PROGRAMMING THE SERIAL

PORT

4-1 BASICS OF SERIAL COMMUNICATION …………………………….………………43

4-2 STANDARD SERIAL INTERFACE………………………………………….…………44

7

Page 8: Final Report

4-3 INTERFACING THE SERIAL PORT TO PC……………………………………………44

4-4 MODES OF SERIAL PORT………………………………………………………………45

4-5 SERIAL PORT CONTROL REGISTER…………………………………………………46

4-6 SERIAL PORT CONTROL ( SCON ) REGISTER………………………………….…46

4-7 SERIAL COMMUNICATION INTERRUPTS……………………………………….…46

4-8 WRRITING TO THE SERIAL PORT……………………………………………………48

4-9 READING THE SERIAL PORT…………………………………………………….……49

4-10 PROGRAMMING……………………………………………………………….………50

CHAPTER 5

VISUAL BASIC FOR 8051 MICROCONTROLLER

5-1 VISUAL BASIC 6.0………………………………………………………….……………57

5-1-1 FEATURES………………………………………………………………………………57

5-2 SERIAL COMMUNICATION WITH VISUAL BASIC 6.0………………………….……58

5-2-1 COMMUNICATIONS CONTROL………………………………………………………58

5-2-2 PROPERTIES OF COMMUNICATION PORT CONTROL……………………….……59

5-3 COMM PORT………………………………………………………………………….……60

5-4 INPUTTING DATA…………………………………………………………………...……60

5-5 OUTPUTTING DATA………………………………………………………………...……61

5-6 GETTING STARTED WITH PC REMOTE CONTROL……………………………….…62

8

Page 9: Final Report

MICROCONTROLLER 8051 BASED PC

REMOTE CONTROL VIA SERIAL PORT

CHAPTER# 1

MICROCONTROLLER BASED PC REMOTE

CONTROL

DEPARTMENT OF ELECTRONIC ENGINEERING

QUAID-E-AWAM UNIVERSITY OF ENGINEERING

SCIENCE AND TECHNOLOGY NAWABSHAH

9

Page 10: Final Report

CHAPTER-1

MICROCONTROLLER BASED PC REMOTE

CONTROL

1-1 INTRODUCTION

This chapter explains about the Microcontroller based Pc remote control ,motivation

to build this project ,a short history of remote control world .Remote control are mostly

infra red which plays a revolutionary vital role in changing our lives. This project

contains all about how to make a system to control your Personal computer remotely

with almost any remote controller you can find in your house TV Sets. Along this we

discuss the Remote control and it’s commercially and economically reviews how a

simple remote control works and in the end of the chapter we concluded the future of the

remote controls.

1-2 DESCRIPTION OF THE PROJECT

The convenience of selecting TV channels using your remote and then pointing the same

remote to your Computer so that you can control the whole system using the single

remote control. The Following functions can be done with PC Remote control. All

Numerical Keys (0 - 9) Arithmetic Keys (+, -, /, *) Enter, Escape, Help, Refresh, Caps

lock, Tab, Back space, delete, Left, Right, Up, down arrows, Page up, Page down,

Window keys. Calculator, Notepad CD drive Open/Close Control panel Computer log

off, reboot or shutdown Volume Up, down, Mute 5 User defined Programs

>All Numerical Keys (0 - 9)

>Arithmetic Keys (+, -, /, *)

>Enter, Escape, Help, Refresh, Caps lock, Tab, Back space, delete, Left, Right, Up,

10

Page 11: Final Report

down arrows, Page up, Page down, Window keys.

> Calculator, Notepad

> CD drive Open/Close

>Control panel

>Computer log off, reboot or shutdown

>Volume Up, down, Mute

>5 User defined Programs

1-3 HISTORY OF REMOTE CONTROLS

1-3-1 THE FIRST REMOTE CONTROL

The first TV remote control, called "Lazy Bones," was developed in 1950 by Zenith

Electronics Corporation (then known as Zenith Radio Corporation). Lazy Bones used a

cable that ran from the TV set to the viewer. A motor in the TV set operated the tuner

through the remote control.  Although customers liked having remote control of their

television, they complained that people tripped over the unsightly cable that meandered

across the living room floor.

1-3-2 MILLITARY USES OF REMOTE CONTROL IN

WWI & WWII

The first machines to be operated by remote control were used mainly for military

purposes.  Radio-controlled motorboats, developed by the German navy, were used to

ram enemy ships in WW I.  Radio controlled bombs and other remote control weapons

were used in WW II. Once the wars were over, United States scientists experimented to

find nonmilitary uses for the remote control. 

1-3-3 INFRARED DEVICES REPLACED THE

ULTRASONIC RC

In the early 1960s, solid-state circuitry (i.e., transistors) began to replace vacuum tubes.

Hand-held, battery-powered control units could now be designed to generate the

11

Page 12: Final Report

inaudible sound electronically. In this modified form, Dr. Adler's ultrasonic remote

control invention lasted through the early 1980s, a quarter century from its inception. By

the early 1980s, the industry moved to infrared, or IR, remote technology. The IR

remote works by using a low frequency light beam, so low that the human eye cannot

see it, but which can be detected by a receiver in the TV. Zenith's development of cable-

compatible tuning and tale text technologies in the 1980s greatly enhanced the

capabilities and uses for infrared TV remotes.

1-4 HOW A SIMPLE REMOTE CONTROL WORKS

For the purpose of designing a PC remote control we should aware to the basic

fundamentals of the working of simple IR remote control. A remote control works on the

principle of IR technology. Here we prescribed the TV remote control which is the

mother of PC remote control

An IR remote control (the transmitter) sends out pulses of infrared light that represent

specific binary codes. These binary codes correspond to commands, such as Power

On/Off and Volume Up. The IR receiver in the TV, stereo or other device decodes the

pulses of light into the binary data (ones and zeroes) that the device's microcontroller

can understand. The microcontroller then carries out the corresponding command.

The basic parts involved in sending an IR signal include:

Buttons

Integrated circuit

Button contacts

Light-emitting diode (LED)

1-4-1 INFRARED REMOTE CONTROL: THE PROCESS

Pushing a button on a remote control sets in motion a series of events that causes the

controlled device to carry out a command. The process works something like this:

1. You push the "volume up" button on your remote control, causing it to touch

the contact beneath it and complete the "volume up" circuit on the circuit

board. The integrated circuit detects this.

12

Page 13: Final Report

2. The integrated circuit sends the binary "volume up" command to the LED at

the front of the remote.

3. The LED sends out a series of light pulses that corresponds to the binary

"volume up" command.

One example of remote-control codes that includes the following 7-bit binary

commands:

Button Code

1 000 0000

2 000 0001

3 000 0010

4 000 0011

Channel up 001 0000

Channel down 001 0001

Power on 001 0101

Power off 010 1111

Volume up 001 0010

`Volume down 01 0011

The remote signal includes more than the command for "volume up," though. It carries

several chunks of information to the receiving device, including:

a "start" command

the command code for "volume up"

the device address (so the TV knows the data is intended for it)

a "stop" command (triggered when you release the "volume up" button)

So when you press the "volume up" button on a Sony TV remote, it sends out a series of

pulses that looks something like this: When the infrared receiver on the TV picks up the

signal from the remote and verifies from the address code that it's supposed to carry out

this command, it converts the light pulses back into the electrical signal for 001 0010. . It

13

Page 14: Final Report

then passes this signal to the microcontroller, which goes about increasing the volume.

The "stop" command tells the microprocessor it can stop increasing the volume.

FIG 1-1: A typical IR signal

1-5 EMBEDED SYSTEM CHALLENGES

This portion attempts to investigate the approach of embedded systems to design the

microcontroller based PC remote control via serial port. The embedded system is a

combination of computer hardware, software and perhaps additional mechanical or other

parts, designed to perform a specific function within a given time frame.

.1-5-1 SHORTER DEVELOPMENT TIME

An embedded application not only saves money but also shorten development time since

there is a vast library of soft ware already written for the DOS & Windows platforms.

The fact the windows is a widely used & well understood platform means that

developing our windows based embedded product reduces the cost & shorten the

developing time considerably.

1-5-2 LOWER MANUFACTURING COST

14

Page 15: Final Report

Our goal to complete our project in the minimum availability of resources and cost to

over come the market needs. Because we use microcontrollers they have a fixed amount

of RAM Rom and I/O ports. Makes them ideal for PC remote control in which cost and

space are critical

1-5-3 LOWER POWER CONSUMPTION

In this remote system there is no need for the computing power of a 486 or even an 8086

microprocessor in this application, the space it takes the power it consumes.

1-6 OPPURTUNITIES OF THIS PROJECT IN

MARKET

PC remote control system has a high potential in Pakistani markets due its min: size &

low cost. We call this system “The big thing in small packaging”

1-6-1 HOUSE HOLD PRODUCTS

As we design a method to control the machines & other electronic devices through a

remote control so ,we can implement this method to control home appliances & devices

such as to control home appliances such as room lights, fans, air condition, door lock

and unlock, washing machines, ,microwave oven and other stuff of home appliances

etc. It is concluded that our project have a high potential in the market of home

automation.

1-6-2 ON INDUSTRIAL BASIS

Remote controllers are the amazing applications and commercial opportunities in many

industries for example in auto industry they are used to lock and unlock the car doors,

car parking with a remote control .Remote controls are the major part of industries in

any perspectives

15

Page 16: Final Report

1-7 WHY USING SERIAL PORT INSTEAD OF

PARALLEL PORT

We use serial port in our project to communicate with PC which has some advantages

over Parallel port

1. The serial port cable can be longer than a parallel port cable, as serial port

transmits '1' as voltage from -5 to -12V and '0' as voltage from +5 to +12 V,

while parallel port transmits '1' as voltage of 5 volts and '0' as voltage of 0 volts.

At the same time the receiver of the serial port receives '1' as voltage from -3 to -

25 V and '0' as voltage from +3 to +25 V. Thus serial port can have maximal

swing up to 50 volts, while parallel port has maximal swing of 5 volts. Thus the

losses in the cable when transmitting data using serial port are less substantial

then losses when transmitting data using parallel port.

2. The number of wires needed when transmitting data serially is less than when the

transmission is parallel. Is the external device has to be installed at a great

distance from the computer, the cable with three wires is much cheaper than the

cable with 19 or 25 wires if the transmission is parallel. Still one should

remember that there are interface creation expenses for every

receiver/transmitter.

3. Further development of serial port is usage of infrared devices which

immediately proved popular. Many electronic diaries and palmtop computers

have inbuilt infrared devices for connection with external devices.

1-8 THE FUTURE OF REMOTE CONTROL

The future of remote controls is already happening and it is from a remote source via

fiber, satellite, and wireless to you and the devices in your home. The electrical company

can already control your household appliances, including your computers through the

electrical wiring. Remote brain interfaces that you have no choice over are next. The

16

Page 17: Final Report

opportunities are obvious with more intuitive interaction, but so are the dangers and they

are not being presented truthfully

MICROCONTROLLER 8051 BASED PC

REMOTE CONTROL VIA SERIAL PORT

CHAPTER# 2

COMPONENTS AND PARTS USED IN THE

PROJECT

17

Page 18: Final Report

DEPARTMENT OF ELECTRONIC ENGINEERING

QUAID-E-AWAM UNIVERSITY OF ENGINEERING

SCIENCE AND TECHNOLOGY NAWABSHAH

CHAPTER -2

COMPONENTS AND PARTS USED IN THE

PROJECT

In this chapter we discuss briefly different components and parts used in the project and

the criteria to choosing these parts. All parts can found in market with very low price but

whole project worth much more.

2-1 MICRO-CONTROLLER

Microcontroller is the backbone and the center of activity of our project. First we justify

the importance of using a microcontroller in our project before choosing.

2-1-1 MICRO-CONTROLLER: WHY? WHAT? HOW?

So, why do we use a micro-controller for the soul of our project? It is simply because

micro-controllers are cost effective and small enough to fit into our design. So the next

question is what type of micro-controller should we use?

2-1-2 CHOSSING A MICRO-CONTROLLER

There are four major 8-bit microcontrollers. There are Motorola’s 6811 Intel’s 8051,

Zilog`s Z8 and PIC 16X from Microchip Technology. There are also 16 bit and 32 bit

microcontrollers made by various chip makers. With all these different microcontrollers

our criteria to consider in choosing one? Three criteria in choosing one are as follows

18

Page 19: Final Report

MEETING THE COMPUTING NEEDS OF THE TASK AT HAND EFFICEINTLY AND

COST EFFECTIVE

In analyzing the needs of a microcontroller based project we must see whether an 8 bit,

16bit or 2 bit microcontroller can best handle the computing needs of the task most

effectively. Among other considerations in this category are:

Speed

Packaging

Power consumption

The amount of RAM and ROM on chip

The amount of I/O pins and the timer on the chip

How it is easy to upgrade to higher performance or lower power consumption

versions

Cost per unit

Availability of software development tools

Availability of an assembler, debugger, a code-efficient C language compiler, emulator,

technical support and both in house and out side house expertise

Availability and reliable resources of the microcontroller

The third criterion in choosing a microcontroller is its availability in needed quantities

both now in the future.

In this broad sense of choosing a microcontroller we chose 8051. We chose the 8051

because of its popularity and cost effectiveness. Moreover there are plenty of public

domain software as well as commercial libraries available in the industry.

2-1-3 HISTORY OF MICRO-CONTROLLER

The 8051 is originally an Intel product, a cousin of the 80X 86 families designed for the

embedded control market in the 80s. As popular standards especially software standards

don’t easily die down, it flourishes for years and becomes one of the most popular 8-bit

micro-controllers worldwide.

19

Page 20: Final Report

2-1-4 MICRO-CONTROLLER 8051(AT892051)

The AT89C2051 is a low-voltage, high-performance CMOS 8-bit microcomputer with 2

Kbytes of Flash programmable and erasable read only memory (PEROM).This is 8-bit

microcontroller consists of 64 Bytes of RAM, IK bytes of on chip Flash Memory, 1

timers, 15 programmable I/O lines.

Why we chose AT892051? The AT89C2051 and is an economical and cost-effective

member of Atmel's growing family of microcontrollers. It contains 2 Kbytes of flash

program memory. It is fully compatible with the MCS-51 architecture, and can be

programmed using the MCS-51 instruction set. However, there are a few considerations

one must keep in mind when utilizing certain instructions to program this device it is the

responsibility of the controller user to know the physical features and limitations of the

device being used and adjust the instructions used correspondingly.

FIG 2-1: AT892051 Pin description

2-1-5 PIN DESCRIPTION OF AT892051

Port 1

Port 1 is an 8-bit bidirectional I/O port. Port pins P1.2 to P1.7 provide internal pull-ups.

P1.0 and P1.1 require external pull-ups. P1.0 and P1.1 also serve as the positive input

20

Page 21: Final Report

(AIN0) and the negative input (AIN1), respectively, of the on-chip precision analog

comparator.

Port 3

Port 3 pins P3.0 to P3.5, P3.7 are seven bidirectional I/O pins with internal pull-ups.

P3.6 is hard-wired as an input to the output of the on-chip comparator and is not

accessible as a general purpose I/O pin. The Port 3 output buffers can sink 20 mA. When

1s are written to Port 3 pins they are pulled high by the internal pull-ups and can be used

as inputs. As inputs, Port 3 pins that are externally being pulled low will source current

(IIL) because of the pull-ups. Port 3 also serves the functions of various special features

of the AT89C2051 as listed below: Port Pin P3.0 P 3.1 P3.2 P3.3 P3.4 P3.5 Alternate

Functions RXD (serial input port) TXD (serial output port) INT0 (external interrupt 0)

INT1 (external interrupt 1) T0 (timer 0 external input) T1 (timer 1 external input)

RST

Reset input. All I/O pins are reset to 1s as soon as RST goes high. Holding the RST pin

high for two machine cycles while the oscillator is running resets the device. Each

machine cycle takes 12 oscillator or clock cycles.

XTAL1

Input to the inverting oscillator amplifier and input to the internal clock operating

circuit.

XTAL2

Output from the inverting oscillator amplifier.

2-1-6 FUNDAMENTAL OPERATION OF AN 8051 MICRO-

CONTROLLER BASED SYSTEM

Micro-controller system has the following configuration

The microprocessor is connected to a clock, which determines the operating

frequency of the system

After the reset switch is depressed, the microprocessor first loads the content of

a startup location from the startup ROM (0000H for 8051).

2-1-7 THE 8051 SYSTEM ARRCHITECTURE

21

Page 22: Final Report

To understand the operation of the 8051 based system, we need to study how

data and programs are stored in the system. Here we will examine two memory

storage subsystems:

FIG2-2: Fundamental system of 8051 Microcontroller Block diagram

1. The 8051 memory model

Below is the memory model of an 8051 micro-controller.

FIG 2-3: 8051 Memory Model

Internal ROM is vendor dependant. On power-up PC starts at 0000H in ROM space

2. The 8051 internal registers (special function registers SFR)

The 8051 has 128 bytes internal RAM for general-purpose storage, plus a number of

Locations within the range 80-FFH for storing Special Function Registers.

2-2 RS232 SERIAL PORT

22

Page 23: Final Report

RS232 is a popular communications protocol for connecting modems and data

acquisition devices to computers. RS232 devices can be plugged straight into the

computer's serial port (also known as the COM or Com port). We use RS232 connector

DB-9 male to interface to PC COMM port. We know that 8051 consist of built on serial

port for interfacing to the serial port of a personal computer (PC).

2-2-1 INTRODUCTION TO RS232

Rs232 can be used to connect between the PC and 8051. RS-232 is the interface that

your computer uses to talk to and exchange data with your.

2-2-2 PIN DESCRIPTION

There is a standardized pin out for RS-232 on a DB9 connector, as shown below.

FIG 2-4: Pin description according to the function

DCD: - Data Carrier Detect (DCD) indicates that carrier for the transmit data is ON.

RXD: - This pin carries data from the serial device to the computer.

Tad: - This pin carries data from the computer to the serial device.

23

Page 24: Final Report

DTR signals: - DTR is used by the computer to signal that it is ready to communicate

with the serial device like modem. In other words, DTR indicates to the Dataset (i.e., the

modem or DSU/CSU) that the DTE (computer) is ON.

24

Page 25: Final Report

Fig 2-4: Serial Port of a typical Personal Computer

DSR: - Similarly to DTR, Data set ready (DSR) is an indication from the Dataset that

itIs ON.

RTS: - This pin is used to request clearance to send data to a modem

CTS: - This pin is used by the serial device to acknowledge the computer's RTS Signal.

In most situations, RTS and CTS are constantly on throughout the communication

session.

RI: - RI stands for Ring Indicator. A modem toggles (keystroke) the state of this line

when an incoming call rings your phone. In other words, this is used by an auto answer

modem to signal the receipt of a telephone ring signal

2-2-3 SPEED

The speed of RS232 communications is expressed in Baud The length of the cable also

plays a part in maximum speed. The longer the cable, the greater the cable's capacitance

and the slower the speed at which you can obtain accurate results.

2-2-4 RS232 SPECIFICATIONS

25

Page 26: Final Report

FIG 2-5: RS232 Specification chart

2-3 MAX232

Microcontroller 8051 works on CMOS voltage levels which cannot be used to

communicate over RS-232 protocol. So a voltage or level converter is needed which can

convert TTL to RS232 and RS232 to TTL voltage levels. The most commonly used RS-

232 level converter is MAX232. This IC includes charge pump which can generate

RS232 voltage levels (-10V and +10V) from 5V power supply. It also includes two

receiver and two transmitters and is capable of full-duplex UART/USART

communication. MAX232 IC chips are referred to as “Line Driver”.

2-3-1 FEATURES

Meets or Exceeds TIA/EIA-232-F and ITU Recommendation V.28

Operates From a Single 5-V Power Supply With 1.0-_F Charge-Pump Capacitors

Operates Up To 120 Kit/s

Two Drivers and Two Receivers

±30-V Input Levels

26

Page 27: Final Report

Low Supply Current . . . 8 mA Typical

FIG 2-6: MAX232 Pin outs

2-4 TSOP1738

TSOP1738 is an Infrared (IR) receiver which is widely used in large number of

electronic products for receiving and demodulating infrared signals. The received

demodulated signals can be easily decoded by a microcontroller. It supports RC5, RC6

code, Sony format (SIRCS), NEC code, Sharp code, etc.

FIG 2-7: TSOP1738 IC

2-4-1 PIN CONFIGURATION

27

Page 28: Final Report

Pin 1= GND

Pin 2=VCC

Pin 3=OUT

2-5 POWER SUPPLY

Sourcing of power is very essential for our receiving circuit to remain in ON condition.

We use power regulated IC 7805 IC to change 220 volts acc to +5 volt dc output,

output current capability of 100 mA.

2-5-1 PIN CONGIGURATION

1. Unregulated voltage in

2. Ground

3. Regulated voltage out

Fig: 2-8:7804 regulated IC

2-6 COMPONENT LISTING

No Component Units

1 AT89C2051 1

2 RS232 Connector(FEMALE) 1

3 RS232 CABLE 1

4 Capacitor (33pF) 2

28

Page 29: Final Report

5 Capacitor 0.1 uF 2

6 Diode 1N4148 2

7 Capacitor 10 uF 5

8 Crystal (11.0592 MHz) 1

9 7805 ic 1

10 Toggle Switch 1

11 TSOP 1738 1

12 PHILIPS RC5 HAND SET 1

MICROCONTROLLER 8051 BASED PC

REMOTE CONTROL VIA SERIAL PORT

CHAPTER# 3

PROGRAMMING AND INTERFACING OF

MICROCONTROLLER 8051

29

Page 30: Final Report

DEPARTMENT OF ELECTRONIC ENGINEERING

QUAID-E-AWAM UNIVERSITY OF ENGINEERING SCIENCE AND

TECHNOLOGY NAWABSHAH

CHAPTER-3

PROGRAMMING AND INTERFACING OF

MICROCONTROLLER 8051

In this chapter we will discussed about the construction and programming method

of an 8051 microcontroller based Pc remote control. We will program it using (Personal

Computer) PC through LPT port. We assumed that after figure out the programming

issues we are able to design out project circuit using different interfacing techniques of

8051.

3-1 PROGRAMS THE 8051 MICROCONTROLLER

The scope of this work confirming the 8051 microcontroller input/output (I/O) signals

are compatible with our IR proximity sensor. We also learnt and familiarized with 8051

using assembly language and converting the assembly language code to hexadecimal

code using a development board.

3-1-1 PRE-REQUEST FOR PROGRAMMING

30

Page 31: Final Report

A general knowledge of programming

An understanding of decimal, hexadecimal and binary no systems.

A general knowledge of microcontroller 8051 hardware.

3-1-2 INTRODUCTION TO ASSEMBLY LANGUAGE

Assembly language is a low-level, pseudo-English representation of the

microcontroller's machine language. Each assembly language instruction has a one-to-

one relation to one of the microcontroller's machine-level instructions.

3-1-3 ASSEMBLY LANGUAGE AND OTHER COMPUTER

LANGUAGES

High-level languages, such as "C", Basic, Visual Basic, etc. are one or more steps above

assembly language in that no significant knowledge of the underlying architecture is

necessary. It is possible (and common) for a developer to program a Visual Basic

application in Windows without knowing much of anything about the Windows API,

much less the underlying architecture of the Intel Pentium.

3-1-4 STRUCTURE OF ASSEMBLY PROGRAMMING

An assembly language program consist of, among other things a series of lines of

Assembly language instructions. An assembly language instruction consists of a

mnemonic, optionally followed by one or two operands. Operand is the data items being

manipulated and mnemonics are the commands to the CPU, telling it what to do this

An assembly language program is a series of statements or lines such as ADD and MOV

or statement called directives

An assembly language instruction consist of four fields.

(Label:) mnemonic (operands) (;comment)

31

Page 32: Final Report

FIG 3-1:Environment of assembly language

3-1-5 ASSEMBLY CODE OVERVEIW

Oppcodes are operation codes. The codes assigned to each processor instruction. In the

8051 all codes 00h-FFh are defined. Operands are the objects used by the operation

represented by the Oppcodes. Mnemonics are the human readable names given to

individual Oppcodes.

3-1-6ARITHEMATIC AND LOGIC INSTRUCTIONS

Processor instructions are often classified into groups, such as:

Data transfer instructions (e.g. MOV, MOVX, PUSH, POP, XCH)

Arithmetic instructions (e.g. INC, ADDC, DEC, SUBB, MUL, DIV)

Logic instructions (e.g. CLR, SETB, ANL, RRC, ORL, XRL)

Control transfer (e.g. AJMP, LJMP, JMP, ACALL, LCALL, RET, DJNZ, JNB)

3-1-7 ADDRESSING MODES

A processor may support different addressing modes, such as register addressing, direct

addressing, indirect addressing, or immediate addressing

Register addressing: The content of the named register (R0-R7) is used and the least

significant bits of the Oppcodes specify the register (e.g. MOV R0,A)

Direct addressing: The operand specifies the address of the register/memory to be used

(e.g. MOV $80,$81)

Indirect addressing: The content of the addressed register is used as an address

32

Page 33: Final Report

(pointer), which is then accessed to provide the data for the instruction (e.g. MOV

[R0],A or MOV @R0,A)

Immediate addressing: the value to be used as data in the instruction is included

directly in the instruction syntax and in the program memory (e.g. MOV R0,#0)

3-1-8 ASSEMBLING AND RUNNING A PROGRAM

1. First we use an editor to type our program such as MS-DOS edit program or Notepad,

the source program has the extension “asm” or “src”.

2. The” asm” source file is fed to an 8051 assembler which converts the instructions into

machine code.

3. Assembler required a third step called linking. The link program takes one or more

object files and produces an absolute file with extension “abs”.

4. ”abs” file is fed into a program called “OH”(object to hex converter) which creates a

file with extension ”hex” that is ready to burn in ROM.

3-1-9 I/O PORT PROGRAMMING

The two 8-bit I/O ports P1and P3 each uses 8 pins. All the ports upon RESET are

configured as input, relay to use as input ports. When the 0 is written to apart ,it

becomes an output port and to configure it as an input ,a 1 must be sent to the port.

To use any of these ports as an input port, it must be programmed.

33

Page 34: Final Report

FIG 3-2: Steps to create a program

Port 1:

34

Page 35: Final Report

Port 1 is configured as an input port upon Reset. Port 1 is configured first

as an input port by writing 1s to it, then data is received from that port and

saved R7 and. R5.

Port 3:

Port 3 can be used as input or out put and does not need any pull up

resistors .Port 3 is configured as an input port upon reset. It has the

additional function of providing some extremely important signals.

3-1-10 INTERRUPTS PROGRAMMING

An interrupt is an external or internal event that interrupts the microcontroller to inform

it that a device needs its service.

Interrupt system of 8051

There are five interrupt sources in order to priority.

i. External interrupt 0

ii. Timer 0

iii. External interrupt 1

iv. Timer 1

v. Serial Port

While programming the interrupts some points should be remember

Each interrupt can be enabled separately

Each interrupt type has a separate vector address

Each interrupt can be programmed to one of two priority levels

External interrupts can be programmed for edge or level sensitivity

35

Page 36: Final Report

IE: Interrupt enable register

FIG 3-3: IE Register

EA: Global interrupt enable

ES: Serial Interface

ET1, ET0: Enable Interrupt Timer 1, Timer 0

EX0, EX1: External interrupt 0, 1

Interrupts Rom

Location(Hex)

Pin

Reset 0000 9

External hardware interrupt

0(INT0)

0003 P3.2(12)

Timer 0 interrupt (TF0) 000b

External hardware interrupt

1(INT1)

0013 P3.3(13)

Timer 1 interrupt(TF1) 001b

Serial COM interrupt (RI and TI) 0023

FIG 3-4: Interrupt Vector Table for the 8051

IP: Interrupt priority register

We can alter the sequence by assigning a higher priority to any one of the interrupts.

This is done by programming a register IP.

36

Page 37: Final Report

FIG 3-5: IP Register

PS: serial interface

PT1: Timer 1

PX1: External Interrupt 1

PT0: Timer 0

PX0: External Interrupt 0

(0= low priory , 1= high priority)

3-2 MICROCONTROLLER 8051 INTERFACING

TECHNIQUES

Micro-controllers are useful to the extent that they communicate with other devices,

such as sensors, motors, switches, keypads, displays, memory and even other

microcontrollers. Microcontroller system can be viewed as a system that reads from

(monitors) inputs, performs processing and writes to (controls) outputs.

The Digital input/outputs compensating a larger advantage over analog input and output.

In digital interface the microcontroller continuously reading monitor and control the

status of button or switch. They are simplest to interface. They have lower cost to

implement high speed and lower programming over head.

37

Page 38: Final Report

FIG: 3-6: Microcontroller 8051 interface techniques classification

38

Page 39: Final Report

3-2-1INTERFACING WITH RS232 AND MAX232

8051 consist of a built-in serial port for interfacing the serial I/O devices. We can

connect a microcontroller to the serial port of a personal computer (PC) .The PC serial

port follows the RS232 or EIA-232 serial interface standards. A normal RS232 cable can

be used to connect between a PC and the 8051.The RS232 cable is terminated at both

ends at connector called DB-9/DB25.When connecting the 8051 to the RS232 serial

interface, one major concern is the difference in voltage levels between them. The 8051

uses the TTL voltage levels where a 5V would indicate a high while a 0 indicates a low.

Mean while ,for the RS232 a High is defined as being +3V to +15V while a low is

between -5V to -15V.Because of this the connection between the 8051 and the RS232

have to done through line divers(Max232). Line drivers basically function to convert

between the two different voltage levels so that a high or low as understand by the 8051

also means a high or low to the RS232 and vice versa..

RxD and TxD pins in the 8051

The 8051 has two pins that are used specially for transferring and receiving data serially.

These two pins are called TxD and RxD and are the part of the port 3 groups

(P3.0,P3.1).

FIG 3-7: Interface to RS232 through Max232(line driver)

39

Page 40: Final Report

FIG 3-8: Block diagram of PC Remote control receiver circuit

40

Page 41: Final Report

3-2-2 SENSORS

Sensors are electronics or electrical devices that change in some significant way in

response to an applied stimulus. Sensors, transducers and electrodes are used to compute

the physical standards by producing voltage signal that represents that parameter or

standard.

3-2-3NTERFACING TO INFRA RED SENSOR

IR Proximity sensor is the major component of the receiver circuit of our project, it has

an IR sensor which act as IR receiver with a built-in 38 KHz demodulator. We choose

TSOP1738 because it compatible to 8051 microcontroller TTL logic level. The

demodulated output signal can be directly decoded by the microcontroller.

FIG 3-9: Interfacing 8051 microcontroller to TSOP1738

FIG 3-10: Schematic circuit of TSOP1738 application

41

Page 42: Final Report

FIG 3-11: Circuit Diagram of PC Remote Control

42

Page 43: Final Report

MICROCONTROLLER 8051 BASED PC

REMOTE CONTROL VIA SERIAL PORT

CHAPTER# 4

SERIAL COMMUNICATION AND PROGRAMMING

THE SERIAL PORT

DEPARTMENT OF ELECTRONIC ENGINEERING

QUAID-E-AWAM UNIVERSITY OF ENGINEERING

SCIENCE AND TECHNOLOGY NAWABSHAH

43

Page 44: Final Report

CHAPTER # 4

SERIAL COMMUNICATION AND

PROGRAMMING THE SERIAL PORT

Computer transfers data in two ways: parallel and serial. In parallel data transfers often 8

or more lines (wire connection) are used to transfer data to a that is only a few feet

away. to transfer to a device located many meters away. The serial method is used. In

serial communication, the data is sent one bit at a time. In this chapter we discuss the

basics of serial communication and its programming

4-1 BASICS OF SERIAL COMMUNICATION

When a microcontrollers communicate with the out side world’s .it provides the data in

byte-sized chunks. it uses a single data line instead of 8-bit line of parallel

communication makes it cheaper and further used in long distance communication. For

serial data communication to work, the byte of data must be covered to serial bits using a

parallel-in-serial-out shift register; then it can be transmitted over a single data line

Serial data communication uses two methods

I. Asynchronous

II. Synchronous

The synchronous method transfer a block of data(characters) at a time while the

asynchronous transfers a single byte at a time. It is possible to write software to

use either of the method but the program can be tedious and long. For this

reason, there are special IC chips made by many manufacturers for serial

communication. These chips are commonly referred to as UART(universal

asynchronous receiver-transmitter) and USART (universal synchronous-

asynchronous recover-transmitter).The 8051 chips has a built-in UART

4-2 STANDARD SERIAL INTERFACE

44

Page 45: Final Report

The serial port is full duplex, meaning it can transmit and receive simultaneously. It is

also receive-buffered, meaning it can commence reception of a second byte before a

previously received byte has been read from the register The serial port receive and

transmit registers are both accessed at Special Function Register SBUF. Writing to

SBUF loads the transmit register, and reading SBUF accesses a physically separate

receive register.

FIG 4-1: Standard interface of RS232 to PC comm. port

4-3 INTERFACING THE SERIAL PORT TO PC

To allow data transfer between the PC and an 8051 system without any error, we must

make sure that the baud rate of 8051 system matches the baud rate of the PC’s COM

port .HyperTerminal function supports baud rates much higher than listed below

45

Page 46: Final Report

FIG 4-2: PC Baud rate supported by 486/Pentium IBM PC BIOS

4-4 MODES OF SERIAL PORT

The serial port can operate in 4 modes.

Mode 0:

Serial data enters and exits through RxD. TxD outputs the shift clock. 8 bits are

transmitted/received (LSB first). The baud rate is fixed at 1/12 the oscillator frequency.

Mode 1:

10 bits are transmitted (through TxD) or received (through RxD): a start bit (0), 8 data

bits (LSB first), and a stop bit (1). On receive; the stop bit goes into RB8 in Special

Function Register SCON. The baud rate is variable.

Mode 2:

11 bits are transmitted (through TxD) or received (through RxD): start bit (0), 8 data bits

(LSB first), a programmable 9th data bit, and a stop bit (1). On Transmit, the 9th data bit

(TB8 in SCON) can be assigned the value of 0 or 1. Or, for example, the parity bit (P, in

the PSW) could be moved into TB8. On receive, the 9th data bit goes into RB8 in

Special Function Register SCON, while the stop bit is ignored. The baud rate is

programmable to either 1/32 or 1/64 the oscillator frequency.

46

Page 47: Final Report

Mode 3:

11 bits are transmitted (through TxD) or received (through RxD): a start bit (0), 8 data

bits (LSB first), a programmable 9th data bit, and a stop bit (1). In fact, Mode 3 is the

same as Mode 2 in all respects except baud rate. The baud rate in Mode 3 is variable.

4-5 SERIAL PORT CONTROL REGISTERThe serial port control and status register is the Special Function Register SCON, shown

in Figure 11. This register contains not only the mode selection bits, but also the 9th data

bit for transmit and receive (TB8 and RB8), and the serial port interrupt bits (TI and RI).

4-6 SERIAL PORT CONTROL ( SCON ) REGISTER

MSB   LSB

SM0 SM1 SM2 REN TB8 RB8 TI RI

Where SM0, SM1 specify the serial port mode, as follows

SM0 SM1 Mode Description Baud Rate

0 0 0 Shift Register fuss/ 12

0 1 1 8 bit UART variable

1 0 2 9 bit UART fosc/64 fosc/32

1 1 3 9 bit UART variable

4-7 SERIAL COMMUNICATION INTERRUPTS

In the 8051 there is only one interrupt set aside for serial communication This interrupt

is used to both send and receive data If the interrupt bit in the IE register (IE.4) is

enabled, when RI or TI is raised the 8051 gets interrupted and jumps to memory location

0023H to execute the ISR In that ISR we must examine the TI and RI flags to see which

one caused the interrupt and respond accordingly. The serial interrupt is used mainly for

receiving data and is never used for sending data serially

47

Page 48: Final Report

Program in which the 8051 reads data from P1 and writes it to P2 continuously while

giving a copy of it to the serial COM port to be transferred serially. Assume that

XTAL=11.0592. Set the baud rate at 9600.

CODE:

ORG

LJMP 0000H

ORG MAIN

LJMP 23H

ORG SERIAL; jump to serial int ISR

MAIN: MOV 30H

MOV P1,#0FFH ;make P1 an input port

MOV TMOD,#20H ;timer 1, auto reload

MOV TH1,#0FDH ;9600 baud rate

MOV SCON,#50H ;8-bit,1 stop, ren enabled

SETB IE,10010000B ;enable serial int.

BACK: MOV TR1;start timer 1

MOV A,P1;read data from port 1

MOV SBUF,A;give a copy to SBUF

SJMP P2,A;send it to P2

BACK;stay in loop indefinitely;-----------------SERIAL PORT ISR

ORG 100H

SERIAL: JB TI,TRANS;jump if TI is high

MOV A,SBUF ;otherwise due to receive

CLR RI;clear RI since CPU doesn’t

RETI;return from ISR

TRANS: CLR TI;clear TI since CPU doesn’t

RETI;return from ISR

END...

48

Page 49: Final Report

Programming the 8051Microcontroller to transfer character bytes serially:

1. TMOD register is loaded with the value20H, indicating the use of timer 1 in

mode (8-bit auto-reload) to set baud rate

1. The TH1 is loaded with one of the values to set baud rate for serial data transfer

2. The SCON register is loaded with the value 50H, indicating serial mode 1, where

an 8- bit data is framed with start and stop bits

3. TR1 is set to 1 to start timer 1

4. TI is cleared by CLR TI instruction

5. The character byte to be transferred serially is written into SBUF register

6. The TI flag bit is monitored with the use of instruction JNB TI,xx to see if the

character has been transferred completely

7. To transfer the next byte, go to step 5

4-8 WRITING TO THE SERIAL PORT

Once the Serial Port has been properly configured as explained above, the serial port is

ready to be used to send data and receive data. If you thought that configuring the serial

port was simple, using the serial port will be a breeze. To write a byte to the serial port

one must simply write the value to the SBUF (99h) SFR. For example, if you wanted to

send the word "yes" to the serial port, it could be accomplished as easily as:

Write a program for the 8051 to transfer “YES” serially at 9600 baud, 8-bit data, 1

stop bit, do this continuously

49

Page 50: Final Report

CODE:

MOV

TMOD,#20H ;timer 1,mode 2(auto reload)

MOV TH1,#-3;9600 baud rate

MOV SCON,#50H ;8-bit, 1 stop, REN enabled

SETB TR1;start timer 1

AGAIN: MOV A,#”Y”;transfer “Y”

ACALL TRANS

MOV A,#”E”;transfer “E”

ACALL TRANS

MOV A,#”S”;transfer “S”

ACALL TRANS

SJMP AGAIN;keep doing it

;serial data transfer subroutine

TRANS: MOV SBUF,A;load SBUF

HERE: JNB TI,HERE;wait for the last bit

CLR TI;get ready for next byte

RET

4-9 READING THE SERIAL PORT

Reading data received by the serial port is equally easy. To read a byte from the serial

port one just needs to read the value stored in the SBUF (99h) SFR after the 8051 has

automatically set the RI flag in SCON.

For example, if your program wants to wait for a character to be received and

subsequently read it into the Accumulator, the following code segment may be used:

JNB RI,$ ;Wait for the 8051 to set the RI flag

MOV A,SBUF ;Read the character from the serial port

50

Page 51: Final Report

The first line of the above code segment waits for the 8051 to set the RI flag; again, the

8051 sets the RI flag automatically when it receives a character via the serial port. So as

long as the bit is not set the program repeats the "JNB" instruction continuously. Once

the RI bit is set upon character reception the above condition automatically fails and

program flow falls through to the "MOV" instruction which reads the value.

4-10 PROGRAMMING

This is the code which is burn in the microcontroller 8051 chip. This software is for

Phillips RC-5 remote decoding and controlling your pc with the help of a TV remote.

Pin used for reception is P3.3 while .you can use any pin as per your requiremnet.This

program is written for 8051 MCU running at 11.0592MHz.

CODE:

INPUT EQU P3.2 ; Port3,Bit2 is used as input. The demodulated signal

; with active low level is connected to this pin

RB0 EQU 000H ; Select Register Bank 0

RB1 EQU 008H ; Select Register Bank 1 ...poke to PSW to use

DSEG ; This is internal data memory

ORG 20H ; Bit addressable memory

FLAGS: DS 1

CONTROL BIT FLAGS.0 ; toggles with every new keystroke

NEW BIT FLAGS.1 ; Bit set when a new command has been received

COMMAND: DS 1 ; Received command byte

51

Page 52: Final Report

SUBAD: DS 1 ; Device sub address

BUFFER: DS 30 ; Buffer to store length of transmitted pulses

TOGGLE: DS 1 ;Toggle every bit

ANS: DS 1 ;

ADDR: DS 1

STACK: DS 1 ; Stack begins here

CSEG ; Code begins here

;---------==========----------==========---------=========---------

; PROCESSOR INTERRUPT AND RESET VECTORS

;---------==========----------==========---------=========---------

ORG 00H ; Reset

JMP MAIN

ORG 0003H ; External Interrupt0

JMP RECEIVE

;---------==========----------==========---------=========---------

; ---------==========----------==========---------=========---------

; Interrupt 0 routine

; ---------==========----------==========---------=========---------

RECEIVE:

CPL P3.4

MOV 2,#235 ; Time Loop (3/4 bit time)

DJNZ 2,$ ; Waste Time to sync second bit

MOV 2,#235 ; Time Loop (3/4 bit time)

Djnz 2,$ ; Waste Time to sync second bit

Mov 2,#134 ; Time Loop (3/4 bit time)

Djnz 2,$ ; Waste Time to sync second bit

52

Page 53: Final Report

clr a

mov r6,#07h

pol1: mov c,Input

rlc a

Mov 2,#235 ; Waste time for next BIT

Djnz 2,$

Mov 2,#235 ; Time Loop (3/4 bit time)

Djnz 2,$ ; Waste Time to sync second bit

Mov 2,#235 ; Time Loop (3/4 bit time)

Djnz 2,$ ; Waste Time to sync second bit

Mov 2,#105 ; Time Loop (3/4 bit time)

Djnz 2,$ ; Waste Time to sync second bit

djnz r6,pol1

MOV SUBAD,A

mov r6,#06h

pol2:

mov c,Input

rlc a

Mov 2,#235 ; Waste time for next BIT

Djnz 2,$

Mov 2,#235 ; Time Loop (3/4 bit time)

Djnz 2,$ ; Waste Time to sync second bit

Mov 2,#235 ; Time Loop (3/4 bit time)

Djnz 2,$ ; Waste Time to sync second bit

Mov 2,#105 ; Time Loop (3/4 bit time)

Djnz 2,$ ; Waste Time to sync second bit

djnz r6,pol2

Mov COMMAND,A ; Save Command at IRData memory

53

Page 54: Final Report

MOV A,SUBAD

MOV ADDR,A

ANL A,#0FH

MOV SUBAD,A

CJNE A,#03H,ZXC1

MOV A,COMMAND

CPL A

MOV COMMAND,A

AJMP ASZ

ZXC1: MOV A,SUBAD

CJNE A,#00H,ANSS

AJMP ASZ

ASZ: MOV A,ADDR

ANL A,#20H

MOV TOGGLE,A

CJNE A,ANS,ANSS

AJMP WAR

ANSS: JMP ANS1

WAR:

MOV TMOD,#20H

MOV TH1,#0FDH

MOV SCON,#50H

SETB TR1

MOV A,COMMAND

MOV P0,A

MOV SBUF,A

54

Page 55: Final Report

JNB TI,$

CLR TI

CLR TR1

MOV ANS,TOGGLE

MOV A,ANS

CPL ACC.5

MOV ANS,A

SETB NEW ; Set flag to indicate the new command

;################################################################

ANS1:

RETI

; ---------==========----------==========---------=========---------

; Main routine. Program execution starts here.

; ---------==========----------==========---------=========---------

MAIN:

MOV SP,#60H

SETB EX0 ; Enable external Interrupt0

CLR IT0 ; triggered by a high to low transition

SETB EA; /* Enable global interrupt */

MOV ANS,#00H ;clear temp toggle bit

CLR NEW

LOO:

55

Page 56: Final Report

JNB NEW,LOO

CLR NEW

AJMP LOO

END

56

Page 57: Final Report

MICROCONTROLLER 8051 BASED PC

REMOTE CONTROL VIA SERIAL PORT

CHAPTER# 5

VISUAL BASIC FOR 8051 MICROCONTROLLER

DEPARTMENT OF ELECTRONIC ENGINEERING

QUAID-E-AWAM UNIVERSITY OF ENGINEERING

SCIENCE AND TECHNOLOGY NAWABSHAH

57

Page 58: Final Report

5-1 VISUAL BASIC 6.0

Visual Basic (VB) is an event driven programming language and associated

development environment from Microsoft for its COM programming model. Visual

Basic was derived from BASIC and enables the rapid application development (RAD) of

graphical user interface (GUI) applications, access to databases using DAO, RDO, or

ADO, and creation of ActiveX controls and objects.

A programmer can put together an application using the components provided with

Visual Basic itself. Programs written in Visual Basic can also use the Windows API, but

doing so requires external function declarations.

5-1-1FEATURESVisual Basic was designed to be easy to learn and use. The language not only allows

programmers to easily create simple GUI applications, but also has the flexibility to

develop fairly complex applications as well. Programming in VB is a combination of

visually arranging components or controls on a form, specifying attributes and actions of

those components, and writing additional lines of code for more functionality. Since

default attributes and actions are defined for the components, a simple program can be

created without the programmer having to write many lines of code. Performance

problems were experienced by earlier versions, but with faster computers and native

code compilation this has become less of an issue.

Although programs can be compiled into native code executables

from version 5 onwards, they still require the presence of runtime libraries of

approximately 2 MB in size. This runtime is included by default in Windows 2000 and

later, but for earlier versions of Windows it must be distributed together with the

executable.

Forms are created using drag and drop techniques. A tool is used to place controls (e.g.,

text boxes, buttons, etc.) on the form (window). Controls have attributes and event

handlers associated with them. Default values are provided when the control is created,

but may be changed by the programmer. Many attribute values can be modified during

run time based on user actions or changes in the environment, providing a dynamic

application. For example, code can be inserted into the form resize event handler to

58

Page 59: Final Report

reposition a control so that it remains centered on the form, expands to fill up the form,

etc. By inserting code into the event handler for a keypress in a text box, the program

can automatically translate the case of the text being entered, or even prevent certain

characters from being inserted.

5-2 SERIAL COMMUNICATION WITH VISUAL

BASIC 6.0

This chapter discusses how Visual Basic can be used to access serial communication

functions. Windows hides much of the complexity of serial communications and

automatically puts any received characters in a receive buffer and characters sent into a

transmission buffer. The receive buffer can be read by the program whenever it has time

and the transmit buffer is emptied when it is free to send characters.

5-2-1COMMUNICATIONS CONTROL Visual Basic allows many additional components to be added to the toolbox. The

Microsoft Comm. component is used to add a serial communication facility.In order

to use the Comm. component the files MSCOMM16.OCX (16-bit module) or

MSCOMM32.OCX (for a 32-bit module) must be present in the WINDOWS SYSTEM

directory. The class name is MSComm. The communications control provides the

following two ways for handling communications: Event-driven. Event-driven

communications is the best method of handling serial communication as it frees the

computer to do other things. The event can be defined as the reception of a character, a

change in CD (carrier detect) or a change in RTS (request to send). The OnComm event

can be used to capture these events. and also to detect communications errors.

Visual Basic uses the standard Windows drivers for the serial communication ports

(such as serialui.dll and serial.vxd). The communication control is added to the

application for each port. The parameters (such as the bit rate, parity, and so on) can be

changed by selecting Control Panel >System >Device Manager  >Ports (COM and

LPT)  >Port Settings. The settings of the communications port (the IRQ and the port

address) can be changed by selecting Control Panel >System  >Device Manager  > Ports

(COM and LPT)  > Resources for IRQ and Addresses.

59

Page 60: Final Report

5-2-2 PROPERTIES OF COMMUNICATION PORT

CONTROL

The Comm component is added to a form whenever serial communications are required.

By default, the first created object is named MSComm1 (the second is named

MSComm2, and so on). It can be seen that the main properties of the object are:

Comport, DTREnable, EOFEnable, Handshaking, InBufferSize, Index, InputLen,

InputMode, Left, Name, NullDiscard, OutBufferSize, ParityReplace, RThreshold,

RTSEnable, Settings, SThreshold, Tag and Top.

Settings

    The Settings property sets and returns the RS-232 parameters, such as baud rate,

parity, the number of data bit, and the number of stop bits. Its syntax is:

[form.]MSComm.Settings = setStr[$]

where the strStr is a string which contains the RS-232 settings. This string takes the

form:

"BBBB,P,D,S"

where

BBBBdefines the baud rate,

P the parity,

D the number of data bits, and

S the number of stop bits.

The following lists the valid baud rates (default is 9600Baud):

110, 300, 600, 1200, 2400, 9600, 14400, 19200, 38400, 56000, 128000, 256000.

The valid parity values are (default is N): E (Even), M (Mark), N (None), O (Odd),

S(Space).

The valid data bit values are (default is 8): 4, 5, 6, 7 or 8.

The valid stop bit values are (default is 1). 1, 1.5 or 2.

60

Page 61: Final Report

An example of setting a control port to 4800Baud, even parity, 7 data bits and 1 stop bit

is:

Com1.Settings = "4800,E,7,1"

5-3 COMM PORT

The CommPort property sets and returns the communication port number. Its syntax is:

[form.]MSComm.CommPort = portNumber[%]

which defines the portNumber from a value between 1 and 99. A value of 68 is

returnedif the port does not exist.

PortOpen

The PortOpen property sets and returns the state of the  communications port. Its syntax

is:

[form.]MSComm.PortOpen = [{True | False}]

A True setting opens the port, while a False closes the port and clears the receive

andtransmit buffers (this automatically happens when an application is closed).

The following example opens communications port number 1 (COM1:) at 4800 Baud

with even parity, 7 data bits and 1 stop bit:

5-4 INPUTTING DATA

The three main properties used to read data from the receive buffer are Input, InBuffer

Count and InBufferSize.

Input

The Input property returns and removes a string of characters from the receive buffer. Its

syntax is:

61

Page 62: Final Report

[form.]MSComm.Input

To determine the number of characters in the buffer the InBufferCount property is tested

(to be covered in the next section). Setting InputLen to 0 causes the Input property to

read the entire contents of the receive buffer.

InBufferSize

The InBufferSize property sets and returns the maximum number of characters that can

be received in the receive buffer (by default it is 1024 bytes). Its syntax is:

[form.]MSCommInBufferSize = [numBytes%]

The size of the buffer should be set so that it can store the maximum number of

characters that will be received before the application program can read them from the

buffer.

The InBufferCount property returns the number of characters in the receive buffer. It can

also be used to clear the buffer by setting the number of characters to 0. Its syntax is:

[form.]MSCommInBufferCount= [count%]

5-5 OUTPUTTING DATAThe three main properties used to write data to the transmit buffer are Output,

OutBufferCount and OutBufferSize.

The Output property writes a string of characters to the transmit buffer. Its syntax is:

[form.]MSComm. output= [outString$]

OutBufferSize

The OutBufferSize property sets and returns the number of characters in the transmit

buffer (default size is 512 characters). Its syntax is:

[form.]MSCommOutBuffer size = [NumBytes%]

OutBufferCount

The OutBufferCount property returns the number of characters in the transmit

buffer.The transmit buffer can also be cleared by setting it to 0. Its syntax is:

[form.]MSCommOutBufferCount. = [0]

62

Page 63: Final Report

5-6 Getting started with PC remote control

For the working of PC remote control we have to attach the PC remote control receiver

circuit to one of the Personal computer COMM port. The PC which we want to control

remotely must have Windows XP and Visual Basic 6 library files so the software of pc

remote control works properly.

Step 1

Attach the PC comm. port 1 or 2 with RS232 connector of PC remote control receiver

Circuit.

Step 2

Run the software of Pc Remote Control.exe

Step 3

Select the availed port

Step 4

Press any key from the remote handset at that time IR receiver module receives the data

sent by remote handset. The microcontroller decodes the infrared signal data and the

microcontroller will sent the infrared Key code to the PC through the Serial port.

63

Page 64: Final Report

Step 5

The PC remote control software receives the Key code through the Component called

MSCOMM which is a component for the Com port control. Through this component we

get the key code and do the appropriate functions.

Step 6

After getting this key code of the Remote control the program compare the key code

with the code present in the program and if they are equal then the corresponding

function was done.

64

Page 65: Final Report

NOTE: If the software gives an error of missing file "MSCOMM32.ocx", the use

the ox file supplied in the zip file and follows the instructions below:

1. Copy MSCOMM32.ocx in "c:\windows\system" folder.

2. Go to Start-> run and type "regsvr32 mscomm32.ocx" and hit enter.

"It will give you a success dialog.

65

Page 66: Final Report

References

www.8051projects.info

www.8051projects.net

66