LAB MANUAL



LIST OF EXPERIMENTS

|Exp No. |Description of experiment |Page No. |

| | | |

|Exp. 1 |To understand architecture and pin out of 8051 microcontroller. |2 |

|Exp.2 |Demonstration and understanding of Edsim51 simulator. |7 |

|Exp.3 |Demonstration and practice of interfacing of LEDs, 7 segment display and keypad on Edsim51 |10 |

| |simulator. | |

|Exp. 4 |To Demonstrate the Tool Chain for Keil IDE (Embedded Systems Development Tool Chain) with the |11 |

| |example of LED Blinking Program. | |

|Exp. 5 |To demonstrate the procedure for flash programming for reprogrammable embedded system board |14 |

| |PT-51 using ATMEL FLIP 2_4_6. | |

|Exp. 6 |To interface 8 LEDs at Input-output port and create different patterns with PT-51 kit. |17 |

|Exp. 7 |To demonstrate timer working in timer mode and blink LED without using any loop delay routine.|19 |

|Exp. 8 |To demonstrate interfacing of seven-segment LED display and generate counting from 0 to 99 |28 |

| |with fixed time delay. | |

|Exp.9 |To demonstrate serial communication between PC and controller using serial UART of controller.|33 |

|Exp. 10 |To demonstrate interfacing of 16x2 LCD and print some welcome message on it. |38 |

| | | |

Experiment 1

8051 Architecture and Pin out

1.1 Objective: To understand architecture of 8051 in respect to RAM, ROM and SFRs and know about the pins of 8051 IC.

1.2 Description:

The 8051 Microcontroller is a Microcontroller designed by Intel in 1980's. It was based on Harvard Architecture and developed primarily for use in Embedded Systems. Originally it was developed using NMOS technology but as those requires more power to operate therefore Intel redesigned Microcontroller 8051 using CMOS technology and later versions came with a letter 'C' in their name, for example: 80C51. These latest  Microcontrollers requires less power to operate as compared to their predecessors.

You May Also Like: Introduction To Microcontroller 8051 & Features of Microcontroller 8051

              Microcontroller 8051 has two buses for program and data. Thus it has two memory spaces of 64K X 8 size for both program and data. It has an 8 bit processing unit and 8 bit accumulator. It also includes 8 bit B register as main processing blocks. It also have some other 8 bit and 16 bit registers.

             Microcontroller 8051 have an built in RAM for internal processing. This memory is primary memory and is used for storage of temporary data. It is Volatile memory i.e. its contents get vanished when the power is turned OFF.

Read Also: What is Difference Between Microcontroller And Microprocessor

Following is the block diagram of Microcontroller 8051. Let us have a look at each part or block of this Architecture:

[pic]

Explanation:

Central Processor Unit(CPU): As you may know that CPU is the brain of any processing device. It monitors and controls all operations that are performed in the Microcontroller. User have no control over the work of CPU. It reads program written in ROM memory and executes them and do the expected task.

Interrupts: As its name suggests, Interrupt is a subroutine call that interrupts Microcontroller's main operation or work and causes it to execute some another program which is more important at that time. The feature of Interrupt is very useful as it helps in cases of emergency. Interrupts gives us a mechanism

[pic]

to put on hold the ongoing operation , execute a subroutine and then again resumes normal program execution.

             The Microcontroller 8051 can be configured in such a way that it temporarily terminates or pause the main program at the occurrence of interrupt. When subroutine is completed then the execution of main program starts as usual. There are five interrupt sources in 8051 Microcontroller. 2 of them are external interrupts, 2 timer interrupts and one serial port interrupt.

You May Also Like: What is Microcontroller? How it Works?

Memory: Microcontroller requires a program which is a collection of instructions. This program tells Microcontroller to do specific tasks. These programs requires a memory on which these can be saved and read by Microcontroller to perform specific operation. The memory which is used to store the program of Microcontroller, is known as code memory or Program memory . It is known as 'ROM'(Read Only Memory).

                   Microcontroller also requires a memory to store data or operands temporarily. The memory which is used to temporarily store data for operation is known as Data Memory and we uses 'RAM'(Random Access Memory) for this purpose. Microcontroller 8051 has 4K of Code Memory or Program memory that is it has 4KB Rom and it also have 128 bytes of data memory i.e. RAM.

Bus: Basically Bus is a collection of wires which work as a communication channel or medium for transfer of Data. These buses consists of 8, 16 or more wires. Thus these can carry 8 bits, 16 bits simultaneously. Buses are of two types:

You May Also Like: Advantages and Applications of Microcontroller

• Address Bus

• Data Bus

Address Bus: Microcontroller 8051 has a 16 bit address bus. It used to address memory locations. It is used to transfer the address from CPU to Memory.

Data Bus: Microcontroller 8051 has 8 bits data bus. It is used to carry data.

Oscillator: As we know Microcontroller is a digital circuit device, therefore it requires clock for its operation. For this purpose, Microcontroller 8051 has an on-chip oscillator which works as a clock source for Central Processing Unit. As the output pulses of oscillator are stable therefore it enables synchronized work of all parts of 8051 Microcontroller.

Input/Output Port: As we know that Microcontroller is used in Embedded systems to control the operation of machines. Therefore to connect it to other machines, devices or peripherals we requires I/O interfacing ports in Microcontroller. For this purpose Microcontroller 8051 has 4 input output ports to connect it to other peripherals.

Timers/Counters: Microcontroller 8051 has 2 16 bit timers and counters. The counters are divided into 8 bit registers. The timers are used for measurement of intervals , to determine pulse width etc.

PIN DESCRIPTION OF THE 8051

Although 8051 family members (e.g., 8751, 89C51, 89C52, DS89C4xO) come in different packages, such as DIP (dual in-line package), QFP (quad flat package), and LLC (leadless chip carrier), they all have 40 pins that are dedicated to various functions such as I/O, RD, WR, address, data, and interrupts. It must be noted that some companies provide a 20-pin version of the 8051 with a reduced number of I/O ports for less demanding applications. However, since the vast majority of developers use the 40-pin chip, we will concentrate on that. Figure 8-1 shows the pins for the 8051 52. For the 8052 chip some of the pins have extra functions and they will be discussed as we study them.

