Top Banner
Nirma University Institute Of Technology B.Tech. SEM. VI (EC) 2EC314 Microcontroller Labor atory Electronics & Communication Engineering Section Electrical Engineering Department INSTITUTE OF T ECHNOLOGY NIRMA UNIVERSITY, Ahmedabad
42

Microcontroller Lab Manual - 2013-14

Feb 09, 2018

Download

Documents

Shalin Doshi
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Microcontroller Lab Manual - 2013-14

7/22/2019 Microcontroller Lab Manual - 2013-14

http://slidepdf.com/reader/full/microcontroller-lab-manual-2013-14 1/42

Nirma UniversityInstitute Of Technology

B.Tech. SEM. VI (EC)

2EC314 Microcontroller Laboratory 

Electronics & Communication Engineering SectionElectrical Engineering Department

INSTITUTE OF TECHNOLOGYNIRMA UNIVERSITY, Ahmedabad

Page 2: Microcontroller Lab Manual - 2013-14

7/22/2019 Microcontroller Lab Manual - 2013-14

http://slidepdf.com/reader/full/microcontroller-lab-manual-2013-14 2/42

 

2EC314 –  Microcontroller Laboratory   Page 2 of 42  

Laboratory Evaluation Method

Sr.No. 

Type of Activity  No ofEvent 

Evaluation Method  Weightage 

1. Practicals 11 Continuousevaluation

0.75* 

2. Lab Exam/Viva - Performance 0.25

* In continuous evaluation (10) practical carry 70% and project carry 30%  of

the total .075 weigtage of continuous evaluation.

Text / Reference Books:

1. The 8051 Micro controller – 

 Architecture Programming & Application, byK. J. Ayala, Penram International Publications.2. 8051 Micro controller and Embedded System, by MAZIDI &MAZIDI  –  

Pearson Education Publications.

Page 3: Microcontroller Lab Manual - 2013-14

7/22/2019 Microcontroller Lab Manual - 2013-14

http://slidepdf.com/reader/full/microcontroller-lab-manual-2013-14 3/42

 

2EC314 –  Microcontroller Laboratory   Page 3 of 42  

2EC314 Microcontroller Laboratory 

Index 

Sr.No.

Title Date Sign Grade

1. Study of 8051 microcontroller and KeilIDE.

2. Write an Assembly language program fordifferent addressing modes of 8051.

3. Write an Assembly language program toperform Data Transfer and Memory

Data/code fetching Operations.

4. Write an Assembly language program to

perform data manipulation usingarithmetic Instructions. 

5. Write an Assembly language program toperform operation based on logical andcompare instructions.

6. Write an Assembly language program toperform Branching, Looping and

Subroutine Operation.

7. Write an Assembly language program to

perform operation using Timer/Counter.

8. Write an Assembly language program to

toggle LED using Input OutputProgramming

9. Write an Assembly language program tocounter and display on 7-segment LED.(Use Timer/Counter)

10. Write an Assembly language program toperform Serial Communication using

UART Module.

11 Develop a Microcontroller based project.

Page 4: Microcontroller Lab Manual - 2013-14

7/22/2019 Microcontroller Lab Manual - 2013-14

http://slidepdf.com/reader/full/microcontroller-lab-manual-2013-14 4/42

 

2EC314 –  Microcontroller Laboratory   Page 4 of 42  

Exp No: 1 Date:

Aim: Study of 8051 microcontroller and Keil IDE.

Objective·   To understand basic architecture of 8051 microcontroller. 

·   To learn operating flow of Keil IDE. 

a)  8051 Architecture

Intel introduced 8051, referred as MCS-51, in 1981.The 8051 is an 8-bitprocessor i.e. The CPU can work on only 8 bits of data at a time

n  Specifications of 8051

¨  128 bytes of RAM

¨ 4K bytes of on-chip ROM¨  Two timers

¨  One serial port

¨  Four I/O ports, each 8 bits wide¨  6 interrupt sources

¨  I/O pins 32

n   The 8051 became widely popular after allowing other manufactures tomake and market any flavor of the 8051, but remaining code-compatible.

n  8051 follows Harvard architecture.

Basic Block diagram of 8051

Page 5: Microcontroller Lab Manual - 2013-14

7/22/2019 Microcontroller Lab Manual - 2013-14

http://slidepdf.com/reader/full/microcontroller-lab-manual-2013-14 5/42

 

2EC314 –  Microcontroller Laboratory   Page 5 of 42  

Architecture of 8051

8051 architecture contains the followingo 

8 bit CPU with registers A and Bo  16 bit program counter (PC) and data

pointer (DPTR)

o  8 bit Program Status Word (PSW)

o  8 bit Stack Pointer

o  Internal ROM of 0(8031) to 4K(8051)

o  Internal RAM of 128 Bytesn  4 register banks 00-1f (RB0 to RB3)

n  16 bytes (bit addressable) 20-2f

Page 6: Microcontroller Lab Manual - 2013-14

7/22/2019 Microcontroller Lab Manual - 2013-14

http://slidepdf.com/reader/full/microcontroller-lab-manual-2013-14 6/42

 

