mclabfair 2013.pp

126
08.607 Microcontroller Lab Manual SREE BUDDHA COLLEGE OF ENGINEERING, PATTOOR DEPARTMENT OF ELECTRONICS & COMMUNICATION ENGINEERING 08.607 MICROCONTROLLER LAB (TA) LAB MANUAL Issue II, Version III Prepared by: Pavitha P P Pooja S. Mohan Dept. of ECE, Sree Buddha College of Engineering 1

Upload: ar-un

Post on 29-May-2017

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: MClabFair 2013.Pp

08.607 Microcontroller Lab Manual

SREE BUDDHA COLLEGE OF ENGINEERING, PATTOOR

DEPARTMENT OF ELECTRONICS & COMMUNICATION ENGINEERING

08.607 MICROCONTROLLER LAB (TA)LAB MANUAL

Issue II, Version III

Prepared by:

Pavitha P P Pooja S. Mohan

Dept. of ECE, Sree Buddha College of Engineering 1

Page 2: MClabFair 2013.Pp

08.607 Microcontroller Lab Manual

INSTRUCTIONS TO THE STUDENTS

1 General:

All laws and rules of the SBCE apply to the use of this lab. In particular, uniform and

attendance are compulsory. It is the responsibility of any user of the Institutional facilities to

know and abide by all the Institutional regulations.

2 Lab Reports:

Lab reports are required of individual students, and are due one week after the corresponding

experiment has been completed. Reports should be neat and clearly organized, and should

include original data sheets.

3 Care of Equipments:

Please be very careful with the lab equipments. If you suspect something is not operating

correctly, report it to the lab technician. If at any time you are uncertain about lab safety,

please ask the lab-in-charge before proceeding. Make sure that all equipment you used has

been turned off and returned to the place you obtained it from.

Dept. of ECE, Sree Buddha College of Engineering 2

Page 3: MClabFair 2013.Pp

08.607 Microcontroller Lab Manual

KNOW THE LABORATORY PRINCIPLES

Laboratory Experiments

Laboratory experiments are expected to develop intellectual skills, motor skills and learning attitudes in students.

Logical Thinking

Logical thinking is developed in students through system approach, content analysis and sequential planning of laboratory work.

Safety

Instruments having electric circuits can be dangerous; hence safety practices are necessary to be followed to prevent electrical shock, fires, mechanical damage and injuries resulting from improper use of tools. Perhaps the greatest hazard is electrical shock. A current through human body in excess of 10 milli ampere can paralyze the victim. Connecting and disconnecting I/O systems should be done with the power switched off.

Procedure in dealing with electricity and electronic equipment

Always follow the procedure given for conduct of experiment. Use service manuals as often as possible. They often contain specific safety

information. Think before you act. When in doubt, don’t act; ask your instructor or teacher.

Skills to be developed

Can be able to identify logic behind each program. Able to prepare the complete program Enter and execute the program.

Dept. of ECE, Sree Buddha College of Engineering 3

Page 4: MClabFair 2013.Pp

08.607 Microcontroller Lab Manual

08.607 MICROCONTROLLER LAB (TA)Syllabus

L-T- : 0-0-4 Credits: 4

A. Programming experiments using 8051 Trainer Kit.

1. Addition and Subtraction of 16 bit numbers. 2. Multiplication and division of 8 bit numbers. 3. Sorting, Factorial of a number. 4. Multiplication by shift and add method. 5. LCM and HCF of two 8 bit numbers 6. Matrix addition 7. Square, Square root, Fibonacci series.

B. Interfacing experiments

1. DAC interface. 2. Stepper motor interface. 3. Display interface. 4. Realization of Boolean expression using port. 5. Frequency measurement by counting the number of pulses in a fixed amount of time. 6. Frequency measurement by measuring the time period between two consecutive pulses. 7. Waveform generation using lookup tables. 8. PWM generation. 9. Interfacing with 8-bit ADC.

Note: For University examination, the following guidelines should be followed regarding award of marks: (Questions for each batch should be selected equally from part A and B)

(a) Circuit and design - 20% (b) Implementation(Usage of Kits and trouble shooting) - 15% (c) Result - 35% (d) Viva voce - 25% (e) Record - 05%

Practical examination to be conducted covering entire syllabus given above. Students shall be allowed for the University examination only on submitting the duly certified record. The external examiner shall endorse the record.

CONTENTS

Dept. of ECE, Sree Buddha College of Engineering 4

Page 5: MClabFair 2013.Pp

08.607 Microcontroller Lab Manual

Page No.

1. Familiarisation of 8051 microcontroller

2. Familiarisation of 8051 microcontroller trainer kit

3. Experiment Set

A. Programming experiments using 8051 Trainer Kit.

1. Addition and Subtraction of 16 bit numbers.

2. Multiplication and division of 8 bit numbers.

3. Sorting, Factorial of a number.

4. Multiplication by shift and add method.

5. LCM and HCF of two 8 bit numbers

6. Matrix addition

7. Square, Square root

8. Fibonacci series generation.

B. Interfacing experiments

1. DAC interface.

2. Stepper motor interface.

3. Interfacing with 8-bit ADC.

4. Display Interfacing.

5. Waveform generation using lookup tables.

6. PWM generation

7. Realization of Boolean expression using port.

8. Frequency measurement by counting the number of pulses in a fixed amount of

time.

9. Frequency measurement by measuring the time period between two consecutive

        pulses.

10. LED blinking using Embedded c programming

11. ADC interfacing using embedded c programming

12. Stepper motor interfacing using Embedded C programming

4. Viva-voce questions

5. Microcontroller lab question bank

Dept. of ECE, Sree Buddha College of Engineering 5

Page 6: MClabFair 2013.Pp

08.607 Microcontroller Lab Manual

1. FAMILIARISATION OF 8051 MICROCONTROLLER

Aim To familiarise with 8051 microcontroller architectural features.

8051 Microcontroller

A Microcontroller is a programmable digital processor with necessary peripherals. Both microcontrollers and microprocessors are complex sequential digital circuits meant to carry out job according to the program / instructions. Sometimes analog input/output interface makes a part of microcontroller circuit as mixed mode (both analog and digital) in nature. Applications of microcontrollers are numerous. Starting from domestic applications such as in washing machines, TVs, air conditioners, microcontrollers are used in automobiles, process control industries, cell phones, electrical drives, and robotics and in space applications.

Salient features

Eight bit CPU with registers A (Accumulator) and B Sixteen bit Program counter (PC) and a data pointer (DPTR) 8 Bit Program Status Word (PSW) 8 Bit Stack Pointer 4K Code Memory Internal Memory of 128 Bytes 32 I/O Pins arranged as 4 , 8 Bit ports Two 16 Bit Timer/Counter :T0, T1 Full Duplex serial data receiver/transmitter Control Registers : TCON,TMOD,SCON,PCON,IP and IE Two External and Internal Interrupt sources

Dept. of ECE, Sree Buddha College of Engineering 6

Page 7: MClabFair 2013.Pp

08.607 Microcontroller Lab Manual

Fig.1. 8051 functional block diagram

Memory and Register Organisation

The 8051 has a separate memory space for code (programs) and data. It refers here to on-chip memory and external memory as shown in figure below (Fig2). In an actual implementation the external memory may, in fact, be contained within the microcomputer chip. However, we will use the definitions of internal and external memory to be consistent with 8051 instructions which operate on memory. Note, the separation of the code and data memory in the 8051 architecture is a little unusual. The separated memory architecture is referred to as Harvard architecture whereas Von Neumann architecture defines a system where code and data can share common memory.

Dept. of ECE, Sree Buddha College of Engineering 7

Page 8: MClabFair 2013.Pp

08.607 Microcontroller Lab Manual

Fig.2. 8051 memory representation

External Code Memory The executable program code is stored in this code memory. The code memory size is limited to 64KBytes (in a standard 8051). The code memory is read-only in normal operation and is programmed under special conditions e.g. it is a PROM or a Flash RAM type of memory.

External RAM Data Memory This is read-write memory and is available for storage of data. Up to 64KBytes of external RAM data memory is supported (in a standard 8051).

Internal Memory The 8051’s on-chip memory consists of 256 memory bytes organized as follows: First 128 bytes: 00h to 1Fh Register Banks 20h to 2Fh Bit Addressable RAM 30 to 7Fh General Purpose RAM Next 128 bytes: 80h to FFh Special Function RegistersThe first 128 bytes of internal memory is organized as shown in figure below (Fig4), and isreferred to as Internal RAM, or IRAM.

SFR Description

P0 (Port 0, Address 80h, Bit-Addressable): This is input/output port 0. Each bit of this SFR corresponds to one of the pins on the microcontroller. For example, bit 0 of port 0 is pin P0.0, bit 7 is pin P0.7. Writing a value of 1 to a bit of this SFR will send a high level on the corresponding I/O pin whereas a value of 0 will bring it to a low level.

SP (Stack Pointer, Address 81h): This is the stack pointer of the microcontroller. This SFR indicates where the next value to be taken from the stack will be read from in Internal RAM. If you push a value onto the stack, the value will be written to the address of SP + 1. That is to say, if SP holds the value 07h, a PUSH instruction will push the value onto the stack at address 08h. This SFR is modified by all instructions which modify the stack, such as PUSH, POP, LCALL, RET, RETI, and whenever interrupts are provoked by the microcontroller.

DPL/DPH (Data Pointer Low/High, Addresses 82h/83h): The SFRs DPL and DPH work together to represent a 16-bit value called the Data Pointer. The data pointer is used in operations regarding external RAM and some instructions involving code memory. Since it is an unsigned two-byte integer value, it can represent values from 0000h to FFFFh (0 through 65,535 decimal).

Dept. of ECE, Sree Buddha College of Engineering 8

Page 9: MClabFair 2013.Pp

08.607 Microcontroller Lab Manual

Fig.3. Organization of internal RAM (IRAM) memory

PCON (Power Control, Addresses 87h): The Power Control SFR is used to control the 8051's power control modes. Certain operation modes of the 8051 allow the 8051 to go into a type of "sleep" mode which requires much less power. These modes of operation are controlled through PCON. Additionally, one of the bits in PCON is used to double the effective baud rate of the 8051's serial port.

TCON (Timer Control, Addresses 88h, Bit-Addressable): The Timer Control SFR is used to configure and modify the way in which the 8051's two timers operate. This SFR controls whether each of the two timers is running or stopped and contains a flag to indicate that each timer has overflowed. Additionally, some non-timer related bits are located in the TCON SFR. These bits are used to configure the way in which the external interrupts are activated and also contain the external interrupt flags which are set when an external interrupt has occurred.

TMOD (Timer Mode, Addresses 89h): The Timer Mode SFR is used to configure the mode of operation of each of the two timers. Using this SFR your program may configure each timer to be a 16-bit timer, an 8-bit auto reload timer, a 13-bit timer, or two separate timers. Additionally, you may configure the timers to only count when an external pin is activated or to count "events" that are indicated on an external pin.

Dept. of ECE, Sree Buddha College of Engineering 9

Page 10: MClabFair 2013.Pp

08.607 Microcontroller Lab Manual

Fig.4. Memory map of Special Function Registers

TL0/TH0 (Timer 0 Low/High, Addresses 8Ah/8Bh): These two SFRs, taken together, represent timer 0. Their exact behavior depends

on how the timer is configured in the TMOD SFR; however, these timers always count up. What is configurable is how and when they increment in value.

TL1/TH1 (Timer 1 Low/High, Addresses 8Ch/8Dh): These two SFRs, taken together, represent timer 1. Their exact behavior depends