xamining Figure 8-1, note that of the 40 pins, a total of 32 pins are set aside for the four ports PO, PI, P2, and P3, where each port takes 8 pins. The rest of the pins are designated as Vcc, GND, XTAL1, XTAL2, RST, EA, PSEN, and ALE. Of these pins, six (Vcc, GND, XTAL1, XTAL2, RST, and EA) are used by all members of the 8051 and 8031 families. In other words, they must be connected in order for the system to work, regardless of whether the microcontroller is of the 8051 or 8031 family. The other two pins, PSEN and ALE, are used mainly in 8031-based systems. We first describe the function of each pin. Ports are discussed separately.

Vcc

Pin 40 provides supply voltage

to the chip. The voltage source is +5V.

GND

Pin 20 is the ground.

XTAL1 and XTAL2

The 8051 has an on-chip oscillator but requires an external clock to run it. Most often a quartz crystal oscillator is connected to inputs XTAL1 (pin 19) and XTAL2 (pin 18). The quartz crystal oscillator connected to XTAL1 and XTAL2 also needs two capacitors of 30 pF value. One side of each capacitor is connected to the ground as shown in Figure 8-2 (a).

It must be noted that there are various speeds of the 8051 family. Speed refers to the maximum oscillator frequency connected to XTAL. For

example, a 12-MHz chip must be connected to a crystal with 12 MHz frequency or less. Likewise, a 20-MHz microcontroller requires a crystal frequency of no more than 20 MHz. When the 8051 is connected to a crystal oscillator and is powered up, we can observe the frequency on the XTAL2 pin using the oscilloscope.

If you decide to use a frequency source other than a crystal oscillator, such as a TTL oscillator, it will be connected to XTAL1; XTAL2 is left unconnected, as shown in Figure 8-2 (b).

RST

Pin 9 is the RESET pin. It is an input and is active high (normally low). Upon applying a high pulse to this pin, the microcontroller will reset and terminate

EA

The 8051 family members, such as the 8751/52, 89C51/52, or DS89C4xO, all come with on-chip ROM to store programs. In such cases, the EA pin is connected to Vcc. For family members such as the 8031 and 8032 in which there is no on-chip ROM. code is stored on an external ROM and is fetched by the 8031/32. Therefore, for the 8031 the EA pin must be connected to GND to indicate that the code is stored externally. EA. which stands for “external access,” is pin number 31 in the DIP packages. It is an input pin and must be connected to either Vcc or GND. In other words, it cannot be left unconnected.

PSEN

This is an output pin. PSEN stands for “program store enable.” In an 8031-based system in which an external ROM holds the program code, this pin is connected to the OE pin of the ROM. See Chapter 14 to see how this is used.

ALE

ALE (address latch enable) is an output pin and is active high. When connecting an 8031 to external memory, port 0 provides both address and data. In other words, the 8031 multiplexes address and data through port 0 to save pins. The ALE pin is used for demultiplexing the address and data by connecting to the G pin of the 74LS373 chip. This is discussed in detail in Chapter 14.

Ports 0, 1, 2 and 3

As shown in Figure 8-1 (and discussed in Chapter 4), the four ports PO, PI, P2, and P3 each use 8 pins, making them 8-bit ports. All the ports upon RESET are configured as input, since PO – P3 have value FFH on them. The following is a summary of features of PO – P3 based on the materials

Experiment 2

|Objective: Demonstration and understanding of Edsim51 simulator. |

|The Microcontroller Panel |

|  |

|[pic] |

|  |

| |

| |

|A screenshot of the microcontroller panel. This gives the user access to all the 8051's registers and data memory. |

| |

|Boxes that are white can be edited directly. Those that are grey cannot. For example, the port latch bits can be edited directly by the user, but |

|the port pins are controlled by the external peripherals and the port latches and cannot be edited. |

|When the mouse pointer is left to hover over one of the register labels, the register's address appears, as shown opposite for the PCON register. |

|  |

|[pic] |

| |

|The Bit field |

|In the above image, the individual bits for the accumulator are shown (ACC). The user can enter any address or SFR name in the blue box |

|(replacing ACC) and the bits for that given address will then be displayed. Also, if you let the mouse pointer hover over one of the bits |

|momentarily, the bit's description is displayed, as shown below: |

|  |

|[pic] |

|  |

|The bit field for the TMOD SFR is shown. Notice bit 2 is the counter/timer bit. Also notice the background is grey. This is because the TMOD |

|register is not bit-addressable - the user cannot alter these bits directly. |

| |

|  |

|  |

|  |

| |

|[pic] |

|  |

|The PSW is bit-addressable, therefore the background of each bit is white and the user can change any of the bits directly. |

| |

|  |

|  |

|  |

| |

|[pic] |

|  |

|The bitfield can be used to see the bit pattern of any address in RAM (0 to 7FH) by typing the address in the blue box. If the location is |

|bit-addressable, the bit backgrounds are white and the user can alter any of the eight bits. |

| |

|  |

|  |

|  |

| |

|[pic] |

|  |

|As with SFRs, if the location is not bit-addressable the backgrounds are grey. |

| |

The Assembly Code Panel

|A simple assembly program is shown in the assembly code panel to the left. This program|  |[pic] |

|runs in a continuous loop, displaying the numbers 0 to 9 and back to 0 on the first | | |

|7-segment display. A snapshot of its execution is shown to the right. | | |

|When the background of the assembly code text area is white is it editable. The programmer can write code directly here, or can load a |

|program from file using the Load button (dealt with in the next section). |

|  |

|When the program is ready for testing, the user can either click on the Assm button to execute instructions one at a time, or on |

|the Runbutton to run the program continuously. Either way, the program will first be assembled. If an error in the code is discovered, a |

|message is displayed in the message box above the assembly code (with a red background) and the line with the error is highlighted within |

|the code in red. |

|  |

|If the code assembles without errors, Assm is replaced by Step, the text area's background changes to light grey. The code cannot be edited |

|at this point. |

|  |

|If you want to go back to editing your code, simply click on the Reset button. |