2EC314 –  Microcontroller Laboratory   Page 6 of 42  

n  80 bytes of general purpose data memory 30-7f

o  32 I/O pins arranged as four 8 bit ports (P0  –  P3)

o   Two 16-bit timer/counters: T0 and T1

o  Full duplex serial data receiver/ transmitter: SBUF

o  Control registers: TCON, TMOD, SCON, PCON, IP and IE

o  2 external and 3 internal interrupt sourceso  Oscillator and clock circuits

Registerso  8 or 16 bit

o  Each register (except PC) has internal 1 byte address assigned to it

o  IP, IE, TCON, A, B, SCON, PSW, Port 0-4 are bit/byte addressable

o  Specified with address/symbolic name

Special Function Registers (SFRs)o   The Special Function Registers (SFRs) are used for special tasks

o   They should not be used for general purpose tasks

o  Each SFR occupies internal RAM from 0x80 to 0xFF (but someareas are empty!)

o  SFRs which are also bit addressable

o  A, B, IP, IE, TCON, SCON, PSW, P0, P1, P2, P3

o  Other SFRs

o   TMOD, THO, TLO, TH1, TL1, SBUF, PCON, SP, DPTR

o  PC is not SFR

16 bit Program Counter (PC) and Data Pointer (DPTR)

PCo  Has memory address of next byte to be fetched

o  Incremented by 1 after each instruction 

DPTRo   Two 8-bit registers DPH and DPL

o  Has memory address of internal/external code access and externaldata access

o  Under control of instructions, referred as DPTR/DPH/DPL (eachhas different address)

A and B CPU Registers

A (accumulator)o  ALU operations and external moves

B (general purpose) o  With A for multiplication and division operation

o   Temporary storage

Page 7: Microcontroller Lab Manual - 2013-14

7/22/2019 Microcontroller Lab Manual - 2013-14

http://slidepdf.com/reader/full/microcontroller-lab-manual-2013-14 7/42

 

2EC314 –  Microcontroller Laboratory   Page 7 of 42  

PSW and Flags 

Internal ROM

o  For internal program code 0000h-0FFFh

o  PC goes from 0000h-FFFFh

o  Address higher than 0FFFh 8051 automatically fetches from

external program memory (EA pin 31 to Gnd)

Page 8: Microcontroller Lab Manual - 2013-14

7/22/2019 Microcontroller Lab Manual - 2013-14

http://slidepdf.com/reader/full/microcontroller-lab-manual-2013-14 8/42

 

2EC314 –  Microcontroller Laboratory   Page 8 of 42  

Internal RAM

Port Operations

o   Total 4 portso  Port 0 may serve as inputs, outputs, or as a low order address and

data bus for external memory

o  Port 1 may be used as input/output port

o  Port 2 may be used as input/output or high order address byte

o  Port 3 may be used as an input/output and for some alternate

function.

Timers and Counters

o   Two timers, which can be controlled, set, read, and configuredindividually

o   The 8051 timers functions:

n  Count internal clk pulses-timer

n  Count external pulses-counter

n  Generating baud rates for the serial port

Page 9: Microcontroller Lab Manual - 2013-14

7/22/2019 Microcontroller Lab Manual - 2013-14

http://slidepdf.com/reader/full/microcontroller-lab-manual-2013-14 9/42

 

2EC314 –  Microcontroller Laboratory   Page 9 of 42  

Addressing Modes

o  Immediate

o  Register

o  Direct

o  Register Indirecto  Indexed 

Pin Diagram of the 8051 

Page 10: Microcontroller Lab Manual - 2013-14

7/22/2019 Microcontroller Lab Manual - 2013-14

http://slidepdf.com/reader/full/microcontroller-lab-manual-2013-14 10/42

 

2EC314 –  Microcontroller Laboratory   Page 10 of 42  

b)  Introduction of Keil IDE

 To write and run program in keil, follow the given steps.

1.  Click on µVision from the Start menu and following window will open.

2.  Select New µVision Project from the Project Menu.

Page 11: Microcontroller Lab Manual - 2013-14

7/22/2019 Microcontroller Lab Manual - 2013-14

http://slidepdf.com/reader/full/microcontroller-lab-manual-2013-14 11/42

 

2EC314 –  Microcontroller Laboratory   Page 11 of 42  

3.  Select New µVision Project  from the Project Menu. Name that newproject (here toggle has been selected as name) and save it as shown infollowing window.

4.  Once you click on save button, select device for target window will pop

up. Now select the device on which one want to write program. First onehas to select manufacturer of IC and then select particular device. Here

Atmel ‘ s part AT89V51 has been selected as shown in figure below.

Page 12: Microcontroller Lab Manual - 2013-14

7/22/2019 Microcontroller Lab Manual - 2013-14

http://slidepdf.com/reader/full/microcontroller-lab-manual-2013-14 12/42

 

2EC314 –  Microcontroller Laboratory   Page 12 of 42  

5.  After selecting particular IC, press ok, then following window will openand press No.

6.  Create Source file to write program in Keil. To do that click on File menuand Select New as shown in figure below.