on how the timer is configured in the TMOD SFR; however, these timers always count up. What is configurable is how and when they increment in value.

P1 (Port 1, Address 90h, Bit-Addressable): This is input/output port 1. Each bit of this SFR corresponds to one of the pins on

the microcontroller. For example, bit 0 of port 1 is pin P1.0, bit 7 is pin P1.7. Writing a value of 1 to a bit of this SFR will send a high level on the corresponding I/O pin whereas a value of 0 will bring it to a low level.

SCON (Serial Control, Addresses 98h, Bit-Addressable): The Serial Control SFR is used to configure the behavior of the 8051's on-board

serial port. This SFR controls the baud rate of the serial port, whether the serial port is activated to receive data, and also contains flags that are set when a byte is successfully sent or received.

SBUF (Serial Control, Addresses 99h): The Serial Buffer SFR is used to send and receive data via the on-board serial

port. Any value written to SBUF will be sent out the serial port's TXD pin. Likewise, any value which the 8051 receives via the serial port's RXD pin will be delivered to the user program

Dept. of ECE, Sree Buddha College of Engineering 10

Page 11: MClabFair 2013.Pp

08.607 Microcontroller Lab Manual

via SBUF. In other words, SBUF serves as the output port when written to and as an input port when read from.

P2 (Port 2, Address A0h, Bit -Addressable): This is input/output port 2. Each bit of this SFR corresponds to one of the pins on

the microcontroller. For example, bit 0 of port 2 is pin P2.0, bit 7 is pin P2.7. Writing a value of 1 to a bit of this SFR will send a high level on the corresponding I/O pin whereas a value of 0 will bring it to a low level.

IE (Interrupt Enable, Addresses A8h): The Interrupt Enable SFR is used to enable and disable specific interrupts. The

low 7 bits of the SFR are used to enable/disable the specific interrupts, where as the highest bit is used to enable or disable ALL interrupts. Thus, if the high bit of IE is 0 all interrupts are disabled regardless of whether an individual interrupt is enabled by setting a lower bit.

P3 (Port 3, Address B0h, Bit-Addressable): This is input/output port 3. Each bit of this SFR corresponds to one of the pins on

the microcontroller. For example, bit 0 of port 3 is pin P3.0, bit 7 is pin P3.7. Writing a value of 1 to a bit of this SFR will send a high level on the corresponding I/O pin whereas a value of 0 will bring it to a low level.

IP (Interrupt Priority, Addresses B8h, Bit-Addressable): The Interrupt Priority SFR is used to specify the relative priority of each interrupt.

On the 8051, an interrupt may either be of low (0) priority or high (1) priority. An interrupt may only interrupt interrupts of lower priority. For example, if we configure the 8051 so that all interrupts are of low priority except the serial interrupt, the serial interrupt will always be able to interrupt the system, even if another interrupt is currently executing. However, if a serial interrupt is executing no other interrupt will be able to interrupt the serial interrupt routine since the serial interrupt routine has the highest priority.

PSW (Program Status Word, Addresses D0h, Bit-Addressable): The Program Status Word is used to store a number of important bits that are set and cleared by 8051 instructions. The PSW SFR contains the carry flag, the auxiliary carry flag, the overflow flag, and the parity flag. Additionally, the PSW register contains the register bank select flags which are used to select which of the "R" register banks are currently selected.

ACC (Accumulator, Addresses E0h, Bit-Addressable): The Accumulator is one of the most used SFRs on the 8051 since it is involved in

so many instructions. The Accumulator resides as an SFR at E0h, which means the instruction MOV A, #20h is really the same as MOV E0h, #20h. However, it is a good idea to use the first method since it only requires two bytes whereas the second option requires three bytes.

B (B Register, Addresses F0h, Bit-Addressable): The "B" register is used in two instructions: the multiply and divide operations.

The B register is also commonly used by programmers as an auxiliary register to temporarily store values.

Dept. of ECE, Sree Buddha College of Engineering 11

Page 12: MClabFair 2013.Pp

08.607 Microcontroller Lab Manual

Fig.5. Pin diagram of 8051

8051 Clock and Instruction cycle

The heart of 8051 is the circuitry that generates the clock pulses by which all internal operations are synchronized. Pins XTAL1 and XTAL2 are provided for connecting resonator to form an oscillator. The crystal frequency is the basic internal frequency of the microcontroller. 8051 is designed to operate between 1MHz to 16MHz and generally operates with a crystal frequency 11.04962 MHz

The oscillator formed by the crystal, capacitor and an on-chip inverter generates a pulse train at the frequency of the crystal. The clock frequency f establishes the smallest interval to accomplish any simple instruction. The time taken to complete any instruction is called as machine cycle or instruction cycle. In 8051 one instruction cycle consists of 6 states or 12 clock cycles, instruction cycle is also referred as Machine cycle.

Fig.6. Instruction cycle of 8051(Instruction cycle has six states (S 1 - S 6). Each state has two pulses (P1 and P2))

Dept. of ECE, Sree Buddha College of Engineering 12

Page 13: MClabFair 2013.Pp

08.607 Microcontroller Lab Manual

Addressing modes

Immediate Addressing MOV A,#20h

Direct Addressing MOV A,30h

Indirect Addressing MOV A,@R0

External Direct MOVX A,@DPTR

Code Indirect MOVC A,@A+DPTR

Immediate Addressing: Immediate addressing is so-named because the value to be stored in memory

immediately follows the operation code in memory. That is to say, the instruction itself dictates what value will be stored in memory. Immediate addressing is very fast since the value to be loaded is included in the instruction. However, since the value to be loaded is fixed at compile-time it is not very flexible.

Direct Addressing Direct addressing is so-named because the value to be stored in memory is

obtained by directly retrieving it from another memory location. Direct addressing is generally fast since, although the value to be loaded isn’t included in the instruction, it is quickly accessible since it is stored in the 8051’s Internal RAM. It is also much more flexible than Immediate Addressing since the value to be loaded is whatever is found at the given address--which may be variable. Also, it is important to note that when using direct addressing any instruction which refers to an address between 00h and 7Fh is referring to Internal Memory. Any instruction which refers to an address between 80h and FFh is referring to the SFR control registers that control the 8051 microcontroller itself.

Indirect Addressing Indirect addressing is a very powerful addressing mode which in many cases

provides an exceptional level of flexibility. Indirect addressing always refers to Internal RAM; it never refers to an SFR.

External Direct External Memory is accessed using a suite of instructions which use what we call

"External Direct" addressing. It appears to be direct addressing, but it is used to access external memory rather than internal memory.

External Indirect External memory can also be accessed using a form of indirect addressing which is

called as External Indirect addressing.

Dept. of ECE, Sree Buddha College of Engineering 13

Page 14: MClabFair 2013.Pp

08.607 Microcontroller Lab Manual

2. FAMILIARISATION OF 8051 MICROCONTROLLER TRAINER KIT

Aim To familiarise with 8051 microcontroller trainer kit.

InstallationThe power supply is provided with a LED as an indicator for the availability of the power.

The trainer is issued a “power ON Reset” and the message is displayed in the display. If the message displayed is not in the prescribed format, then press”RES” key and check for the message.

Precautions

To utilise the trainer kit to advantage, confirm to certain basic norms, and adhere to certain precautionary measures listed below as don’ts DON’TS

Don’t insert any add-on card while the trainer is power ON. Don’t tamper with any of the components in the trainer. Don’t solder any wire from connectors when the power is up. Wires are to be soldered only from the solder side of the board The 26 pin headers should be used only with cables do not with wires soldered

from the pins. Do not attempt to service the trainer in the case of problems.

General information The hardware and software facilities available on Micro-51 LC are discussed below. The capabilities of the trainer with regard to memory, peripherals, and key functions are all mentioned here.

Functional Block Diagram

Dept. of ECE, Sree Buddha College of Engineering 14

Page 15: MClabFair 2013.Pp

08.607 Microcontroller Lab Manual

Hardware specifications:i. Processor, clock frequency

Intel 8051/89C51 at 12MHz (89C51 max up to 33MHz)

ii. MemorySystem EPROM : 0000-3FFFH & C000-FFFFHSystem RAM : 4000-BFFFHAdditional RAM : 0000-3FFFH & C000-FEFFHMonitor Buffer : 4000-40FFHUser Program/Data RAM area : 4100-BFFFHUser Data RAM area : 0000-3FFFH & C000-FEFFHMemory mapped I/O : FF00-FF1FH, FFC0-FFFFHMemory mapped I/O expansion : FF20-FFBFH

iii. Input/outputParallel : 24 I/O lines using one numbers of 8255Serial : 1 number of RS232 Serial interface using 8051Serial port

Timer : two 16 bit timers namely Timer 0 and Timer 1

Printer : One printer interface through 8255-I port

Interrupt : 5 interrupt sources. Among them two are external interrupts called INT0 and INT1

iv. LCD Interface

16x2 LCD display module

v. IBM PC Keyboard interface

vi. Power supply Specifications

Model : SMPSMain : 230Volts AC at 50 HzOutputs : i. +5V, 3Amps Regulated

ii. +12V, 150mA Regulatediii. -12V, 150mA Regulatediv. +30V, 500mA Regulated

vii. Physical CharacteristicsMicro-51 LC PCB : 6.8”mm x 6.8”mmWeight : 1/2 Kg

Dept. of ECE, Sree Buddha College of Engineering 15

Page 16: MClabFair 2013.Pp

08.607 Microcontroller Lab Manual

viii. Bus Expansion A VTX_Bus has been incorporated in Micro-51 LC which facilitates to patch up

any extra hardware to Micro-51 LC. An unlimited number of add-on boards could be added this way to interface the hardware available on Micro-51 LC. All buffered address, data and control signals are brought out this bus. Using VTX_Bus, VBMB cards can be directly interfaced with Micro-51 LC.

The power supply used is meant only for the trainer and the add-on boards used along with the trainer. This power supply must not be used for any external applications.

Software specifications:

The Micro-51 LC can accept any command related to the following short listed function once in the command prompt mode, indicated by“#” in the leftmost position of the second row in the LCD module. The functions that can be performed using the commands are as follows:

Display and Substitute memory locations Display and modify registers of the CPU Enter and initiate execution of user programs Debug the user program through the trace facility provided by the monitor Fill a block of memory to RAM or within RAM Move a block of memory to RAM or within RAM Compare two blocks of memory Insert bytes to RAM memory Delete bytes from RAM memory Input a byte from an input port Output a byte to an output port Transmit or receive a block of data through the serial port Print a block of memory Display a block of memory contents View a byte in the internal RAM locations(00 to FFH)

Micro-51 LC contains a high performance 32K bytes monitor program. It is designed respond to user input, RS232C serial communications, printer interface through 8255-I port etc. The following is a simple description of the commands in Micro-51 LC.

1. Substitute memory command#SP<Addr><CR> -for program memory#SP<Addr><CR> -for data memory

2. Register View/Modify#R<CR>

3. a)GO command GO<Addr><CR>

b) GO with break point

Dept. of ECE, Sree Buddha College of Engineering 16

Page 17: MClabFair 2013.Pp

08.607 Microcontroller Lab Manual

GO<Start Addr><End Addr><CR>

4. Trace command#TR<Addr><CR>

5. Fill command#FP<Start Addr><End Addr><Data><CR> -for program memory #FD<Start Addr><End Addr><Data><CR> -for data memory

6. Block move command#MP<Start Addr><End Addr><Dest Addr><CR> -for program memory #MD<Start Addr><End Addr><Dest Addr><CR> -for data memory

7. Block compare command#CP<Start Addr><End Addr><Dest Addr><CR> -for program memory