| |

| |

|Source code window a bit small? Read about the source pane unlocker below. |

[pic]

Experiment 3

Objective: Interfacing of LEDs, 7-segment display and keypad.

Diagram:

[pic]

Guideline for program:

Writing to a port pin is very simple, as shown below:

SETB P3.5 ; set pin 5 of port 3

MOV P1, #4AH ; sending data 4AH to port 1 - the binary pattern on the port will be 0100 1010

MOV P2, A ; send whatever data is in the accumulator to port 2

Reading a port pin is also very simple, once the pin has been initialized for input:

SETB P1.0 ; initialize pin 0 of port 1 as an input pin

MOV P2, #FFH ; set all pins of port 2 as inputs

MOV C, P1.0 ; move value on pin 0 of port 1 to the carry

MOV R3, P2 ; move data on port 2 into R3

Experiment 4

Tool Chain of Keil IDE (Embedded Development Tool Chain) with the example of LED Blinking Program

Objective: To understand the procedure of creating source code for reprogrammable embedded system board using IDE such as Keil µVision.

Software Requirement: Editor like Keil µVision Ver 4 or less.

Description:

Understanding any processor or controller needs familiarity with its architecture and instruction set. Any architecture can be best understood using its instruction set through different programs.

One may use assembly language or embedded C for writing programs. Programs written in assembly language are completely processor dependent and need major changes when converting to other processor. While programs written in C are generally independent of processor and needs minor changes during conversion to other processors.

C is thus preferred for programming. But to know and understand a processor better, one must be familiar with assembly language.

All source code written in this document will be written using assembly language for 8051 architecture.

Some development environment is needed to prepare any application. An editor is needed first to provide a platform for writing programs i.e. source code.

A source code written in assembly/C language is needed to be converted to machine language (hex code) before programming into processor. This conversion is done by compiler which converts assembly/C language code to hex code.

IDE i.e. Integrated development Environment, serves both these purposes as well as provide debugging facility.

Assembly language file will be stored by extension .asm, C file by extension .c and hex file by extension .hex.

Procedure:

Many free software are available for educational purpose e.g. Keil, SDCCDown load free tools for IDE from

download/product,



IDE for 8051 architecture can be downloaded using these links. It’s an integrated development environment for creation and compilation of assembly/C source code for any 8051 architecture based target boards. It also provides debugging facility [1].

Steps:

Click on Keil µVision4 icon for getting started.

Click on Project tab>Make new project> Select target device.

Click on File>New file.

Prepare a test code in assembly language as shown in editor window. Save it with .asm extension.

Add this created file to project. One may add one or more than one file in a single project.

Click Target1 ( at left side pane)>Source Group> Right click to add code file.

Open Project tab> Options for target target1> Output tab>check ‘create hex file’ option.

Open Project tab> Build target. This will generate compiled .hex file from the .asm or .C file, in the project created.

One may refer Help tab for further help for using this tool.

*Many video are there in you tube which may be referred to understand the procedure better.

[pic]

Figure : IDE- Keil µVision

2.5 Result: Sample program for LED blinking is written, compiled and hex file generated.

2.6 Conclusion:- Different programs can be written, debugged and simulated using IDE.

2.7 Remarks:- Different programs should be written and tested using assembly/C language for better understanding of the tool .

2.8 References:-

[1]

Experiment 5

Objective: To learn ATMEL Flip programmer.

Board Specifications:

• The specifics of the board are

– Micro controller : Atmel AT89C5131A.

– 24MHz Crystal clock generator.

– 4 LEDs and 4 Switches for simple programs.

– LCD connection port.

– Type B USB connector.

– All ports accessible.

– Can be mounted on breadboard.

Board Layout : Ports

[pic]

CAUTION!!!!!

• Note that the certain port pins have been given multiple functionalities and they can used to perform only one function at a time

• For e.g., Port 1 pins have been used for switches and LEDs. So, when they are used for this function they cannot be used for any other function in your code.

Getting Started:

• Ensure that the you have downloaded and installed

– Keil uvision 4

– Atmel Flip (2.4.6 for Windows XP, 3.4.5 for Windows 7) [If not, installation procedure given at the end].

• Also, make sure that the device driver for Pt-51 has been installed.

Operating modes:

• It operates in 2 modes –

• A. Boot loader mode : In this mode the Board communicates with the PC and the flash memory can be programmed.

• B. Application mode : The board runs the code which has been programmed into its flash memory.

Boot loader Mode:

• To get into this mode, the following steps have to be done in the same sequence as given below

– Press and hold the “Boot” button.

– Press and hold the “Reset” button, for a moment and release it, still holding the Boot button.

– Release the Boot button.



Application mode

• When Pt-51 is powered up, if some code exists in the flash memory then it directly enters the application mode else it enters the boot loader mode.

• In this mode, the board will not communicate with the PC and cannot be programmed.

Experiment 6

Interfacing LEDs at Input/ Output Port

Objective: To interface 8 LEDs at Input-output port and create different patterns.

Software Requirement: Editor like Keil µvision ver 4 or less, Flash programmer.

Hardware Requirement: Target boards with P89V51RD2/AT89S52 controller as per circuit given in figure , Serial Cable with DB9 Connector/USB 89SXX Programmer with cable.

Procedure:

1. Connect 8 LEDs at port 2 (pin 21 to 28) as per given block diagram of figure6.1. Rest of the circuit will remain same as figure 1.6/4.5

2. Write program for LED blinking at port.

3. Build/Compile project.

4. Flash program and observe results.

[pic]

Figure : LED connections at port2

Source Code : Table 6A

|;Program written using assembly language of 8051 architecture for | lcall delay |

|P89V51RD2 |mov p2,#0bfh |

|;This program will lit one LED at a time ;starting from port 2.0 to |lcall delay |

|port 2.7 and create ;LED rotation pattern. All other LEDs will ;remain|mov p2,#7fh |

|off. |lcall delay |

|org 0100 |ljmp main |

| | |

|main: mov p2,#0feh |; Subroutine for some delay generation, to view ;LED blinking |

|lcall delay | |

|mov p2,#0fdh |delay: mov r0,#0ffh |