After selecting new, program editor will open as shown in figure below

.

Page 13: Microcontroller Lab Manual - 2013-14

7/22/2019 Microcontroller Lab Manual - 2013-14

http://slidepdf.com/reader/full/microcontroller-lab-manual-2013-14 13/42

 

2EC314 –  Microcontroller Laboratory   Page 13 of 42  

Now write your program in program window. For example, Program totoggle port 1 and port 2 with delay has been taken here.

7.  After writing program, Click on File Menu and select Save As… by giving

.a51  extension. Here program has been saved with Toggle.a51.  This isshown figure below.

Page 14: Microcontroller Lab Manual - 2013-14

7/22/2019 Microcontroller Lab Manual - 2013-14

http://slidepdf.com/reader/full/microcontroller-lab-manual-2013-14 14/42

 

2EC314 –  Microcontroller Laboratory   Page 14 of 42  

8.  Next step is to add the toggle.a51 to the project. Expand the Target 1 in

the Tree menu, right click on Source Group 1  in the Tree menu andselect Add files to Group ‘Source Group 1’.

Change file type to Asm Source File (*.a; *.src) then Select file (toggle.a51)

and add it.

Page 15: Microcontroller Lab Manual - 2013-14

7/22/2019 Microcontroller Lab Manual - 2013-14

http://slidepdf.com/reader/full/microcontroller-lab-manual-2013-14 15/42

 

2EC314 –  Microcontroller Laboratory   Page 15 of 42  

9.  Expand the Source Group 1in the Tree menu to ensure that the file wasadded to the project. 

Creating HEX File 

1.  Click on Target 1 in Tree menu.

2.  Click on Project Menu and select Options for Target 1 as shown in figure.

Page 16: Microcontroller Lab Manual - 2013-14

7/22/2019 Microcontroller Lab Manual - 2013-14

http://slidepdf.com/reader/full/microcontroller-lab-manual-2013-14 16/42

 

2EC314 –  Microcontroller Laboratory   Page 16 of 42  

3.  Select Target Tab and Change Xtal (MHz) from 33.0 to 11.0592 as shownin figure below.

4.  Now Select Output  Tab from same window and click on create Hex filecheck box and press ok as shown in below.

Page 17: Microcontroller Lab Manual - 2013-14

7/22/2019 Microcontroller Lab Manual - 2013-14

http://slidepdf.com/reader/full/microcontroller-lab-manual-2013-14 17/42

 

2EC314 –  Microcontroller Laboratory   Page 17 of 42  

5.  Click on Project Menu and select Rebuild all Target Files.

6.  In the Build Window it should report ‘ 0 Errors (s), 0 Warnings’ .

Testing Program in Debugger

1. Click on Debug Menu and Select Start/Stop Debug Session.

Page 18: Microcontroller Lab Manual - 2013-14

7/22/2019 Microcontroller Lab Manual - 2013-14

http://slidepdf.com/reader/full/microcontroller-lab-manual-2013-14 18/42

 

2EC314 –  Microcontroller Laboratory   Page 18 of 42  

2.  If you use a free version of Keil the dialog appears. Click OK. 

3.  The Keil Debugger should be now running.

Page 19: Microcontroller Lab Manual - 2013-14

7/22/2019 Microcontroller Lab Manual - 2013-14

http://slidepdf.com/reader/full/microcontroller-lab-manual-2013-14 19/42

 

2EC314 –  Microcontroller Laboratory   Page 19 of 42  

4.  Click on Peripherals. Select I/O Ports, Select Port 1. 

5.  A new window should port will pop up. This represents the Port and Pins.

6.  Step through the code by pressing F11 on the Keyboard. The Parallel Port 1Box should change as one completely step through the code.

Page 20: Microcontroller Lab Manual - 2013-14

7/22/2019 Microcontroller Lab Manual - 2013-14

http://slidepdf.com/reader/full/microcontroller-lab-manual-2013-14 20/42

 

2EC314 –  Microcontroller Laboratory   Page 20 of 42  

7.  To exit, Click on Debug Menu and Select Start/Stop Debug Session.

Learning Outcomes:

Page 21: Microcontroller Lab Manual - 2013-14

7/22/2019 Microcontroller Lab Manual - 2013-14

http://slidepdf.com/reader/full/microcontroller-lab-manual-2013-14 21/42

 

2EC314 –  Microcontroller Laboratory   Page 21 of 42  

Exp No: 2 Date:

Aim: Write a Assembly language program for different addressing modes of

8051. 

Objective:

·   To understand different types of addressing modes in 8051.

Example

·  Write a program to copy the value 55h into RAM location from 40h to43h using different addressing mode of 8051.

Immediate

ORG 0000H

MOV 40H,#55HMOV 41H,#55H

MOV 42H,#55HMOV 43H,#55HEND 

Direct

ORG 0000H

MOV A,#55HMOV 40H,A

MOV 41H,AMOV 42H,AMOV 43H,A

END

Indexed

ORG 0000H

MOV DPTR,#40HCLR A

MOVC A,@A+DPTRMOV 40H,A