#CD<Start Addr><End Addr><Dest Addr><CR> -for data memory

8. Insert command#IP<Insert Addr><Program End Addr><No. of bytes><CR> #ID<Insert Addr><Data Mem End Addr><No. of bytes ><CR>

9. Delete command#EP<Start Addr><End Addr> <Program End Addr><CR> #ED<Start Addr><End Addr><Data Mem End Addr><CR>

10. Input command#IN <Port Addr><CR>

11. Output command#O<PortAddr><Data><CR>

12. Serial Input command#PI<Start Addr><CR> -for program memory#DI<Start Addr><CR> -for data memory.

13. Serial Output command

#PO<Start Addr><End Addr><CR> -for program memory #DO<Start Addr><End Addr><CR> -for data memory

14. Print command#PP<Start Addr><End Addr><CR> -for program memory

#PD<Start Addr><End Addr><CR> -for data memory

15. Dump memory contents#DP<Start Addr><End Addr><CR> -for program memory

#DD<Start Addr><End Addr><CR> -for data memory

16. Internal command

Dept. of ECE, Sree Buddha College of Engineering 17

Page 18: MClabFair 2013.Pp

08.607 Microcontroller Lab Manual

#IR< Addr><CR>

17. Assemble#A<CR>

18. Unassemble#U<CR>

19. EPROM programmer#E<CR> (To program the EPROMs like 2716,2732 etc)

20. Serial mode#SM<CR> (To access the kit via serial port)

GENERAL PROCEDURE TO CODE IN TO THE KIT

The procedure to enter the user program into trainer kit and execute it is discussed below. Execution of a program can be done either using the unconditional GO and EXEC command or using the STEP command.

Writing a programWrite an appropriate program in the form of mnemonic codes. This program has to be

entered into the kit in the form of hex codes. These are called object codes which can be fount by referring to the instruction set given in the Micro-51 LC User Manual.

Entering the program and executing itThe program can be executed directly placing the opcodes in the memory and entering a

GO command. The procedure is as follows:1. Enter substitute byte command2. Enter the opcodes from 4100 location3. After entering the program, come back to command prompt4. Now issue the GO command

The program will now be executedTracing the program

Enter the trace command, it can be seen that instruction at location 4100 is being executed and the next instruction is displayed. By pressing carriage return, this instruction can be executed. Thus trace each step in the program by single stepping and register view.

Assembler list out

An assembler list out can be written in 8051 mnemonics. The list out should include line number, memory location address, Opcode and operands, label, mnemonics and operand. And the program should involve a lot of assembler directives like the ORG, EQU, END and many more. It is a must that one should use these directives while assembling programs in PC.

Dept. of ECE, Sree Buddha College of Engineering 18

Page 19: MClabFair 2013.Pp

08.607 Microcontroller Lab Manual

3. EXPERIMENT SET

CYCLE-1

1. Addition and subtraction of 16 bit numbers.

2. Multiplication and division of 8 bit numbers.

3. Sorting a set of numbers.

4. Factorial of a number.

5. LCM and HCF of two 8 bit numbers.

6. Multiplication by shift and add method.

CYCLE-2

7. Matrix addition.

8. Square and square root of a number.

9. Fibonacci series generation.

10. DAC Interfacing.

11. Stepper motor interfacing.

12. Interfacing with 8-bit ADC.

CYCLE-3

13. Display Interfacing.

14. Waveform generation using lookup tables.

15. PWM generation

16. Realization of Boolean expression using port.

17. Frequency measurement by counting the number of pulses in a fixed amount of time.

18. Frequency measurement by measuring the time period between two consecutive

pulses.

19. LED blinking using Embedded c programming

20. ADC interfacing using embedded c programming

21. Stepper motor interfacing using Embedded C programming

Dept. of ECE, Sree Buddha College of Engineering 19

Page 20: MClabFair 2013.Pp

08.607 Microcontroller Lab Manual

PART A

PROGRAMMING EXPERIMENTS USING 8051 TRAINER KIT

Dept. of ECE, Sree Buddha College of Engineering 20

Page 21: MClabFair 2013.Pp

08.607 Microcontroller Lab Manual

FLOW CHART: Addition of two 16 bit numbers

Dept. of ECE, Sree Buddha College of Engineering 21

Page 22: MClabFair 2013.Pp

08.607 Microcontroller Lab Manual

Exp No : 1

Date :

ADDITION AND SUBTRACTION OF 16 BIT NUMBERS

Aim To find the sum and difference of two 16 bit numbers and store the result in a memory location.Apparatus Required:

No Name Quantity1 Microcontroller kit 1

Algorithm:

Addition of two 16 bit numbers

1. Start the program.2. Move the first data lower order byte to A register.

3. Add the second data lower order byte with A register.

4. Move the lower order byte result data from A reg to 8300 Memory location.

5. Move the first data higher order byte to A register.

6. Add A reg. and Second higher order byte with carry flag.

7. Move the higher order byte result data from A reg to 8301 Memory location.

8. Check the carry flag; if it is one move 01 data to 8302 else move 00 data to 8302.

9. Stop the program.

Subtraction of two 16 bit numbers

1. Start the program.2. Move the first data lower order byte to A register.

3. Subtract the second data lower order byte from A register.

4. Move the lower order byte result data from A reg to 8300 Memory location.

5. Move the first data higher order byte to A register.

6. Subtract A reg. and Second higher order byte with carry flag.

7. Move the higher order byte result data from A reg to 8301 Memory location.

8. Stop the program.

Dept. of ECE, Sree Buddha College of Engineering 22

Page 23: MClabFair 2013.Pp

08.607 Microcontroller Lab Manual

FLOW CHART: Subtraction of two 16 bit numbers

Dept. of ECE, Sree Buddha College of Engineering 23

START

Move the Lower order byte of Data1 to Acc. Reg

Subtract Acc. Reg with Lower order byte of Data2

Move the Lower order byte result from Acc. Reg to the Memory location 8300h

Move the Higher order byte of Data1 to Acc. Reg

Subtract Acc. Reg and Higher order byte of Data2 with carry

Move the Higher order byte result from Acc. Regto the Memory location 8301h

Stop

Page 24: MClabFair 2013.Pp

08.607 Microcontroller Lab Manual

Program: Addition of two 16 bit numbers

Memory Hex. Label Assembly Codes Comments address code                                                                                   

     

Output:

Memory DataAddress  

Program: Subtraction of two 16 bit numbers

Memory Hex. Label Assembly Codes Comments address code                                                        

     

Dept. of ECE, Sree Buddha College of Engineering 24

Page 25: MClabFair 2013.Pp

08.607 Microcontroller Lab Manual

Output:

Memory DataAddress  

Result:Thus assembly language programs were written to add and subtract two 16 bit numbers and executed successfully.

Reference: The 8051 Microcontroller and Embedded Systems - Muhammad Ali Mazidi, Janice Gillispie Mazidi.

FLOW CHART: Two 8 Bit Multiplication

Dept. of ECE, Sree Buddha College of Engineering 25

Page 26: MClabFair 2013.Pp

08.607 Microcontroller Lab Manual

Exp No : 2

Dept. of ECE, Sree Buddha College of Engineering 26

Start

Move the Data1 fromMem.Location 4200 to B reg

Multiply Acc. Reg with B Reg.

Move the Result from Acc. Reg. to Memory Location 4501h

Stop

Move the Result from B. Regto Memory Location 4500h

Move the Data2 frommem.Location 4201 to Acc Acc. reg

reg

Page 27: MClabFair 2013.Pp

08.607 Microcontroller Lab Manual

Date :

MULTIPLICATION AND DIVISION OF 8 BIT NUMBERS

Aim To perform 8-bit Multiplication and Division of two 8-bit data using indirect addressing and store the result in memory.

Apparatus Required:

No Name Quantity1 Microcontroller kit 1

Algorithm:

Two 8 Bit Multiplication

1. Move the multiplicand to accumulator.

2. Move the multiplier to ‘B’ register (SFR with direct address FO)

3. Multiply the contents of Accumulator and ‘B’ register.

4. Store the lower byte result from ‘A’ register to 4501 memory location

5. Store the higher byte result from ‘B’ register to 4500 memory location

6. Stop or Halt the program execution.

Division of two 8 bit numbers

1. Move the dividend to accumulator.

2. Move the divisor to ‘B’ register (SFR with direct address FO)

3. Divide the contents of Accumulator and ‘B’ register.

4. Store the lower byte result from ‘A’ register to 4501 memory location

5. Store the higher byte result from ‘B’ register to 4500 memory location

6. Stop or Halt the program execution.

FLOW CHART: Division of two 8 bit numbers

Dept. of ECE, Sree Buddha College of Engineering 27

Page 28: MClabFair 2013.Pp

08.607 Microcontroller Lab Manual

Program: Two 8 Bit Multiplication

Dept. of ECE, Sree Buddha College of Engineering 28

Start

Move the Data1 fromMem.Location 4200 to B reg

Divide Acc. Reg with B Reg.

Move the Result from Acc. Reg. to Memory Location 4500h

Stop

Move the Result from B. Regto Memory Location 4501h

Move the Data2 frommem.Location 4201 to Acc. reg

reg

Page 29: MClabFair 2013.Pp

08.607 Microcontroller Lab Manual

Memory Hex. Label Assembly Codes Comments address code                                                                                          

     

Input:

Memory Data

Output:

Memory DataAddress  

Program: Division of two 8 bit numbers

Dept. of ECE, Sree Buddha College of Engineering 29

Page 30: MClabFair 2013.Pp

08.607 Microcontroller Lab Manual

Memory Hex. Label Assembly Codes Comments address code                                                                                               

Input:

Memory Data

Output:

Memory DataAddress  

Result: Thus assembly language programs were written to multiply and divide two 8 bit numbers and executed successfully.

Reference: The 8051 Microcontroller and Embedded Systems - Muhammad Ali Mazidi, Janice Gillispie Mazidi.

FLOW CHART: Sorting a set of numbers

Dept. of ECE, Sree Buddha College of Engineering 30

Start

Page 31: MClabFair 2013.Pp

08.607 Microcontroller Lab Manual

NO

YES

NO

NO

Exp No : 3

Date :

Dept. of ECE, Sree Buddha College of Engineering 31

Read the length of array, Count

R3=R4=Count-1

Load data pointer with initial location address of array

R5R6= DPTR

Read the element pointed by DPTR from array and store in B

Is A> B?

Exchange A and B

Increment pointer and decrement R3

Is R3=0?

DPTR= R5R6

Read the next element from Array and store in A

Increment pointer and decrement R4

Is R4=0?

Stop

Page 32: MClabFair 2013.Pp

08.607 Microcontroller Lab Manual

SORTING A SET OF NUMBERS

Aim To write and execute an assembly language program to arrange an 8 bit array ofdatas in ascending order.

Apparatus Required:

No Name Quantity1 Microcontroller kit 1

Algorithm:

Sorting a set of numbers

1: Start

2: Read the length of the array, Count.

3: R3=R4=Count -1

4: Read the element in array pointed by data pointer and store in B.

5: Read the next element from array and store in A

6: Is A>B

If no, exchange the values of A and B.

7: Increment the pointer and decrement R3.

8: Is R3=0

If no, go to step 5

9: Load the data pointer with the initial value (first location of array).

10: Increment the pointer and decrement R4.

11: Is R4=0

If no, go to step 4.

12: Stop

Program: Sorting a set of numbers

Memory Hex. Label Assembly Codes Comments address code      

Dept. of ECE, Sree Buddha College of Engineering 32