|lcall delay |loop1: mov r1,#05h |

|mov p2,#0fbh |loop2: mov a,#05h |

|lcall delay |loop3: dec a |

|mov p2,#0f7h |jnz loop3 |

|lcall delay |djnz r1,loop2 |

|mov p2,#0efh |djnz r0,loop1 |

|lcall delay |ret |

|mov p2,#0dfh |end |

*Bold lines shows change in instructions

Source code : Table6B

Same function can be achieved using source code 3B with less number of instructions

|;Rotate accumulator left command is use to ;create same rotating |; subroutine for some delay generation |

|effect. | |

|org 0100 |delay: mov r0,#0ffh |

|main: mov a,#0feh |Loop1: mov r1,#05h |

|main1: mov p2,a |loop2: mov r2,#05h |

|lcall delay |loop3: djnz r2,loop3 |

|lcall delay |djnz r1,loop2 |

|rl a |djnz r0,loop1 |

|ljmp main1 |ret |

| |end |

Result: Output at LEDs observed as per programs, i.e. LED rotating in circular manner is achieved at port2.

Conclusion:- with smart use of instructions memory area of controller can be saved as can be seen from above example.

Remarks:- Different LED patterns can be generated and tested e.g. blinking of all LEDs, Blinking of alternate LEDs, 8 bit/4bit binary pattern generation, BCD number pattern generation, dancing LED etc.

Reference :- Datasheet 89V51RD2/ AT89S52

Experiment 7

Timer in Timer Mode

Objective: To achieve timer working in timer mode and blink LED without using any loop delay routine.

Software Requirement: Editor like Keil µvision ver 4 or less, Flash programmer.

Hardware Requirement: Target board with P89V51RD2/AT89S52 controller as per circuit given in figure 1.6/4.5, Serial Cable with DB9 Connector/ USB 89SXX programmer with cable.

| The 8051 Timers |

|  |The basic 8051 has two on-chip timers that can be used for timing durations or for counting external events. |

|  |  |

|  |Interval timing allows the programmer to perform operations at specific instants in time. For example, in our LED flashing program the |

| |LED was turned on for a specific length of time and then turned off for a specific length of time. We achieved this through the use of |

| |time delays. Since the microcontroller operates at a specific frequency, we could work out exactly how many iterations of the time delay |

| |was needed to give us the desired delay. |

|  |However, this is cumbersome and prone to error. And there is another disadvantage; the CPU is occupied, stepping through the loops. If we|

| |use the on-chip timers, the CPU could be off doing something more useful while the timers take on the menial task of keeping track of |

| |time. |

|  |  |

|  |The Timers' SFRs |

|  |The 8051 has two 16-bit timers. The high byte for timer 1 (TH1) is at address 8DH while the low byte (TL1) is at 8BH. |

|  |The high byte for timer 0 (TH0) is at 8CH while the low byte (TL0) is at 8AH. |

|  |  |

|  |Both timers can be used in a number of different modes. The programmer sets the timers to a specific mode by loading the appropriate |

| |8-bit number into the Timer Mode Register (TMOD) which is at address 89H. |

|  |  |

|  |Timer Mode Register |

|  |  |

|  |TMOD |

| | |

| |Bit |

| |Name |

| |Timer |

| |Description |

| | |

| |7 |

| |Gate |

| |1 |

| |Gate bit; when set, timer only runs while INT-bar is high. This bit is used in conjunction with interrupts and will be dealt with later. |

| | |

| |6 |

| |C/T-bar |

| |1 |

| |Counter/timer select bit; when set timer is an event counter, when cleared timer is an interval timer. |

| | |

| |5 |

| |M1 |

| |1 |

| |Mode bit 1 |

| | |

| |4 |

| |M0 |

| |1 |

| |Mode bit 0 |

| | |

| |3 |

| |Gate |

| |0 |

| |Gate bit; when set, timer only runs while INT-bar is high. |

| | |

| |2 |

| |C/T-bar |

| |0 |

| |Counter/timer select bit; when set timer is an event counter, when cleared timer is an interval timer. |

| | |

| |1 |

| |M1 |

| |0 |

| |Mode bit 1 |

| | |

| |0 |

| |M0 |

| |0 |

| |Mode bit 0 |

| | |

|  |The functions of the 8-bits of TMOD are described in the above table. The top four bits are for timer 1 and the bottom four bits have the|

| |exact same function but for timer 0. |

|  |The Gate bits are used in conjunction with interrupts and will be dealt with at a later stage. For the moment we can take it that bits 7 |

| |and 3 are always cleared. |

|  |As mentioned above, the timers can be used for counting external events or for timing intervals. If you wish the timer to be an event |

| |counter you set the corresponding C/T-bar bit. Similarly, if you wish it to be an interval timer you reset the corresponding C/T-bar bit.|

|  | There are two mode bits (M1 and M0) for each timer. The table below describes their function. |

|  |M1 |

| |M0 |

| |Mode |

| |Description |

| | |

| |0 |

| |0 |

| |0 |

| |13-bit timer mode (this mode exists simply to keep the 8051 backwards compatible with its predecessor, the 8048, which had a 13-bit timer)|

| |- we will not be using mode 0. |

| | |

| |0 |

| |1 |

| |1 |

| |16-bit timer mode |

| | |

| |1 |

| |0 |

| |2 |

| |8-bit auto-reload mode |

| | |

| |1 |

| |1 |

| |3 |

| |Split timer mode - this mode will be dealt with at a later stage |

| | |

|  |There are four timer modes, set by the bits M1 and M0. Mode 0 is not commonly used. Mode 3 we will deal with later. |

|  |  |

|  |Mode 1 - 16-bit mode |

|  |The high byte (THx) is cascaded with the low byte (TLx) to produce a 16-bit timer. This timer counts from 0000H to FFFFH - it has |

| |216 (65,536) states. An overflow occurs during the FFFFH to 0000H transition, setting the overflow flag (to be dealt with shortly). |

|  |  |

|  |Mode 2- 8-bit auto-reload mode |

|  |The timer low byte (TLx) operates as an 8-bit timer (counting to FFH) while the high byte holds a reload value. When the timer overflows |