CLR AINC DPTRMOVC A,@A+DPTR

MOV 41H,ACLR A

INC DPTRMOVC A,@A+DPTR

MOV 42H,ACLR AINC DPTR

MOVC A,@A+DPTRMOV 43H,A

END 

Register

ORG 0000H

MOV R0,#55HMOV 40H,R0

MOV 41H,R0MOV 42H,R0

MOV 43H,R0END 

Register Indirect

ORG 0000H

MOV A,#55HMOV R1,#40H

MOV @R1,AINC R1

MOV @R1,AINC R1MOV @R1,A

INC R1MOV @R1,A

END 

Exercise:

1.  Write a program to clear 16 RAM locations starting at RAM address 60H.

2.  Transfer the contents of the register A, R0 and R1 respectively of bank 0 to

the register B, R0 and R1; of bank 1 using stack operation.

3.  The result of a signed arithmetic operation is stored in RAM location 27H.

Verify if the stored result is positive or negative. If it is negative send a highvalue to P1.7, otherwise send a low value.

Page 22: Microcontroller Lab Manual - 2013-14

7/22/2019 Microcontroller Lab Manual - 2013-14

http://slidepdf.com/reader/full/microcontroller-lab-manual-2013-14 22/42

 

2EC314 –  Microcontroller Laboratory   Page 22 of 42  

Exp No: 3 Date:

Aim: Write an Assembly language program to perform Data Transfer andMemory Data/code fetching Operations.

Objective

·   To learn instructions to transfer data between register and memory

and register to register.

Exercise:

1.  Write Instructions to use the registers of bank 3, and load the same

value 05H in the registers R0 toR3.

2.  Copy the data in internal RAM location 0123h to TL0 and the data inexternal RAM Location 0234h to TH0.

3.  Exchange contents of memory location 21H and 22H using PUSH andPOP.

4.  Exchange the both the lower nibbles of R0 and R1; put the low nibbleof R0 in R1 and low nibble of R1 in R0.

5.  Copy the contents of external Code memory address 0040 to 1E.

Page 23: Microcontroller Lab Manual - 2013-14

7/22/2019 Microcontroller Lab Manual - 2013-14

http://slidepdf.com/reader/full/microcontroller-lab-manual-2013-14 23/42

 

2EC314 –  Microcontroller Laboratory   Page 23 of 42  

Exp No: 4 Date:

Aim: Write an Assembly language program to perform data manipulation usingarithmetic Instructions. 

Objective:

·   To learn the instructions for arithmetic operations.

Exercise:

1.  Find the sum of the values 79H, F5H, E2H. Put the sum in registers R0

(low byte) and R5 (high byte).

2.  Add the BCD number found in internal RAM locations 25h, 26h and 27h

together and put the result in RAM locations 30h (MSB) and 31(LSB).

3.  Multiply the unsigned number in register R3 by the unsigned number on

port 2 and put the result in the external RAM locations 10h (MSB) and11h (LSB).

4.  Square the content of R5 and put the result in R0 (High Byte) and R1(lowbyte).

5.  Divide the number in RAM location 15h by the data in RAM location 16h;put the result in external RAM location 7Ch.

Page 24: Microcontroller Lab Manual - 2013-14

7/22/2019 Microcontroller Lab Manual - 2013-14

http://slidepdf.com/reader/full/microcontroller-lab-manual-2013-14 24/42

 

2EC314 –  Microcontroller Laboratory   Page 24 of 42  

Exp No: 5 Date:

Aim: Write program to perform operation based on logical and compare

Instructions.

Objective:

·   To learn instruction related logic operation.

·   To learn instructions to rotate data.

Exercise:

1.  OR the content of port 1 and port 2; put the result in external RAMlocation 0100h.

2.  Count the number of ones in any number in register B and put the countin R5.

3.  Write a program to check if the character string of length 7 stored in RAMlocation 50H onwards is a palindrome. If it is, output ‘ Y’  to P1.

4.  If lower nibble of any number in A is larger than upper nibble set theCarry flag to 1, otherwise clear it.

Page 25: Microcontroller Lab Manual - 2013-14

7/22/2019 Microcontroller Lab Manual - 2013-14

http://slidepdf.com/reader/full/microcontroller-lab-manual-2013-14 25/42

 

2EC314 –  Microcontroller Laboratory   Page 25 of 42  

Exp No: 6 Date:

Aim: Write an Assembly language program to perform operation based on logicaland compare instructions. 

Objective:

·   To learn instruction related branch and loop operation.

Exercise:

1.  Put a random number in R2 and another in R5. Increment R2 and

decrement R5 until both become equal.

2.  Random unsigned numbers are stored in register R0 to R4. Find the

largest number and put it in R6.

3.  Assume that register A has packed BCD. Write a program to convert

packed BCD to two ASCII numbers and place them in R2 and R6.

Page 26: Microcontroller Lab Manual - 2013-14

7/22/2019 Microcontroller Lab Manual - 2013-14

http://slidepdf.com/reader/full/microcontroller-lab-manual-2013-14 26/42

 