Page 33: MClabFair 2013.Pp

08.607 Microcontroller Lab Manual

           

                                                                                                                                                                

Input:

Memory Data

Dept. of ECE, Sree Buddha College of Engineering 33

Page 34: MClabFair 2013.Pp

08.607 Microcontroller Lab Manual

4500 Data 14501 Data 2

4502 Data 34503 Data 44504 Data 5

Output:

Memory Data4500 Data 14501 Data 24502 Data 34503 Data 44504 Data 5

Result: Thus an assembly language program was written to arrange 8 bit array of datas inascending order and it was executed successfully.

Reference: The 8051 Microcontroller and Embedded Systems - Muhammad Ali Mazidi, Janice Gillispie Mazidi.

FLOW CHART: Factorial of a number

Dept. of ECE, Sree Buddha College of Engineering 34

Page 35: MClabFair 2013.Pp

08.607 Microcontroller Lab Manual

NO

YES

Exp No : 4

Date :

FACTORIAL OF A NUMBER

Dept. of ECE, Sree Buddha College of Engineering 35

Start

Read the number N

R0=R4=N and B=1

A=R0

P=A X B

A=B and R5=B=P

Decrement R0 and R4

Is R4=0?

Write R5 into output memory location

Stop

Page 36: MClabFair 2013.Pp

08.607 Microcontroller Lab Manual

Aim To write and execute an assembly language program to find the factorial of a given number.Apparatus Required:

No Name Quantity1 Microcontroller kit 1

Algorithm:

Factorial of a number

1: Start

2: Read the number N from external memory location.

3: R0=R4=N and B=1

4: A=R0.

5: Multiply A and B.

6: Save the product into R5.Also reload the B with the product.

7: Decrement R0

8: Decrement R4

9: Check whether R4=0

If no, go to step 4

If yes, write the value of R5 to output memory location.

10: Stop

P rogram : Factorial of a number

          Memory Hex. Label Assembly Codes Comments address code             

Dept. of ECE, Sree Buddha College of Engineering 36

Page 37: MClabFair 2013.Pp

08.607 Microcontroller Lab Manual

                                                                                          

Input:

Memory Data4200  

Output:

Memory Data4500  

Result: Thus an assembly language program was written to find the factorial of a given number and it was executed successfully.

Reference: The 8051 Microcontroller and Embedded Systems - Muhammad Ali Mazidi, Janice Gillispie Mazidi.

FLOW CHART: LCM of two 8 bit numbers

Dept. of ECE, Sree Buddha College of Engineering 37

Start

Enter two 8 bit numbers and save them in R2 and R3.

Page 38: MClabFair 2013.Pp

08.607 Microcontroller Lab Manual

NO

YES

Exp No : 5

Date :

LCM AND HCF OF TWO 8 BIT NUMBERS

Dept. of ECE, Sree Buddha College of Engineering 38

Stop

Write the values of R7 and R6 to output memory location. location.

Initialize register R1=01h.

Load Accumulator with value in R2 and load register B with value in R1.

Multiply A and B.

Load registers R6 and R7 with values in A and B respectively.

Load register B with value in R3.

Divide A and B.

Load A with value in B.

Is remainder=0

?

increment R1

Page 39: MClabFair 2013.Pp

08.607 Microcontroller Lab Manual

Aim To write and execute an assembly language programs to find the highest common factor (HCF) and least common multiplier (LCM) of two 8 bit numbers.

Apparatus Required:

No Name Quantity1 Microcontroller kit 1

Algorithm:

LCM of two 8 bit numbers.

1: Start 2: Initialize register R1=01h. 3: Enter two 8 bit numbers and save them in R2 and R3. 4: Load Accumulator with value in R2 and load register B with value in R1. 5: Multiply A and B. 6: Load registers R6 and R7 with values in A and B respectively. 7: Load register B with value in R3. 8: Divide A and B. 9: Load A with value in B. 10: Check whether the remainder of division is zero

If no, increment R1 and go to step 4 If yes, write the values of R7 and R6 to output memory location. 11: Stop

HCF of two 8 bit numbers.

1: Start 2: Enter two 8 bit numbers and save them in R0 and R1. 3: Load Accumulator with value in R0 and load register B with value in R1. 4: Is A>B,

If no, exchange the values of A and B and load the values in A and B to registers R0 and R1 respectively.

5: Load registers B and A with values in R1 and R0 respectively.6: Divide A and B.7: Load A with value in B.8: Check whether the remainder of division is zero

If no, load R0 with value in R1, R1 with value in register B and go to step 5 If yes, write the values of R1 to output memory location. 11: Stop

FLOW CHART: HCF of two 8 bit numbers

Dept. of ECE, Sree Buddha College of Engineering 39

Start

Enter two 8 bit numbers and save them in R0 and R1.

Page 40: MClabFair 2013.Pp

08.607 Microcontroller Lab Manual

NO

YES

NO

YES

Program: LCM of two 8 bit numbers

Memory Hex. Label Assembly Codes Comments address code           

Dept. of ECE, Sree Buddha College of Engineering 40

Stop

Write the values of R1 to output memory location. location.

Load Accumulator with value in R0 and load register B with value in R1.

Exchange the values of A and B and load the values in A and B

Load registers B and A with values in R1 and R0 respectively.

Divide A and B.

Load A with value in B.

Is remainder=0

?

Load R0 with value in R1 and R1 with value in register B

Is A>B?

Page 41: MClabFair 2013.Pp

08.607 Microcontroller Lab Manual

                                                                                                                        

Input:

Memory Data4200 Data14201 Data 2

Output:

Memory Data4500 Higher order byte4501 Lower order byte

P rogram : HCF of two 8 bit numbers

Memory Hex. Label Assembly Codes Comments address code           

Dept. of ECE, Sree Buddha College of Engineering 41

Page 42: MClabFair 2013.Pp

08.607 Microcontroller Lab Manual

                                                                                                                                       

Input:

Memory Data4200 Data14201 Data 2

Output:

Memory Data4500  

Result: Thus an assembly language programs were written to find the LCM and HCF of a given number and it was executed successfully.

Reference: The 8051 Microcontroller and Embedded Systems - Muhammad Ali Mazidi, Janice Gillispie Mazidi.

FLOW CHART: Multiplication by Shift and Add method

Dept. of ECE, Sree Buddha College of Engineering 42

Start

R0←Data1, R1←Data 2R2←0, R3←8 (Count)

Page 43: MClabFair 2013.Pp

08.607 Microcontroller Lab Manual

NO

YES

NO

YES

Exp No : 6

Date :

Dept. of ECE, Sree Buddha College of Engineering 43

A←R1, A←A (AND ) 01HR4←A

R4 =1

A←R2, A←A+R0+carryR2←A

A←R0

Shift A left, R0←A

A←R1, Shift A right, R1←A

Count ← Count-1

Is Count =0 ?

Store the value of R2 to output memory location

Stop

Page 44: MClabFair 2013.Pp

08.607 Microcontroller Lab Manual

MULTIPLICATION BY SHIFT AND ADD METHOD

Aim To write and execute an assembly language program to find the product of two 8-bit numbers using repeat addition method.Apparatus Required:

No Name Quantity1 Microcontroller kit 1

Algorithm:

Multiplication by Shift and Add method

1: Start

2: Enter two 8 bit numbers and save them in R0 and R1.

3: Initialize 2 registers R2= 0for output storage and R3=8 (count)

4: Load accumulator with R1 and AND Accumulator with 01H

5: Load R4 with accumulator

6: Check whether R4=1

If NO go to step 8

If YES Acc=R2

7: Add Acc and R0, with carry and load R2 with Accumulator.

8: Load accumulator with value in R0

9: Rotate Accumulator to left and load R0 with Accumulator

10: Reload accumulator with value in R1

11: Rotate Accumulator to right and load Accumulator to R1

12: Decrement Count and check whether it is equal to zero

If non-zero, go to step 4

13: Output the value in output registers

14: Stop

Program: Multiplication by Shift and Add method

Memory Hex. Label Assembly Codes Comments address code                

Dept. of ECE, Sree Buddha College of Engineering 44

Page 45: MClabFair 2013.Pp

08.607 Microcontroller Lab Manual

                                                                                                                             

Input:

Memory Data4200 Data14201 Data 2

Output:Memory Data

4500  

Result: Thus an assembly language program was written to find the product of two 8 bit numbers and it was executed successfully

Reference: The 8051 Microcontroller and Embedded Systems - Muhammad Ali Mazidi, Janice Gillispie Mazidi.

FLOW CHART: Matrix addition

Dept. of ECE, Sree Buddha College of Engineering 45

Start

Add them

Save the matrices in external memory

Page 46: MClabFair 2013.Pp

08.607 Microcontroller Lab Manual

Exp No : 7

Date :

MATRIX ADDITION

Dept. of ECE, Sree Buddha College of Engineering 46

Read the element of first matrix from its memory location using data pointer.

Store the sum into corresponding memory location using data pointer.

Stop

Add them

Read the element of second matrix

from its memory location using data

Pointer.

Page 47: MClabFair 2013.Pp

08.607 Microcontroller Lab Manual

Aim To write and execute an assembly language program to add two 2*2 matrices and save the output to specified memory location.

Apparatus Required:

No Name Quantity1 Microcontroller kit 1

Algorithm:

Matrix addition

1: Start

2: Save the matrices in external memory

3: Read the element of first matrix from its memory location using data pointer.

4: Read the element of second matrix from its memory location using data

Pointer.

5: Add the two elements

6: Store the sum into corresponding memory location using data pointer.

7: Stop

P rogram : Matrix addition

Memory Hex. Label Assembly Codes Comments address code                

Dept. of ECE, Sree Buddha College of Engineering 47

Page 48: MClabFair 2013.Pp

08.607 Microcontroller Lab Manual

                                                                                                                                                                

Input:

Memory Data Memory Data4200 Data1 4300 Data14201 Data 2 4301 Data 24202 Data3 4302 Data34203 Data 4 4303 Data 4

Dept. of ECE, Sree Buddha College of Engineering 48

Page 49: MClabFair 2013.Pp

08.607 Microcontroller Lab Manual

Output:Memory Data

4500 Data14501 Data 24502 Data34503 Data 4

Result: Thus an assembly language program was written to find the sum of two 2*2 matrices and it was executed successfully

Reference: The 8051 Microcontroller and Embedded Systems - Muhammad Ali Mazidi, Janice Gillispie Mazidi.

FLOW CHART: Square of a number.

Dept. of ECE, Sree Buddha College of Engineering 49

Start

Read the number N

Multiply N with itself to obtain the square of N

Output the Square of NStop

Page 50: MClabFair 2013.Pp

08.607 Microcontroller Lab Manual

FLOW CHART: Square root of a number.

NO

YE

YES

Exp No : 8

Date :

SQUARE AND SQUARE ROOT OF A NUMBER

Dept. of ECE, Sree Buddha College of Engineering 50

Read the number N

i= 1

Q= N/i

D= Q- i

Is D=0

?

i= i+1

Output square root= Q

Stop

Start

Page 51: MClabFair 2013.Pp

08.607 Microcontroller Lab Manual

Aim To write and execute an assembly language programs to find the square and squareroot of a given number and save the output to specified memory location.

Apparatus Required:

No Name Quantity1 Microcontroller kit 1

Algorithm:

Square of a number

1: Start

2: Read the number

3: Multiply the number with itself

4: Display the product found, which is the square of the number.

5: Stop

Square root of a number.

1: Start

2: Read the number, N

3: Initialize a variable i=1

4: Divide the number N by i

5: Save the quotient and remainder of division