| |from FFH, rather than starting again from 00H, the value in THx is loaded into TLx and the count continues from there. |

|  |  |

|  |[pic] |

|  |A diagrammatic representation of mode 1 and mode 2. (The 8051 Microcontroller, 3rd Edition - I. Scott MacKenzie) |

|  |Timer Control Register |

|  |TCON |

| | |

| |Bit |

| |Symbol |

| |Bit Address |

| |Description |

| | |

| |7 |

| |TF1 |

| |8FH |

| |Timer 1 overflow flag; set by hardware upon overflow, cleared by software. |

| | |

| |6 |

| |TR1 |

| |8EH |

| |Timer 1 run-control bit; manipulated by software - setting starts timer 1, resetting stops timer 1. |

| | |

| |5 |

| |TF0 |

| |8DH |

| |Timer 0 overflow flag; set by hardware upon overflow, cleared by software. |

| | |

| |4 |

| |TR0 |

| |8CH |

| |Timer 0 run-control bit; manipulated by software - setting starts timer 0, resetting stops timer 0. |

| | |

| |3 |

| |IE1 |

| |8BH |

| |The bottom four bits of TCON are used in conjunction with interrupts - they will be dealt with at a later stage. |

| | |

| |2 |

| |IT1 |

| |8AH |

| | |

| | |

| |1 |

| |IE0 |

| |89H |

| | |

| | |

| |0 |

| |IT0 |

| |88H |

| | |

| | |

|  |The top four bits of TCON are the only ones that interest us at the moment since the bottom four are used with interrupts. |

|  |Bit 7 and bit 5 are the timer overflow flags (TFx). The overflow flag is set by the hardware once an overflow occurs. For example, if |

| |timer 0 is in mode 1 (16-bit mode) then, during the state transition from FFFFH to 0000H the overflow flag TF0 is set by the hardware. |

|  |Bit 6 and bit 4 are the timer run-control bits (TRx). A timer is started by setting TRx and stopped by clearing TRx. |

|  |The TCON register is bit addressable because the programmer needs ease of access to the individual bits. You may wish to test the contents|

| |of TF1, to see when timer 1 overflows. Or you may wish to stop timer 0 without effecting timer 1, as shown below. Clearing TR0 does not |

| |effect the other seven bits of TCON. |

|  |CLR TR0 |

|  |Initialising the Timers |

|  |The timers are initialised (ie; put into a particular mode of operation) by moving the appropriate value into the TMOD register. For |

| |example, if you wished to use timer 1 as a 16-bit interval timer and timer 0 as an 8-bit auto-reload event counter you would load the |

| |following value into TMOD. |

|  |  |

|  |  |

| |bit |

| |value |

| |note |

| | |

| |Timer 1 |

| |7 |

| |0 |

| |gate (set when using interrupts, reset otherwise) |

| | |

| | |

| |6 |

| |0 |

| |C/T-bar (0 because timer 1 is to be an interval timer) |

| | |

| | |

| |5 |

| |0 |

| |mode bits (01 for mode 1 - 16-bit) |

| | |

| | |

| |4 |

| |1 |

| | |

| | |

| |Timer 0 |

| |3 |

| |0 |

| |gate (set when using interrupts, reset otherwise) |

| | |

| | |

| |2 |

| |1 |

| |C/T-bar (1 because timer 0 is to be an event counter) |

| | |

| | |

| |1 |

| |1 |

| |mode bits (10 for mode 2 - 8-bit auto-reload) |

| | |

| | |

| |0 |

| |0 |

| | |

| | |

|  |The instruction for initializing the timers as above would therefore be: |

|  |MOV TMOD, #16H |

|  |(0001 0110bin = 16H) |

|  |Starting and Stopping the Timers |

|  |The run/control bits in the TCON register are used for starting and stopping the timers. As shown in the TCON table above, bit 6 is the |

| |run control bit for timer 1 while bit 4 is the run control bit for timer 0. The following two instructions start timer 0 and timer 1 |

| |respectively. |

|  |SETB TR0 |

| |SETB TR1 |

|  |Setting the run control bit starts the timer. To stop the timer, you clear the corresponding run control bit |

|  |CLR TR0; stop timer 0 |

| |CLR TR1; stop timer 1 |

|  |Reading the Timers on the Fly |

|  |Reading the current value of a timer when it is not running is quite easy. You simply move the high byte and the low byte to the desired |

| |memory location (usually a register). |

|  |MOV R7, TH0; move the high byte of timer 0 into R7 |

| |MOV R6, TL0; move the low byte of timer 0 into R6 |

|  |  |

|  |However, to read the contents of a timer while it is running (ie; on the fly) poses a problem. Let's say we put timer 0's current value |

| |into R7 and R6, as shown above. The problem arises when the value in the low byte goes from FFH to 00H immediately after we read the high |

| |byte. |

|  |For example, let's say the high byte is 08H and the low byte is FFH. The controller moves the high byte into R7. Then, before it gets a |

| |chance to read the low byte, it changes from FFH to 00H. The controller then moves this value into R6, getting an overall 16-bit reading |

| |of 0800H when the value should have been 08FFH. |

|  |The solution is to read the high byte, then read the low byte, then read the high byte again. If the two readings of the high byte are not|

| |the same repeat the procedure. The code for this method is detailed below. |

|  |  |

|  |tryAgain: |

| | |

| |MOV A, TH0 |

| |MOV R6, TL0 |

| |CJNE A, TH0, tryAgain; if the first reading of the high byte (in A) is not equal |

| | |

| |to current reading in the high byte (TH0) try again |

| | |

| |MOV R7, A; if both readings of the high byte are the same move the first reading into R7 |

| | |

| |- the overall reading is now in R7R6 |

| | |

|  | Having said all that, I tend to not bother reading the timer on the fly. Instead, I wait for it to overflow, as we shall see, |

|  |  |

|  |Clock Sources |

|  |As discussed earlier, both timers can be used either as interval timers or event counters. The only difference between the timer as an |

| |interval timer and an event counter is the clock source. The timers are triggered by a negative edge to their clock inputs. |

|  |  |