2EC314 –  Microcontroller Laboratory   Page 26 of 42  

Exp No: 7 Date:

Aim: Write an Assembly language program to perform operation using

 Timer/Counter.

Objective:

·   To understand different timer/Counter mode. 

·   To learn the function of TCON (Timer Control) Register. 

·   To be familiar with 8051 kit. 

·   To learn loading of HEX file and Test program on 8051 kit. 

Theory:8051 has two timers/counters. They can be used as either Timers to generate a

time delay or as event counter to count events happening outside themicrocontroller. Both of them are 16 bits wide. Since 8051 has an 8-bitarchitecture, each 16-bits timer is accessed as two separate registers of low bytes

and high bytes. The low byte register is called as TL0/TL1 and high byte register iscalled as TH0/TH1 as per selection of Timer0 or Timer1. Both timer 0 and 1 usethe same register, called TMOD (timer mode), to set the various time operation

mode. The construction of TMOD is given below:

Page 27: Microcontroller Lab Manual - 2013-14

7/22/2019 Microcontroller Lab Manual - 2013-14

http://slidepdf.com/reader/full/microcontroller-lab-manual-2013-14 27/42

 

2EC314 –  Microcontroller Laboratory   Page 27 of 42  

.

Example:

Create a square wave of 50% duty cycle (with equal portions high and low) on the

P1.5 bit. Timer 0 is used to generate the time delay. Analyze the program

MOV TMOD,#01 ;Timer 0, mode 1(16-bit mode)HERE: MOV TL0,#0F2H ;TL0=F2H, the low byte

MOV TH0,#0FFH ;TH0=FFH, the high byteCPL P1.5 ;toggle P1.5ACALL DELAY

SJMP HEREDELAY:

SETB TR0 ;start the timer 0

AGAIN: JNB TF0,AGAIN ;monitor timer flag 0

;until it rolls overCLR TR0 ;stop timer 0CLR TF0 ;clear timer 0 flagRET

In the above program notice the following step.1.  TMOD is loaded.

2.  FFF2H is loaded into TH0-TL0.3.  P1.5 is toggled for the high and low portions of the pulse.

Page 28: Microcontroller Lab Manual - 2013-14

7/22/2019 Microcontroller Lab Manual - 2013-14

http://slidepdf.com/reader/full/microcontroller-lab-manual-2013-14 28/42

 

2EC314 –  Microcontroller Laboratory   Page 28 of 42  

4.  The DELAY subroutine using the timer is called.5.  In the DELAY subroutine, timer 0 is started by the SETB TR0 instruction.6.  Timer 0 counts up with the passing of each clock, which is provided by the

crystal oscillator. As the timer counts up, it goes through the states of FFF3,FFF4, FFF5, FFF6, FFF7, FFF8, FFF9, FFFA, FFFB, and so on until it

reaches FFFFH. One more clock rolls it to 0, raising the timer flag (TF0=1).At that point, the JNB instruction falls through.

7.  Timer 0 is stopped by the instruction CLR TR0. The DELAY subroutine ends,and the process is repeated. Notice that to repeat the process, we mustreload the TL and TH registers, and start the process is repeated … 

How to calculate the amount of time delay in the DELAY with XTAL =11.0592MHz ?

Solution:

·  The timer works with a clock frequency of 1/12 of the XTAL frequency;therefore, we have 11.0592 MHz / 12 = 921.6 kHz as the timer frequency.

·  As a result, each clock has a period of T = 1/921.6kHz = 1.085us.

·  In other words, Timer 0 counts up each 1.085us resulting in delay = numberof counts × 1.085us.

·   The number of counts for the roll over is FFFFH  –   FFF2H = 0DH (13decimal).

·  However, we add one to 13 because of the extra clock needed when it rolls

over from FFFF to 0 and raise the TF flag.

·   This gives 14 × 1.085us = 15.19us for half the pulse. For the entire period itis T = 2 × 15.19us = 30.38us as the time delay generated by the timer.  

Page 29: Microcontroller Lab Manual - 2013-14

7/22/2019 Microcontroller Lab Manual - 2013-14

http://slidepdf.com/reader/full/microcontroller-lab-manual-2013-14 29/42

 

2EC314 –  Microcontroller Laboratory   Page 29 of 42  

Follow the given steps to load HEX file on board.

1.  Open Flash Magic from the start menu. Following window will open.

2.  Select Baud Rate 9600, then select device 89V51. 

3.  Click on Tools and select Terminal 

Page 30: Microcontroller Lab Manual - 2013-14

7/22/2019 Microcontroller Lab Manual - 2013-14

http://slidepdf.com/reader/full/microcontroller-lab-manual-2013-14 30/42

 

2EC314 –  Microcontroller Laboratory   Page 30 of 42  

3.  After selecting that, following Terminal Setting window will open.

4.  In the terminal in the Input section keep pressing ‘ U’  from the keyboard and

reset the 8051 board, the input will be displayed in the output windowthereafter. Now the board is in program mode and one can close thiswindow. It is shown in following figure.

ORDo not connect peripheral until board will be programmed.