6: Subtract the variable i from the quotient obtained

7: Check whether this difference is equal to zero

If no increment the value of ‘i’ and go to step 4

If yes, output the value of ’Q’ as the square root

8: Stop

Program: Square of a number

Memory Hex. Label Assembly Codes Comments address code                  

Dept. of ECE, Sree Buddha College of Engineering 51

Page 52: MClabFair 2013.Pp

08.607 Microcontroller Lab Manual

                                        

Input:Output:

Memory Data Memory Data

4200   4500 upper byte

4501 Lower byteProgram: Square root of a perfect square number

Memory Hex. Label Assembly Codes Comments address code                                                                                             

Input:Output:

Memory Data Memory Data4200 Data 4500  

Result: Thus an assembly language programs were written to find the square and square root of a given number and it was executed successfully

Dept. of ECE, Sree Buddha College of Engineering 52

Page 53: MClabFair 2013.Pp

08.607 Microcontroller Lab Manual

Reference: The 8051 Microcontroller and Embedded Systems - Muhammad Ali Mazidi, Janice Gillispie Mazidi.

FLOW CHART: Fibonacci series generation

Dept. of ECE, Sree Buddha College of Engineering 53

Start

Page 54: MClabFair 2013.Pp

08.607 Microcontroller Lab Manual

NO

YES

Exp No : 9

Dept. of ECE, Sree Buddha College of Engineering 54

Read the number of Fibonacci numbers to be generated, Count

Fibonacci numbers

Put A=1, B=0

Write the value of A into memory location pointed by the data pointer

S= A+B

A=B and B=S

Increment data pointer

Decrement Count

Is Count=0

?

Stop

Page 55: MClabFair 2013.Pp

08.607 Microcontroller Lab Manual

Date :

FIBONACCI SERIES GENERATIONAim To write and execute an assembly language program to generate afibonacci series of a number and save the output to specified memory location.

Apparatus Required:

No Name Quantity1 Microcontroller kit 1

Algorithm:

Fibonacci series generation

1: Start

2: Read the number of Fibonacci numbers to be generated, Count

3: Put A=0, B=1.

4: Write the value of A into memory location pointed by the data pointer

5: Add A and B

6: Save the sum

7: Load with the value in B and B with the value of sum.

8: Increment data pointer

9: Decrement count

10: Check whether the Count is equal to zero

If no, go to step 4.

11: Stop

Program: Fibonacci series generation.

Dept. of ECE, Sree Buddha College of Engineering 55

Page 56: MClabFair 2013.Pp

08.607 Microcontroller Lab Manual

Memory Hex. Label Assembly Codes Comments

address code                                                                                        

Input:Output:

Memory Data Memory Data

4200   4201 

Result: Thus an assembly language program was written to generate the fibonacci series of a given number and it was executed successfully

Reference: The 8051 Microcontroller and Embedded Systems - Muhammad Ali Mazidi, Janice Gillispie Mazidi.

Dept. of ECE, Sree Buddha College of Engineering 56

Page 57: MClabFair 2013.Pp

08.607 Microcontroller Lab Manual

PART B

INTERFACING EXPERIMENTS

Dept. of ECE, Sree Buddha College of Engineering 57

Page 58: MClabFair 2013.Pp

08.607 Microcontroller Lab Manual

Circuit diagram-DAC Interfacing

Dept. of ECE, Sree Buddha College of Engineering 58

Page 59: MClabFair 2013.Pp

08.607 Microcontroller Lab Manual

DAC INTERFACING

Aim: To interface a DAC to the 8051 and to write and execute assembly language programs

to generate a square, triangular and saw tooth waveforms at DAC 1 output

Apparatus Required:

No Name Quantity1 Microcontroller kit 12 DAC interface board 1

Algorithm: Square wave

1. Start

2. Load data pointer with the port address for DAC 1.

3. Load the accumulator with the value 00h and then load it to the external

Memory location.

4. Call delay.

5. Load the accumulator with the value FFh and then load it to the external memory

location.

6. Call delay. Go to step 2.

7. Stop

Algorithm: Sawtooth wave

1. Start

2. Load data pointer with the port address for DAC 1.

3. Load the accumulator with the value 00h and then load it to the external

Memory location.

4. Increment the value stored in Acc. and go to step 3.

5. Stop

Algorithm: triangular wave

1. Start

2. Load data pointer with the port address for DAC 1.

3. Load the accumulator with the value 00h and then load it to the external

Memory location.

4. Increment the value stored in Acc. and go to step 3 till Acc≠00h.

5. Load the accumulator with the value FFh and then load it to the external memory

Dept. of ECE, Sree Buddha College of Engineering 59

Exp No : 10

Date :

Page 60: MClabFair 2013.Pp

08.607 Microcontroller Lab Manual

location.

6. Decrement the value in Acc. and go to step 5 till Acc.≠00h

7. Go to step 3

8. Stop

Program: DAC interfacing.

Square wave generation

Memory Hex. Label Assembly Codes Comments address code            ORG 4100        MOV DPTR, #FFC0      START: MOV A, #00        MOVX @DPTR, A        LCALL DELAY        MOV A, #FF        MOVX @DPTR, A        LCALL DELAY        LJMP START      DELAY: MOV R1, #05      LOOP: MOV R2, #FF      HERE: DJNZ R2, HERE        DJNZ R1, LOOP        RET        SJMP START  

Sawtooth wave form generation

Memory Hex. Label Assembly Codes Comments address code            ORG 4100        MOV DPTR, #FFC0      MOV A, #00       LOOP: MOVX @DPTR, A        INC A        SJMP LOOP                                                         

Dept. of ECE, Sree Buddha College of Engineering 60

Page 61: MClabFair 2013.Pp

08.607 Microcontroller Lab Manual

Triangular wave form generation

Memory Hex. Label Assembly Codes Comments address code            ORG 4100        MOV DPTR, #FFC0      START: MOV A, #00       LOOP1: MOVX @DPTR, A  

   

INC AJNZ LOOP1MOV A,#FF  

     LOOP2: MOVX @DPTR,A        DEC A        JNZ LOOP2        LJMP START                                    

Procedure:

1. Enter the above opcodes from 4100.2. Execute the program.3. Check different waveforms using CRO.

Result: Thus assembly language programs were written to generate square ,triangular and saw tooth waveforms at DAC 1 output and it was executed successfully

References: The 8051 Microcontroller and Embedded Systems - Muhammad Ali Mazidi, Janice Gillispie Mazidi.

Dept. of ECE, Sree Buddha College of Engineering 61

Page 62: MClabFair 2013.Pp

08.607 Microcontroller Lab Manual

Dept. of ECE, Sree Buddha College of Engineering 62

Page 63: MClabFair 2013.Pp

08.607 Microcontroller Lab Manual

Exp No : 11

Date :

STEPPER MOTOR INTERFACING

Aim:

i) To interface a stepper motor to 8051 and to write and execute an assembly language

program to run stepper motor at different speed.

ii) To rotate stepper motor in forward and reverse direction.

Apparatus Required:

No Name Quantity1 Microcontroller kit 12 Stepper motor Interface card 1

Algorithm:

1: Start

2: Load accumulator with the value corresponding to 4-step sequence (C8H).

3: Load a count variable with a value equal to number of step sequences required

4: Rotate the accumulator content to right

5: Move the accumulator content to port

6: Call delay

7: Decrement the count variable

8: Check the count variable, If not zero, go to step 4

9. Call a delay of 1 second

10: Reload count variable

11: Load accumulator with C8H

12: Rotate the accumulator to left

13: Move accumulator content to port

14: Call delay

15: Decrement count variable

16: Check the count variable, if not zero, go to step 11

17: Continue indefinitely from the beginning

18: End

Dept. of ECE, Sree Buddha College of Engineering 63

Page 64: MClabFair 2013.Pp

08.607 Microcontroller Lab Manual

Program: To run a stepper motor at different speed.

Memory Hex. Label Assembly Codes Comments address code            ORG 4100H      START: MOV DPTR, #LOOKUP        MOV R0, #04      JO: MOVX A,@DPTR        PUSH DPH        PUSH DPL        MOV DPTR, #FFC0H        MOV R2,#04H        MOV R1,#0FH      DLY1: MOV R3, #0FH      DLY: DJNZ R3, DLY        DJNZ R1, DLY1        DJNZ R2, DLY1        MOVX @DPTR,A        POP DPL        POP DPH        INC DPTR        DJNZ R0, JO        SJMP START        END      LOOKUP: DB 09H, 05H, 06H, 0AH ; Forward rotation

Dept. of ECE, Sree Buddha College of Engineering 64

Page 65: MClabFair 2013.Pp

08.607 Microcontroller Lab Manual

Program: To run the stepper motor in both forward and reverse direction

Memory Hex. Label Assembly Codes Comments address code            ORG 4100H      START: MOV R4,#C8H  

   L2: MOV

DPTR,#FORWARD      LCALL L1      DJNZ R4,L2      LCALL DELAY      MOV R4,#C8H      L3: MOV DPTR,#REVERSE      LCALL L1      DJNZ R4,L3      LCALL DELAY      SJMP START      L1: MOV R0,#04H      LOOP: MOVX A,@DPTR      PUSH DPH      PUSH DPL      MOV DPTR,#FFC0H      MOV R2,#04H      L7: MOV R1, #05H      L6: MOV R3, #FFH      L4: DJNZ R3, L4      DJNZ R1, L6      DJNZ R2, L7      MOVX @DPTR, A      POP DPL      POP DPH      INC DPTR      DJNZ R0, LOOP      RET      DELAY: MOV R5, #01H      L9: MOV R2, #05H      L8: DJNZ R2, L8      DJNZ R5, L9      RET      FORWARD: DB 09H, 05H, 06H, 0AH      REVERSE: DB 0AH, 06H, 05H, 09H  

Dept. of ECE, Sree Buddha College of Engineering 65

Page 66: MClabFair 2013.Pp

08.607 Microcontroller Lab Manual

Procedure:

1. Enter the above opcodes from 4100.2. Execute the program.3. Connect Motor in P4 Connector. Motor is rotate in a given direction at given angle.

Result: Thus an assembly language programs were written to rotate stepper motor in

different direction at different angle and at different speed and it was executed successfully

References: The 8051 Microcontroller and Embedded Systems - Muhammad Ali Mazidi, Janice Gillispie Mazidi.

Dept. of ECE, Sree Buddha College of Engineering 66

Page 67: MClabFair 2013.Pp

08.607 Microcontroller Lab Manual

Circuit diagram: Interfacing with 8 bit ADC

Exp No : 12

Dept. of ECE, Sree Buddha College of Engineering 67

Page 68: MClabFair 2013.Pp

08.607 Microcontroller Lab Manual

Date :

INTERFACING WITH 8-BIT ADCAim: To interface an ADC 0809 to the 8051 and to write and execute an assembly

language program to convert the analog input at channel 0 to a digital value. Apparatus Required:No Name Quantity1 Microcontroller kit 12 ADC Interface card 1

Theory:

Connect the ADC0809 to the 8051 and write a program to see the corresponding digital value in the LED display. Program selects channel 0.Start the analog to digital conversion process by pressing SOC switch. ADC 0809 converts the analog input at channel 0 to a digital value and 74LS374 latachet the data to glow the LEDs accordingly.

Algorithm:

1: Start2: Assign 7 bits of a port to ALE, OE, SC, EOC signals and to analog