|  |When the timer is operating as an interval timer it is being clocked by the internal clock (this internal clock, usually 12MHz, is |

| |actually divided by twelve to yield a reasonable clock frequency for most applications - therefore, the internal clocking signal is |

| |usually 1MHz). |

|  |  |

|  |When the timer is operating as an event counter it is triggered by an external source connected to pin T0 (port 3, pin 4) for timer 0 and |

| |pin T1 (port 3, pin 5) for timer 1. In this way the timer can count events rather than keep track of time. |

|  |For example, if a sensor on a conveyor belt system produces a negative edge everytime an item on the belt passes by, this sensor could be |

| |connected to pin T0. If timer 0 were then initialised as an event counter and started, everytime an item passed in front of the sensor, a |

| |negative edge would be generated on pin T0, thus triggering timer 0 which would proceed to the next stage in its count sequence. In this |

| |way, timer 0 is counting the number of items passing by on the conveyor belt. |

|  |  |

|  |Generating Pulse Trains |

|  |We can very effectively use the timers where exact timing conditions are necessary. For example, if we need to generate a 4KHz pulse train|

| |on pin 5 of port 1, we could use either timer 0 or timer 1 in 8-bit auto-reload interval timing mode. |

|  |Let's say we decide to use timer 0. We must first work out the value that must be placed in the TMOD register to put timer 0 into the |

| |desired mode. |

|  |  |

|  |  |

| |bit |

| |value |

| |note |

| | |

| |Timer 1 |

| |7 |

| |0 |

| |since we are not using timer 1 we can put all four upper bits to 0 |

| | |

| | |

| |6 |

| |0 |

| | |

| | |

| | |

| |5 |

| |0 |

| | |

| | |

| | |

| |4 |

| |0 |

| | |

| | |

| |Timer 0 |

| |3 |

| |0 |

| |gate (set when using interrupts, reset otherwise) |

| | |

| | |

| |2 |

| |0 |

| |C/T-bar (0 because timer 0 is to be an interval timer) |

| | |

| | |

| |1 |

| |1 |

| |mode bits (10 for mode 2 - 8-bit auto-reload) |

| | |

| | |

| |0 |

| |0 |

| | |

| | |

|  |Therefore, the value to be placed in TMOD is 02H. |

|  |  |

|  |Mode 2 is the 8-bit auto-reload mode. The low byte of the counter behaves like an 8-bit counter. The high byte is used for storing the |

| |reload value. When an overflow occurs (ie; state transition from FFH) the timer does not revert to zero. Rather, the value in the high |

| |byte is placed in the low-byte and the sequence begins again from there. |

|  |If the high byte contains 00H, then the timer behaves exactly like an 8-bit timer, counting continuously from 00H to FFH and back to 00H |

| |again. |

|  |As an interval timer, the clocking signal arrives every 1us (microsecond). The 12MHz system clock is divided by 12 and then fed to the |

| |timer. Therefore, the timer's clocking signal is 1MHz, resulting in a negative edge every 1us (time = 1/frequency). |

|  |If the high byte contains 00H, the counter goes through the full 256 states (00H to FFH). Therefore, an overflow occurs every 256us. |

|  |We require a 4KHz pulse train on pin 5 of port 1; in other words, P1.5 must toggle every 125us (4KHz signal - one cycle every 250us - one |

| |half cycle every 125us). |

|  |We need to load the high byte with a suitable value to result in an overflow every 125us. To find the value, we subtract the required time|

| |from the maximum time (256us - 125us = 131). Changing this value to HEX gives 83H. |

|  |The code for generating the pulse train is detailed below: |

|  |MOV TMOD, #02H; set up timer 0 as 8-bit auto-reload interval timer |

| |MOV TH0, #83H; put reload value into timer 0 high byte |

| |SETB TR0; start timer 0 |

| | |

| |waitForOverflow: |

| | |

| |JNB TF0, $; repeat this line while timer 0 overflow flag is not set |

| |CLR TF0; timer 0 overflow flag is set by hardware on transition from FFH - the flag must be reset by software |

| | |

| |CPL P1.5; complement (invert) pin 5 on port 1 - this instruction toggles the specified bit |

| |JMP waitForOverflow; go back and wait for overflow again |

| | |

|  |Note: with the above program, P1.5 is at 1 for the same length of time that it's at 0 (125us each), therefore the waveform has 50% duty |

| |cycle. |

Procedure:

1. Write source code e.g. LED blinking at port.

2. Build/Compile project.

3. Program controller with desired code and observe results.

Source Code: Table

|;Timer0 is used in timer mode. Runs from ;0000 to ffffh and blink LED |; Timer0 ISR routine |

|at port 2.2, Timer ;0 ISR,Timer0 in mode 1 | |

| |tmr0_int: clr tr0 |

|org 0000h |clr tf0 cpl p2.2 |

|ljmp start |mov tl0,#00h |

|org 000bh |mov th0,#00h |

|start: mov tmod,#01h |setb tr0 reti |

|mov tl0,#00h |end |

|mov th0,#00h | |

|SETB EA | |

|SETB ET0 | |

|setb tr0 ljmp $ | |

Result: Results observed as per programs, i.e. one LED blinking at port2.2 of controller, as per program.

Conclusion:- As timer is used to generate delay, controller is free to complete other tasks while generating delay.

Remarks:- Using timers different functions can be achieved. E.g. generation of fixed time delay, generation of frequency at, any I/O port etc.

Counter function of timers allow measurement of unknown frequency, event counting etc.

Reference :- Datasheet 89V51RD2/AT89S52

Experiment 8

Seven segment LED display interfacing with PT-51 board.

Objective: To achieve interfacing of seven segment LED display and generate counting from 0 to 99 with fixed time delay.

Software Requirement: Editor like Keil µvision ver 4 or less, Flash programmer.

Hardware Requirement: Target board with P89V51RD2/AT89S52 controller as per circuit given in figure 1.6/4.5, Display circuit as per figure 11.1, Serial Cable with DB9 Connector/ USB 89SXX programmer with cable.

Procedure:

1. Connect circuit as per figure 11.1. Connect J1 and J2 between points 2 and 3.Connect point3 of J1 to port1.6 and point3 of J2 to port1.7 of target board controller (figure1.6/4.5).

2. Short CON5 and CON6 and connect them to CON2 of target board controller (figure1.6/4.5).

3. Write desired source code.

4. Build/Compile project and program controller with desired code and observe results.

[pic]

Figure Seven Segment LED display connections with controller

Source Code: Table

|;Only for two seven segment connected at ;port2, ;control pins for | setb p1.7 |

|common cathode type ;Seven segment is connected at port 1.6 and ;1.7 |djnz r6,nextdig |

| |mov r6,#0ffh |

|org 0000h | |

|ljmp start1 |inc r4 |

|start1: setb p1.7 |djnz r3,nextdig |

|setb p1.6 |inc r5 |

|mov r5,#00h |mov a,r5 |

|mov r4,#00h |mov r4,#00h |

|mov r6,#0ffh |setb p1.6 |

|nextseg: mov r3,#0ah |cjne r5,#0ah,nextseg |

|mov dptr,#1000h |sjmp start1 |

|nextdig: mov a,r4 |;delay routine |

|movc a,@a+dptr |delay: mov r0,#02 |

|mov p2,a |loop1: mov r1,#10 |

|lcall delay |loop2: mov r2,#100 |

|clr p1.7 |loop3: djnz r2,loop3 |

|setb p1.6 |djnz r1,loop2 |

|mov a,r5 |djnz r0,loop1 |

|movc a,@a+dptr |ret |

|mov p2,a | |

|lcall delay |org 1000h |

|clr p1.6 |db 03fh, 06h, 05bh, 04fh, 66h, |

| |06dh, 07dh, 07h, 07fh, 067h end |

Result: Two common cathode seven segments LED displays are showing counting starting from 0 to 99 with fixed time delay.

Interfacing 7-segment display using 7447 decoder:

The Light Emitting Diode (LED), finds its place in many applications in this modern electronic fields. One of them is the Seven Segment Display. Seven-segment displays contains the arrangement of the LEDs in “Eight” (8) passion, and a Dot (.) with a common electrode, lead (Anode or Cathode). The purpose of arranging it in that passion is that we can make any number out of that by switching ON and OFF the particular LED’s. Here is the block diagram of the Seven Segment LED arrangement.

Pin configuration of a seven segment display:

[pic]

LED’s are basically of two types:

1. Common Cathode (CC)

All the 8 anode legs uses only one cathode, which is common.

2. Common Anode (CA)

The common leg for all the cathode is of Anode type.

For the discussion purpose, we use CC LED, where by just reversing the logical voltages we can implement the same for CA LED also.

In a CC LED, all the 8 legs (‘a’ through ‘h’) are of anode type and the common cathode will be connected to the GND of the supply. By energizing any of the legs with +5 Volts will lead to switch the correspondent segment ON. In the microprocessor binary system, 0Volts will be considered as Binary 0, and 5Volts will be considered as Binary1. Considering these two condition, we can make an arrangement as the microcontroller gives OUT the 0s and 1s through its ports, which is connected to the 8 legs of the LED. Of course, we can control the Port Output; implicitly we can Switch-ON required legs of the display.

There 2 methods of interfacing LED with the Microcontroller Intel 8051/8951.

1. Using lookup table. This uses 7 output pins of microcontroller

2. Using 7447 decoder. This method uses 4 output pins of microcontroller

The difference between the two main methods is simple and clear. In both the cases, microcontroller communicates with external world through its ports. But, in the 1st case, we connect all the 8 pins of the port directly to the LED and control the voltage through the ports manually to display the desired number. But, in the second case, we send the BCD of the number that we wanted to display to a middleware IC 7447, the BCD to LED code converter, which by itself gives out the correspondent 7 segment codes to the LED.

Here we explain using lookup table. Click here for the method “using 7447 decoder”

Using 7447 decoder:

The IC7447 is a BCD to 7-segment pattern converter. This setup is the advanced form of the setup where we entered the patterns manually to display the desired character. Here in this case, the IC7447 takes the Binary Coded Decimal (BCD) as the input and outputs the relevant 7 segment code. We connect first four pins of the microcontroller Port 2  to the 7447 and the Output 8 pins of 7447 to the 8 legs of the LED as shown in the figure. Te circuit diagrams are shown below, the first figure is interfacing the CA LED where as the second is of CC LED. The number required to display is sent as the lower nibble of the Port 2 of the Microcontroller. The 7447 converts the four input bits (BCD) to their corresponding 7-segment codes. The outputs of the 7447 are connected to the 7-segment display.

[pic]

[pic]

Program:

|This program displays characters 0 through 9 on seven-segment display |

|using IC 7447 as the middle wear. |

|  |

|again: mov a,#00h ; Start form zero |

|up: mov p2, a ; Move to Port 2 |

|mov r3,#255 ; Delay |

|D1: mov r1,#255 |

|D: djnz r1,D |

|djnz r3,D1 |

|inc a |

|cjne a,#0ah,up |

|sjmp again |

Conclusion:- Interfacing of seven segment LED displays is achieved.

Remark:- More than two seven segment LED displays can be connected at same port with different control pins. Alpha-numeric displays can also be connected in similar manner.

Reference :-

1. Datasheet 89V51RD2/AT89S52

2.

Experiment 9

RS232 Serial Communication between PC and controller

Objective: To achieve serial communication between PC and controller using serial UART of controller.

Software Requirement: Editor like Keil µvision ver 4 or less, Flash programmer, Hyper terminal at PC (download from ).

Hardware Requirement: Target board with P89V51RD2/AT89S52 controller as per circuit given in figure 1.6/4.5, Serial Cable with DB9 Connector/ USB 89SXX programmer with cable.

Procedure:

1. Write desired source code.

2. Build/Compile project.

3. Program controllers with desired code.

4. Close Flash programmer.

5. Connect target board with PC through DB9 serial connector, open hyper terminal for desired communication.

6. Set hyper terminal as shown in figures 12.1 to 12.4. Select Com port, baud rate as 9600, data bits as 8, parity as none, stop bit as 1 and flow control as none.