5.  Now click on Browse in Flash Magic Utility, select hex file of relevantprogram and press start. It will dump the program on the board. Again press

reset on the board the program will run in the board.

Page 31: Microcontroller Lab Manual - 2013-14

7/22/2019 Microcontroller Lab Manual - 2013-14

http://slidepdf.com/reader/full/microcontroller-lab-manual-2013-14 31/42

 

2EC314 –  Microcontroller Laboratory   Page 31 of 42  

Example: Write a program TO TURN ON LED CONNECTED TO P2.4. 

Program:ORG 00h

SJMP MAINORG 0BhMAIN: acall ok

here: sjmp hereok: clr p1.1

clr p1.7retend

Exercise:

1.  WAP to generate a square wave of 2 kHz frequency on pin 1.4, use TIMER 1

to generate the time delay. (Draw Control word selection)

2.  Find the delay generated by timer 0 in the following code. Do not include theoverhead due to instruction.

Page 32: Microcontroller Lab Manual - 2013-14

7/22/2019 Microcontroller Lab Manual - 2013-14

http://slidepdf.com/reader/full/microcontroller-lab-manual-2013-14 32/42

 

2EC314 –  Microcontroller Laboratory   Page 32 of 42  

CLR P2.3 ;Clear P2.3MOV TMOD,#01 ;Timer 0, 16-bitmode

HERE: MOV TL0,#3EH ;TL0=3Eh, the low byte

MOV TH0,#0B8H ;TH0=B8H, the high byteSETB P2.3 ;SET high timer 0

SETB TR0 ;Start the timer 0AGAIN: JNB TF0,AGAIN ;Monitor timer flag 0

CLR TR0 ;Stop the timer 0CLR TF0 ;Clear TF0 for next round

CLR P2.3

Page 33: Microcontroller Lab Manual - 2013-14

7/22/2019 Microcontroller Lab Manual - 2013-14

http://slidepdf.com/reader/full/microcontroller-lab-manual-2013-14 33/42

 

2EC314 –  Microcontroller Laboratory   Page 33 of 42  

Exp No: 8 Date:

Aim:  Write an Assembly language program to toggle LED using Input OutputProgramming

Objective:

·  To understand concept of Interrupt Enable Register. 

·  To test the program on 8051 kit 

Theory: 

Exercise:

1.  WAP to to toggle LED connected to pin P1.5 continuously every 250 ms. Use

 Timer 0, mode 2 (8-bit auto-reload) to create the delay.

2.  To generate square wave with frequency 100 kHz using interrupt.

Page 34: Microcontroller Lab Manual - 2013-14

7/22/2019 Microcontroller Lab Manual - 2013-14

http://slidepdf.com/reader/full/microcontroller-lab-manual-2013-14 34/42

 

2EC314 –  Microcontroller Laboratory   Page 34 of 42  

Exp No: 9 Date:

Aim:  Write an Assembly language program to to counter and display on 7-

segment LED. (Use Timer/Counter)

Objective:

·  To interface seven segments led with 8051 kit. 

Example: 

·  Program to interface one no of 7-segment led. 

Org 00

sjmp MAIN

org 0B

MAIN: mov p1,#0x00mov p2,#0xff

mov p2,#0xfe

mov p1,#0xbf

acall delay

mov p1,#0x00

mov p2,#0xfd

mov p1,#0x86

acall delay

mov p1,#0x00

mov p2,#0xfb

mov p1,#0xdbacall delay

mov p1,#0x00

mov p2,#0xf7

mov p1,#0xcf

acall delay

sjmp MAIN

delay: nop

nop

nop

nop

nop

ret

END

Exercise:

1.  WAP to interface 4-7 segment LEDS to 8051 & displa7 0, 1, 2, & 3 ONrespective 7 segment LEDS.

Page 35: Microcontroller Lab Manual - 2013-14

7/22/2019 Microcontroller Lab Manual - 2013-14

http://slidepdf.com/reader/full/microcontroller-lab-manual-2013-14 35/42

 

2EC314 –  Microcontroller Laboratory   Page 35 of 42  

Exp No: 10 Date:

Aim: Write an Assembly language program to perform Serial Communication using

UART Module. 

Objective:

·   To understand functionality of Serial Control Register. 

Theory:Serial transmission is used to transfer to a device located many meters away. Thedata is sent one bit at a time. At the transmitting end, the byte of data must beconverted to serial bits using parallel-in-serial-out shift register. At the receiving

end, there is a serial-in - parallel-out shift register to receive the serial data andpack them into byte. When the distance is short, the digital signal can be

transferred as it is on a simple wire and requires no modulation If data is to betransferred on the telephone line, it must be converted from 0s and 1s to audiotones This conversion is performed by a device called a modem,

“Modulator/demodulator”. Serial data communication uses two methods:

·  Synchronous method transfers a block of data at a time

o  Block-oriented data transfers use the synchronous method. The startbit is always one bit, but the stop bit can be one or two bits

·  Asynchronous method transfers a single byte at a time

o  widely used for character-oriented transmissions. Each character is

placed in between start and stop bits, this is called framing.