channel selection signals, namely A, B, C.3: Select an analog channel.4: Activate the ALE pin (address latch enable; L-to-H pulse).5: Activate SC (start of conversion; L-to-H pulse)6: Monitor EOC (end of conversion; if conversion over and data is ready to

be picked up, H- to-L output)7: Activate OE (output enable) to read data out of the ADC chip.8: Convert the HEX data to ASCII and pass the characters to display on to

LED9: Stop

Program: Interfacing with 8-bit ADC

Memory Hex. Label Assembly Codes Comments address code            ORG 4100  

    MOV DPTR, #FFC8 ;CHANNEL 0 SELECTION

    MOV A,#10 ;AND ALE LOW      MOVX @DPTR,A      MOV A,#18 ; ALE HIGH    MOVX @DPTR,A  

Dept. of ECE, Sree Buddha College of Engineering 68

Page 69: MClabFair 2013.Pp

08.607 Microcontroller Lab Manual

    HLT: SJMP HLT  Procedure:

1.Place jumper J2 in B position.

2. Place jumper J5 in A position.

3. Enter the above opcodes from 4100.

4. Execute the program.

5. Vary the analog input(using trimpot) and give the SOC by pressing the SOC switch.

6. See the corresponding digital value in the LED display.

Result: Thus an assembly language programs was written to convert the analog input at channel 0 to a digital value and it was executed successfully.

References: The 8051 Microcontroller and Embedded Systems - Muhammad Ali Mazidi, Janice Gillispie Mazidi.

Dept. of ECE, Sree Buddha College of Engineering 69

Page 70: MClabFair 2013.Pp

08.607 Microcontroller Lab Manual

Circuit diagram- LCD display

Dept. of ECE, Sree Buddha College of Engineering 70

Page 71: MClabFair 2013.Pp

08.607 Microcontroller Lab Manual

Exp No : 13

Date :

DISPLAY INTERFACINGAim: To interface a LCD to the 8051 and to write and execute an assembly language

program to display some text on LCD.Apparatus Required:

No Name Quantity1 Microcontroller kit 12 LCD Interface card 1

Algorithm:

1: Start2: Initialize the LCD 2 lines, 5 X 7 matrix3: Start the command mode4: Pass command for display on and cursor on to the LCD5: Pass command to clear LCD6: Pass command to shift cursor right7: Enter in display mode for LCD8: Pass the characters to display on to LCD9: Stop

Dept. of ECE, Sree Buddha College of Engineering 71

Page 72: MClabFair 2013.Pp

08.607 Microcontroller Lab Manual

Program: To display text ' VI MICROSYSTEMS' on LCD.

Memory Hex. Label Assembly Codes Comments address code            ORG 4100H      LATCH EQU 0FF08H  

   

DEN EQU 0FF04HDENL EQU 04HLATCHL EQU O8HIOHIGH EQU 0FFH  

    START: LCALL BUSYCHECK      MOV DPTR,#DEN      MOV A,#38H ;TO FUNCTION SET    MOVX @DPTR,A      LCALL BUSYCHECK      MOV A,#01H ; CLEAR DISPLAY    MOVX @DPTR,A      LCALL BUSYCHECK      MOV A,#06H ;ENTRY MODE SET    MOVX @DPTR,A      LCALL BUSYCHECK      MOV A,#0FH ;DISPLAY THE CUROR ON    MOVX @DPTR,A      LCALL BUSYCHECK    MOV A,#80H

    MOVX @DPTR,AMOV DPTR,#LOOKUP  

    MOV R2,#0FH      MORE: LCALL BUSYCHECK      MOVA,#01      MOV P2,#IOHIGH      MOVX @R0,A      MOVX A,@DPTR      MOV P2,#IOHIGH      MOVX @R1,A      INC DPTR      DJNZ R2,MORE      HLT SJMP HLT      BUSYCHECK MOV R1,#DENL      MOV R0,#LATCHL      MOV P2,#IOHIGH      MOV A,#02      MOVX @R0,A      BSY: MOV P2,#IOHIGH      MOVX A,@R1      ANL A,#80H      JNZ BSY      MOV P2,#IOHIGH      MOV A,#00      MOVX @R0, A      RET      LOOKUP: DB ‘VI MICROSYSTEMS  

Dept. of ECE, Sree Buddha College of Engineering 72

Page 73: MClabFair 2013.Pp

08.607 Microcontroller Lab Manual

Look-up Table:

Memory Hex. code4152 56 49 20 4D 4156 49 43 52 4F415A 53 59 53 54415E 45 4D 53 20

Procedure:

1. Enter the above opcodes from 4100.2. Execute the program.3. See output in LCD. “VI MICROSYSTEMS “ is displayed.

Result: Thus an assembly language programs was written to display some text on LCD and it was executed successfully.

References: The 8051 Microcontroller and Embedded Systems - Muhammad Ali Mazidi, Janice Gillispie Mazidi.

Dept. of ECE, Sree Buddha College of Engineering 73

Page 74: MClabFair 2013.Pp

08.607 Microcontroller Lab Manual

Circuit diagram-waveform generation

Generating Look up table

Vout= 5V + (5V* sin θ)

DAC input value= Vout * 25.6

Where θ varies between 0 and 360 degrees.

Dept. of ECE, Sree Buddha College of Engineering 74

Page 75: MClabFair 2013.Pp

08.607 Microcontroller Lab Manual

Exp No : 14

Date :

WAVEFORM GENERATION USING LOOKUP TABLES

Aim: To interface a DAC to the 8051 and to write and execute an assembly language

program to generate a sine waveform at DAC 1 output

Apparatus Required:

No Name Quantity1 Microcontroller kit 12 DAC interface board 1

Algorithm:

1: Start

2: Prepare the look up table and store the values in consecutive memory locations

3: Initialize R3 with the number of elements in the look up table

4:Load R1 and R2 with lower and higher bytes of starting address of look up table.

5. Load the data pointer with the values in R1 and R2 respectively.

6: Load the value written in the memory location to Accumulator.

7: Output that value through an output port

8: Increment R1.

9: Decrement R3 and check whether it is zero, If not go to step5.

10: Go to step 3

11: Stop

Program: Waveform generation using lookup tables.

Memory Hex. Label Assembly Codes Comments address code            ORG 4100        MOV R1,#00      START: MOV R2,#42        MOV R3,#46 ;Count      LOOP: MOV DPL,R1        MOV DPH,R2        MOVX A,@DPTR      MOV DPTR,#FFC0 ; DAC 1 output      MOVX @DPTR,A        INC R1        DJNZ R3,LOOP        LJMP START  

Dept. of ECE, Sree Buddha College of Engineering 75

Page 76: MClabFair 2013.Pp

08.607 Microcontroller Lab Manual

Look-up table:

Memory Hex. Code address

4200 7F 8A 95 A04204 AA B5 BF C84208 D1 D9 E0 E7420C ED F2 F7 FA4210 FC FE FF FE4214 FC FA F7 F24218 ED E7 E0 D9421C D1 C8 BF B54220 AA A0 95 8A4224 7F 74 69 5F4228 53 49 3F 36422C 2D 25 1D 174230 10 0B 07 044234 01 00 01 044238 07 0B 10 17423C 1D 25 2D 364240 3F 49 53 5F4244 69 74    

Procedure:

1. Enter the above opcodes from 4100.2. Execute the program.3. Check sine wave using CRO.

Result: Thus an assembly language programs was written to generate a sine waveform at

DAC 1 output and it was executed successfully.

References: The 8051 Microcontroller and Embedded Systems - Muhammad Ali Mazidi, Janice Gillispie Mazidi.

Dept. of ECE, Sree Buddha College of Engineering 76

Page 77: MClabFair 2013.Pp

08.607 Microcontroller Lab Manual

Exp No : 15

Date :

PWM GENERATION

Aim: To generate a pulse width modulation signal.

Apparatus Required:

No Name Quantity1 Microcontroller kit 1

Theory:

Pulse width modulation is a commonly used technique for controlling power to inertial electrical devices, made practical by modem electronic power switches. The average value of voltage (and current) fed to the load is controlled by turning the switches between supply and load on and off at a fast pace. The longer switch is on compared to the off periods, the higher the power supplied to the load is. The PWM switching frequency has to be much faster than what would affect the load, which is to be stay the device that uses the power. The term duty cycles describes the proportion of ‘on ‘ time to the regular interval or ‘period of time; a low duty cycle corresponds to low power , because the power is off for most of the time. Duty cycle is expressed in percent,100% being fully on. The main advantage of PWM is that the power loss in the switching devices is very low. When a switch is off there is practically no current, and when it is on ,there is almost no voltage drop across the switch.Pwer loss being the product of voltage and current, is thus in both cases close to zero. PWM works also well with digital controls, which, because of their on/off nature, can easily set the needed duty cycle.

In 89C51RD2 have 16 bit compare module. Each mode can be programmed to

operate one of the four mode.

1. Rising and/or falling edge capture

2. Software timer

3. High speed output

4. Pulse width modulation

P1.3 to P1.7 are the those five pins. First we set these pins as PWM mode.

Duty cycle of PWM is automatically change 0% to 100%.

PWM 0 - P1.3 PWM 2 - P1.5

PWM 1 - P1.4 PWM 3 - P1.6

PWM 4 - PI.7 CCAPnH = 256 (1-Duty cycle).

Dept. of ECE, Sree Buddha College of Engineering 77

Page 78: MClabFair 2013.Pp

08.607 Microcontroller Lab Manual

Algorithm:

1: Start

2: Make a port in (say p1.0)as high.

3: Store an immediate value of FFh in register R1.

4: Check R1=0.If, no ,proceed, otherwise go to step 6.

5: Call the delay loop inside which the R1 value will decrement.Go to step 4.

6:Complement the port pin P1.0.

7:Store the register R1 with the immediate value(100-K)

8: Go to step 4.

Step 9: Stop.

Program: PWM0.

Memory Hex. Label Assembly Codes Comments address code            ORG 4100H      MOV CMOD,#02H      MOV CL,#00H      MOV CH,#00H      MOV CCAPM0,#42H      MOV CCAP0L,#00H      MOV R1,#0FFH      L1: MOV CCAP0H,R1      CALL DELAY      MOV CCON,#40H      DJNZ R1,L1      DELAY: MOV R2,#0FFH      L3: MOV R3,#0FFH      L2: DJNZ R3,L2      DJNZ R2,L3      RET        END  

Procedure:1. Enter the above opcodes from 4100.2. Execute the program.3. Connect P17 & P16 Header Using 10 Pin FRC cable.4. PWM wave is generate in P12 1st PIN.

Dept. of ECE, Sree Buddha College of Engineering 78

Page 79: MClabFair 2013.Pp

08.607 Microcontroller Lab Manual

Program: PWM1.

Memory Hex. Label Assembly Codes Comments address code            ORG 4100H        MOV CMOD,#02H        MOV CL,#00H        MOV CH,#00H        MOV CCAPM1,#42H        MOV CCAP1L,#00H        MOV R1,#0FFH      L1: MOV CCAP1H,R1        CALL DELAY        MOV CCON,#40H        DJNZ R1,L1      DELAY: MOV R2,#0FFH      L3: MOV R3,#0FFH      L2: DJNZ R3,L2        DJNZ R2,L3        RET        END  

Procedure:1. Enter the above opcodes from 4100.2. Execute the program.3. Connect P17 & P16 Header Using 10 Pin FRC cable.4. PWM wave is generate in P12 2nd PIN.

Result: Thus an assembly language programs were written to generate a pulse width

modulation signal and it was executed successfully.

References: The 8051 Microcontroller and Embedded Systems - Muhammad Ali Mazidi, Janice Gillispie Mazidi.

Dept. of ECE, Sree Buddha College of Engineering 79

Page 80: MClabFair 2013.Pp

