Project Bsc

download Project Bsc

of 116

Transcript of Project Bsc

  • 8/14/2019 Project Bsc

    1/116

    For all those who keep their Aims high and

    keep on struggling

  • 8/14/2019 Project Bsc

    2/116

    ACKNOWLEGEMENTS

    First and foremost, I bow deeply and sincerely in front of Allah Almighty,

    the most beneficent and the most merciful, Who enabled me to tackle the

    assigned project.

    It is indeed a great honor for me to express my indebtedness to my

    AdvisorProfessor M.Zahir Khan

    who guided me through all the project termand his devotion and commitment enabled me to accomplish the task assigned

    to me.

    I would like to acknowledge the faculty in general and all my instructors in

    particular for bringing me up to the point where I can undertake a project

    independently. They imparted me the power of knowledge, which is a base for

    not only this project, but also for the future tasks I might have to undertake.

  • 8/14/2019 Project Bsc

    3/116

    Chapter - 1

    Introduction

    1

  • 8/14/2019 Project Bsc

    4/116

    1.1 Introduction

    The idea behind the project is to make microcontroller communicate with

    computer situated anywhere in the world but connected to the Internet. Toachieve this goal we need one server connected to the Internet and the client canaccess to that server from anywhere in the world by typing the address of theserver in any Internet browser (web page solution) or by typing the IP address ofthe server to get connected to it. When the client is connected to the server hecan send the data to it and the server will do the required operation according tothe data it received from the client. The server is further connected to themicrocontroller through serial port. Thus any data received by the server from theclient is just forwarded to the microcontroller by the server. Such a program isburnt in microcontroller that enables microcontroller to receive data from serverthrough serial port. When microcontroller receives data from the server it further

    sends it to its Port-0. The microcontroller is interfaced with the load through solid-state relay. When the input to the solid state relay is high it makes the loadconnected to the supply and thus the circuit is complete the load gets connectedto the supply while when the input form the microcontroller is made low the solidstate relay act as open circuit and thus the load is disconnected from the supply.

    1.2 The problem

    Initially I went for making software that can be used for the communicationbetween client and server and also between server and microcontroller thus all

    we need is that software to be run on server and then the same software is usedby the client for communication with server. But the problem in this method waslater recognized that we will need the software to be with us in disk etc all thetime so that we can run it from other computers that dont have the software thusI worked to solve this problem also.

    1.3 Solution

    In this project I developed the software program that allows user tocommunicate with the server by simply using its address, which is fixed and

    doesn't change with the location of the server. The client or user just need theInternet browser and type the address of the server to send data to it.

    1.4 Objective and our achievement

    Main task of the project was to develop software in visual basic (VB) tomake communication between two PCs (client and server) possible and build a

    2

  • 8/14/2019 Project Bsc

    5/116

    hardware that is connected to server, which is controlled by the client. When thecontrol command is sent through the network the hardware is accessed throughthe serial port. This hardware contains the microcontroller and the solid-staterelays connected to the load.

    In this project I have been succeeded to send data from the client to

    server. The server further sends the data it received from the client to themicrocontroller. The microcontroller is programmed to receive the data andtransfer it to its Port0 where the solid-state relays are connected.

    Further I also succeeded to build a website using HTML and ASP (activeserver page) which do the same but is easy to use because then we dont needto remember the IP address of the server which can be variable but we need toknow the address of server website which is easy to remember. The website helpto transfer specific data that the user (browsing that website) want to send to theserver. Further the server transfer the data it received to the microcontrollerthrough serial port using software running on it.

    In this way it can be said that I have done the project in two ways i.e.

    through software method (VB6) and through using website (ASP and HTML).

    internet

    M.C

    Server

    Client

    Relay and load

    1.5 Conclusion

    Many applications can be developed by this system primarily in field of

    remote monitoring and controlling. For example remote monitoring of WeatherStation, Oil Storage, Home Automation, etc. We can build this system with lowcost and easy to install.

    As the system is independent of location of client and server so it can beinstalled anywhere provided the Internet facility is available. Thus we can installthis system anywhere. Also we don't need any person to monitor the situation onserver side once the system is installed and running smoothly (no electricityproblem etc).

    3

  • 8/14/2019 Project Bsc

    6/116

    Chapter - 1

    Introduction

    1

  • 8/14/2019 Project Bsc

    7/116

    1.1 Introduction

    The idea behind the project is to make microcontroller communicate with

    computer situated anywhere in the world but connected to the Internet. Toachieve this goal we need one server connected to the Internet and the client canaccess to that server from anywhere in the world by typing the address of theserver in any Internet browser (web page solution) or by typing the IP address ofthe server to get connected to it. When the client is connected to the server hecan send the data to it and the server will do the required operation according tothe data it received from the client. The server is further connected to themicrocontroller through serial port. Thus any data received by the server from theclient is just forwarded to the microcontroller by the server. Such a program isburnt in microcontroller that enables microcontroller to receive data from serverthrough serial port. When microcontroller receives data from the server it further

    sends it to its Port-0. The microcontroller is interfaced with the load through solid-state relay. When the input to the solid state relay is high it makes the loadconnected to the supply and thus the circuit is complete the load gets connectedto the supply while when the input form the microcontroller is made low the solidstate relay act as open circuit and thus the load is disconnected from the supply.

    1.2 The problem

    Initially I went for making software that can be used for the communicationbetween client and server and also between server and microcontroller thus all

    we need is that software to be run on server and then the same software is usedby the client for communication with server. But the problem in this method waslater recognized that we will need the software to be with us in disk etc all thetime so that we can run it from other computers that dont have the software thusI worked to solve this problem also.

    1.3 Solution

    In this project I developed the software program that allows user tocommunicate with the server by simply using its address, which is fixed and

    doesn't change with the location of the server. The client or user just need theInternet browser and type the address of the server to send data to it.

    1.4 Objective and our achievement

    Main task of the project was to develop software in visual basic (VB) tomake communication between two PCs (client and server) possible and build a

    2

  • 8/14/2019 Project Bsc

    8/116

    hardware that is connected to server, which is controlled by the client. When thecontrol command is sent through the network the hardware is accessed throughthe serial port. This hardware contains the microcontroller and the solid-staterelays connected to the load.

    In this project I have been succeeded to send data from the client to

    server. The server further sends the data it received from the client to themicrocontroller. The microcontroller is programmed to receive the data andtransfer it to its Port0 where the solid-state relays are connected.

    Further I also succeeded to build a website using HTML and ASP (activeserver page) which do the same but is easy to use because then we dont needto remember the IP address of the server which can be variable but we need toknow the address of server website which is easy to remember. The website helpto transfer specific data that the user (browsing that website) want to send to theserver. Further the server transfer the data it received to the microcontrollerthrough serial port using software running on it.

    In this way it can be said that I have done the project in two ways i.e.

    through software method (VB6) and through using website (ASP and HTML).

    internet

    M.C

    Server

    Client

    Relay and load

    1.5 Conclusion

    Many applications can be developed by this system primarily in field of

    remote monitoring and controlling. For example remote monitoring of WeatherStation, Oil Storage, Home Automation, etc. We can build this system with lowcost and easy to install.

    As the system is independent of location of client and server so it can beinstalled anywhere provided the Internet facility is available. Thus we can installthis system anywhere. Also we don't need any person to monitor the situation onserver side once the system is installed and running smoothly (no electricityproblem etc).

    3

  • 8/14/2019 Project Bsc

    9/116

  • 8/14/2019 Project Bsc

    10/116

    2.1 INTRODUCTION:

    Although computers have been with us only for a few decades, their impacthas been profound as they are used in a variety of applications ranging fromsimple entertainment purposes to their use in industry and up to guiding missiles

    in military applications. Now the stage has reached where it is hard to imaginethe present world of science without the use of computers.

    In 1971 INTEL CORPORATION introduced the 8080, the first microprocessorand since then a lot of achievements have been made in this field. A devicesimilar to microprocessor is microcontroller, which is actually a dedicatedmicroprocessor. It can be very efficiently used in the industry for controlpurposes. Thus eliminating the need of more expensive computers in suchapplications.

    Whereas a microprocessor is a single chip CPU a microcontroller contains ina single IC a CPU and much of the remaining circuitry of microcomputer system.Microprocessors are commonly used as CPU in the microcomputer systems.

    This is what they are designed for. Microcontrollers on the other hand are suitedto control of I/O devices in designs requiring a minimum component count,whereas microprocessors are suited to processing information in computersystems.

    2.2 Pins Description OF 8051

    The general layout of 8051 is shown in the figure (next page):8051 is a 40-pin device. Out of these 40 pins, 24 are used for I/O purposes.

    Pin#20 is connected to ground and pin#40 to Vcc (supply). An 11.059 MHz crystal

    is connected between pin#18 and pin#19.The device has four ports, which are as follows:

    2.2.1 Ports:

    Port 0 is a dual-purpose port on pins 32-39 of the 8051 IC. It canbe used as a general purpose I/O port or for large designs itbecomes a multiplexed address data bus.

    Port 1 is a dedicated I/O port on pins 1-8. These pins are availablefor interfacing to external devices as required

    5

  • 8/14/2019 Project Bsc

    11/116

    The Pin diagram of 8051

    6

  • 8/14/2019 Project Bsc

    12/116

    Port 2 on pins 21-28 is a dual-purpose port serving as general purpose

    I/O or as the high byte of address bus if external code memory or morethan 256 bytes of external data memory are used.

    Port 3 is a dual-purpose port on pins 10-17 and like other ports it can beused for input and output. Port 3 is used for some of the most important

    functions given below.

    P3.0 RXD B0H Receive data for serial port.P3.1 TXD B1H Transmit data for serial port.P3.2 INT0 B2H External interrupt 0.P3.3 INT1 B3H External interrupt 1.P3.4 TO B4H Timer/Counter 0 External input.P3.5 T1 B5H Timer/counter1 External input.P3.6 WR B6H External data memory write strobe.

    P3.7 RD B7H External data memory read strobe.

    Note: To use port 0 as input and output ports, each pin must be connectedto 1kohm pull-up resistor externally. Other ports dont need the pull-upresisters because they have them internally.

    2.2.2 Control Signals.The 8051 have four dedicated bus control signals. These are as follows. PSEN (program store enable): When we want to use external ROM

    then PSEN should be connected to the OE (output enable) pin of theROM. If PSEN is low means instructions are fetched from external

    ROM and when PSEN is high, means instructions are taken frominternal ROM.

    ALE (address latch enable): ALE is used for demultiplexing theaddress data bus. It pulses at a rate of 1/6 th of on chip oscillatorfrequency and can be used as general-purpose clock for the rest ofsystem.

    EA (external access): When EA signal is high it mean no externalROM is connected to 8051 microcontroller & when EA pin is groundedit mean no external ROM is connected. Note that when external ROMis used internal ROM of 8051 microcontroller will not work.

    RST (RESET): This input on pin 9 is the master reset for the 8051

    microcontroller. On the resetting of microcontroller the program counterwill reset to 0000 and all the values in microcontroller registers will belost e.g. Accumulator register will reset to 0000.

    2.2.3 On chip oscillatorThe 8051 features an on chip oscillator that is typically driven by a crystal

    connected to pins 18 and 19. The crystal oscillator also needs two capacitors of30pF value. One side of each capacitor is connected to the ground.

    7

  • 8/14/2019 Project Bsc

    13/116

    Usually the frequency of crystal oscillator is 11.059 MHz.

    2.2.4 Power Connections:The 8051 operate from a single +5 volts supply. The Vcc connection is on

    pin#40 and the ground or Vss connection on pin #20.

    2.3 Memory organization

    8051 implements a separate memory space for programs and data. Bothcode and data may be internally stored or expanded to external memory.

    The internal memory consists of On-chip ROM and On-chip data RAM. TheOn-chip RAM contains a rich arrangement of general-purpose storage, bitaddressable storage, register banks and special function registers (SFR). Theregisters and I/O ports are memory mapped.

    2.3.1 General purpose RAMIt has 80 bytes of general purpose from addresses 30H 7FH. Any locationcan be accessed freely using direct of indirect addressing modes.

    2.3.2 Bit addressable RAMThe 8051 contains 210 bit addressable locations of which 128 are at byte

    addresses 20H 2FH. This is a powerful feature of most Microcontrollers. Bitscan be set, cleared, ANDed, ORed.

    2.3.3 Register banksThe bottom 32 locations (00H-1FH) of internal memory contain register

    banks. The 32 bits are divided into four banks of registers in which each bankhas 8 registers (R0 to R7).

    2.3.4 Special function registers (SFR)These are configured as a part of on chip RAM. Therefore each register also hasan address. There are 21 SFRs at the top of the internal RAM from addresses80H FFH.The various SFRs used commonly are:AccumulatorProgram status wordB Register.

    Stack pointer.Data pointer.Port registers.Timer registers.Serial port registers.Interrupt registers.Power control registers.

    8

  • 8/14/2019 Project Bsc

    14/116

    RAM

    7F

    30

    GENERAL PURPOSE RAM

    B 2F 7F 7E 7D 7C 7B 7A 79 78I 2E 77 76 75 74 73 72 71 70T 2D 6F 6E 6D 6C 6B 6A 69 68

    2C 67 66 65 64 63 62 61 60A 2B 5F 5E 5D 5C 5B 5A 59 58D 2A 57 56 55 54 53 52 51 50D 29 4F 4E 4D 4C 4B 4A 49 48R 28 47 46 45 44 43 42 41 40E 27 3F 3E 3D 3C 3B 3A 39 38S 26 37 36 35 34 33 32 31 30S 25 2F 2E 2D 2C 2B 2A 29 28A 24 27 26 25 24 23 22 21 20B 23 1F 1E 1D 1C 1B 1A 19 18L 22 17 16 15 14 13 12 11 10

    E 21 0F 0E 0D 0C 0B 0A 09 0820 07 06 05 04 03 02 01 00

    1F18

    REGISTER BANK 3

    1710

    REGISTER BANK 2

    0F08

    REGISTER BANK 1

    0700

    REGISTER BANK 0 ( DEFAULT )

    Memory organization of 8051

    9

  • 8/14/2019 Project Bsc

    15/116

    special function registers

    FF

    F0 F7 F6 F5 F4 F3 F2 F1 F0 BE0 E7 E6 E5 E4 E3 E2 E1 E0 ACCD0 D7 D6 D5 D4 D3 D2 D1 D0 PSW

    B8 - - - BC BB BA B9 B8 IPBY B0 B7 B6 B5 B4 B3 B2 B1 B0 P3TE A8 AF - - AC AB AA A9 A8 IE

    A0 A7 A6 A5 A4 A3 A2 A1 A0 P2AD 99 NOT BIT ADDRESSABLE SBUF

    D 98 9F 9E 9D 9C 9B 9A 99 98 SCONRE 90 97 96 95 94 93 92 91 90 P1SS 8D NOT BIT ADDRESSABLE TH1E 8C NOT BIT ADDRESSABLE TH0

    S 8B NOT BIT ADDRESSABLE TL1

    8A NOT BIT ADDRESSABLE TL0

    89 NOT BIT ADDRESSABLE TMOD

    88 8F 8E 8D 8C 8B 8A 89 88 TCON

    87 NOT BIT ADDRESSABLE PCON

    83 NOT BIT ADDRESSABLE DPH

    82 NOT BIT ADDRESSABLE DPL

    81 NOT BIT ADDRESSABLE SP

    80 87 86 85 84 83 82 81 80 P0

    Memory organization of 8051

    10

  • 8/14/2019 Project Bsc

    16/116

    2.4 Timers in 8051 microcontroller

    Timers are used in virtually all control-oriented applications, and the 8051timers are no exception. There are two 16-bit timers each has four modes of

    operation.

    2.4.1 Importance of timersThe timers are used basically for. Interval timing: In interval timing operation, a timer is programmed to

    overflow at a regular interval and set the timer overflow flag. The flag isused to synchronize the, program to perform an action such as, checkingthe state of I/p or sending data to o/p.

    Event counting: Event counting is used to determine the number ofoccurrence of an event. An event is any external stimulus that provides a1-to-0 transition to a pin on the 8051 IC

    Baud rate generation for the built-in serial port: The 8051 transfersand receives data serially at many different baud rates. The baud rate is inthe 8051 is programmable. This is done with the help of timer1. The rateof data transfer in serial communication is expressed in bps (bits perseconds). Baud rate is the modem terminology and is defined as thenumber of signal changes per second. In modems there is an occasionwhen a single change of signal transfers several bits of data. As far as theconductor wire is concerned the bit rate and baud rate are equal.

    2.4.2 Timer RegistersThe 8051 timers are accessed using six special function register SFRS

    The first one is timer mode register (TMOD). The TMOD registercontains two groups of four bits that sets the operating mode for timer 0and timer 1. TMOD is not bit-addressable, nor does it do needs to be.Generally it is loaded initially at the beginning of program through softwareto initialize (set) the timers (or timer) mode.

    TMOD RegisterGATE C/T M1 M0 GATE C/T M1 M0

    Explanation of different bits of TMOD registerGATE Timers in 8051 can be started by method of software and hardware. To

    start or stop the timer by software when GATE=0 otherwise timer isstopped or started by external source when GATE=1C/T This bit is used to decide if timer is used as counter or timer. When

    C/T=0 means timer is used in timer mode & C/T=1 mean counter mode.M1M0

    The two bits are used to set the mode of the timer. There are 4 modes oftimers in 8051 microcontroller.

    11

  • 8/14/2019 Project Bsc

    17/116

    Mode M0 M1 Operation mode0 0 0 13 bit timer mode1 0 1 16 bit timer mode2 1 0 8 bit auto reload timer/counter3 1 1 Split timer mode

    Note: The upper four bits of TMOD register are used for Timer-1 and thelower four bits are used for Timer-0

    The second SFR used for timer operations is TCON (timer control)register. TCON register is 8-bit register but some of its bits are used intimer operations.

    TCON registerTF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0

    TR1 and TR0 are bits used for starting and stopping the timer-1 and timer-0 bysoftware method.TF1 and TF0 are flag bits for timer1 and timer0 and are set when the timer countreach to its limit.

    Timer 0 is a 16-bit register. The 16-bit register of timer 0 is accessed aslow byte and high byte. The low byte is called TL0 and the upper byte iscalled TH0. These registers are used like any other register such as A, Betc.

    Timer 1 register is also 16 bits register and split in to two bytes, referredas TL1 and TH1. These register are accessible same as Timer0 registers

    .2.4.3 8-bit Auto-Reload Mode (timer mode 2)

    This mode has many applications, including setting the baud rate in serialcommunication. Mode 2 is an 8-bit timer however it has auto-reloadcapability. In auto-reload the higher byte of timer is loaded with initial countand a copy of it is given to lower byte of that timer. The following are thecharacteristics of Mode 2 operation. It is 8-bit timer thus it allow only values of 00 to FFH to be loaded into

    the higher byte of timers register. After higher byte of 16-bit timer is loaded with 8-bit value, the 8051

    give a copy of it to lower byte of timer register. Then the timer must be

    started. This is done by making the TR0=1 of TCON register for timer0used in mode 2 (similarly making TR1=1 of TCON register for timer1used in mode 2). The instruction to do so is SETB TR0.

    After the timer is started the timer starts counting by increasing thecontents of the lower bytes of timer register. It counts until it reachesFFH. When it reaches to FFH then it roll back to initial value that wasset in higher byte of the timer register. Mover over it also set the TF0(or TF1) flag of TCON register. Thus normally the TF flag for the

    12

  • 8/14/2019 Project Bsc

    18/116

    particular timer is always monitored to check if the required time haspassed or not.

    2.5 8051 Serial communication

    Computers can transfer data in two ways: parallel and serial. In parallel data transfer, often 8 or more lines (wire conductors) are used

    to transfer data to a device that is only a few feet away. E.g. printers andhard disks are connected to computer using parallel method.

    In serial communication the data is sent one bit at a time in contrast toparallel communication in which data is sent a byte or more a time. Serialcommunication is slow but to transfer data many meters away serial datatransfer is used.

    2.5.1 Basics of serial communication

    Serial data communication uses two methods: synchronous and synchronous The synchronous method transfers a block of data at a time. The asynchronous serial communication transfer single byte at a time.

    It is possible to write software to use either of these methods, but theprograms can be tedious and long. For this reason there is special IC chip madeby manufacturers for serial communication named as UART (universalasynchronous receiver-transmitter) and USART (universal synchronous-asynchronous receiver-transmitter). The 8051 has built-in UART chip.

    2.5.2 RS232 StandardsTo allow compatibility among data communication equipment made by

    various manufacturers an interfacing standard called RS232 was set by the EIA.This standard is used in PCs and other electronic equipments, but RS232 is notTTL compatible. In RS232 logic 1 is represented by 3 to 25V while the logic 0is represented by +3 to +25V making 3 to +3V undefined. Thus to connect anyRS232 to microcontroller we use voltage converters such as MAX232 to convertthe TTL logic levels to RS232 voltage levels and vice versa.

    The RS232 cables are commonly referred to as DB25 connectors having 25pins but as not all pins are used in PCs thus IBM introduced the DB9 connectorshaving 9 pins used in PCs.

    2.5.3 Baud Rate in 8051

    The 8051 can communicate using different baud rates. In 8051 baud rates areprogrammable i.e. they can be changed using timer 1 in mode 2. The value to beinserted in TH1 of timer register for different baud rates is given in the tablebelow.

    Baud rate TH1 (decimal)9600 -34800 -62400 -12

    13

  • 8/14/2019 Project Bsc

    19/116

    2.5.4 8051 connection to RS232The 8051 have two pins that are used specifically for transferring and receivingdata serially. These pins are TXD (pin 11 or p3.1) and RXD (pin 10 or p3.0) usedfor transmission and reception of data (respectively) serially. These pins are TTLcompatible; therefore, they require a line driver (MAX232) to make them RS232

    compatible.

    2.5.5 SBUF RegisterSBUF (serial buffer) register is 8 bit register used only for serial communication in8051 microcontroller. For a byte of data to be transmitted through T/R line, itmust be placed in SBUF register. Similarly when 8051 microcontroller receivesthe 8 bit data it is placed in the SBUF register by it. SBUF can be accessed likeany other register.

    2.5.6 SCON RegisterSCON (serial control) is 8 bit register used to program the start bit, stop bit, and

    the data bits of the data framing among other things. The following shows thevarious bits of SCON register.SCON register

    SM0 SM1 SM2 REN TB8 RB8 T1 R1

    Bits of SCON register and their functionsSM0 SCON.7 Serial port mode specifierSM1 SCON.6 Serial port mode specifierSM2 SCON.5 Used for multiprocessing communication (normally 0)REN SCON.4 If we want to both transfer and receive data, REN =1

    If REN=0 mean 8051 can only send data thus data

    reception is blockedTB8 SCON.3 Not usedRB8 SCON.2 Not usedTI SCON.1 When 8051 finishes the transfer of 8-bit character, it

    raised TI (transmit interrupt) flag to indicate that it isready to transmit other byte.

    RI SCON.0 When 8051 receive data serially, it get rid of start and stopbit (transmitted along with data) and place the byte of datareceive in the SBUF register. Then it raises the TI(receive interrupt) flag to indicate that the byte isreceived and should be picked up from SBUF register.

    Different modes of serial communicationSM0 SM1 Mode0 0 Serial mode 00 1 Serial mode 1, 8bit data, 1 start bit, 1 stop bit1 0 Serial mode 21 1 Serial mode 3

    14

  • 8/14/2019 Project Bsc

    20/116

    2.5.7 Programming the 8051 to transfer data serially

    To program 8051 to transfer data/receive data serially the following stepsmust be taken out. The TMOD (timer mode) register is loaded with 20H indicating the use of

    timer1 in mode 2 (8-bit auto reload) to set the baud rate. The TH1 (higher byte of timer1) is loaded with appropriate value to set thebaud rate.

    SCON register is loaded with value 50H indicating serial mode1 where 8-bits are framed with 1stop and 1 start bit.

    TR1 bit is set to start the timer. TI bit is cleared for transferring data serially while RI bit is cleared to

    receive data serially by instruction e.g. CLR TI and similarly CLR RI If data is transmitted serially then it is placed in SBUF register and then

    the TI flag is monitored to see it the data transmission is completed or not.While to receive data serially the RI flag is monitored to see if the entire

    character is received yet or not and as it get set it mean all the data hasarrived in SBUF register thus should be moved from it for appropriateoperation.

    2.6 8051 training kit

    The Intel MCS-51 series of microcontroller is widely recognized as a standardfor incorporation into wide range of product from automatic washing machines,automatic vending machines, digital weighing machines, welding machines,microwave ovens and robot control cards to programmable logic controllers

    (PLCs). Their simplicity makes them ideal for where it is necessary to quicklybuild up a control system. The 8051 microcontroller is versatile and easilyprogrammable. It finds extensive applications in automation because of its simplearchitecture and built in I/O capabilities. The use of this controller considerablyreduces the chip count. Because of the same reason this controller is alsoconsidered as a good teaching model for the learning a teaching platform in theInstitutions.Microcontroller Training & Development Kit has been designed specifically forstudying the functionality of MCS-51 microcontroller through experimentation andexercises.The Kit is aimed to train a wide range of users from beginners to technicians,

    students, hardware designers and experienced engineers. The board is adynamic learning platform specifically designed keeping in view the requirementsof both small scale and large-scale projects. The Kit has been designed withfeatures, which make it ideal as a first step educational tool, as well as anadvanced and power full development tool in a minimum possible time.

    15

  • 8/14/2019 Project Bsc

    21/116

    The salient features of the board are as under: On Board Regulated Power Supply 8 bit A/D Module for Real Time Data Acquisition RS232 Interface Expandable (can be interfaced with other devices and hardware) and

    Flexible Output Devices includes: 8 x LEDs 4 x Seven Segment Displays 2 x 20 Line LCD 3 x status LEDs Power Supply Condition monitoring Relay status 1 x Triac (for AC load) 1 x Darlington (for DC load)

    1x DPDT 12 V DC Relay Buzzer ZIF Socket for Microcontroller 5 x Momentary Switches Prototype Area (Plated Through Hole on 0.1 x 0.1 Grid All ICs mounted on bases

    16

  • 8/14/2019 Project Bsc

    22/116

    Summary

    In this chapter we examined the hardware features of the 8051

    Microcontroller. We specially examined the 8051 on chip timers

    and the on chip serial port that operates in several modes over

    wide range of frequencies.

    References

    The 8051 Microcontroller and Embedded systems

    By Mohammad Ali Mazidi

    Janice Gillespie Mazidi

    The 8051 Microcontroller (3rd Edition)

    By I. Scott Mackenzie

    17

  • 8/14/2019 Project Bsc

    23/116

    Chapter 3A

    Software of the project(Using Visual Basic)

  • 8/14/2019 Project Bsc

    24/116

    3a.1 Introduction

    There is much excitement over the Internet and the World Wide Web.The Internet ties the information world together. The web makes the Interneteasy to use and gives it the flair and sizzle of multimedia. Some organizationssee the Internet and the web as crucial to their information systems strategies.Visual basic provides several built-in networking capabilities that make it easyto develop Internet and web based program and applications. Visual basiccan enable the programs to search the world for information and tocollaborate with programs running on other computers worldwide or just withinan organization. Visual basic can even enable the applications running on thesame computer to communicate with on another.

    Networking is massive and complex topic. Visual basic provides a richcomplement of networking capabilities and will likely be used as animplementation vehicle in computer networking courses. Visual basic offerssocket based communication that enable applications to view networking as

    streams of bytes similar to file input/output - a program can read from a socketor write to a socket as simple as reading from a file or writing to a file. Socketconnections are similar to a telephone calls - the telephone handsetrepresents the socket. The mouthpiece of the telephone is the output streamand the earpiece of the telephone is the input stream. The call (i.e. connection)terminates until one of the participants in the conversation terminates the callby hanging up the phone. We show how to create and manipulate sockets.With socket-based networking a process establishes a connection to anotherprocess. While the connection is in place, data flows between the processesin continuous streams. Sockets are said to provide a connection-orientedservice. The protocol used for transmission is the popular TCP-transmission

    control protocol. Sockets and TCP protocol will be the most desirable for thevast majority of Visual Basic programmers.

    Our project focuses on both sides of client/server relationship. Theclient requests that some action be performed and the server performs theaction returning the result (if any) to the client. The client first attempts toestablish a connection to the server. The server can accept or deny theconnection. If the connection is accepted, then the client and servercommunicate through sockets in much the same manner as if they were doingfile I/O. When the communication connection is no longer needed, the clientand server each close the connection.

    3a.2 Visual basic Internet controls

    Visual basic provides several networking controls to facilitatedevelopment of network-oriented applications. Each of them is summarized intable below.

    Control DescriptionWebBrowser

    controlEnables applications to provide web browsing, localdocument viewing and file downloading capabilities. Thiscontrol provides applications with many features of the

    Internet explorer.

  • 8/14/2019 Project Bsc

    25/116

    InternetTransfercontrol

    Enables applications to use Hyper Text TransferProtocol (HTTP) and File Transfer Protocol (FTP) forinternet based applications. An application using thiscontrol can retrieve files from and send files to any sitethat uses one to these protocols.

    Winsock control Enable client server application programming usingeither User Datagram Protocol (UDP) or TransmissionControl Protocol (TCP)

    We will discuss the Winsock control only because it is related to out project.

    3a.3 Winsock control

    The Winsock control operates at the lowest level of all the Internet controls,allowing client/server applications to communicate using both TransmissionControl Protocol (TCP) and User Datagram Protocol (UDP). Winsock

    control is used in the project for attaining the first and major phase of theproject i.e. communication between two computers connected to each otherthrough Internet. This phase is shown in the figure.

    internet

    Server

    ClientCommunication with TCP is similar to a telephone conversation - there

    must be a connection between the client and server before they cancommunicate. TCP is commonly referred to as a connection-based orconnection oriented protocol. Communication with UDP is similar to sendingletters in the mail. A packet- called datagram- is created, addressed and sentover the network. There is no direct connection between the origin anddestination. UDP is commonly referred to as connectionless protocol.

    TCP protocol is normally used when large amount of data are transmittedbetween a server and a client and when reliability us imperative (i.e. the bytes

    of information must get to their destination with acknowledgment of theirreceipt). The connection maintained between the server and the clientensures data integrity. UDP is typically used when small amount of data aresent or data are sent intermittently.

    3a.3.1 Establishing a Simple Server (Using TCP Protocol)

    Establishing a simple server in Visual Basic requires several steps.Step 1 is to add a Winsock control to the form. To use Internet Transfercontrol it must first be added to the toolbox by selecting components from the

    project menu to display the components dialog box. In the dialog box, scrolldown and select the option Microsoft Winsock control 6.0. When selectedproperly a small check mark appears in the box to the left of the option. Click

  • 8/14/2019 Project Bsc

    26/116

    the ok button when you are done to dismiss the dialog box. The icon for theWinsock control will be at the bottom of the toolbox.In step 2, the server must be set up to listen for connection from clients on aspecific port number as specified by Winsock controls local port property.Each client will ask to connect to the server on this port. Port numbers arepositive integer values up to 65535. Typically, port numbers below 1024 arereserved for system services. The command that registers with Winsockcontrols tcpserver an available port number on local machine is

    serverclient.LocalPort = Val(txtsport.Text)

    Where txtsport.text can be any value defining the port number and is enteredby the user.NOTE: serverclient is the name given to the Winsock control and LocalPort is

    the name of one of its property. The serverclient means the one Winsock control

    is used by the software for both client and server operation.

    Each client connection is managed with a Winsock control. In step 3, once thetcpserver port number is established, the server must be told to listenindefinitely for an attempt by client to connect. This is accomplished with a callto Winsock controls Listen method using the command

    Call serverclient.Listen

    When a connection request is received while the server us listening, eventprocedure ConnectionRequest executes i.e. the control shift to the line

    Private Sub serverclient_ConnectionRequest(ByVal requestID As Long)

    Step 4 is to accept the request for connection from the client typicallyperformed in the connectionrequest event procedure. This is accomplished byWinsock controls Accept method as in the command below

    Call serverclient.Accept(requestID)

    Where RequestID is received as an argument to event procedureConnectionRequest and simply passed to the Accept method. Once thismethod is called, there is connection between the server and the clientthrough which the stream of data can be passed.

    Step 5 is the processing phase in which the server and the client

    communicate through the connection. When data arrives at the server, theWinsock controls DataArrival event procedure executes i.e.

    Private Sub serverclient_DataArrival(ByVal bytesTotal As Long)

    And the statement

    Call serverclient.GetData(message)

    retrieves string-based information from the client and stores it in the stringmessage. Also two optional arguments can be used to specify the type ofdata received and the maximum length of the data to receive. Data is sent to

    the client using the SendData method e.g. the statementCall serverclient.SendData(code)

  • 8/14/2019 Project Bsc

    27/116

    Sends the string code to the client (where code is the number set by serverand the client should know it).

    In step 6 when the transmission is complete and the client closes theconnection the Winsock controls Close event procedure executes i.e.

    Private Sub serverclient_Close()

    At this point the sever connection should be closed with the statement like

    Call serverclient.Close

    The server application can be told to listen for a new client connection byinvoking the Listen method on the Winsock object (as discussed above).

    3a.3.2 Establishing a Simple client (Using TCP Protocol)

    Establishing a simple client in Visual Basic requires several steps.

    Step1 is to add Winsock control to the form. To use the Internet Transfercontrol it must be added to the toolbox by same way as discussed above. Butin our software the client and server uses one Winsock control that is given aname serverclient thus there is no need for adding the second Winsockcontrol. Thus software can be used as server as well as client but not at asame time as client and server. When the software is used as client the serverside disables and similarly when software is used as server the client sidedisables.In step 2 the client side Winsock object must be told the server to which toconnect and the port number on that server. This is accomplished with thestatements

    serverclient.RemoteHost = txtip.Textserverclient.RemotePort = Val(txtcport.Text)

    The value to the RemoteHost property of the serverclient Winsock object isa string (i.e. txtip.text is the value in the text box with name txtip) indicatingeither the name of the server (e.g. www.microsoft.com or localhost) or theInternet IP address for the server (e.g. 127.168.68.3 or for the local host theaddress is 127.0.0.1). There is an option that if a user doesnt enter the IPaddress then it will be considered by default as 127.0.0.1 i.e. localhostaddress. The statements are

    If serverclient.RemoteHost = "" Thenserverclient.RemoteHost = "localhost"End If

    Note that the server name localhost normally represents the local machinethus enable the programmer to check the working of its software withouthaving Internet connected by running both client and server software andcommunicating with the localhost i.e. with itself.The other statement written above in which serverclient property

    RemotePort is used and is assigned a value that is taken from the textboxhaving the name txtcport. The value assigned to the property RemotePort of

    the serverclient Winsock object must match the port number on which the

    http://www.microsoft.com/http://www.microsoft.com/
  • 8/14/2019 Project Bsc

    28/116

    sever application is listening; otherwise no connection can be establishedbetween the client and server.In step 3 the connection to the server is established using a call to theWinsock objects Connect method as in statement

    Call serverclient.Connect

    If the connection attempt is successful a connection is established and theclient can now communicate with the server otherwise a run time error occursand the Winsock objects Error event procedure executes i.e.

    Private Sub serverclient_Error(ByVal Number As Integer, Description AsString, ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As

    String, ByVal HelpContext As Long, CancelDisplay As Boolean)

    Step 4 is the processing phase in which the client and server communicatethrough the connection established. As with the server, when data arrives atthe client the Winsock controls DataArrival event procedure executes.

    But more important is the client sending data to the server. Data is sent to theserver using the SendData method. For example the statement

    Call serverclient.SendData(PinNumber)

    Sends the string PinNumber to the server i.e. the certain code that sets thepins of microcontroller on or off.In step 5 when the transmission is complete and the server closes theconnection the Winsock controls Close event procedure executes. At thispoint the client connection should be closed with the statement like

    Call serverclient.Close

    If server doesnt closes the connection the client should still invoke the closemethod to close its side of the connection.

    3a.4 Serial Port Interfacing Using Visual Basic

    Serial Interfacing is one of the most commonly used mediums for dataacquisition and control using computers. In this section we will learn how tointerface the serial port using Visual Basic Programming. Serial Interfacingprovides an easier way of data acquisition and control using computers. AnIBM compatible PC usually is equipped with 2 serial ports and one parallel

    port for external communication. A serial port sends and receives data bit bybit compared to the parallel port in which data is received and transmitted inmultiple bits. A serial port started out as an interface between Data TerminalEquipment (such as a monitor) and data communication equipment (such asa modem). Nowadays the applications of serial ports have increased asmanufacturers started using serial ports for a lot of applications e.g.programmers use it to interface additional devices like the printer or anothercomputer for serial communication between the computer.

    The serial port programming was difficult to do specific tasks becauseearlier it was being programmed in DOS but with advances in computer

    technology with Windows taking over the DOS, there needed to be ways tohave a serial interface with the computer and Visual Basic seems to provide a

  • 8/14/2019 Project Bsc

    29/116

  • 8/14/2019 Project Bsc

    30/116

    3a.5 Complete software

    The screen shots and the code of the software (in visual basics) thatenables the client and server to communicate with each other and alsoallows server to send data to microcontroller is given below. The

    working of the software will be discussed later.

    Screen shot of design mode

    Screen shot of code window

  • 8/14/2019 Project Bsc

    31/116

    Property window of

    Winsock control

    Property window of

    Ms Comm. control

    3a.5.1 Code of the program

    The code of the program is written in the code window and is written below.To understand this code basic knowledge of computer language and visual

    basic is must.

    Dim optionselect As Integer 'for selecting different optionsDim txt As Long

    Private Sub cbopinnumber_Click()

    '**********************************'best part of the programDim tobinary As IntegerDim digit(7) As Integer

    tobinary = cbopinnumber.ListIndexFor p = 0 To 7digit(p) = tobinary Mod 2

  • 8/14/2019 Project Bsc

    32/116

    If digit(p) = 0 Thencmdled(p).BackColor = vbWhiteElsecmdled(p).BackColor = vbRedEnd Iftobinary = tobinary \ 2Next p

    '**********************************End Sub

    Private Sub cmdconnect_Click()

    Select Case cmdconnect.CaptionCase Is = "Connect"

    serverclient.RemoteHost = txtip.Text

    If serverclient.RemoteHost = "" Thenserverclient.RemoteHost = "localhost"End If

    serverclient.RemotePort = Val(txtcport.Text)

    Call serverclient.Connect

    cmdserver.Enabled = False

    cmdconnect.Caption = "Disconnect"cmdsend.Enabled = True

    Case Is = "Disconnect"

    Call serverclient.Closecmdconnect.Caption = "Connect"cmdserver.Enabled = Truecmdsend.Enabled = False

    End Select

    End Sub

    Private Sub cmdlisten_Click()

    Select Case cmdlisten.CaptionCase Is = "Listen"serverclient.LocalPort = Val(txtsport.Text)Call serverclient.Listen

    cmdclient.Enabled = Falsecmdlisten.Caption = "Close"

  • 8/14/2019 Project Bsc

    33/116

    '***************************************************

    Case Is = "Close"serverclient.Closecmdlisten.Caption = "Listen"cmdclient.Enabled = True

    End Select

    End Sub

    Private Sub Form_Load()'***************************************************'tcpclient is selectedoptionselect = 1

    '***************************************************txt = 0

    Dim pins As Long 'for the pin numbers in cbopinnumber'add the pin numbers 0 to 255 to cbopinnumberFor pins = 0 To 255cbopinnumber.AddItem CStr(pins)Next pins'default to pin 0 being selectedcbopinnumber.ListIndex = 0'***************************************************

    Dim i As Integer 'to make all led whiteFor i = 0 To 7cmdled(i).BackColor = vbWhiteNext i'***************************************************

    frconnect.Height = 1815frconnect.Width = 3615frconnect.Left = 5400frconnect.Top = 480

    frsendrec.Visible = Truefrsendrec.Height = 1815frsendrec.Width = 4935frsendrec.Left = 360frsendrec.Top = 480

    frcode.Visible = Truefrcode.Height = 2895frcode.Width = 3615frcode.Left = 5400frcode.Top = 2400

  • 8/14/2019 Project Bsc

    34/116

    frinout.Height = 2895frinout.Width = 4935frinout.Left = 360frinout.Top = 2400txtinout.Height = 2535txtinout.Width = 4695txtinout.Left = 120txtinout.Top = 240'***************************************************Dim name As IntegerFor name = 1 To 2cboname.AddItem CStr(name)Next name

    End Sub

    Private Sub cmdclient_Click()

    '***************************************************'to select the option of tcpclientoptionselect = 1'***************************************************

    frmain.Caption = "Client"frserstatus.Visible = Falsefrserialsetting.Visible = False

    cmdsend.Visible = Truefrsendrec.Caption = "Send"frsendrec.Visible = Truefrsendrec.Height = 1815frsendrec.Width = 4935frsendrec.Left = 360frsendrec.Top = 480

    frconnect.Visible = Truefrconnect.Height = 1815frconnect.Width = 3615

    frconnect.Left = 5400frconnect.Top = 480

    frcode.Visible = Truefrcode.Height = 2895frcode.Width = 3615frcode.Left = 5400frcode.Top = 2400

    frinout.Visible = Truefrinout.Caption = "Data Sent"frinout.Height = 2895

  • 8/14/2019 Project Bsc

    35/116

    frinout.Width = 4935frinout.Left = 360frinout.Top = 2400txtinout.Height = 2535txtinout.Width = 4695txtinout.Left = 120txtinout.Top = 240'***************************************************

    Select Case cmdconnect.CaptionCase Is = "Connect"cmdsend.Enabled = FalseCase Is = "Disconnect"cmdsend.Enabled = TrueEnd Select

    End Sub

    Private Sub cmdexit_Click()

    End

    End Sub

    Private Sub cmdhelp_Click()

    frmain.Caption = "Help"

    frconnect.Visible = Falsefrsendrec.Visible = Falsefrcode.Visible = Falsefrserialsetting.Visible = Falsefrserstatus.Visible = False

    frinout.Visible = False

    End Sub

    Private Sub cmdled_Click(Index As Integer)

    '*************************************************If cmdled(Index).BackColor = vbWhite Thencmdled(Index).BackColor = vbRedElseIf cmdled(Index).BackColor = vbRed Thencmdled(Index).BackColor = vbWhiteEnd If

  • 8/14/2019 Project Bsc

    36/116

    '*************************************************'the best code in programeDim bit(7) As IntegerDim todigit As IntegerFor p = 0 To 7If cmdled(p).BackColor = vbWhite Thenbit(p) = 0Elsebit(p) = 1End IfNext ptodigit = bit(7) * 2 ^ 7 + bit(6) * 2 ^ 6 + bit(5) * 2 ^ 5 + bit(4) * 2 ^ 4 + bit(3) * 2 ^3 + bit(2) * 2 ^ 2 + bit(1) * 2 ^ 1 + bit(0) * 2 ^ 0cbopinnumber.ListIndex = todigit

    '*************************************************

    End Sub

    Private Sub cmdopen_Click()

    Dim name As IntegerDim baud As Integer

    Select Case cmdopen.CaptionCase Is = "Open"

    name = cboname.ListIndex

    Select Case nameCase Is = -1serial.CommPort = 1Case Is = 1serial.CommPort = 2End Select

    baud = cbobaudrate.ListIndex

    Select Case baudCase Is = -1serial.Settings = "1200,N,8,1"Case Is = 1serial.Settings = "2400,N,8,1"Case Is = 2serial.Settings = "4800,N,8,1"Case Is = 3serial.Settings = "9600,N,8,1"End Select

    'disable DTR(data tranmission)

  • 8/14/2019 Project Bsc

    37/116

    serial.DTREnable = False'open the portserial.PortOpen = True

    cmdopen.Caption = "Close"cmdsend.Enabled = True

    Case Is = "Close"serial.PortOpen = Falsecmdopen.Caption = "Open"cmdsend.Enabled = False

    End Select

    End Sub

    Private Sub cmdsend_Click()Dim PinNumber As LongPinNumber = cbopinnumber.ListIndex'**********************************Select Case optionselect

    'tcpclientCase Is = 1'Call serverclient.SendData("Client >>> " & cbopinnumber.ListIndex)

    Call serverclient.SendData(PinNumber)

    txtinout.Text = txtinout.Text & cbopinnumber.ListIndex & vbCrLf & vbCrLftxtinout.SelStart = Len(txtinout.Text)

    'serial communicationCase Is = 3

    'send out dataserial.Output = Chr$(PinNumber)End Select

    '**********************************

    End Sub

    Private Sub cmdserial_Click()

    'to select serial communication optioinoptionselect = 3

    Select Case cmdopen.CaptionCase Is = "Open"

  • 8/14/2019 Project Bsc

    38/116

    cmdsend.Enabled = FalseCase Is = "Close"cmdsend.Enabled = TrueEnd Select

    frmain.Caption = "Serial communication"

    frcode.Visible = Truefrcode.Height = 2895frcode.Width = 3615frcode.Left = 5400frcode.Top = 2400

    frsendrec.Visible = True

    frsendrec.Caption = "Send"cmdsend.Visible = True

    frconnect.Visible = False

    frserstatus.Visible = False

    frserialsetting.Visible = Truefrserialsetting.Height = 1815frserialsetting.Width = 3615frserialsetting.Left = 5400

    frserialsetting.Top = 480

    frinout.Visible = Truefrinout.Caption = "Sent Data"

    '***********************************

    Dim baud As IntegerFor baud = 0 To 3cbobaudrate.AddItem CStr((2 ^ baud) * 1200)Next baud

    End Sub

    Private Sub cmdserver_Click()

    'to select the tcpserver optionoptionselect = 2

    frmain.Caption = "Server"frconnect.Visible = False

  • 8/14/2019 Project Bsc

    39/116

    frsendrec.Visible = Truefrsendrec.Caption = "Recieved"cmdsend.Visible = False

    frserstatus.Visible = Truefrserstatus.Height = 1815frserstatus.Width = 3615frserstatus.Left = 5400frserstatus.Top = 480

    frcode.Visible = Truefrcode.Height = 2895frcode.Width = 3615frcode.Left = 5400

    frcode.Top = 2400

    frserialsetting.Visible = False

    frinout.Visible = Truefrinout.Caption = "Recieved Data"'***************************************

    End Sub

    Private Sub cmdstatus_Click()

    frmain.Caption = "Status"

    frconnect.Visible = Falsefrsendrec.Visible = Falsefrcode.Visible = Falsefrserialsetting.Visible = Falsefrserstatus.Visible = False

    frinout.Visible = False

    'to tell the status of microcontroller by recieving data from microcontroller'to tell status of server and client (their ip adresses ,ports# etc)

    End Sub

    Private Sub Form_Terminate()

    'close the connectionCall serverclient.Close

    End Sub

  • 8/14/2019 Project Bsc

    40/116

    Private Sub serverclient_Close()

    Select Case optionselect

    Case Is = 1'client sidecmdsend.Enabled = FalseCall serverclient.Closetxtinout.Text = txtinout.Text & "server closed connection. " & vbCrLftxtinout.SelStart = Len(txtinout.Text)

    cmdconnect.Caption = "Connect"cmdsend.Enabled = Falsecmdserver.Enabled = True

    Case Is = 2'server sidecmdsend.Enabled = FalseCall serverclient.Closetxtinout.Text = txtinout.Text & "client closed connection. " & vbCrLf & vbCrLftxtinout.SelStart = Len(txtinout.Text)Call serverclient.Listen

    cmdlisten.Caption = "Listen"cmdclient.Enabled = True

    End Select

    End Sub

    Private Sub serverclient_Connect()

    cmdsend.Enabled = Truetxtinout.Text = "connected to ip adress :" & serverclient.RemoteHostIP &

    vbCrLf & "port # " & serverclient.RemotePort & vbCrLf & vbCrLf

    End Sub

    Private Sub serverclient_ConnectionRequest(ByVal requestID As Long)'**************************************If serverclient.State sckClosed ThenCall serverclient.CloseEnd If

    '**************************************

    cmdsend.Enabled = TrueCall serverclient.Accept(requestID)txtinout Text = "connection from ip address : " & serverclient RemoteHostIP &

  • 8/14/2019 Project Bsc

    41/116

    vbCrLf & " port# : " & serverclient.RemotePort & vbCrLf & vbCrLf

    '**************************************End Sub

    Private Sub serverclient_DataArrival(ByVal bytesTotal As Long)

    Dim message As LongDim code As Long

    Call serverclient.GetData(message)

    If optionselect = 2 ThenSelect Case messageCase Is = 2000code = Val(Text2(0).Text)Call serverclient.SendData(code)Case Is = 2100code = Val(Text2(1).Text)Call serverclient.SendData(code)Case Is = 2200code = Val(Text2(2).Text)Call serverclient.SendData(code)Case Is = 3000code = Val(Text3(0).Text)Call serverclient.SendData(code)

    Case Is = 3100code = Val(Text3(1).Text)Call serverclient.SendData(code)

    Case Is = 3200code = Val(Text3(2).Text)Call serverclient.SendData(code)Case Elsetxtinout.Text = txtinout.Text & message & vbCrLf & vbCrLftxtinout.SelStart = Len(txtinout.Text)

    cbopinnumber.ListIndex = messageserial.Output = Chr$(message)End Select

    ElseIf optionselect = 1 ThenSelect Case txtCase Is = 2000Text2(0).Text = messageCase Is = 2100Text2(1).Text = messageCase Is = 2200

    Text2(2).Text = messageCase Is = 3000

  • 8/14/2019 Project Bsc

    42/116

    Text3(0).Text = messageCase Is = 3100Text3(1).Text = messageCase Is = 3200Text3(2).Text = messageEnd Select

    End If

    End Sub

    Private Sub serverclient_Error(ByVal Number As Integer, Description AsString, ByVal Scode As Long, ByVal Source As String, ByVal HelpFile AsString, ByVal HelpContext As Long, CancelDisplay As Boolean)

    Dim result As Integer

    Select Case optionselectCase Is = 1 'clientresult = MsgBox(Source & ":" & Description, vbOKOnly, "TCP/IP Error")

    Case Is = 2 'serverresult = MsgBox(Source & ":" & Description, vbOKOnly, "TCP/IP error")

    End Select

    End Sub

    Private Sub Text2_Click(Index As Integer)

    If optionselect = 1 Then

    Select Case IndexCase Is = 0txt = 2000

    Call serverclient.SendData(txt)Case Is = 1txt = 2100Call serverclient.SendData(txt)Case Is = 2txt = 2200Call serverclient.SendData(txt)End Select

    End If

    End Sub

  • 8/14/2019 Project Bsc

    43/116

    Private Sub Text3_Click(Index As Integer)

    If optionselect = 1 Then

    Select Case IndexCase Is = 0txt = 3000Call serverclient.SendData(txt)Case Is = 1txt = 3100Call serverclient.SendData(txt)Case Is = 2txt = 3200Call serverclient.SendData(txt)End Select

    End If

    End Sub

    3a.5.2 Screen shots of the software in running mode(server side)

    When software is started it will start as client but we first click the serial

    communication button to open the port.

  • 8/14/2019 Project Bsc

    44/116

    When the serial port is open by clicking the open button then the server can send the

    data by itself to the microcontroller but for client to send data to microcontroller the

    server should also open itself for communication with client which is done by clicking

    the TCP server button as in fig below.

  • 8/14/2019 Project Bsc

    45/116

    The server is listening on port 5000 from the client.

    3a.5.3 Screen shoots of the software in running mode (client side)

    The figure above shows the client using the software. After typing the IP address of

    the server and port number the client clicked the connect button and the button

    caption changed to disconnect while the client got connected to server. Now the clientcan send the data to the server by selection the correct code from the combo box and

    clicking the send button as shown below in which the code 12 is send to the server.

  • 8/14/2019 Project Bsc

    46/116

    The server will receive the data as shown in the following figure.

    Note in the figure that combo box as well as the received data frame shows the

    received data.

    The other feature of the software is the code frame in which different type of loads is

    given different code. If the server changes the code of the any load (e.g. light) then the

    client can know the new code by simply clicking the textbox as shown in the figures

    below in which the server has changed the code of the light from 12 to 20.

  • 8/14/2019 Project Bsc

    47/116

    Code of light changed by server.

    Note that the code on client side changed by clicking the textbox.

    3a.5.4 Captions and names of different command buttons

    The following are the name of different command buttons along with their

    captions that will help in understanding the code of the program. The captionmeans the text written on the buttons.

  • 8/14/2019 Project Bsc

    48/116

    Caption Name Caption NameTCP client cmdclient Exit CmdexitTCP server cmdserver Send cmdsendSerial Communication cmdserial Listen cmdlistenOpen cmdopen Connect Cmdconnect-(In send frame above thesend command button)

    cmdled Used to show graphically the pictureof the signal send

    3a.5.5 Names of textboxes and their corresponding functions

    Name Function Name Functiontextinout To display the data send

    by client and received byserver

    txtsport Where the port number iswritten by server to listenfrom client side

    txtip Where IP address ofserver is written by client

    text2 Where the code of theelectric loads is written to

    ON the loadtxtcport Where the port number is

    written by client tocommunicate with server

    text3 Where the code of theelectric loads is written toOFF the load

    3a.5.6 Name of the combo boxes and their functions

    Name Function

    cbopinnumber Used to select data from 0 to 255 to send to microcontrollercboname Used to select the com port from 0 to 1cbobaudrate Used to select the baud rate out of available options

    Summary

    In this chapter we examined the software of the project that

    was programmed in visual basic. We also discussed the different

    controls of VB which can help us in the project (i.e. Winsock

    control and MS Comm. control. The code of the project along with

    screen shoots are also given that will help in understanding the

    project.

  • 8/14/2019 Project Bsc

    49/116

    References

    Visual Basic 6 How to program

    By Deitel & Deital

    T.R. Nieto

  • 8/14/2019 Project Bsc

    50/116

    Chapter 3B

    Software of the project(Using HTML & ASP)

    44

  • 8/14/2019 Project Bsc

    51/116

    3b.1 Introduction

    Active Server Page ASP is used in World Wide Web Hypertext MarkupLanguage (HTML) documents to enhance the functionality of a web page displayedin a web browser (e.g. Internet Explorer) and used on Microsofts InternetInformation Server (IIS) to enhance the functionality of server-side applications. Let

    us first discuss HTML to facilitate in understanding the software of the project donein HTML and ASP.

    3b.1.1 Introduction to HTML & Microsoft Front Page

    The basic documents of World Wide Web are called Pages and are written inHTML. Pages can either be part of a web, or they can stand-alone. An HTMLdocument consists of text that specifies the content of a web page to be displayedand the format in which to display it. Most of the formatting is specified by using thepairs of tags of the form and that indicate the start andend of the format in the document e.g. the tags and are normally

    used at the beginning and end of an HTML document to specify where the HTMLformatting begins and ends respectively. The code of HTML file is written in and texteditor like notepad and is saved with extension html e.g. demo.html is an HTML file.

    Writing code in HTML for web page designing is hard and requireprofessional to do it thus Microsoft introduces software called FrontPage, which isused for web designing. We do not need to know HTML to use Microsoft FrontPage.While we edit pages as in any Microsoft Office software (e.g. MS Word or PowerPoint typing and formatting text, and adding graphics, tables, and other pageelements ) FrontPage adds the HTML tags in the background i.e. FrontPage dothe coding for us by itself. Our page is displayed, as it would appear in a Webbrowser. However, we can also do the HTML coding in FrontPage if we want to by

    switching to HTML tab in page view.To help in creating professional-looking and well-designed web pages,

    FrontPage provides several page templates so we can quickly create pages with avariety of layouts and functions. For example, we can use a FrontPage template tocreate a two-column page or a page with a search form. We can also use one ofseveral themes to create pages with a consistent design. A theme contains unifieddesign elements with a color scheme, including fonts, graphics, backgrounds,navigation bars, horizontal lines, and other page elements.

    If someone prefers to design and lay out pages himself, he can start with ablank page.Some of the features of FrontPage are.

    Use frames, tables, or absolute positioning to precisely position text andgraphics on a page.

    Add page elements, such as text, graphics, page banners, tables, forms,hyperlinks, banner ads, marquees, hover buttons, time stamps, hit counters.

    Format text by applying styles or using style sheets. Animate page elements and set page transitions for lively pages. Set the background color, picture, or sound. Create your own page templates.

    45

  • 8/14/2019 Project Bsc

    52/116

    3b.1.2 Introduction to ASP

    ASP stands for Active Server Pages. ASP is a program that runs inside IIS.An ASP file can contain text, HTML tags and scripts. Scripts in an ASP file areexecuted on the server. An ASP file is just the same as an HTML file but has the fileextension ".asp". There is different between HTML and ASP file i.e. when a browser

    (on client side) requests an HTML file, the server returns the file while when abrowser requests an ASP file, IIS passes the request to the ASP engine. The ASPengine reads the ASP file, line by line, and executes the scripts in the file. Finally,the ASP file is returned to the browser as plain HTML thus provides security sinceyour ASP code cannot be viewed from the browser. ASP also responds to userqueries or data submitted from HTML forms. An ASP file normally contains HTMLtags, just like an HTML file. However, an ASP file can also contain server scripts,surrounded by the delimiters . Server scripts are executed on theserver, and can contain any expressions, statements, procedures, or operators validfor the scripting language you prefer to use.To get further information on ASP see the whole chapter dedicated to ASP

    3b.2 Software of the project

    Microsoft FrontPage is used in designing the web page. The different figuresbelow give some idea of how to design web page in FrontPage.

    46

  • 8/14/2019 Project Bsc

    53/116

    The previous figure shows the page view when the normal tab is selected. In figurewe can see that the web page is designed by inserting The background color using page properties Marquee with behavior as scroll and the text

    CONTROLLING OF DEVICES THROUGH INTERNET USING 8051

    MICROCONTROLLER

    Horizontal lines of yellow color Form with one line textbox

    And the normal text as shown in figure above with some as hyperlinks e.g.the text thesis is hyperlink to MS Word file, which has all the thesis of theproject.

    Note that the send data button is inserted along with one line textbox and is ofinput type

    submitthey are not the push buttons i.e. ofinput type button.

    The figure below shows the page view when the HTML tab is selected and we cansee the HTML coding of the web page that was easily designed in FrontPage.

    After the designing of the web page we include the ASP code in it. This is done bywriting the ASP code in HTML code but ASP code is enclosed in tabindicating that it is ASP code. The whole code thus will have ASP code written inHTML code and is give as follow.

    47

  • 8/14/2019 Project Bsc

    54/116

    3b.2.1 HTML and ASP code of the web page

    "FINAL YEAR PROJECT"

    CONTROLLINGOF DEVICES THROUGH INTERNET USING 8051 MICROCONTROLLER

    software( in VB )Thesispresentations

    help

    ENTER CORRECT CODE TO ON/OFF THE LOAD

  • 8/14/2019 Project Bsc

    55/116

    txt.Write NUMSET fs=NOTHINGSET txt=NOTHING%>

    After writing the code the program is saved in C:\Inetpub\wwwroot\project with anyname but having .asp extension.Now the file can be opened for checking using web browser by typing the addressas http://localhost/PROJECT/FINAL.ASPthen the page will appear will appear asfollow.

    When we type any number in the text box and click SEND DATA button then theabove software will first create and then send that number in to the text fileC:\input.txt file.In following figure the user has sent the number 12 and then the textbox is cleanedwhile the message come on the screen YOU HAVE SEND 12

    49

    http://localhost/PROJECT/FINAL.ASPhttp://localhost/PROJECT/FINAL.ASP
  • 8/14/2019 Project Bsc

    56/116

    After loading the website on the Internet the server will receive the data from theclient who will type the address of the site and then typing the correct code. Butthere is still one thing left. The data received by server is in the text file input.txt sowe have to transfer it to the microcontroller which is done by using a software madein VB and the procedures is same as discussed in previous chapter.

    The code of the software is as under.

    Private Sub Form_Load()

    serial.CommPort = 1serial.Settings = "4800,N,8,1"'disable DTR(data tranmission)serial.DTREnable = False'open the portserial.PortOpen = True

    End Sub

    Private Sub Text1_Change()

    Dim message As IntegerDim txt As Stringtxt = Text1.Textmessage = Val(Text1.Text)

    50

  • 8/14/2019 Project Bsc

    57/116

  • 8/14/2019 Project Bsc

    58/116

    In above figure nothing is visible as there is no need to be.The following is the brief explanation of the software for serial communication.The timer with the name Timer1 is used to check the input.txt file after each 100milliseconds. The MS Comm. control with name serial is used to send data seriallyto microcontroller. The RichTextBox with name rt is used to take data from theinput.txt file. It can be selected similarly as MS Comm. control. The textbox with

    name Text1 is used to take the data from RichTextBox so that it can be send tomicrocontroller. Note that as soon as text in textbox is changed it is sent tomicrocontroller.

    52

  • 8/14/2019 Project Bsc

    59/116

    Summary

    In this chapter we examined the software of the project that was

    programmed mainly in HTML & ASP while the serial communication with

    microcontroller was done in visual basic. We also discussed the

    Microsoft FrontPage and how it makes our work easy while doing the

    web page designing. The code of the project along with screen shoots

    are also given that will help in understanding the project.

    References

    Microsoft FrontPage Help file

    http://www.w3schools.com/asp/default.asp

    Visual Basic 6 How to program

    By Deitel & DeitalT.R. Nieto

    53

  • 8/14/2019 Project Bsc

    60/116

  • 8/14/2019 Project Bsc

    61/116

    4.1 Introduction

    Up to now we talked in detail about how to make communication possiblebetween client and server. But we havent talked much about the communicationbetween server and microcontroller. As we know that client and server are

    connected with each other through Internet by telephone line or any other wayand we designed software for making their communication possible. Similarly theserver and the microcontroller are connected to each other through a cable (DB9Connecter) using serial communication and software is designed for serialcommunication between microcontroller and server. But microcontroller is a chipwith 40 pins and how can we make microcontroller compatible with computer isthe big task.

    In this chapter we will discuss what should be done on server sideparticularly concentrating on hardware so that the server can communicate withmicrocontroller serially. Also we will discuss how to connect the home applianceswith microcontroller so that they are being controlled by microcontroller signals,

    which are basically the client signals.

    4.2 Serial communication with microcontroller

    As we know that 8051 microcontroller is a 40-pin device but it has two pinsthat are used specifically for transferring and receiving data serially. These pinsare TXD (pin 11 or p3.1) and RXD (pin 10 or p3.0) used for transmission andreception of data (respectively) serially. These pins will be used for serialcommunication.

    Serial data communication uses two methods: synchronous andsynchronous

    The synchronous method transfers a block of data at a time. The asynchronous serial communication transfer single byte at a time.

    It is possible to write software to use either of these methods, but theprograms can be tedious and long. For this reason there is special IC chip madeby manufacturers for serial communication named as UART (universalasynchronous receiver-transmitter) and USART (universal synchronous-asynchronous receiver-transmitter). The 8051 has built-in UART chip.

    To allow compatibility among data communication equipment made byvarious manufacturers an interfacing standard called RS232 was set by the EIA.This standard is used in PCs and other electronic equipments, but RS232 is not

    TTL compatible. In RS232 logic 1 is represented by 3 to 25V while the logic 0is represented by +3 to +25V making 3 to +3V undefined. Thus to connect anyRS232 to microcontroller we use voltage converters such as MAX232 to convertthe TTL logic levels to RS232 voltage levels and vice versa. Other advantage ofMAX232 chip is that it uses a +5 volt power source, which is the same as thesource voltage for the 8051.The MAX232 has two sets of line drivers fortransferring and receiving data, i.e. pins 10 and 11 are used for transmitting andpins 9 and 12 are used for reception of data. Only one set is used for 8051 and

    55

  • 8/14/2019 Project Bsc

    62/116

    the other set is left unused. E.g. pins 9 and 10 are used with TXD and RXD of8051 and second set is left unused in figure below. The capacitors widely usedare of 22-microfarad value.

    The RS232 cables are commonly referred to as DB25 connectors having 25pins but as not all pins are used in PCs thus IBM introduced the DB9 connectors

    having 9 pins used in PCs.

    4.2.1 Circuit diagram for serial communication

    162738495

    RS232

    4.2.2 Programming microcontroller for receiving data seriallyAfter the above circuit formed and connected to microcontroller then the

    microcontroller can receive and transmit data serially from the computer i.e.server but the microcontroller has to be programmed to do that which is doneusing the assembly language. The program is then written inside themicrocontroller chip using the burner. The steps of the program and the programare given below.

    In the program below the following steps have been taken:

    1. The TMOD register is loaded with the value 20H=00100000, indication theuse of timer 1 in mode 2 (8bit auto reload) to set the baud rate2. TH1 is loaded with 6 decimal (i.e. 4800 baud rate while assuming the

    XTAL=11.0596).3. The SCON register is loaded with the value 50H=01010000, indication

    serial mode 1, where 8-bit data is framed with start and stop bits. Also bythis way RxD is enabled.

    4. TR1 is set to 1 to start timer 1.

    TXD

    RXD

    18

    3

    4

    5

    6

    7

    2

    10

    9

    15

    16U3

    C6

    C4

    VCC

    C5

    Microcontroller

    side or TTL side

    MAX232

    C3

    Schematic circuit of RS232

    56

  • 8/14/2019 Project Bsc

    63/116

    5. CLR TI command is used to clear SCON.1 flag (flag for transmission).6. Character to be transmitted is written in SBUF register and received are

    also put by microcontroller in SBUF register7. TI flag is monitored by JNB TI,label to see if character is transferred.8. To start next character we go to step five again.

    The program is shown below:

    ; Program for serial communication; 4800 baud rate

    MOV P0,#00HMOV TMOD,#20H ;TIMER1, MODE 2 (AUTO RELOAD)

    MOV TH1,#-6 ;4800 BAUD RATEMOV SCON,#50H ;8-BIT, 1 STOP, REN (RECIEVE ENABLE)SETB TR1 ;START TIMER 1

    ;;;;;;;;; receive from PC ;;;;;;;;

    HERE: JNB RI,HERE ; WAIT FOR DATA TO COMEMOV A,SBUF ;SAVE INCOMMING BYTE IN A REGISTERMOV P0,A ;SEND TO PORT 0CLR RI ;GET READY TO RECEIVE NEXT BYTESJMP HERE ;JUMP TO LABEL HERE

    END

    4.3 Power Supply circuit for microcontroller

    The microcontroller needs Adapter of the following electrical rating and is themain source of supply:Input: 220 V ACOutput: 20 V DC

    The output of the adapter is connected to the AC IN jack on the board, whichhas the capability to get AC input or DC input. In case of AC input the AC isconverted to DC through a on board rectifier BRIDGE" and filter circuitcomprising capacitors C1, C2, C3 . If adapter is not available DC input (20 VDC) can also be applied to the connector DC IN on board through a bench toppower supply.After applying the proper rated input to the board, it is passed through on board

    voltage regulators U1 (7812) and U2 (7805) for 12 VDC and 5 VDC respectively.These voltages are used for proper functioning of the board. LED0 will glow up ifboard supply is proper.

    The following diagrams show the circuit that is essential in any microcontroller.

    57

  • 8/14/2019 Project Bsc

    64/116

    DC IN

    1 3

    2

    V V

    GNDIN OUT

    U2

    +

    C0

    1 3

    2

    V V

    GND

    I OU

    U1

    1AC IN

    2

    VCC

    DC IN 12V2

    1

    C2

    +

    AC2

    AC1+

    +

    BRIDGE

    +

    C1

    4.4 Reset circuit for microcontroller

    There can be two types of resets i.e. Automatic Reset and manual Reset.Automatic reset is obtained when power is applied to the board, which providesVcc to the RST pin of the controller through 10F capacitor and to GND through10K resistor, providing the Vcc rise time does not exceed 1ms.Manual Reset isactivated using a switch RESET . The schematic of Reset circuit is shown inFigure.

    C10

    R6

    VCC

    Reset

    R7

    VCC

    RESET

    58

  • 8/14/2019 Project Bsc

    65/116

    4.5 Relays

    Relays are extremely useful when we have a need to control a largeamount of current and/or voltage with a small electrical signal. The relay coil,

    which produces the magnetic field, may only consume fractions of a watt ofpower, while the contacts closed or opened by that magnetic field may be able toconduct hundreds of times that amount of power to a load. In effect, a relay actsas a binary (on or off) amplifier.

    In the figure shown, the relay's coil is energized by the low-voltage (12 VDC)source, while the single-pole, single-throw (SPST) contact interrupts the high-voltage (480 VAC) circuit. It is quite likely that the current required to energize therelay coil will be hundreds of times less than the current rating of the contact.Typical relay coil currents are well below 1 amp, while typical contact ratings forindustrial relays are at least 10 amps.

    One relay coil/armature assembly may be used to actuate more than one set ofcontacts. Those contacts may be normally open, normally closed, or anycombination of the two. As with switches, the "normal" state of a relay's contactsis that state when the coil is de-energized, just as you would find the relay sittingon a shelf, not connected to any circuit.Relay contacts may be open-air pads of metal alloy, mercury tubes, or evenmagnetic reeds, just as with other types of switches. The choice of contacts in arelay depends on the same factors, which dictate contact choice in other types ofswitches. Open-air contacts are the best for high-current applications, but theirtendency to corrode and spark may cause problems in some industrialenvironments. Mercury and reed contacts are sparkless and won't corrode, but

    they tend to be limited in current-carrying capacity.

    59

  • 8/14/2019 Project Bsc

    66/116

    Shown here is small relay that take up to 12V dc to control the 220 V ac voltages:

    Aside from the ability to allow a relatively small electric signal to switch arelatively large electric signal, relays also offer electrical isolation between coil

    and contact circuits. This means that the coil circuit and contact circuit(s) iselectrically insulated from one another. One circuit may be DC and the other AC(such as in the example circuit shown earlier), and/or they may be at completelydifferent voltage levels, across the connections or from connections to ground.In our project the input to the relay is taken from the pin of microcontroller andthus the relay is made ON/OFF and hence the load is controlled.

    60

  • 8/14/2019 Project Bsc

    67/116

    Summary

    In this chapter we examined the hardware of the project specially

    the hardware related to serial communication between

    microcontroller and server. We have also discussed briefly about

    relays that are used to switch on and off the load. Some circuit

    diagrams for making microcontroller kit are also given along with

    explanation.

    References

    The 8051 Microcontroller and Embedded systems

    By Mohammad Ali Mazidi

    Janice Gillespie Mazidi

    The 8051 Microcontroller (3rd Edition)

    By I. Scott Mackenzie

    http://www.ibiblio.org/obp/electricCircuits/Digital/DIGI_5.html

    Or Lessons in electric circuits volume iv digital

    By Tony R.kuphaldt

    61

  • 8/14/2019 Project Bsc

    68/116

    Notes for:

    Visual Basic 6.0

  • 8/14/2019 Project Bsc

    69/116

    1 What is Visual Basic?

    Visual Basic is a tool that allows us to develop Windows (Graphic UserInterface - GUI) applications. The applications have a familiar appearance to theuser.

    Visual Basic is event-driven; meaning code remains idle until called upon torespond to some event (button pressing, menu selection...). An event processorgoverns Visual Basic. Nothing happens until an event is detected. Once anevent is detected, the code corresponding to that event (event procedure) isexecuted. Program control is then returned to the event processor.

    1.1 Some Features of Visual Basic Full set of objects - you 'draw' the application Lots of icons and pictures for your use Response to mouse and keyboard actions Clipboard and printer access

    Full array of mathematical, string handling, and graphics functions Can handle fixed and dynamic variable and control arrays Sequential and random access file support Useful debugger and error-handling facilities Powerful database access tools ActiveX support Package & Deployment Wizard makes distributing your applications

    simple

    1.2 Structure of a Visual Basic ApplicationVB Application (Project) is made up of:

    Forms - Windows that we create for user interface Controls - Graphical features drawn on forms to allow userinteraction (text boxes, labels, scroll bars, command buttons, etc.)(Forms and Controls are objects.)

    Properties - Every characteristic of a form or control is specified bya property. Example properties include names, captions, size,color, position, and contents. Visual Basic applies defaultproperties. We can change properties at design time or run time.

    Methods - Built-in procedure that can be invoked to impart someaction to a particular object.

    Event Procedures - Code related to some object. This is the code

    that is executed when a certain event occurs. General Procedures - Code not related to objects. This code mustbe invoked by the application.

    Modules - Collection of general procedures, variable declarations,and constant definitions used by application.

    1

  • 8/14/2019 Project Bsc

    70/116

    2 Steps in Developing Application

    There are three primary steps involved in building a Visual Basic application:

    1) Draw the user interface

    2) Assignproperties to controls3) Attachcode to controls

    Well look at each step.

    2.1 Drawing the User Interface and Setting PropertiesVisual Basic operates in three modes.

    Design mode - used to build application Run mode - used to run the application Break mode - application halted and debugger is available

    We focus here on the design mode.

    Six windows appear when we start Visual Basic.

    The Main Window consists of the title bar, menu bar, and toolbar.The title bar indicates the project name, the current Visual Basicoperating mode, and the current form. The menu bar has drop-downmenus from which you control the operation of the Visual Basicenvironment. The toolbar has buttons that provide shortcuts to someof the menu options. The main window also shows the location of thecurrent form relative to the upper left corner of the screen and the

    width and length of the current form.

    The Form Window is central to developing Visual Basic applications.It is where you draw your application.

    2

  • 8/14/2019 Project Bsc

    71/116

    The Toolbox is the selection menu for controls used in your

    application.

    The Properties Window is used to establish initial property values forobjects. The drop-down box at the top of the window lists all objects inthe current form. Two views are available: Alphabetic and

    Categorized. Under this box are the available properties for thecurrently selected object.

    3

    Pointer

    Label

    Frame

    Check Box

    Combo Box

    Horizontal ScrollTimer

    Director List Box

    Sha es

    Ima e Box

    Ob ect Linkin

    Picture Box

    Text Box

    Command Button

    O tion Button

    List Box

    Vertical Scroll Bar

    Drive List Box

    File List Box

    Lines

    Data Tool

  • 8/14/2019 Project Bsc

    72/116

    The Form Layout Window shows where (upon program execution)

    your form will be displayed relative to your monitors screen:

    The Project Window displays a list of all forms and modules makingup your application. You can also obtain a view of the Form or Codewindows (window containing the actual Basic coding) from the Projectwindow.

    4

  • 8/14/2019 Project Bsc

    73/116

    As mentioned, the user interface is drawn in the form window. There are twoways to place controls on a form:

    1. Double-click the tool in the toolbox and it is created with a default size

    on the form. You can then move it or resize it.2. Click the tool in the toolbox, and then move the mouse pointer to theform window. The cursor changes to a crosshair. Place the crosshairat the upper left corner of where you want the control to be, press theleft mouse button and hold it down while dragging the cursor towardthe lower right corner. When you release the mouse button, thecontrol is drawn.

    To move a control you have drawn, click the object in the form window and dragit to the new location. Release the mouse button.To resize a control, click the object so that it is select and sizing handles appear.Use these handles to resize the object.

    5

    Click hereto move

    Use sizinghandles toresize

  • 8/14/2019 Project Bsc

    74/116

    2.2 Assign properties to controls

    Each form and control has properties assigned to it by default when we startup a new project. There are two ways to display the properties of an object. Thefirst way is to click on the object (form or control) in the form window. Then, click

    on the Properties Window or the Properties Window button in the tool bar. Thesecond way is to first click on the Properties Window. Then, select the objectfrom the Object box in the Properties Window. The figure below shows theProperties Window.

    The drop-down box at the top of theProperties Window is the Object box. Itdisplays the name of each object in theapplication as well as its type. Thisdisplay shows the Form object. TheProperties list is directly below this box.

    In this list, we can scroll through the listof properties for the selected object. Wemay select a property by clicking on it.We can change the properties by typinga new value or choosing from a list ofpredefined settings (available as a dropdown list). Properties can be viewed intwo ways: Alphabetic andCategorized.

    A very important property for eachobject is its name. The name is used byVisual Basic to refer to a particularobject in code.Object names can be up to 40characters long, must start with a lettermust contain only letters, numbers, andthe underscore (_) character. Namesare used in setting properties at run time

    and also in establishing procedure names for object events.

    2.3 Attach code to controlsWere now ready to attach code to our application. As objects are added to theform, Visual Basic automatically builds a framework of all event procedures. Wesimply add code to the event procedures we want our application to respond to.Attaching code to control is the programming side of the visual basics andrequires good programming skills. Thus we will understand differentprogramming terminologies and techniques used in visual basic to attach code tocontrols after drawing user interface and setting properties (we will mainly f