Page 36: Microcontroller Lab Manual - 2013-14

7/22/2019 Microcontroller Lab Manual - 2013-14

http://slidepdf.com/reader/full/microcontroller-lab-manual-2013-14 36/42

 

2EC314 –  Microcontroller Laboratory   Page 36 of 42  

 There are special IC chips made by many manufacturers for serialcommunications.

·  UART (universal asynchronous Receiver-transmitter)

·  USART (universal synchronous-asynchronous Receiver-transmitter)

One need to configure Serial Control (SCON) register for serial communicationusing 8051. SCON register detail is given below.

Exercise:

1. WAP to transfer the message

“YES

” serially at 2400 buadrate, 8-bit data,1 stop bit. Do this continuously. (Draw Control word selection)

.

Page 37: Microcontroller Lab Manual - 2013-14

7/22/2019 Microcontroller Lab Manual - 2013-14

http://slidepdf.com/reader/full/microcontroller-lab-manual-2013-14 37/42

 

2EC314 –  Microcontroller Laboratory   Page 37 of 42  

Exp No: 11 Date:

Aim: Develop a microcontroller based project. 

Instructions:

1.  You must design project based on either PIC Controller or AVR Controller.

(Material related both controllers available on course website).

2.  Project should be carried out in the group of two students. (preferable from

same batch)

3.  Project title should be selected after consulting your lab Mentor. Sampleproject title is available on course website.

4.  project title must be entered in spread sheet by end of 3rd  week of Jan-2014.

5.  Final Submission Deadline: 1st Week of April, 2014.

6.  Submission includes:

·  Hardware module of project.

·  Project Report

Page 38: Microcontroller Lab Manual - 2013-14

7/22/2019 Microcontroller Lab Manual - 2013-14

http://slidepdf.com/reader/full/microcontroller-lab-manual-2013-14 38/42

 

2EC314 –  Microcontroller Laboratory   Page 38 of 42  

Appendix

8051 Instruction Set SummaryMnemonic Description Bytes Cycles

Arithmetic operationsADD A,Rn Add registertoaccumulator 1 1

ADD A,direct Add direct byte toaccumulator 2 1

ADD A,@Ri Add indirect RAMtoaccumulator 1 1

ADD A,#data Add immediate datatoaccumulator 2 1

ADDC A,direct Add direct byte toAwithcarryflag 2 1

ADDC A,@Ri Add indirect RAMtoA with carryflag 1 1

ADDC A,#data Add immediate datatoA with carryflag 2 1

SUBB A,Rn Subtractregisterto accumulator with borrow 1 1

SUBB A,direct Subtractdirectbyte to A with carry borrow 2 1

SUBB A,@Ri Subtract indirect RAM toAwithcarryborrow 1 1

SUBB A,#data Subtract immediate data toA with

carryborrow

2 1

INC A Increment accumulator 1 1

INCRn Increment register 1 1

INCdirect Increment direct byte 2 1

INC @Ri Incrementindirect RAM 1 1

DEC A Decrement accumulator 1 1

DEC Rn Decrement register 1 1

DECdirect Decrement direct byte 2 1

DEC @Ri Decrement indirect RAM 1 1

INC DPTR Increment data pointer 1 2

MUL AB MultiplyA and B->[B hi]:[A lo] 1 4

DIV AB Divide A byB ->A=result, B=remainder 1 4

DAA Decimal adjust accumulator 1 1

CLR A Clear accumulator 1 1

CPLA Complement accumulator 1 1

RL A Rotateaccumulatorleft 1 1

RLC A Rotateaccumulatorleftthrough carry 1 1

Page 39: Microcontroller Lab Manual - 2013-14

7/22/2019 Microcontroller Lab Manual - 2013-14

http://slidepdf.com/reader/full/microcontroller-lab-manual-2013-14 39/42

 

2EC314 –  Microcontroller Laboratory   Page 39 of 42  

RR A Rotateaccumulatorright 1 1

RRC A Rotateaccumulatorright through carry 1 1

SWAP A Swap nibbles withinthe accumulator 1 1

Logic operationsANL A,Rn AND registertoaccumulator 1 1

ANL A,direct ANDdirect byte toaccumulator 2 1

ANL A,@Ri AND indirect RAM toaccumulator 1 1

ANL A,#data ANDimmediate datatoaccumulator 2 1

ANL direct,A ANDaccumulatorto direct byte 2 1

ANL direct,#data ANDimmediate datatodirect byte 3 2

ORL A,Rn OR registerto accumulator 1 1ORL A,direct OR direct byte toaccumulator 2 1

ORL A,@Ri OR indirect RAM toaccumulator 1 1

ORL A,#data ORimmediate data to accumulator 2 1

ORL direct,A OR accumulatortodirect byte 2 1

ORL direct,#data ORimmediate data to direct byte 3 2

XRL A,Rn Exclusive OR registerto accumulator 1 1

XRL A,Rn Exclusive OR registerto accumulator 1 1

XRL A,Rn Exclusive OR registerto accumulator 1 1

XRL A,@Ri ExclusiveOR indirect RAM toaccumulator 1 1