08.607 Microcontroller Lab Manual

Circuit diagram -Realization of Boolean expression

A, B, C, D are the input to this equation

SW15 - ASW14 - BSW13 - CSW12 – D

Given input data also display in LED.

Dept. of ECE, Sree Buddha College of Engineering 80

Page 81: MClabFair 2013.Pp

08.607 Microcontroller Lab Manual

REALIZATION OF BOOLEAN EXPRESSION USING PORT

Aim: To write and execute an assembly language program to realize Boolean expression, by

taking the state of Boolean variables using ports. Y= AB+CD.

Apparatus Required:

No Name Quantity1 Microcontroller kit 1

Algorithm:

1: Start

2: Initialize four port pins as input ports.

3: Save state of first port pin p1.0 to Carry flag (CY).

4: AND status of port pin p1.1 with carry and save status of CY to p2.0.

5: Save state of port pin p1.2 to Carry flag (CY). AND p1.3 to CY.

6: Save state of CY to p2.1 and save state of p2.0 to CY.

7: OR CY with status of p2.1 and save state of CY to p2.3.

8: Stop.

Program:

Memory Hex. Label Assembly Codes Comments address code            MOV P2, #00H      START: MOV C, P1.0        ANL C, P1.1        MOV P2.0, C        MOV C, P1.2        ANL C, P1.3        MOV P2.1, C        MOV C, P2.0        ORL C, P2.1        MOV P2.3, C        SJMP START      HLT: SJMP HLT  

Dept. of ECE, Sree Buddha College of Engineering 81

Exp No : 16

Date :

Page 82: MClabFair 2013.Pp

08.607 Microcontroller Lab Manual

Procedure:

If

Input A is high → LED 16 ON

Input A is low → LED 16 OFF

Input B is high → LED 15 ON

Input B is low → LED 15 OFF

The first “AND” gate output either high or low. This is shown in LED 14.

Input C is high → LED 13 ON

Input C is low → LED 13 OFF

Input D is high → LED 12 ON

Input D is low → LED 12 OFF

This second AND gate output either high or low. If is shown in LED 11.

The final output Y is high means LED 9 is ON otherwise LED 9 is OFF.

Result: Thus an assembly language program was written to realize the given Boolean expression and it was executed successfully

References: The 8051 Microcontroller and Embedded Systems - Muhammad Ali Mazidi, Janice Gillispie Mazidi.

Dept. of ECE, Sree Buddha College of Engineering 82

Page 83: MClabFair 2013.Pp

08.607 Microcontroller Lab Manual

Exp No : 17

Date :

FREQUENCY MEASUREMENT BY COUNTING THE NUMBER OF

PULSES IN A FIXED AMOUNT OF TIMEAim: To measure the frequency of a pulse by measuring the number of pulses in a fixed

amount of time.

Apparatus Required:

No Name Quantity1 Microcontroller kit 1

Theory:

This program is measure frequency of given input in a particular time. For that we initialize timer 0 for 1ms delay. And also we initialize PCA timer interrupt. When PCA interrupt is come the timer 0 is turn ON and count the number of rising edge will come. When timer 0 interrupt is come the PCA timer & timer 0 is turn off and calculate frequency of a given input in that time.

Note: In this program we set 1ms delay. So at least given frequency of is 2 kHz. So only the counts will 2.Calculation:

PCA clock input = 12/11.0859 MHz = 1.0825µsInitialize timer value = 1 kHz and enable timer 0 overflow interrupt. Input frequency is 10 KHz.If count value = 2Total count = 2 × 10 = 20We set timer 0 value in 1ms delay. After that it will generate interrupt.

Algorithm:

1. initialize timer 0 for 1ms delay2. initialize serial port and PCA timer interrupt3. Clear CCF0. If flag not set means to increment count value, otherwise to enable to

timer interrupt4. Disable capture interrupt and timer interrupt.5. Initialize timer value and enable capture interrupt6. Calculate frequency of a given input in that time.

Dept. of ECE, Sree Buddha College of Engineering 83

Page 84: MClabFair 2013.Pp

08.607 Microcontroller Lab Manual

Program: Frequency measurement by counting the number of pulses in a fixed amount

of time.

Memory Hex. Label Assembly Codes Comments address code      

      ORG 1000H      CALL SERIAL      CALL TIMER0_INIT      CALL PCA_INIT      MOV SBUF,#'A'      JNB TI,$      LCALL DELAY      CLR TI      L1: SJMP L1      PCA_INIT: MOV CMOD, #00H ; Initialize PCA timer    MOV CH, #00H      MOV CL, #00H      MOV CCAPM0, #21H      SETB EC      SETB EA      CLR FLAG      RET      SERIAL: MOV TMOD,#20H ;to initialize serial port    MOV TH1 ,#0FDH      MOV SCON,#52H      SETB TR1 ;SETB TI    RET      TIMER0_INIT: SETB TR0 ;to initialize Timer    MOV A,TMOD      ORL A,#01H      MOV TMOD,A      MOV TH0,#0FCH      MOV TL0,#48H      MOV IE,#80H      RET      DELAY: MOV 40H,#01H      LOP5: MOV 41H,#0FH      LOP4: DJNZ 41H,LOP4      DJNZ 40H,LOP5      RET      ORG 0033H ;PCA INTERRUPT    CLR CCF0  

    JB FLAG,GO1 ; if flag not set means to increment count value

    SETB ET0 ;otherwise to enable to timer interrupt    SETB FLAG      GO1: INC R3      RETI      ORG 000BH ;TIMER INTERRUPT    CLR EC ;disable capture interrupt    CLR ET0 ;disable Timer interrupt    CLR FLAG      MOV TH0,#0FCH ;initialize timer value    MOV TL0,#48H ;67    SETB EC ;enable capture interrupt    MOV A,R3      MOV B,#10H      MUL AB      MOV SBUF,A      JNB TI,$      LCALL DELAY      CLR TI      MOV R3,#00H        RETI  

Dept. of ECE, Sree Buddha College of Engineering 84

Page 85: MClabFair 2013.Pp

08.607 Microcontroller Lab Manual

Procedure:

1. Enter the above opcodes from 4100.2. Execute the program.3. In serial window the frequency value is shown in KHz.

Result: Thus an assembly language programs was written to measure the frequency of a

pulse by measuring the number of pulses in a fixed amount of time and it was executed

successfully.

References: The 8051 Microcontroller and Embedded Systems - Muhammad Ali Mazidi, Janice Gillispie Mazidi.

Dept. of ECE, Sree Buddha College of Engineering 85

Page 86: MClabFair 2013.Pp

08.607 Microcontroller Lab Manual

Exp No : 18

Date :

FREQUENCY MEASUREMENT BY MEASURING THE TIME PERIOD

BETWEEN TWO CONSECUTIVE PULSES

Aim: To write and execute an assembly language program to measure the frequency of a

pulse by measuring the time period between two consecutive pulses.

Apparatus Required:

No Name Quantity1 Microcontroller kit 1

Theory:

This program is measure frequency of two connective pulse first we initialize PCA interrupt for rising edge detection. When rising edge is detect the PCA timer is ON and then falling edge is detect the timer is run between rising edge to falling edge detect. Using PCA timer count we calculate frequency of the given signal.Example Calculation

Frequency =11.059 MHZ PCA clock input =(1/12 ) *FOSC

PCA clock input =(12/11.0859 ) =1.0825 µsTake count value from CL & CH. If total count is 0×7D. Decimal value is 125.

Time = Count *1.0825 µs =125*1.0825 µs

=135.3125 µsFrequency =1/T

=1/(135.3125*10-6 ) =106/135.3125 =0.0073903*106

=7.3903 KHZAlgorithm:

1: Start

2: Set timer 1 as timer in mode 1

3: Make one port pin as input port

4. Initialize PCA timer for rising edge detection

4: When rising edge is detect the PCA timer is ON and then falling edge is detect the

timer is run between rising edge to falling edge detect.

5: PCA timer count is used to calculate frequency of the given signal

6. Stop.

Dept. of ECE, Sree Buddha College of Engineering 86

Page 87: MClabFair 2013.Pp

08.607 Microcontroller Lab Manual

Program: Frequency measurement by measuring the time period between two consecutive pulses

Memory Hex. Label Assembly Codes Comments address code      

      ORG 0033H        JMP PCA_INTERRUPT        ORG 1000H      L1: CALL PCA_INIT        CALL SERIAL ;CALL SEND1      ;call calc      CALL SEND        SJMP L1               PCA_INIT: MOV CMOD, #00H ; Initialize PCA timer      MOV CH, #00H        MOV CL, #00H        MOV CCAPM0, #21H        SETB EC        SETB EA        SETB CR        CLR FLAG        RET      PCA_INTERRUPT: CLR CCF0        JB FLAG, SECOND_CAPTURE      FIRST_CAPTURE: MOV CL ,#00H ;CLR CR      MOV CH ,#00H        MOV CCAPM0, #11H ;FALLING EDGE      SETB FLAG        RETI      SECOND_CAPTURE: CLR C        MOV A, CL        MOV R2,A        MOV A, CH        MOV R3,A        MOV CCAPM0, #21H ;RISIGING EDGE      CLR FLAG        RETI      CALC: MOV A,R2        MOV B,#02H        MUL AB        MOV A,#11H        DIV AB        MOV A,R2        RET      SEND: MOV SBUF,#'A'        JNB TI,$        LCALL DELAY        CLR TI        MOV SBUF,R3 ;HIGHER BYTE VALUES      JNB TI,$        LCALL DELAY        CLR TI        MOV SBUF,R2 ;LOWER BYTE VALUES      JNB TI,$        LCALL DELAY        CLR TI        RET      DELAY: MOV 40H,#7FH      LOP5: MOV 41H,#0FFH      LOP4: DJNZ 41H,LOP4        DJNZ 40H,LOP5        RET      SERIAL: MOV TMOD,#20H        MOV TH1 ,#0FDH        MOV SCON,#52H        SETB TR1  

      RET  

Dept. of ECE, Sree Buddha College of Engineering 87

Page 88: MClabFair 2013.Pp

08.607 Microcontroller Lab Manual

Procedure:

1. Enter the above opcodes from 4100.2. Execute the program.3. The given Frequency value is display in serial window in KHz.

Result: Thus an assembly language programs was written to measure the frequency of a

pulse by measuring the time period between two consecutive pulses and it was executed

successfully.

References: The 8051 Microcontroller and Embedded Systems - Muhammad Ali Mazidi, Janice Gillispie Mazidi.

Exp No : 19

Dept. of ECE, Sree Buddha College of Engineering 88

Page 89: MClabFair 2013.Pp

08.607 Microcontroller Lab Manual

Date :

LED BLINKING USING EMBEDDED C PROGRAMMINGAim: To write and execute a c program to blink LED.

Apparatus Required:No Name Quantity1 Microcontroller kit 1

Program

#include<reg51.h>#include<stdio.h>xdata char *ledsel;int i,val;void delay(){int i,j;for(i=0;i<0xff;i++)for(j=0;j<0xff;j++);}

void main(){

ledsel = 0xff23;while(1){

for(i=0;i<5;i++){

*ledsel = 0x00;delay();*ledsel = 0xff;delay();

}

for(i=0;i<5;i++){

*ledsel = 0x55;delay();*ledsel = 0xaa;delay();

}

}}

Dept. of ECE, Sree Buddha College of Engineering 89

Page 90: MClabFair 2013.Pp

08.607 Microcontroller Lab Manual

Result: Thus a c language program was written to blink LED and it was executed

successfully.

