0 Bewertungen0% fanden dieses Dokument nützlich (0 Abstimmungen)
25 Ansichten64 Seiten
Submitted in the fulfillment of partial requirement for the degree of Bachelor of electronic Engineering. Title thesis is "microcontroller 8051 Based Pc Remote Control via serial port"
Submitted in the fulfillment of partial requirement for the degree of Bachelor of electronic Engineering. Title thesis is "microcontroller 8051 Based Pc Remote Control via serial port"
Copyright:
Attribution Non-Commercial (BY-NC)
Verfügbare Formate
Als TXT, PDF, TXT herunterladen oder online auf Scribd lesen
Submitted in the fulfillment of partial requirement for the degree of Bachelor of electronic Engineering. Title thesis is "microcontroller 8051 Based Pc Remote Control via serial port"
Copyright:
Attribution Non-Commercial (BY-NC)
Verfügbare Formate
Als TXT, PDF, TXT herunterladen oder online auf Scribd lesen
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 o f Electronic Engineering Microcontroller 8051 based PC Remote Control via Serial Port Project Supervisor ENGR. Abdul Rafay Khatri BY ENGR. WAHAB UDDIN NAZAR (GROUP LEADER) ENGR. SAMI CHANNA (ASST: GROUP LEADER) (06ES56) (06ES50) MEMBERS ENGR. WASIM ABBAS ENGR.LATEEF GUL ENGR. SHERYAR JAMALI (06ES51) (06ES20) (06ES11) 1 CERTIFICATE This is certify that Mr.___________________________________ S/O_________________ _______ Roll No.________ Final Year student of Bachelor of Electronic Engineerin g has completed the compulsory requirement of Project/Thesis during session; 200 8 2009.The title thesis is “Microcontroller 8051 Based Pc Remote Control via Ser ial Port”. It is submitted to the Quaid-e-Awam University of Engineering science & Technology Nawabshah for award of the Degree of Bachelor of Electronic Engine ering. ENGR. ABDUL RAFAY KHATRI SUPERVISOR PROF: MUEEN DIN MEMEON CHAIRMAN DEPARTMENT OF ELECTRONIC ENGINEERING QUAID-E-AWAM UNIVERSITY OF ENGINEE RING SCIENCE AND TECHNOLOGY NAWABSHAH ACKNOWLEDGEMENT 2 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. W e would like to express my sincere gratitude to my project supervisor, Mr. Abdul Rafay Khatri lecturer, for his useful information, help, guidance and construct ive comments towards the completion of this project. With his supported we can d o this project and finish it. And we would like to say thank you to our family b ecause 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 ABSTRACT This final report is established to fulfill the Final year Project/Thesis requir ement for Engineering for final semester student, a standard guideline is prepar ed 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 de vice that designed. We introduce a method of how electronics devices and machine s can be controlled remotely from an operator/user. As a practical model we desi gn a microcontroller 8051 based receiver circuit may connected to Personal compu ter. This project is an implementation of RC5-remote reception on an 8051 microc ontroller. The received code is decoded and sent to the PC IR remote software de veloped using high level graphical programming language (Visual Basic). The curs or position is moved according to the keys pressed. The project report focuses o n the system which is designed by using the Microcontroller AT89c2051. The infra red receiver have three pins in which two pins are for +5v supply and ground whi le the third pin is for data output. The Infrared Receiver is designed for demod ulating 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 conv erts it to MCU compatible voltage format and outputs it on its data output pin. The microcontroller decodes the infrared signal data and the microcontroller wil l 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 wh ich is a component for the Com port control. Through this component we get the k ey code and do the appropriate functions. After getting this key code of the Rem ote control the program compare the key code with the code present in the progra m and if they are equal then the corresponding function was done. CHAPTER 1 4 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 REMOT E CONTROL IN WWI & WWII…………..………11 1-3-3 INFRARED DEVICES REPLACED THE ULTRASONI C RC………………………11 1-4 HOW A SIMPLE REMOTE CONTROL WORKS………………………………………12 1-4-1 INF RARED REMOTE CONTROL: THE PROCESS……………………….…………12 1-5 EMBEDED SYSTEM DESIGN CHAL LENGES………………………….……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……………………………………1 5 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 CHAPTER 2 COMPONENTS AND PARTS USED IN THE PROJECT 2-1 MICRO-CONTROLLER…………………………………………………….…………18 5 2-1-1 MICRO-CONTROLLER: WHY? WHAT? HOW?......................................... ............18 2-1-2 CHOSSING A MICRO-CONTROLLER………………………………….…………18 2-1-3 HISTO RY 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 2-5 POWER SUPPLY……………………………………………………………..………27 2-5-1 PIN CONGIGURA TION……………………………………………………………27 2-6 COMPONENT LISTING…………………………………………………..…………28 CHAPTER 3 PROGRAMMING AND INTERFACING OF MICROCONTROLLER 8051 3-1 PROGRAMS THE 8051 MICROCONTROLLER…………………………………………30 6 3-1-1 PRE-REQUEST FOR PROGRAMMING…………………………………………..……30 3-1-2 INTRODUCTION TO AS SEMBLY LANGUAGE…………………………...…………30 3-1-3 ASSEMBLY LANGUAGE AND OTHER COMPUTER LA NGUAGES………………30 3-1-4 STRUCTURE OF ASSEMBLY PROGRAMMING……………………………..………31 3-1-5 ASSEMBLY CODE OVERVEIW……………………………………………….………31 3-1-6ARITHEMATIC AND LOGIC INSTRU CTIONS……………………………….………31 3-1-7 ADDRESSING MODES………………………………………………………………….32 3-1- 8 ASSEMBLING AND RUNNING AN 8051 PROGRAM:……………………………….32 3-1-9 I/O PORT PROGRAMM ING………………………………………………….…………32 3-1-10 INTERRUPTS PROGRAMMING……………………………………………………… 34 3-2 MICROCONTROLLER 8051 INTERFACING TECHNIQUES…………………..………36 3-2-1INTERFACIN G 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 INTE RFACE………………………………………….…………44 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 7 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 COMMUNIC ATION 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 MICROCONTROLLER 8051 BASED PC REMOTE CONTROL VIA SERIAL PORT 8 CHAPTER# 1 MICROCONTROLLER BASED PC REMOTE CONTROL DEPARTMENT OF ELECTRONIC ENGINEERING QUAID-E-AWAM UNIVERSITY OF ENGINEERING SCIE NCE AND TECHNOLOGY NAWABSHAH CHAPTER-1 MICROCONTROLLER BASED PC REMOTE CONTROL 1-1 INTRODUCTION This chapter explains about the Microcontroller based Pc remote control ,motivat ion to build this project ,a short history of remote control world .Remote contr ol are mostly infra red which plays a revolutionary vital role in changing our l ives. This project 9 contains all about how to make a system to control your Personal computer remote ly with almost any remote controller you can find in your house TV Sets. Along t his 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 th e 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 contr ol. 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 pan el Computer log off, reboot or shutdown Volume Up, down, Mute 5 User defined Pro grams >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 >C ontrol panel >Computer log off, reboot or shutdown >Volume Up, down, Mute >5 Use r 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 Zenit h Electronics Corporation (then known as Zenith Radio Corporation). Lazy Bones u sed a cable that ran from the TV set to the viewer. A motor in the TV set operat ed the tuner through the remote control. Although customers liked having remote control of their 10 television, they complained that people tripped over the unsightly cable that me andered 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 militar y 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 wea pons were used in WW II. Once the wars were over, United States scientists exper imented 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 v acuum tubes. Hand-held, battery-powered control units could now be designed to g enerate the inaudible sound electronically. In this modified form, Dr. Adler s u ltrasonic remote control invention lasted through the early 1980s, a quarter cen tury 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 cablecompatible tuning and tale text technol ogies in the 1980s greatly enhanced the capabilities and uses for infrared TV re motes. 1-4 HOW A SIMPLE REMOTE CONTROL WORKS For the purpose of designing a PC remote control we should aware to the basic fu ndamentals 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 11 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 mic rocontroller can understand. The microcontroller then carries out the correspond ing command. The basic parts involved in sending an IR signal include: • • • • B uttons 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 caus es the controlled device to carry out a command. The process works something lik e 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. 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 follo wing 7-bit binary commands: Button 1 2 3 4 Channel up Channel down Power on Powe r off Volume up `Volume down Code 000 0000 000 0001 000 0010 000 0011 001 0000 0 01 0001 001 0101 010 1111 001 0010 1 0011 12 The remote signal includes more than the command for "volume up," though. It car ries 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 ser ies 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 i t s supposed to carry out this command, it converts the light pulses back into t he electrical signal for 001 0010. . It then passes this signal to the microcont roller, which goes about increasing the volume. The "stop" command tells the mic roprocessor 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 mechanic al or other parts, designed to perform a specific function within a given time f rame. 13 .1-5-1 SHORTER DEVELOPMENT TIME An embedded application not only saves money but also shorten development time s ince 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 mean s that developing our windows based embedded product reduces the cost & shorten the developing time considerably. 1-5-2 LOWER MANUFACTURING COST Our goal to complete our project in the minimum availability of resources and co st to over come the market needs. Because we use microcontrollers they have a fi xed amount of RAM Rom and I/O ports. Makes them ideal for PC remote control in w hich 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 cons umes. 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 condit ion, 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 t he market of home automation. 1-6-2 ON INDUSTRIAL BASIS 14 Remote controllers are the amazing applications and commercial opportunities in many industries for example in auto industry they are used to lock and unlock th e car doors, car parking with a remote control .Remote controls are the major pa rt of industries in any perspectives 1-7 WHY USING SERIAL PORT INSTEAD OF PARALLEL PORT We use serial port in our project to communicate with PC which has some advantag es over Parallel port 1. The serial port cable can be longer than a parallel por t cable, as serial port transmits 1 as voltage from -5 to -12V and 0 as volt age from +5 to +12 V, while parallel port transmits 1 as voltage of 5 volts an d 0 as voltage of 0 volts. At the same time the receiver of the serial port re ceives 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 maxi mal 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 parall el port. 2. The number of wires needed when transmitting data serially is less t han when the transmission is parallel. Is the external device has to be installe d at a great distance from the computer, the cable with three wires is much chea per than the cable with 19 or 25 wires if the transmission is parallel. Still on e 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 sourc e via fiber, satellite, and wireless to you and the devices in your home. The el ectrical company 15 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 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 16 DEPARTMENT OF ELECTRONIC ENGINEERING QUAID-E-AWAM UNIVERSITY OF ENGINEERING SCIE NCE 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 pr oject and the criteria to choosing these parts. All parts can found in market wi th 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 choo sing. 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 b ecause micro-controllers are cost effective and small enough to fit into our des ign. 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 8 051, 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 m icrocontrollers our criteria to consider in choosing one? Three criteria in choo sing one are as follows 17 MEETING THE COMPUTING NEEDS OF THE TASK AT HAND EFFICEINTLY AND COST EFFECTIVE I n 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 amoun t of I/O pins and the timer on the chip How it is easy to upgrade to higher pe rformance or lower power consumption versions Cost per unit Availability of so ftware development tools Availability of an assembler, debugger, a code-efficien t C language compiler, emulator, technical support and both in house and out sid e house expertise Availability and reliable resources of the microcontroller The third criterion in choosing a microcontroller is its availability in needed qua ntities both now in the future. In this broad sense of choosing a microcontrolle r we chose 8051. We chose the 8051 because of its popularity and cost effectiven ess. Moreover there are plenty of public domain software as well as commercial l ibraries 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 designe d for the embedded control market in the 80s. As popular standards especially so ftware standards don’t easily die down, it flourishes for years and becomes one of the most popular 8-bit micro-controllers worldwide. 18 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-bi t 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 i s an economical and cost-effective member of Atmel s growing family of microcont rollers. It contains 2 Kbytes of flash program memory. It is fully compatible wi th the MCS-51 architecture, and can be programmed using the MCS-51 instruction s et. 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 con troller user to know the physical features and limitations of the device being u sed 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 19 (AIN0) and the negative input (AIN1), respectively, of the on-chip precision ana log 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 t he on-chip comparator and is not accessible as a general purpose I/O pin. The Po rt 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 t he 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 interr upt 0) INT1 (external interrupt 1) T0 (timer 0 external input) T1 (timer 1 exter nal input) RST Reset input. All I/O pins are reset to 1s as soon as RST goes hig h. Holding the RST pin high for two machine cycles while the oscillator is runni ng resets the device. Each machine cycle takes 12 oscillator or clock cycles. XT AL1 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 MICROCONTROLLER BASED SYSTEM Micro-controller system has the following configuration • • The microprocessor i s connected to a clock, which determines the operating frequency of the system A fter the reset switch is depressed, the microprocessor first loads the content o f a startup location from the startup ROM (0000H for 8051). 2-1-7 THE 8051 SYSTEM ARRCHITECTURE To understand the operation of the 8051 based system, we need to study how 20 data and programs are stored in the system. Here we will examine two memory stor age 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 b ytes 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 21 RS232 is a popular communications protocol for connecting modems and data acquis ition devices to computers. RS232 devices can be plugged straight into the compu ter 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 seri al 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 th at 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. DTR signals: - DT R is used by the computer to signal that it is ready to communicate with the ser ial device like modem. In other words, DTR indicates to the Dataset (i.e., the m odem or DSU/CSU) that the DTE (computer) is ON. 22 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 mod em CTS: - This pin is used by the serial device to acknowledge the computer s RT S Signal. In most situations, RTS and CTS are constantly on throughout the commu nication session. 23 RI: - RI stands for Ring Indicator. A modem toggles (keystroke) the state of thi s line when an incoming call rings your phone. In other words, this is used by a n 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 a lso 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 FIG 2-5: RS232 Specification chart 2-3 MAX232 Microcontroller 8051 works on CMOS voltage levels which cannot be used to commun icate 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 RS2 32 level converter is MAX232. This IC includes charge pump which can generate 24 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 communica tion. 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 Singl e 5-V Power Supply With 1.0-_F Charge-Pump Capacitors Operates Up To 120 Kit/s T wo Drivers and Two Receivers ±30-V Input Levels Low Supply Current . . . 8 mA Ty pical • • • FIG 2-6: MAX232 Pin outs 2-4 TSOP1738 TSOP1738 is an Infrared (IR) receiver which is widely used in large number of el ectronic 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. 25 FIG 2-7: TSOP1738 IC 2-4-1 PIN CONFIGURATION 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 co ndition. 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 26 Fig: 2-8:7804 regulated IC 2-6 COMPONENT LISTING No 1 2 3 4 5 6 7 8 9 10 11 12 Component AT89C2051 RS232 Connector(FEMALE) RS232 CABLE Capacitor (33pF) Capacitor 0.1 uF Diode 1N4148 Capacitor 10 uF Crystal (11 .0592 MHz) 7805 ic Toggle Switch TSOP 1738 PHILIPS RC5 HAND SET Units 1 1 1 2 2 2 5 1 1 1 1 1 MICROCONTROLLER 8051 BASED PC REMOTE CONTROL VIA SERIAL PORT 27 CHAPTER# 3 PROGRAMMING AND INTERFACING OF MICROCONTROLLER 8051 DEPARTMENT OF ELECTRONIC ENGINEERING QUAID-E-AWAM UNIVERSITY OF ENGINEERING SCIE NCE 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 (Pe rsonal Computer) PC through LPT port. We assumed that after figure out the progr amming 28 issues we are able to design out project circuit using different interfacing tec hniques of 8051. 3-1 PROGRAMS THE 8051 MICROCONTROLLER The scope of this work confirming the 8051 microcontroller input/output (I/O) si gnals are compatible with our IR proximity sensor. We also learnt and familiariz ed with 8051 using assembly language and converting the assembly language code t o hexadecimal code using a development board. 3-1-1 PRE-REQUEST FOR PROGRAMMING 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 microcont roller s machine language. Each assembly language instruction has a one-toone re lation 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 ste ps above assembly language in that no significant knowledge of the underlying ar chitecture 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 cons ist of four fields. 29 (Label:) mnemonic (operands) (;comment) 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 gi ven to individual Oppcodes. 3-1-6ARITHEMATIC AND LOGIC INSTRUCTIONS Processor instructions are often classified into groups, such as: Data transfe r 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 addres sing: The content of the named register (R0-R7) is used and the least significan t bits of the Oppcodes specify the register (e.g. MOV R0,A) Direct addressing: T he 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 a ddress 30 (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 i nstruction is included directly in the instruction syntax and in the program mem ory (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 Not epad, the source program has the extension “asm” or “src”. 2. The” asm” source f ile is fed to an 8051 assembler which converts the instructions into machine cod e. 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. ”ab s” 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. 31 FIG 3-2: Steps to create a program Port 1: 32 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 save d R7 and. R5. Port 3: Port 3 can be used as input or out put and does not need any pull up res istors .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 microcontrolle r to inform it that a device needs its service. Interrupt system of 8051 There a re five interrupt sources in order to priority. i. External interrupt 0 ii. Time r 0 iii. External interrupt 1 iv. Timer 1 v. Serial Port While programming the i nterrupts some points should be remember Each interrupt can be enabled separat ely 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 IE: Interrupt enable register 33 FIG 3-3: IE Register EA: Global interrupt enable ES: Serial Interface ET1, ET0: Enable Interrupt Time r 1, Timer 0 EX0, EX1: External interrupt 0, 1 Interrupts Rom Location(Hex) Rese t External hardware interrupt 0(INT0) Timer 0 interrupt (TF0) External hardware interrupt 1(INT1) Timer 1 interrupt(TF1) Serial COM interrupt (RI and TI) 001b 0 023 0013 000b P3.3(13) 0000 0003 9 P3.2(12) Pin 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 . 34 FIG 3-5: IP Register PS: serial interface PT1: Timer 1 PX1: External Interrupt 1 PT0: Timer 0 PX0: Ex ternal 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 devi ces, such as sensors, motors, switches, keypads, displays, memory and even other microcontrollers. Microcontroller system can be viewed as a system that reads f rom (monitors) inputs, performs processing and writes to (controls) outputs. The Digital input/outputs compensating a larger advantage over analog input and out put. In digital interface the microcontroller continuously reading monitor and c ontrol the status of button or switch. They are simplest to interface. They have lower cost to implement high speed and lower programming over head. 35 FIG: 3-6: Microcontroller 8051 interface techniques classification 3-2-1INTERFACING WITH RS232 AND MAX232 8051 consist of a built-in serial port for interfacing the serial I/O devices. W e 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 no rmal RS232 cable can be used to connect between a PC and the 8051.The RS232 cabl e 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. T he 8051 uses the TTL voltage levels where a 5V would indicate a high while a 0 i ndicates 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 8 051 and the RS232 have to done through line divers(Max232). Line drivers basical ly function to convert between the two different voltage levels so that a high o r low as understand by the 8051 also means a high or low to the RS232 and vice v ersa.. RxD and TxD pins in the 8051 36 The 8051 has two pins that are used specially for transferring and receiving dat a 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) FIG 3-8: Block diagram o f PC Remote control receiver circuit 37 3-2-2 SENSORS 38 Sensors are electronics or electrical devices that change in some significant wa y in response to an applied stimulus. Sensors, transducers and electrodes are us ed 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 projec t, it has an IR sensor which act as IR receiver with a built-in 38 KHz demodulat or. We choose TSOP1738 because it compatible to 8051 microcontroller TTL logic l evel. The demodulated output signal can be directly decoded by the microcontroll er. FIG 3-9: Interfacing 8051 microcontroller to TSOP1738 FIG 3-10: Schematic circuit of TSOP1738 application 39 FIG 3-11: Circuit Diagram of PC Remote Control 40 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 SCIE NCE AND TECHNOLOGY NAWABSHAH CHAPTER # 4 41 SERIAL COMMUNICATION AND PROGRAMMING THE SERIAL PORT Computer transfers data in two ways: parallel and serial. In parallel data trans fers 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 ti me. In this chapter we discuss the basics of serial communication and its progra mming 4-1 BASICS OF SERIAL COMMUNICATION When a microcontrollers communicate with the out side world’s .it provides the d ata in byte-sized chunks. it uses a single data line instead of 8-bit line of pa rallel communication makes it cheaper and further used in long distance communic ation. For serial data communication to work, the byte of data must be covered t o serial bits using a parallel-in-serial-out shift register; then it can be tran smitted over a single data line Serial data communication uses two methods I. As ynchronous II. Synchronous The synchronous method transfer a block of data(chara cters) 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 t edious and long. For this reason, there are special IC chips made by many manufa cturers for serial communication. These chips are commonly referred to as UART(u niversal asynchronous receiver-transmitter) and USART (universal synchronousasyn chronous recover-transmitter).The 8051 chips has a built-in UART 4-2 STANDARD SERIAL INTERFACE The serial port is full duplex, meaning it can transmit and receive simultaneous ly. It is also receive-buffered, meaning it can commence reception of a second b yte before a previously received byte has been read from the register The serial port receive and 42 transmit registers are both accessed at Special Function Register SBUF. Writing to SBUF loads the transmit register, and reading SBUF accesses a physically sepa rate receive register. FIG 4-1: Standard interface of port RS232 to PC comm. 4-3 INTERFACING THE SERIAL PORT TO PC To allow data transfer between the PC and an 8051 system without any error, we m ust 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 b elow 43 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 thr ough RxD. TxD outputs the shift clock. 8 bits are transmitted/received (LSB firs t). 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 bi ts (LSB first), and a stop bit (1). On receive; the stop bit goes into RB8 in Sp ecial Function Register SCON. The baud rate is variable. Mode 2: 11 bits are tra nsmitted (through TxD) or received (through RxD): start bit (0), 8 data bits (LS B 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 bi t 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. Mode 3: 11 bits are transmitted (through TxD) or received (through RxD): a star t bit (0), 8 data 44 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 REGISTER The serial port control and status register is the Special Function Register SCO N, 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 SM0 SM1 SM2 REN TB8 RB8 TI LSB RI Where SM0, SM1 specify the serial port mode, as follows SM0 0 0 1 1 SM1 0 1 0 1 Mode 0 1 2 3 Description Shift Register 8 bit UART 9 bit UART 9 bit UART Baud Ra te fuss/ 12 variable fosc/64 fosc/32 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 r egister (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 t he 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 s ending data serially Program in which the 8051 reads data from P1 and writes it to P2 continuously wh ile giving a copy of it to the serial COM port to be transferred serially. Assum e that XTAL=11.0592. Set the baud rate at 9600. CODE: ORG LJMP 0000H 45 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 b aud rate MOV SCON,#50H ;8-bit,1 stop, ren enabled SETB IE,10010000B ;enable seri al int. BACK: MOV TR1;start timer 1 MOV A,P1;read data from port 1 MOV SBUF,A;gi ve 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 fro m ISR TRANS: CLR TI;clear TI since CPU doesn’t RETI;return from ISR END... Programming the 8051Microcontroller to transfer character bytes serially: 1. TMO D 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 t o 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 46 3. TR1 is set to 1 to start timer 1 4. TI is cleared by CLR TI instruction 5. Th e 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 ch aracter 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 con figuring 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 “YE S” serially at 9600 baud, 8-bit data, 1 stop bit, do this continuously 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”;transfe r “Y” ACALL TRANS 47 MOV A,#”E”;transfer “E” ACALL TRANS MOV A,#”S”;transfer “S” ACALL TRANS SJMP AGA IN;keep doing it ;serial data transfer subroutine TRANS: MOV SBUF,A;load SBUF HE RE: 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 th e serial port one just needs to read the value stored in the SBUF (99h) SFR afte r the 8051 has automatically set the RI flag in SCON. For example, if your progr am wants to wait for a character to be received and subsequently read it into th e Accumulator, the following code segment may be used: JNB RI,$ ;Wait for the 80 51 to set the RI flag MOV A,SBUF ;Read the character from the serial port The fi rst 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 se rial port. So as long as the bit is not set the program repeats the "JNB" instru ction continuously. Once the RI bit is set upon character reception the above co ndition automatically fails and program flow falls through to the "MOV" instruct ion which reads the value. 4-10 PROGRAMMING This is the code which is burn in the microcontroller 8051 chip. This software i s for Phillips RC-5 remote decoding and controlling your pc with the help of a T V remote. 48 Pin used for reception is P3.3 while .you can use any pin as per your requiremne t.This program is written for 8051 MCU running at 11.0592MHz. CODE: INPUT EQU RB0 RB1 EQU EQU P3.2 000H 008H ; Port3,Bit2 is used as input. The demodulated signal ; Select Register Bank 0 ; Select Register Bank 1 ...poke to PSW to use ; with active low level is connected to this pin DSEG ORG 20H ; This is internal data memory ; Bit addressable memory 1 FLAGS.0 ; toggles with every new keystroke FLAGS.1 ; Bit set when a new command has been received FLAGS: DS CONTROL BIT NEW BIT COMMAND: DS SUBAD: DS BUFFER: DS TOGGLE: DS ANS: DS ADDR: STACK: DS CSEG 1 DS 1 1 30 1 1 ; Received command byte ; Device sub address ; Buffer to store length of transmi tted pulses ;Toggle every bit ; 1 ; Stack begins here ; Code begins here ;---------==========----------==========---------=========--------; PROCESSOR IN TERRUPT AND RESET VECTORS ;---------==========----------==========---------===== ====--------49 ORG JMP ORG JMP 00H MAIN ; Reset 0003H ; External Interrupt0 RECEIVE ;---------==========----------==========---------=========--------; ---------=== =======----------==========---------=========--------; Interrupt 0 routine ; --- ------==========----------==========---------=========--------RECEIVE: CPL P3.4 MOV 2,#235 DJNZ 2,$ MOV 2,#235 Djnz 2,$ Mov 2,#134 Djnz 2,$ clr mov pol1: mov rl c a r6,#07h c,Input a ; Waste time for next BIT ; Time Loop (3/4 bit time) ; Was te Time to sync second bit ; Time Loop (3/4 bit time) ; Waste Time to sync secon d bit ; Time Loop (3/4 bit time) ; Waste Time to sync second bit 50 ; Time Loop (3/4 bit time) ; Waste Time to sync second bit ; Time Loop (3/4 bit time) ; Waste Time to sync second bit ; Time Loop (3/4 bit time) ; Waste Time to sync second bit Mov 2,#235 Djnz 2,$ Mov 2,#235 Djnz 2,$ Mov 2,#235 Djnz 2,$ Mov 2,#105 Djnz 2,$ djnz r6,pol1 MOV SUBAD,A mov pol2: mov rlc c,Input a ; Waste time for next BIT ; Time Loop (3 /4 bit time) ; Waste Time to sync second bit ; Time Loop (3/4 bit time) ; Waste Time to sync second bit ; Time Loop (3/4 bit time) ; Waste Time to sync second b it ; Save Command at IRData memory r6,#06h Mov 2,#235 Djnz 2,$ Mov 2,#235 Djnz 2,$ Mov 2,#235 Djnz 2,$ Mov 2,#105 Djnz 2,$ djnz r6,pol2 Mov COMMAND,A 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 51 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 M OV SBUF,A JNB TI,$ CLR TI CLR TR1 MOV ANS,TOGGLE MOV A,ANS CPL ACC.5 MOV ANS,A S ETB NEW ; Set flag to indicate the new command ;################################################################ ANS1: RETI 52 ; ---------==========----------==========---------=========--------; Main routin e. Program execution starts here. ; ---------==========----------==========----- ----=========--------MAIN: MOV SP,#60H SETB EX0 CLR IT0 ; Enable external Interr upt0 ; triggered by a high to low transition SETB EA; /* Enable global interrupt */ MOV ANS,#00H CLR NEW ;clear temp toggle b it LOO: JNB NEW,LOO CLR NEW AJMP LOO END 53 MICROCONTROLLER 8051 BASED PC REMOTE CONTROL VIA SERIAL PORT CHAPTER# 5 VISUAL BASIC FOR 8051 MICROCONTROLLER 54 DEPARTMENT OF ELECTRONIC ENGINEERING QUAID-E-AWAM UNIVERSITY OF ENGINEERING SCIE NCE AND TECHNOLOGY NAWABSHAH 5-1 VISUAL BASIC 6.0 Visual Basic (VB) is an event driven programming language and associated develop ment environment from Microsoft for its COM programming model. Visual Basic was derived from BASIC and enables the rapid application development (RAD) of graphi cal user interface (GUI) applications, access to databases using DAO, RDO, or AD O, and creation of ActiveX controls and objects. A programmer can put together a n application using the components provided with Visual Basic itself. Programs w ritten in Visual Basic can also use the Windows API, but doing so requires exter nal function declarations. 5-1-1FEATURES Visual Basic was designed to be easy to learn and use. The language not only all ows programmers to easily create simple GUI applications, but also has the flexi bility to develop fairly complex applications as well. Programming in VB is a co mbination of visually arranging components or controls on a form, specifying att ributes and actions of those components, and writing additional lines of code fo r more functionality. Since default attributes and actions are defined for the c omponents, a simple program can be created without the programmer having to writ e many lines of code. Performance problems were experienced by earlier versions, but with faster computers and native 55 code compilation this has become less of an issue. Although programs can be comp iled 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 in cluded 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 dr ag and drop techniques. A tool is used to place controls (e.g., text boxes, butt ons, etc.) on the form (window). Controls have attributes and event handlers ass ociated 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 r un 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 handl er to reposition a control so that it remains centered on the form, expands to f ill 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 communicati on functions. Windows hides much of the complexity of serial communications and automatically puts any received characters in a receive buffer and characters se nt into a transmission buffer. The receive buffer can be read by the program whe never it has time and the transmit buffer is emptied when it is free to send cha racters. 5-2-1COMMUNICATIONS CONTROL Visual Basic allows many additional components to be added to the toolbox. The M icrosoft 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.O CX (for a 32-bit module) must be present in the WINDOWS SYSTEM directory. The cl ass name is MSComm. The communications control provides the following two ways f or handling communications: Event-driven. Event-driven 56 communications is the best method of handling serial communication as it frees t he computer to do other things. The event can be defined as the reception of a c haracter, 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 communi cations errors. Visual Basic uses the standard Windows drivers for the serial co mmunication ports (such as serialui.dll and serial.vxd). The communication contr ol is added to the application for each port. The parameters (such as the bit ra te, 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. 5-2-2 PROPERTIES OF COMMUNICATION PORT CONTROL The Comm component is added to a form whenever serial communications are require d. By default, the first created object is named MSComm1 (the second is named MS Comm2, and so on). It can be seen that the main properties of the object are: Co mport, DTREnable, EOFEnable, Handshaking, InBufferSize, Index, InputLen, InputMo de, 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: [fo rm.]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, 57 P the parity, D the number of data bits, and S the number of stop bits. The foll owing lists the valid baud rates (default is 9600Baud): 110, 300, 600, 1200, 240 0, 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. An example of setting a control port to 4800Baud, ev en 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 p ort, while a False closes the port and clears the receive andtransmit buffers (t his 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, I nBuffer 58 Count and InBufferSize. Input The Input property returns and removes a string of characters from the receive b uffer. Its syntax is: [form.]MSComm.Input To determine the number of characters in the buffer the InBu fferCount property is tested (to be covered in the next section). Setting InputL en to 0 causes the Input property to read the entire contents of the receive buf fer. 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 se t so that it can store the maximum number of characters that will be received be fore the application program can read them from the buffer. The InBufferCount pr operty returns the number of characters in the receive buffer. It can also be us ed to clear the buffer by setting the number of characters to 0. Its syntax is: [form.]MSCommInBufferCount= [count%] 5-5 OUTPUTTING DATA The three main properties used to write data to the transmit buffer are Output, OutBufferCount and OutBufferSize. The Output property writes a string of charact ers to the transmit buffer. Its syntax is: [form.]MSComm. output= [outString$] OutBufferSize The OutBufferSize property sets and returns the number of characters in the tran smit buffer (default size is 512 characters). Its syntax is: [form.]MSCommOutBuf fer size = [NumBytes%] OutBufferCount The OutBufferCount property returns the number of characters in the transmit 59 buffer.The transmit buffer can also be cleared by setting it to 0. Its syntax is : [form.]MSCommOutBufferCount. = [0] 5-6 Getting started with PC remote control For the working of PC remote control we have to attach the PC remote control rec eiver 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 s oftware 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 t he software of Pc Remote Control.exe Step 3 Select the availed port 60 Step 4 Press any key from the remote handset at that time IR receiver module r eceives the data sent by remote handset. The microcontroller decodes the infrare d signal data and the microcontroller will sent the infrared Key code to the PC through the Serial port. Step 5 The PC remote control software receives the Key code through the Component calle d MSCOMM which is a component for the Com port control. Through this component w e 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 co de with the code present in the program and if they are equal then the correspon ding function was done. 61 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 MS COMM32.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. 62 References www.8051projects.info www.8051projects.net 63