XRL A,#data Exclusive ORimmediate data to accumulator 2 1

XRL direct,A Exclusive ORaccumulatortodirect byte 2 1

XRL direct,#data Exclusive OR immediate data to direct byte 3 2

Boolean Variable Manipulation

CLR C Clear carry flag 1 1

CLR bit Clear direct bit 2 1

SETB C Set carry flag 1 1

SETBbit Set direct bit 2 1

CPL C Complement carry flag 1 1

CPL bit Complement direct bit 2 1

Page 40: Microcontroller Lab Manual - 2013-14

7/22/2019 Microcontroller Lab Manual - 2013-14

http://slidepdf.com/reader/full/microcontroller-lab-manual-2013-14 40/42

 

2EC314 –  Microcontroller Laboratory   Page 40 of 42  

ANL C,bit AND direct bit to carry flag 2 2

ANL C,/bit AND complement of direct bit to carry 2 2

ORL C,bit OR direct bit to carry flag 2 2

ORL C,/bit OR complement of direct bit to carry 2 2MOV C,bit Move direct bit to carry flag 2 1

MOV bit,C Move carry flag to direct bit 2 2

Program and machine control

ACALL addr11 Absolutesubroutine call 2 2

LCALL addr16 Longsubroutine call 3 2

RET Return from subroutine 1 2

RETI Return from interrupt 1 2AJMPaddr11 Absolute jump 2 2

LJMPaddr16 Long jump 3 2

SJMP rel Jump(relative address) 2 2

 JMP @A+DPTR Jumpindirectrelativetothe DPTR 1 2

 JZ rel Jump if accumulator is zero 2 2

 JNZ rel Jumpifaccumulator is not zero 2 2

 JC rel Jump if carry flag is set 2 2

 JNC rel Jump if carry flag is not set 2 2

 JB bit,rel Jump if bit is set 3 2

 JNB bit,rel Jumpifbitis not set 3 2

 JBC bit,rel Jumpifdirectbit isset and clear bit 3 2

CJNE A,direct,rel Compare directbyte toA and jumpif notequal

3 2

CJNE A,#data,rel Compare immediate toA and jumpif not

equal

3 2

CJNE Rn,#data,rel Compare immed. toreg. and jump if notequal

3 2

CJNE @Rn,#data,rel

Compare immed. to ind. and jumpif notequal

3 2

DJNZ Rn,rel Decrement register and jump in not zero 2 2

DJNZ direct,rel Decrement direct byte and jumpin not zero 3 2

Page 41: Microcontroller Lab Manual - 2013-14

7/22/2019 Microcontroller Lab Manual - 2013-14

http://slidepdf.com/reader/full/microcontroller-lab-manual-2013-14 41/42

 

2EC314 –  Microcontroller Laboratory   Page 41 of 42  

Data transfer 

MOV A,Rn Moveregistertoaccumulator 1 1

MOV A,direct*) Movedirect byte to accumulator 2 1

MOV A,@Ri MoveindirectRAM to accumulator 1 1MOV A,#data Moveimmediatedata toaccumulator 2 1

MOV Rn,A Moveaccumulator toregister 1 1

MOV Rn,direct Movedirect byte to register 2 2

MOV Rn,#data Moveimmediatedata toregister 2 1

MOV direct,A Moveaccumulator todirectbyte 2 1

MOV direct,Rn Move register to direct byte 2 2

MOV direct,direct Movedirect byte to direct byte 3 2MOV direct,@Ri MoveindirectRAM to directbyte 2 2

MOV direct,#data Moveimmediatedata todirect byte 3 2

MOV @Ri,A Moveaccumulator toindirectRAM 1 1

MOV @Ri,direct MovedirectbytetoindirectRAM 2 2

MOV @Ri,#data Moveimmediatedata toindirect RAM 2 1

MOVDPTR,#data16

Loaddata pointer with a 16-bitconstant 3 2

MOVC A,@A+DPTR Movecode byte relative to DPTR toaccumulator

1 2

MOVC A,@A+PC Movecode byte relative to PC to accumulator 1 2

MOVX A,@Ri Moveexternal RAM(8-bitaddr.) to A 1 2

MOVX A,@DPTR Moveexternal RAM (16-bit addr.) to A 1 2

MOVX @Ri,A MoveAto external RAM (8-bitaddr.) 1 2

MOVX @DPTR,A MoveA to external RAM (16-bit addr.) 1 2

PUSH direct Push direct byte onto stack 2 2

POP direct Pop direct byte from stack 2 2

XCH A,Rn Exchange register to accumulator 1 1

XCH A,direct Exchange direct byte to accumulator 2 1

XCH A,@Ri Exchange indirect RAM to accumulator 1 1

XCHD A,@Ri Exchange low-order nibble in dir. RAM withA

1 1

Page 42: Microcontroller Lab Manual - 2013-14

7/22/2019 Microcontroller Lab Manual - 2013-14

http://slidepdf.com/reader/full/microcontroller-lab-manual-2013-14 42/42

 

Label Mnemonic / Operand Comments