References: The 8051 Microcontroller and Embedded Systems - Muhammad Ali Mazidi, Janice Gillispie Mazidi.

Dept. of ECE, Sree Buddha College of Engineering 90

Page 91: MClabFair 2013.Pp

08.607 Microcontroller Lab Manual

Exp No : 20

Date :

ADC INTERFACING USING EMBEDDED C PROGRAMMING Aim: To interface an ADC 0809 to the 8051 and to write and execute a c program to convert the analog input at channel 0 to a digital value. Apparatus Required:No Name Quantity1 Microcontroller kit 1

Program

#include<reg51.h>#include<string.h>#include<stdio.h>

//Prototype Declarationvoid I2C_WRITE(unsigned char);void Data_Write(unsigned char [],unsigned int);unsigned char I2C_READ();void Delay_Time();void busycheck();void I2C_START();void I2C_STOP() ;void lcdint();void ser_int();unsigned int READ;sbit SCLK=P3^2;sbit SDA =P3^3;bit ACK;#define HIGH 01;#define LOW 00;idata unsigned char dat;idata unsigned char i;unsigned int adc_data;void main(){

ser_int();

I2C_START();I2C_WRITE(0X9e);I2C_WRITE(0X41);while(1){I2C_START();

Dept. of ECE, Sree Buddha College of Engineering 91

Page 92: MClabFair 2013.Pp

08.607 Microcontroller Lab Manual

I2C_WRITE(0x9f);adc_data = I2C_READ();printf("\n\r Adc Data :%x",adc_data);I2C_STOP();

I2C_START();I2C_WRITE(0x9f);adc_data = I2C_READ();I2C_STOP(); }

}

void ser_int(){

TMOD = 0X20;TH1 = 0XFD;TR1 = 1;TI = 1;

}

void I2C_START(){

SCLK =LOW;SDA =LOW;

Delay_Time();Delay_Time();SCLK=HIGH;Delay_Time();Delay_Time();SDA=HIGH;Delay_Time();Delay_Time();SDA=LOW;Delay_Time();Delay_Time();SCLK=LOW;

}

void I2C_WRITE(unsigned char j){

dat=j; for(i=0;i<8;i++) {

SDA = dat & 0x80; dat=dat<<1;SCLK=HIGH;Delay_Time();

Dept. of ECE, Sree Buddha College of Engineering 92

Page 93: MClabFair 2013.Pp

08.607 Microcontroller Lab Manual

Delay_Time();SCLK = LOW;

} SDA=HIGH; Delay_Time(); Delay_Time(); SCLK = HIGH; Delay_Time(); Delay_Time(); ACK = SDA; Delay_Time(); Delay_Time(); SCLK=LOW; }

void I2C_STOP(){

SCLK=LOW;Delay_Time();Delay_Time();SDA=HIGH;

}

unsigned char I2C_READ(){

unsigned char i,j;j=0;j=SDA;

for(i=0;i<8;i++){

j<<=1;SCLK=HIGH;j|=SDA;Delay_Time();SCLK=LOW;

}

Delay_Time();Delay_Time();SDA = LOW;Delay_Time();Delay_Time();SCLK = HIGH;Delay_Time();

Dept. of ECE, Sree Buddha College of Engineering 93

Page 94: MClabFair 2013.Pp

08.607 Microcontroller Lab Manual

Delay_Time();SCLK = LOW;Delay_Time();Delay_Time();SDA = HIGH;

return(j);}

void Delay_Time(){ unsigned long int i;

for(i=0;i<500;i++);

}

Result: Thus a c language program was written to interface an ADC 0809 to the 8051 and it was executed successfully.

References: The 8051 Microcontroller and Embedded Systems - Muhammad Ali Mazidi, Janice Gillispie Mazidi.

Dept. of ECE, Sree Buddha College of Engineering 94

Page 95: MClabFair 2013.Pp

08.607 Microcontroller Lab Manual

Exp No : 21

Date :

STEPPER MOTOR INTERFACING USING EMBEDDED C PROGRAMMING

Aim: To interface stepper motor to the 8051 and to write and execute a c program to rotatestepper motor in a clockwise and anti clockwise direction.Apparatus Required:

No Name Quantity1 Microcontroller kit 1

Program

#include <Intel/8051.h>#define p8255_ctl 0x2043#define portc 0x2042#define ctlr_word 0x80 // for clockwise phasea is 0d, phaseb is 0e, phasec is 07 and phased is 0b// for anti clockwise phasea is 0b, phaseb is 07, phasec is 0e and phased is 0d#define phasea 0x0d#define phaseb 0x0e#define phasec 0x07#define phased 0x0bxdata unsigned char *ptr_8255_ctl;xdata unsigned char *ptr_8255_portc;void delay(void);void main (){int i;ptr_8255_ctl = p8255_ctl;*ptr_8255_ctl = ctlr_word;while(1){ptr_8255_portc=portc; //porta address is taken in pointer variable//different speeds are loaded into pointer*ptr_8255_portc = phasea;delay();*ptr_8255_portc = phaseb;delay();*ptr_8255_portc = phasec;delay();*ptr_8255_portc = phased;delay();}}void delay(void){

Dept. of ECE, Sree Buddha College of Engineering 95

Page 96: MClabFair 2013.Pp

08.607 Microcontroller Lab Manual

int i=0;for(i=0;i<=10000;i++){}}

Result: Thus a c language program was written to interface stepper motor to the 8051 and it was executed successfully.

References: The 8051 Microcontroller and Embedded Systems - Muhammad Ali Mazidi, Janice Gillispie Mazidi.

Dept. of ECE, Sree Buddha College of Engineering 96

Page 97: MClabFair 2013.Pp

08.607 Microcontroller Lab Manual

Viva-voce questions

1. The internal RAM memory of the 8051 is:

2. The 8051 has ________ 16-bit counter/timers.

3.The 8051 can handle how many interrupt sources?

4. An alternate function of port pin P3.4 in the 8051 is:

5. The I/O ports that are used as address and data for external memory are:

6. The 8051 has ________ parallel I/O ports.

7. The total external data memory that can be interfaced to the 8051 is:

8. Bit-addressable memory locations are:

9. The 8-bit address bus allows access to an address range of:

10. The number of data registers is:

11. What is the difference between the 8031 and the 8051?

12. The total amount of external code memory that can be interfaced to the 8051 is:

13. What are the advantages and disadvantages of using Harvard architecture in 8051?

14. How much maximum external program memory can be interfaced?

15. What are the power consumptions in power down and idle modes

16. What is the maximum delay the Timer0 produces when 8051 is operated at 12MHz?

17. Explain how in Serial communication mode 0 expands I/O lines with the help of shift

18. Explain Quasi Bidirectional ports of 8051

19. What is the status of all registers on reset?

20. Explain how in Serial communication mode 0 expands I/O lines with the help of shift

21. How many bytes are there in the internal memory?

22. How much program memory is in the chip and how much more can be interfaced

externally?

23. List and explain all SFRs .

24. What are the differences between microprocessor and microcontroller?

25. What address is assigned to register A?

26. How to change register bank in 8051?

27. What is the use of DJNZ instruction? Give the syntax.

28.Explain all the bits in the PSW.

.29. What are the addressing modes in the 8051?

30. How to copy the contents from one memory location to the others?

Dept. of ECE, Sree Buddha College of Engineering 97

Page 98: MClabFair 2013.Pp

08.607 Microcontroller Lab Manual

31. What is difference between LCALL and ACALL?

32.What is operating frequency of microcontroller?

33.What is the frequency of timers?

34.How to provide the external frequency to the 8051?

35.What are the modes in which the 8051 timer works? Which mode you used in your

program?

36.How to generate the delay using timer?

37.State the difference between timer and counter.

38.How to start the functioning of the timer?

39.How to load the initial count values in timers?

40.State the use of TMOD and TCON registers in timer programming.

41.Draw the TMOD and TCON registers contents.

42. What type of serial communication the 8051 uses?

43.What is baud rate? State standard baud rates.

44..How to use the baud rate in 8051 assembly program?

45.How many interrupts are associated with 8051 serial ports?

46.What is the use of PCON and SBUF registers?

47.By which pins the data is transferred and received serially by 8051?

48.Draw the serial data format.

49.How to calculate the bit transmission time?

50.How to calculate the values of TH1 and TL1 for respective baud rates?

Dept. of ECE, Sree Buddha College of Engineering 98

Page 99: MClabFair 2013.Pp

08.607 Microcontroller Lab Manual

MICRO CONTROLLER LAB QUESTION BANK

1. Write a program to arrange 10 data stored in 4100H onwards in ascending order .Store the result in 4400H.

2. Write a program to subtract two 16 bit numbers.3. Write a program to add two 16 bit numbers.4. Create the following waveform and display it on the CRO.5. Obtain the following waveform on CRO using suitable assembly

language program.6. Generate the following waveform using an assembly language program.7. A set of non-zero bytes are stored in locations 4100H onwards.The last

byte in the data set is indicated by the entry 00H.Write a program to arrange it in ascending order.

8. Write a program to sort 10 numbers stored in memory in the ascending order and get a display of the same with 1 second delay.

9. A set of N numbers are stored in memory location 4500H onwards.Sort the data in the ascending order and store it in memory location 4600H onwards.Also find the largest number and display it on the data field.

10.Write an assembly program to run the stepper motor such a way that the speed of rotation for clockwise direction must be double of that of the speed for anticlockwise direction.

11.Write an assembly language program to rotate the stepper motor first in clockwise direction for 1800 and then in the anticlockwise direction for the remaining period of rotation.

12.Write an assembly program to rotate the stepper motor such a way that the speed of rotation for 180-3600 must be double of that of the speed for 0-1800.

13.Write an assembly language program to find out largest among the 10 given numbers and display the largest number on the data field.

14.Write an assembly language program to generate and store the Fibonacci series.

15.Write a program to add a few numbers and find its square.16.Write an ALP to obtain x2+ y2, where x and y are two nibbles.17.Write a program to find the square of a number and display it on the

data field.18.Write a program to find the square root of a number and display it on the

data field.19.Write a program to add and subtract two 16 bit numbers and store the

result on separate memory locations.20.Find Si= Xi/Yi Where X and Y are 8 bit numbers. i is ranging from

1 to 5.

Dept. of ECE, Sree Buddha College of Engineering 99

Page 100: MClabFair 2013.Pp

08.607 Microcontroller Lab Manual

21.Find Si= Xi*Yi Where X and Y are 8 bit numbers. i is ranging from 1 to 5.

22.An array of 5 eight bit numbers are stored .Find the square of these numbers and stored it in another array.

23.Write a program for the addition of two 3*3 matrix.24.Write a program for the addition of two 4*4 matrix.25.Write a program to verify the relation (A+B)2 = A2+ 2AB+B2 ,where A

and B are two 8 bit numbers.26.Write a program to verify the relation (A-B)2 = A2- 2AB+B2 ,where A

and B are two 8 bit numbers.27.Write an ALP to display the message ECE2011.28.Write an ALP to execute the following .Y=2X + 3, where X and Y are

8 bit numbers .Display the result on data field.29.Write an ALP to generate a triangular waveform of frequency 2KHZ.30.Write an ALP to generate a saw tooth waveform of frequency 2KHZ.31.Write an ALP to generate a sine waveform .32.Write an ALP to generate a square waveform of frequency 2KHZ.33.Write an ALP to multiply two 8 bit numbers by shift and add method.34.Write a program to find the product of the largest and smallest among a

series of numbers.35.Write a program to convert analog voltage to digital value and display

it on LED.

Dept. of ECE, Sree Buddha College of Engineering 100