7. Observe results.

Source Code: Table

|; This code enables controller to communicate ;with PC at 9600 | mov scon, #50h |

|baud-rate. Data sent from PC ;will be echoed ;back to PC. Timer1 of |anl pcon,#7fh setb tr1 |

|;controller is used for generating baud rate. ;Serial interrupt |again: jmp again |

|routine resides at 0023h. LED ;at port 2.0 is used for indication. | |

|Stack ;pointer set at 08h. Timer1 is used in mode 2 ;i.e. auto reload |;Serial interrupt routine for checking transit or ;receive. Only if RI|

|mode. |flag is set, receive will ;occur else transmit. Receive indication is |

| |;given by LED at port 2.0. Read data from ;SBUF, this is data received|

|org 0000h |from PC, clear RI ;flag and load SBUF with same data to sent ;back to |

|jmp 0100h |PC |

|org 0023h | |

|call sr_int |sr_int: jnb ri,chktx clr p2.0 mov a,sbuf clr ri mov |

|reti |sbuf,a ret |

| |;After transmit complete give indication at ;LED connected at port 2.0|

|org 0100h |and clear transmit ;flag TI |

|start: setb p2.0 | |

|mov sp,#08h |chktx: setb p2.0 |

|clr tr1 |clr ti |

|mov tmod, #20h |ret |

|; Enable interrupt servicing and serial port ;interrupt, Load fdh in |end |

|timer1 for 9600 baud ;rate. Set 8 bit UART mode and enable ;reception.| |

|After then start timer1. | |

|mov ie,#90h | |

|mov th1,#0fdh | |

|mov tl1,#0fdh | |

Hyper Terminal settings will be as described under.

[pic]

Figure Open hyper terminal for checking serial communication between PC and Target board

[pic]

Figure Name hyper terminal for connections in serial communication

[pic]

Figure Select communication port for checking serial communication

[pic]

Figure :Select options for checking serial communication

Result: Target processor is receiving from PC, and then sent back received data to PC. Results are observed as per figure 12.5.

[pic]

Figure 12.5 Output received while checking serial communication

Conclusion:- Controller is communicating with PC using serial UART.

Remark:- Communication between two controllers can also be achieved using serial UART.

12.8 Reference :-

Datasheet 89V51RD2/AT89S52







Experiment 10

Interfacing of 16x2 LCD with microcontroller

Objective: To achieve interfacing of 16x2 LCD and print some welcome message on it.

Software Requirement: Editor like Keil µvision ver 4 or less, Flash programmer

Hardware Requirement: Target board with P89V51RD2/AT89S52 controller as per circuit given in figure 1.6/4.5, Serial Cable with DB9 Connector/USB 89SXX programmer with cable, 16x2 LCD , 10K POT, connecting wires.

Procedure:

1. Connect LCD with target board (figure 1.6/4.5) as per figure 15.1.

2. Write desired source code as per table 15. Refer figure 15.2.

3. Build/Compile project.

4. Program controllers with desired code.

5. Close Flash programmer

6. Observe results.

[pic]

Figure : 16x2 LCD module connections with microcontroller

[pic]

Figure :LCD module commands and instruction set

Source Code: Table

|; This code enables controller to send data on ;LCD. Port 0 is used | |

|for data bus, P1.0 for ;enable, P1.1. for RW and P1.2 for RS. |;set entry mode |

| |setb P1.0 |

|org 0000h |nop |

|ljmp start |nop |

|;subroutine to display welcome, use ASCII ;values corresponding to |mov p0,#06h |

|alphabets |nop |

|start: lcall lcdint |clr p1.0 |

|welcome: mov a,#20h |lcall delay |

|lcall disdt1 |;set display on |

|mov a,#57h |setb P1.0 |

|lcall disdt1 |nop |

|mov a,#45h |nop |

|lcall disdt1 |mov p0,#0ch |

|mov a,#4ch |nop |

|lcall disdt1 |clr p1.0 |

|mov a,#43h |lcall delay |

|lcall disdt1 |;set cursor and display shift |

|mov a,#4fh |setb P1.0 |

|lcall disdt1 |nop |

|mov a,#4dh |nop |

|lcall disdt1 |mov p0,#01ch |

|mov a,#45h |nop |

|lcall disdt1 |clr p1.0 |

|jmp $ |lcall delay |

| |;set function set |

|//subroutine for display data on LCD |setb P1.0 |

| |nop |

|disdt1: setb p1.2 |nop |

|setb P1.0 |mov p0,#038h |

|nop |nop |

|nop |clr p1.0 |

|mov p0,a |lcall delay |

|lcall delay |ret |

|clr p1.0 | |

|clr p1.2 |//Subroutine for delay used in LCD delay |

|lcall delay1 |delay: mov r0,# 09 |

|ret |loop1: djnz r0,loop1 |

| |ret |

|;one time usable routine for LCD initialization | |

|lcdint: mov p1, #00h |//Subroutine for delay used in display |

|setb P1.0 |delay1: mov r0,#01 |

|nop |loop11: mov r1,#0100 |

|nop |loop12: mov r2,#0255 |

|mov p0,#01h |loop33: djnz r2,loop33 |

|nop |djnz r1,loop12 |

|clr p1.0 |djnz r0,loop11 |

|lcall delay |ret |

| |end |

Result: 16x2 LCD interfaced with controller is showing message as ‘WELCOME’ on its first line as per the source code.

Conclusion:- LCD is interfaced with controller and is showing messages.

Remark:- With the same procedure 16x4, 20x2, 20x4, 40x4 etc LCD can also be interfaced. Here care must be taken in selecting LCD display segment address. Through proper segment address one can display data anywhere on LCD display panel.

Bidirectional communication between LCD and controller can also be done, where one can use LCD memory space, can create own characters using CGRAM data space of LCD eg hindi character set.

| |

| |

| |

| |

| |

| |

| |

| |

References:-

1. Datasheet 89v51RD2/AT89S52

2.

-----------------------

To view register s/SFRs/memory status and content

P89V51RD2 or AT89S52

Microcontroller

Open hyper terminal

Click start

Type any name

Select communication port

89V51RD2

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download