You are on page 1of 115

08.

607 MICROCONTROLLER LAB

08.607 Microcontroller Lab Manual


Prepared by

Assini H

Department of ECE, VKCET

Page 1

08.607 MICROCONTROLLER LAB


Syllabus L-T-P : 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.

Department of ECE, VKCET

Page 2

08.607 MICROCONTROLLER LAB


Objectives: 1. To study assembly language programming in 8051 2. To study interfacing of various peripherals using 8051 3. To design and develop applications using 8051 Outcomes: 1. Students should develop a system using 8051 microcontroller Instructions to Students These are the instructions for the students attending the lab course 08.607 Microcontroller Lab 1. Read the Handout carefully before coming to the laboratory and be sure about what you are supposed to do. 2. Prepare Rough Record as per the instruction given in the Handout. 3. Your Rough Record is the primary record of all experiments. Every entry must be dated. Use permanent ink (no pencils). Be sure your name and Roll No. is written in the book. 4. Come to the laboratory in time. 5. Must bring Rough Record, Fair Record, writing equipments and calculator. 6. Do the experiments as per the instructions given in the Handout. 7. Take down the readings and results as shown in the Handout. Do necessary calculations and show it to the staffs. 8. Fair Record is to be submitted individually. Try to complete it and submit it to the staff on the next working day. If this is not possible due to some valid reason show whatever done by you and take his permission for submitting it on the next working day. 9. If you have any problems with the Microcontroller Kit contact the Staff. Do not try to solve it yourself as this may lead to irreparable damage to the Kit. Follow these instructions carefully.

Department of ECE, VKCET

Page 3

08.607 MICROCONTROLLER LAB

INDEX 1. INTRODUCTION TO 8051 MICROCONTROLLER KIT.......................................................7 2 . ADDTION AND SUBTRACTION OF 16-BIT NUMBERS.....................................................18 3 . MULTIPLICATION AND DIVISION OF 8-BIT NUMBERS................................................24 4 . MULTIPLICATION BY SHIFT AND ADD METHOD..........................................................27 5 . LCM AND HCF OF TWO 8-BIT NUMBERS..........................................................................31 6. REALIZATION OF BOOLEAN EXPRESSION.......................................................................35 7. SORTING NUMBERS AND FACTORIAL OF A NUMBER..................................................40 8. INTERFACING WITH 8-BIT ADC............................................................................................47 9. SQUARE, SQUARE ROOT OF A NUMBER AND FIBONACCI SERIES...........................55 10. 8-BIT DAC INTERFACE...........................................................................................................61 11. WAVE FORM GENERATION USING LOOK-UP TABLE.................................................72 12. MATRIX ADDITION.................................................................................................................77 13. DISPLAY INTERFACING........................................................................................................82 14. PWM SIGNAL GENERATION................................................................................................90 15. STEPPER MOTOR INTERFACE............................................................................................95 16. FREQUENCY MEASUREMENT BY COUNTING THE NUMBER OF PULSES IN A FIXED AMOUNT OF TIME......................................................................................................109 BIBILIOGRAPHY..........................................................................................................................115

Department of ECE, VKCET

Page 4

08.607 MICROCONTROLLER LAB

Expt. No.1 Objective:

INTRODUCTION TO 8051 MICROCONTROLLER KIT To study the basics of 8051 Microcontroller Kit

Equipments Required: Microcontroller Kit Theory: Specifications of the kit: 1. Hardware specifications: Intel 8051/89C51 at 12MHz Memory address System EPROM location C000H to FFFFH System RAM location Additional RAM C000H to FEFFH (For Micro-51 LC C000H to FF00H (For Vi89C51 kit) Monitor Buffer User Program/Data RAM location User Data RAM location C000H to FEFFH (For Micro-51 LC C000H to FF00H (For Vi89C51 kit) Memory mapped IO FFC0H to FFFFH Memory mapped IO expansion

: 0000H to 3FFFH and : 4000H to BFFFH : 0000H to 3FFFH and Kit), 0000H to 3FFFH and : 4000H to 40FFH : 4100H to BFFFH : 0000H to 3FFFH and Kit), 0000H to 3FFFH and : FF00H to FF2FH and : FF20H to FFBFH

Monitor buffer should not be accessed by the user since it is used by the monitor program. Input and Output 24 IO lines using two Programmable Peripheral Interface IC (8255) One RS232C Serial Interface using 8051 Serial port Two 16 bit timers One Centronics compatible Printer Interface through first 8255 Five interrupt sources
Department of ECE, VKCET Page 5

08.607 MICROCONTROLLER LAB


Display 16x2 alpha numeric LCD display 6 digit, 7 segment red LED display with filter IBM keyboard interface 8051 Trainer kit (Vi89C51 SB) Interface Block diagram

2. Software specifications: The Microcontroller Kit Micro-51 LC of Vi Microsystems contains a high performance 32kB monitor program. It is designed to respond to user input, RS232C serial communications, etc. Some simple commands (Not case sensitive) for the kit are: a) Substitute Memory Command Syntax: #sp <Address> <Enter> : For program memory #sd <Address> <Enter> : For data memory b) Register View/ Modify Command Syntax: #r <Enter> c) Execute Command Syntax: #go <Address> <Enter> d) Execute with break point command Syntax: #go <Start address> <Space> <Enter> e) Assembler command Syntax: #a <Enter> f) Disassembler command Syntax: #u <Enter> g) Internal RAM command Syntax: #ir <Address><Enter>
Department of ECE, VKCET Page 6

08.607 MICROCONTROLLER LAB


3. Memory configurations: All peripherals in Micro-51 LC of Vi Microsystems are memory mapped, ie the peripheral will also be included in the memory configuration. Memory allocation table is shown in table 1: External Program Memory FFFFH EPROM Expansion area C000H User Program Memory BFFFH Combined with Data 4000H Memory 3FFFH Monitor EPROM area 0000H

External Data Memory FF00H C000H 4000H 0000H Memory Mapped IO User Data Memory User Data Memory Combined with Program Memory User Data Memory FFFFH FEFFH BFFFH 3FFFH

Allocation of EPROM: The kit has a standard EPROM configuration of 32kB using one 27256 (32k x 8 EPROM). The address for the monitor EPROM is 0000H to 3FFFH and C000H to FFFFH. Allocation of RAM: The kit has 32kB of R/W Program/Data memory using one 61256 (32k x 8 DRAM). The address is from 4000H to BFFFH, out of which the first 256 locations are used by monitor as its data buffer area. Hence user program area starts from 4100H onwards. The kit has one more 32kB of R/W Data memory using one 61256 and the address is from 0000H to 3FFFH and C000H to FEFFH. Allocation of Memory Mapped IO:
Department of ECE, VKCET Page 7

08.607 MICROCONTROLLER LAB


The peripherals available in Vi-89C51 are all memory mapped. The complete memory mapped IO allocation table is shown in table 2: Used for Add-on Application Boards Not Used Digital Output Digital Input Not Used IBM Keyboard Select Not Used Parallel Ports-I & II (8255) 174 Latch Select LCD Chip Select Not Used FF21H FF1FH FF17H FF13H FF0BH FF07H FF03H FFFFH FFBFH

FFC0H FF24H FF23H FF22H FF20H FF18H FF14H FF0CH FF08H FF04H FF00H

Software features: The kit accept any command related in the command prompt mode, indicated by a # symbol in the leftmost position of the second row in the LCD module as shown below: Micro-51 #_

Department of ECE, VKCET

Page 8

08.607 MICROCONTROLLER LAB


Some functions that can be performed by the command are: 1. Display and substitute memory locations 2. Display and modify the registers of the 8051 3. Enter and initiate execution of your own program 4. Assemble and disassemble Command line editor features: Single line editor can process up to 40 characters Valid key functions are: Enter To validate an entry To increment memory location To select from memory To decrement memory locations To select from menu . To terminate a command Backspace To delete a character and comeback one position Left arrow To come back one position without deleting a character Right arrow To move right one position without deleting a character Space bar To provide space in a command or data Del To delete a character in a command line 0 to 9 Numeric character a-z Alphabetic character Shift Non-alphabetic and uppercase alphabetic character

Department of ECE, VKCET

Page 9

08.607 MICROCONTROLLER LAB


Connector details: The connectors available in kit are: 1. Power Connector (P1) Single row 5 pin unicon Male connector Signal description Pin Details 1 GND 2 -12V 3 +12V 4 No connection 5 Vcc (+5V) 2. CPU Bus Connector (U6) 40 pin IDC male connector 20 pins arranged in two rows 3. ADC & DAC Connector (P11 & P13) Screw type J801 Connector Format for assembly language program: ADDRESS 4100 4102 4104 4107 4108 410A 410C 410E 410F 4110 4111 STOP LABEL MAIN OPCODE & OPERANDS 74 23 24 90 F0 50 74 80 E4 A3 F0 80 FE 04 01 01 22 42 00 MNEMONICS MOV A,#23 ADD A,#22 MOV DPTR,#4200 MOVX @DPTR,A JNC LOOP1 MOV A,#01 SJMP LOOP2 CLR A INC DPTR MOVX @DPTR,A SJMP STOP COMMENTS Load accumulator by 23H Add accumulator with 22H and store result in accumulator Load data pointer by external memory address 4200H Move accumulator content to external memory location pointed by data pointer If any carry in addition, go to loop1 else next address Load accumulator by 1H Go to loop2 Clear accumulator Increment data pointer by one Move accumulator content to external memory location pointed by data pointer Halt

LOOP1 LOOP2

Department of ECE, VKCET

Page 10

08.607 MICROCONTROLLER LAB


Procedure: 1. To enter assembly language program by placing opcodes in the program memory. The procedure is as follows: Micro-51 #SP 4100 <CR> (CR- Carriage Return) Enter substitute memory command with 16 bit address of the program memory Edit Program memory 4100 00Enter the opcode Edit Program memory 4100 00-74 <CR> Enter 74H Edit Program memory 4101 00-22 <CR> Enter 22H Edit Program memory 4102 00-. <CR> After entering the program came back to command prompt by entering . Micro-51 #_ 2. To enter/view data to data memory The procedure is as follows: Micro-51 #SD 4400 <CR> (CR- Carriage Return) Enter substitute memory command with 16 bit address of the data memory Edit Program memory 4400 00Enter the opcode
Department of ECE, VKCET Page 11

08.607 MICROCONTROLLER LAB


Edit Program memory 4400 00-74 <CR> Enter 74H Edit Program memory 4401 00-. <CR> After entering the program came back to command prompt by entering . Micro-51 #_ 3. To enter data/modify to 8051 registers The procedure is as follows: Micro-51 #R Enter register view/modify command Initially register A is displayed Register View A = 80 - _ Press enter or - key to subsequent or the previous register Change the data to any register if desired and enter To terminate enter . And return command prompt 4. To view 8051 internal RAM Micro-51 #ir 40 Enter internal RAM command INTERNAL RAM VIEW ADDRESS 40 - 18 Enter to get command prompt 5. To enter assembly language program by assembler Micro-51 #a <CR> Enter assembler command ORIGIN : Origin is displayed ORIGIN : 4100 <CR> Enter the origin of program
Department of ECE, VKCET Page 12

08.607 MICROCONTROLLER LAB


4100 Address is displayed 4100 MOV A,#23 <CR> Enter the instruction 4100 74 23 MOV A,#23 Assembler gives the opcode and displayed in the LCD Enter for next instruction Enter . For return to command prompt 6. To execute a program Micro-51 #go 4100 <CR> Go command followed by 16 bit address (Program memory) and enter If the program has infinite loop or halt, the following message will appear Executing To exit from execution and to return control to the command editor, press RESET key in the kit. Error conditions: 1. Attempting to modify the contents of ROM or non-existing memory locations 2. Invalid address (ie not within the range of 0000H to FFFFH) 3. Invalid data (ie not within the range of 00H to FFH) 4. Attempting to modify the contents of internal RAM 5. Invalid address of internal RAM (ie not within the range 0000H to 007FH)

Department of ECE, VKCET

Page 13

08.607 MICROCONTROLLER LAB


Demo programs to do in the lab: 1. Program to add two 8-bit data in R0 and R1 of bank0 and store the result to R2 in the same bank. OPCODE & ADDRESS LABEL MNEMONICS COMMENTS OPERANDS 4100 MAIN 78 11 MOV R0,#11 R0 = data 1 4102 4104 4105 4106 4107 79 E8 29 FA 80 FF MOV R1,#FF MOV A,R0 ADD A,R1 MOV R2,A SJMP HALT R1 = data 2 A = R0 A = A + R1 R2 = A Halt the program

HALT

FE

The observed result is: R2 = 10H 2. Program to subtract two 8 bit data in R1 and R2 of bank1 and store the result to R0 of bank1 ADDRESS 4100 4103 4105 4107 4108 4109 410A LABEL MAIN OPCODE & OPERANDS 75 79 7A E9 9A F8 80 D0 FF 10 08 MNEMONICS MOV PSW,#08 MOV R1,#FF MOV R2,#10 MOV A,R1 SUBB A,R2 MOV R0,A SJMP HALT COMMENTS PSW= 0000 1000 , Where RS1=0 and RS0=1 for bank 1 R1=data 1 R2 =data 2 A=R1 A = A R2 R0=A Halt the program

HALT

FE

The observed result is: R0 = EFH

Department of ECE, VKCET

Page 14

08.607 MICROCONTROLLER LAB


3. Program to check port 0. (Use Vi89C51 kit) ADDRESS LABEL OPCODE & OPERANDS 90 E0 FF 22 MNEMONICS COMMENTS DPTR=FF22H, IO mapping address of digital input switches connected to port 0 Reading the Port 0 switches and A=[DPTR] Increment DPTR to FF23H, IO mapping address of digital output LEDs connected to port 0 Rotate A to left Send the status of switches to LEDs connected to Port 0 Repeat the program

4100 4103

MAIN

MOV DPTR,#FF22 MOVX A,@DPTR

4104

A3

INC DPTR

4105 4106 4107

23 F0 80 F8

RL A MOVX @DPTR, A SJMP MAIN

Observed the status of LEDs according to the status of switch is pressed

Department of ECE, VKCET

Page 15

08.607 MICROCONTROLLER LAB


Expt. No.2 ADDTION AND SUBTRACTION OF 16-BIT NUMBERS

Objective: a) To add two 16-bit numbers saved in external RAM and save the result in external RAM locations with carry b) To subtract two 16-bit numbers saved in internal bank 1 registers R0, R1, R2 and R3, and save the result in external RAM. Equipments Required: Microcontroller Kit Theory: The 8051 microcontroller has 8-bit data operation, so to perform 16-bit operation we have to perform byte operations as shown below. For 16 bit addition: Data 1 = 10 F1H Data 2 = F1 10H Perform 8 bit addition Result 1 = Add lower bytes = F1 + 10 = 01H Result 2 = Add upper bytes with carry of previous addition = 10 + F1 + 01 = 02H Sum = 010201H Given program, assume data 1 is stored in 4200H and 4201H RAM, data 2 is stored in 4202H and 4203H locations. And the results can be store in 4204H, 4205H and 4206H For 16 bit subtraction: Data 1 = 10 F1H Data 2 = F1 10H Perform 8 bit subtraction Result 1 = Subtract lower bytes = F1 - 10 = E1H Result 2 = Subtract upper bytes with borrow of previous subtraction = 10 - F1 - 00 = 1FH Sum = FF1FE1H Given program, data 1 is stored in R0 and R1; data 2 is stored in R2 and R3 internal bank 1 registers. And the results can be store in 4200H, 4201H and 4202H external RAM locations

Department of ECE, VKCET

Page 16

08.607 MICROCONTROLLER LAB


Flowchart: a) 16 bit addition
Start Move lower order byte of data 1 to Accumulator Move lower order byte of data 2 to an internal RAM register Add Accumulator and register content Move result in A, which is lower byte of the sum to an internal RAM register Move upper order byte of data 1 to Accumulator Move upper order byte of data 2 to an internal RAM register

Add Accumulator and register content with carry of previous addition Move result in A, which is upper byte of the sum to internal RAM register

No If carry Yes Write a 1 to Accumulator Clear Accumulator

Save all the results to destination

Stop

Department of ECE, VKCET

Page 17

08.607 MICROCONTROLLER LAB


b) 16 bit subtraction
Start Move lower order byte of data 1 to Accumulator Move lower order byte of data 2 to an internal RAM register Subtract Accumulator and register content Move result in A, which is lower byte of the difference to an internal RAM register Move upper order byte of data 1 to Accumulator Move upper order byte of data 2 to an internal RAM register

Subtract Accumulator and register content with borrow of previous addition Move result in A, which is upper byte of the difference to internal RAM register

No If borrow Yes Write a FF to Accumulator Clear Accumulator

Save all the results to destination

Stop

Department of ECE, VKCET

Page 18

08.607 MICROCONTROLLER LAB


Programs: 1. 16 bit addition ADDRESS 4100 4103 4104 4105 4106 4107 4108 4109 410A 410B 410C 410D 410E 410F 4110 4111 4112 4113 4114 4115 4116 4118 411A 411C 411D 411E 411F LABEL MAIN OPCODE & OPERANDS 90 E0 F8 A3 E0 F9 A3 E0 FA A3 E0 FB E8 2A A3 F0 E9 3B A3 F0 50 74 80 E4 A3 F0 80 FE 04 01 01 42 MNEMONICS COMMENTS Load starting address of external RAM where data are saved to data pointer Move lower byte of data 1 to A Save A content to R0 Increment dptr to point next byte

00 MOV DPTR,#4200 MOVX A,@DPTR MOV R0,A INC DPTR

MOVX A, @DPTR Move upper byte of data 1 to A MOV R1,A INC DPTR MOVX A, @DPTR MOV R2,A INC DPTR MOVX A, @DPTR MOV R3,A MOV A,R0 ADD A,R2 INC DPTR MOVX @DPTR,A MOV A,R1 ADDC A,R3 INC DPTR MOVX @DPTR,A JNC LOOP1 MOV A,#01 SJMP LOOP2 CLR A INC DPTR MOVX @DPTR,A SJMP STOP Save A content to R1 Increment dptr to point next byte Move lower byte of data 2 to A Save A content to R2 Increment dptr to point next byte Move upper byte of data 2 to A Save A content to R1 Move lower byte of data 1 to A Add lower bytes of data Increment dptr to point RAM location to store result Store lower byte of the result Move upper byte of data 1 to A Add upper bytes with carry of previous operation Increment dptr to point RAM location to store result Store lower byte of the result If no carry in addition, go to LOOP1, else next instruction Load 1 to A Go to LOOP2 Clear A Increment dptr to point RAM location to store carry Store carry Halt the program

LOOP1 LOOP2 STOP

Department of ECE, VKCET

Page 19

08.607 MICROCONTROLLER LAB


Note: Calculation of short jump and conditional jump address: SJMP and JNC use relative address for branching. Thus the target address is relative to the value of program counter. To calculate relative address uses the following equation: Relative address = Target address Program counter content 1. For LOOP1, 411C 4118 = 04 2. For LOOP2, 411D 411C = 01 (Both are forward jump) 1. For STOP, 411F 4121 = FE (Back ward jump require negative relative address which is in 2s complement form) 2. 16 bit subtraction ADDRESS
4100 4102 4104 4106 4108 410A 410B 410C 410F 4110 4111 4112 4113 4114 4116 4117 4119 411B 411C 411D STOP

LABEL
MAIN

OPCODE & OPERANDS


D2 78 79 7A 7B E8 9A 90 F0 E9 9B A3 F0 40 E4 80 74 A3 F0 80 FE 03 02 FF 42 00 D0 F1 10 10 F1

MNEMONICS
SETB PSW.3 MOV R0,#F1 MOV R1,#10 MOV R2,#10 MOV R3,#F1 MOV A,R0 SUBB A,R2 MOV DPTR,#4200 MOVX @DPTR,A MOV A,R1 SUBB A,R3 INC DPTR MOVX @DPTR,A JC LOOP1 CLR A SJMP LOOP2 MOV A,#FF INC DPTR MOVX @DPTR,A SJMP STOP

COMMENTS
Set RS0 bit of PSW, then PSW= 0000 1000, to select bank 1 Load lower byte of data 1 to R0 Load upper byte of data 1 to R1 Load lower byte of data 2 to R2 Load upper byte of data 2 to R3 Move lower byte of data 1 to A Subtract lower bytes Load starting address of external RAM to store the result in dptr Store the lower byte of the result Move upper byte of the data 1 to A Subtract upper bytes with the borrow of the previous subtraction Increment DPTR to point next external RAM location Store upper byte of the result If borrow go to LOOP1, else next instruction Clear A Go to LOOP2 Load FF to A Increment dptr to point next external RAM location Store borrow Halt the program

LOOP1 LOOP2

Department of ECE, VKCET

Page 20

08.607 MICROCONTROLLER LAB


Relative address calculation: 1. For LOOP1, 4119 4116 = 03 2. For LOOP2, 411B 4119 = 02 3. For STOP, 411D 411F = FE Procedure: 1. 16 bit addition Steps 1. Enter the program (starting address 4100H) 2. Reset the kit 3. Enter the input (starting address 4200H) 4. Reset the kit 5. Execute the program 6. Reset the kit 7. Observe the output 2. 16 bit subtraction Steps 1. Enter the program (starting address 4100H) 2. Reset the kit 3. Execute the program 4. Reset the kit 5. Observe the output

Department of ECE, VKCET

Page 21

08.607 MICROCONTROLLER LAB


Expt. No.3 MULTIPLICATION AND DIVISION OF 8-BIT NUMBERS

Objective: To multiply and divide two 8-bit numbers saved in 4200H and 4201H and save the results in internal RAM location 30H onwards Equipments Required: Microcontroller Kit Theory: To perform 8-bit multiplication and division, the instructions MUL AB and DIV AB can be used. Flowchart: a) 8-bit multiplication
Start Move multiplicand to A Move multiplier to B Multiply A and B content

Store the lower byte in A and upper byte in B of the product to the destination Stop

b) 8-bit division
Start Move dividend to A Move divisor to B Divide A and B content

Store the quotient in A and reminder in B to the destination Stop Department of ECE, VKCET Page 22

08.607 MICROCONTROLLER LAB


Program: ADDRESS 4100 4103 4104 4105 4106 4107 4109 410A 410B 410C 410E 410F 4110 4112 4113 4115 4116 4117 4118 4119 411B STOP LABEL MAIN OPCODE & OPERANDS 90 E0 F9 A3 E0 F5 FA E9 A4 78 F6 08 A6 E9 8A 84 08 F6 08 A6 80 F0 FE F0 F0 30 F0 42 00 MNEMONICS MOV DPTR,#4200 MOV R1,A INC DPTR MOVX A, @DPTR MOV B,A MOV R2,A MOV A,R1 MUL AB MOV R0,#30 MOV @R0,A INC R0 MOV @R0,B MOV A,R1 MOV B,R2 DIV AB INC R0 MOV @R0,A INC R0 MOV @R0,B SJMP STOP COMMENTS

Load starting address of external RAM where data are saved to data pointer MOVX A,@DPTR Move multiplicand/dividend to A Save multiplicand/dividend to R1 Increment dptr to point multiplier/divisor Move multiplier/divisor to A Move multiplier/divisor to B Save multiplier/divisor to R2 Move saved multiplicand to A Perform multiplication, results A = Lower byte of product and B = Upper byte of the result Load internal RAM scratch pad address to R0 as data pointer Save lower byte of the product to internal RAM Increment R0 to point next location Save upper byte of the product to internal RAM Move dividend to A Move divisor to B Perform division, results A = quotient and B = reminder Increment R0 to point next location to store result Store quotient Increment R0 to point next location to store result Store reminder Halt the program

Department of ECE, VKCET

Page 23

08.607 MICROCONTROLLER LAB


Procedure: Steps 1. 2. 3. 4. 5. 6. 7. Enter the program (starting address 4100H) Reset the kit Enter the input (starting address 4200H) Reset the kit Execute the program Reset the kit Observe the outputs in Internal RAM

Department of ECE, VKCET

Page 24

08.607 MICROCONTROLLER LAB


Expt. No.4 MULTIPLICATION BY SHIFT AND ADD METHOD

Objective: To multiply two 4-bit numbers saved in 4200H and 4201H without using MUL AB instruction and store the result in 4102H. Equipments Required: Microcontroller Kit Theory: Shift-and-add multiplication is similar to the multiplication performed by paper and pencil. This method adds the multiplicand X to itself Y times, where Y denotes the multiplier. To multiply two numbers by paper and pencil, the algorithm is to take the digits of the multiplier one at a time from right to left, multiplying the multiplicand by a single digit of the multiplier and placing the intermediate product in the appropriate positions to the left of the earlier results. As an example, consider the multiplication of two unsigned 4-bit numbers, 8 (1000b) and 9 (1001b).

Thus the multiplication can be performed by shifting and adding method. Shifting multiplier by one bit left and if the MSB is high, performs addition between product (intermediate) and multiplicand followed by shift. If MSB is low perform shifting only and the process continues for 2n times, where n is the number of bits in multiplier and multiplicand. The main advantage of this type process is its faster operation for large number of bit multiplication. In general the multiplication require n-bit multiplicand by n-bit multiplier require 2n registers to hold numbers and product. And require 2n-bit adders and shifters. An e.g. 4 bit multiplicand x 4-bit multiplier results 8-product and require 8-bit registers to hold data.

Department of ECE, VKCET

Page 25

08.607 MICROCONTROLLER LAB


Flowchart: Start Assign a register for 8-bit product = 0 Assign a register for counter = 8 (for shifting) Store multiplicand to a register, where upper nibble as 0 Store multiplier to two registers, where upper nibble as 0 Shift product to left by 1 bit Shift multiplier to left by 1 bit

No Is carry for multiplier? Yes Product = Product + Multiplicand Count = Count -1

No

If count = 0 Yes Store the product as result Stop

pr

Department of ECE, VKCET

Page 26

08.607 MICROCONTROLLER LAB


Program: ADDRESS 4100 4102 4104 4107 4108 410A 410B 410C 410D 410F 4110 4111 4112 4114 4115 4116 4117 4119 411A 411C 411D 411E 411F 4121 4122 4123 4124 NOADD NEXT LABEL MAIN OPCODE & OPERANDS 78 7B 90 E0 54 F9 A3 E0 54 FA E8 23 C2 F8 EA 33 C2 FA 50 E8 29 F8 DB A3 E8 F0 80 EF 03 E0 E0 0F 0F 00 08 42 00 MNEMONICS MOV R0,#00 MOV R3,#08 MOV DPTR,#4200 MOVX A,@DPTR ANL A,#0F MOV R1,A INC DPTR MOVX A,@DPTR ANL A,#0F MOV R2,A MOV A,R0 RL A CLR ACC.0 MOV R0,A MOV A,R2 RLC A CLR ACC.0 MOV R2,A JNC NOADD MOV A,R0 ADD A,R1 MOV R0,A DJNZ R3,NEXT INC DPTR MOV A,R0 MOVX @DPTR,A SJMP HERE COMMENTS Clear product register Set counter = 8 Point external memory for multiplicand Load multiplicand to Acc. Mask upper nibble to ensure 4-bit multiplicand Store multiplicand Point multiplier in external memory Load multiplier to Acc. Mask upper nibble to ensure 4-bit multiplier Store multiplier Load product to Acc. Rotate product left by one bit Clear LSB of product (for shifting operation) Store shifted product Load multiplier to Acc. Rotate multiplier through carry Clear LSB of multiplier (for shifting operation) Store shifted multiplier If no carry in the shifting operation skip addition, else add product and multiplicand Load product to Acc. Add product and multiplicand (8bit addition) Store result to product Decrement counter by one and if not equal to zero go to next shiftadd operation, else store the result Increment pointer to store result Load the product to Acc. Store product to external memory Halt
Page 27

HERE

FE

Department of ECE, VKCET

08.607 MICROCONTROLLER LAB


Procedure: Steps 1. 2. 3. 4. 5. 6. 7. Enter the program (starting address 4100H) Reset the kit Enter the input (starting address 4200H) Reset the kit Execute the program Reset the kit Observe the outputs from 4202H

Department of ECE, VKCET

Page 28

08.607 MICROCONTROLLER LAB


Expt. No.5 LCM AND HCF OF TWO 8-BIT NUMBERS

Objective: To find LCM and HCF of two 8-bit numbers stored in 4200H and 4201H and store the result in 4202H and 4203H Equipments Required: Microcontroller Kit Theory: The least common multiple (also called the lowest common multiple or smallest common multiple) of two integers a and b, usually denoted by LCM (a, b), is the smallest positive integer that is a multiple of both a and b. An example: The LCM of 4 and 6: Multiples of 4 are: 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 60, 64, 68, 72, 76 And the multiples of 6 are: 6, 12, 18, 24, 30, 36, 42, 48, 54, 60, 66, 72... Common multiples of 4 and 6 are simply the numbers that are in both lists: 12, 24, 36, 48, 60, 72... So the least common multiple of 4 and 6 is the smallest one of those 12 The highest common factor (HCF), also known as the greatest common factor (GCF), or greatest common divisor (GCD), of two or more non-zero integers, is the largest positive integer that divides the numbers without a remainder An example: The number 54 can be expressed as a product of two other integers in several different ways: Thus the divisors of 54 are: Similarly the divisors of 24 are: The numbers that these two lists share in common are the common divisors of 54 and 24: The greatest of these is 6. That is the HCF of 54 and 24. One writes:

Department of ECE, VKCET

Page 29

08.607 MICROCONTROLLER LAB


Flowchart:
Start Assign a register for number1 Assign a register for number2 Assign a register for LCM and LCM = number1 Divide LCM by number2 No LCM = number1 + LCM Yes Assign a register for HCF HCF = number2 Is reminder = 0?

Divide number1 by number2 No number1 = HCF number2 = reminder Yes Store the results LCM and HCF Stop Is reminder = 0?

Department of ECE, VKCET

Page 30

08.607 MICROCONTROLLER LAB


Program: ADDRE LABEL SS 4100 4103 4104 4105 4106 4107 4108 4109 410A 410C 410D 410F 4111 4112 4113 4114 4116 4117 4118 411A 411B 411C 411E 4120 4121 4122 4123 4125 4126 4127 4128 4129 412A 412B MAIN

OPCODE & OPERANDS 90 E0 F8 A3 E0 F9 E8 FA 89 84 E5 60 EA 28 FA 80 E9 FB 89 E8 84 E5 60 F9 EB F8 80 EA A3 F0 A3 EB F0 80 42 00

MNEMONICS MOV DPTR,#4200 MOVX A,@DPTR MOV R0,A INC DPTR MOVX A,@DPTR MOV R1,A MOV A,R0 MOV R2,A MOV B,R1 DIV AB MOV A,B JZ HCF MOV A,R2 ADD A,R0 MOV R2,A SJMP LCM MOV A,R1 MOV R3,A MOV B,R1 MOV A,R0 DIV AB MOV A,B JZ RESULT MOV R1,A MOV A,R3 MOV R0,A SJMP HCF MOV A,R2 INC DPTR MOVX @DPTR,A INC DPTR MOV A,R3 MOVX @DPTR,A SJMP STOP

COMMENTS Point external memory for inputs Load number1 to Acc. Save number1 to R0 Point number2 Load number2 to Acc. Save number2 to R1 Load number1 to Acc. Set R2 for LCM and set number1 as LCM Load number2 to B Divide number1 by number2 Move B (reminder) to Acc. If Acc = 0, go to find HCF, else next step Move LCM to Acc. Acc = number1 + LCM Store Acc to R2 (as LCM) Go to next check Move number2 to Acc. Set R3 for HCF and set number2 as HCF Load number2 to B Move number1 to Acc. Divide number1 by number2 Move B (reminder) to Acc. If Acc. = 0, go to store result, else next step number2 = reminder Move HCF to Acc. number1 = HCF Go to next check Load LCM to Acc. Point the external memory location for storing LCM Store LCM Point the external memory location for storing HCF Load HCF to Acc. Store HCF Halt the program
Page 31

LCM

F0 F0 05

NEXT

F4

HCF

F0

F0 05

F1

RESULT

STOP

FE

Department of ECE, VKCET

08.607 MICROCONTROLLER LAB


Procedure: Steps 1. 2. 3. 4. 5. 6. 7. Enter the program (starting address 4100H) Reset the kit Enter the inputs (starting address 4200H) Reset the kit Execute the program Reset the kit Observe the outputs from 4202H

Department of ECE, VKCET

Page 32

08.607 MICROCONTROLLER LAB


Expt. No.6 REALIZATION OF BOOLEAN EXPRESSION

Objective: To realize Boolean expressions using 8051 ports Equipments Required: 1. Microcontroller Kit (Vi89C51) 2. Flat Ribbon Cable (FRC) 1 No. Theory: Boolean expressions can be represented by either min-term (SOP) or max-term (POS). In general SOP form is represented as Where a,b,c, are input variables and xk.mk are min-terms An example: Four variable systems,

In Boolean expression

Using logical instructions these expressions can easily implemented. Similarly POS form can be represented as

For the above example, in Boolean expression

Again this will also implemented by logical instructions. Boolean expressions to implement using Vi89C51 kit: Consider the Boolean expression

The kit (Vi89C51) Port 1 digital IO lines can be used as input and Port 0 digital IO lines can be used as output. In which the Port 0 is used as memory mapped IO lines with digital input of address FF22H and digital output of address FF23H.
Department of ECE, VKCET Page 33

08.607 MICROCONTROLLER LAB


The headers P17 and P18 can be used to connect switch SW8 - SW15 into Port 1 pins P1.0 P1.7. The connections created by FRC are as given below: SW15 to P1.0, SW14 to P1.1, SW13 to P1.2 . SW8 to P1.7 Then we can assign the input variables as switches; SW15 as d, SW14 as c, SW13 as b, SW12 as a Memory mapped digital output with address FF23H line is connected with 8 LEDs L17 to L24 and if we write some data from Acc. to this address cause enable/disable the LEDs. The mapping of the Acc. bits and LEDs are as follows: After writing the data from Acc to LEDs, the status of LEDs are as follows: L24 Acc.0, L23 Acc.1, L22 Acc.2, .L17 Acc.7 Therefore for the given Boolean expression assign LED L24 as output f , thus we have to create the status of expression in Acc.0 Truth table Inputs a b c 0 0 0 0 0 0 0 0 1 0 0 1 0 1 0 0 1 0 0 1 1 0 1 1 1 0 0 1 0 0 1 0 1 1 0 1 1 1 0 1 1 0 1 1 1 1 1 1

d 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1

Output f 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1

Department of ECE, VKCET

Page 34

08.607 MICROCONTROLLER LAB


Flowchart:
Start
Point digital output LEDs, DPTR FF23H, and clear L24

Set Port 1 as input Read P1.0, P1.1, P1.2 and P1.3 Perform abcd, abcd ,and abcd and save the result in bit addressable RAM area Perform abcd + abcd + abcd and save the status Point digital output LEDs, DPTR FF23H Send the status of Boolean expression to digital output LED

Department of ECE, VKCET

Page 35

08.607 MICROCONTROLLER LAB


Program: ADDRESS 4100 4103 4104 4105 4108 410A 410B 410D 410F 4111 4113 4115 4117 4119 411B 411D 411F 4120 4122 4124 4126 4128 412A 412B 412C 412D REPEAT LABEL MAIN OPCODE & OPERANDS 90 E4 F0 75 A2 B3 B0 B0 B0 92 A2 B0 B0 82 92 82 B3 82 B0 82 72 72 E4 33 F0 80 91 92 93 00 90 91 92 93 01 90 91 92 93 01 00 90 90 FF FF 23 MNEMONICS MOV DPTR,#FF22 CLR A MOVX @DPTR,A MOV P1,#FF MOV C,90 CPL C ANL C,/91 ANL C,/92 ANL C,/93 MOV 00,C MOV C,90 ANL C,/91 ANL C,/92 ANL C,93 MOV 01,C MOV C,90 CPL C ANL C,91 ANL C,/92 ANL C,93 ORL C,01 ORL C,00 CLR A RLC A MOVX @DPTR,A SJMP REPEAT COMMENTS Point the address of memory mapped IO digital output LED L24, DPTR FF23H A 00 [DPTR] A P1 FFH , to set as all pins as input CY d, where 90 is direct address of P1.0 CY d CY cd, where 91 is direct address of P1.1 CY bcd, where 92 is direct address of P1.2 CY abcd, where 93 is direct address of P1.3 [00] abcd CY d CY cd CY bcd CY abcd [01] abcd CY d CY d CY cd CY bcd CY abcd CY abcd + abcd CY abcd + abcd + abcd A 00 Acc.0 abcd + abcd + abcd [DPTR] A Repeat the process

Department of ECE, VKCET

Page 36

08.607 MICROCONTROLLER LAB

Procedure: Steps 1. 2. 3. 4. 5.

Connect P17 and P18 using FRC Switch on the system Enter the program (starting address 4100H) Terminate from program edit mode Execute program 6. Give the inputs a, b, c and d using switches SW15, SW14, SW13 and SW12 respectively 7. Observe the output by LED L24 and verify the truth table

Department of ECE, VKCET

Page 37

08.607 MICROCONTROLLER LAB


Expt. No.7 SORTING NUMBERS AND FACTORIAL OF A NUMBER

Objective: a) To sort n numbers stored in external RAM (starting address 4201H) in ascending order, were n is stored 4200H external RAM location b) To find factorial of an 8 bit number stored in 4400H (external RAM) and store 8 bit result in 4401H Equipments Required: 1. Microcontroller Kit Theory: Sorting numbers: There are different algorithms for sorting array of numbers. One simple method is Bubble sort and is a simple sorting algorithm that works by repeatedly stepping through the list to be sorted, comparing each pair of adjacent items and swapping them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted. Although the algorithm is simple, it is not efficient for sorting large lists; other algorithms like are better. A step by step example is shown below: Let us take the array of numbers "5 4 2 8 1", and sort the array from lowest number to greatest number using bubble sort algorithm. In each step, elements written in bold are being compared. To sort n numbers, n-1 passes are required First Pass: (54281) ( 4 5 2 8 1 ), Here, algorithm compares the first two elements, and swaps them. (45281) ( 4 2 5 8 1), Swap since 5 > 2 (42581) ( 4 2 5 8 1 ),No Swap since 8 > 5 (42581) ( 4 2 5 1 8 ), Swap since 8>1 Second Pass: (42518) ( 2 4 5 1 8 ), Swap since 4 > 2 (24518) ( 2 4 5 1 8 ), No swap since 5 > 4 (24518) ( 2 4 1 5 8 ), Swap since 5 > 1 (24158) ( 2 4 1 5 8 ), No swap since 8 > 5 Third Pass: (24158) ( 2 4 1 5 8 ), No swap since 4 > 2 (24158) ( 2 1 4 5 8 ), Swap since 4 > 1 (21458) ( 2 1 4 5 8 ), No swap since 5 > 4 (21458) ( 2 1 4 5 8 ), No swap since 8 > 5 Fourth Pass: (21458) ( 1 2 4 5 8 ), Swap since 2 > 1 (12458) ( 1 2 4 5 8 ), No swap (12458) (12458) (12458) (12458) The disadvantage of this sorting is all pass must perform whether elements are in order or not.
Department of ECE, VKCET Page 38

08.607 MICROCONTROLLER LAB


Factorial : The factorial of a non-negative integer n, denoted by n!, is the product of all positive integers less than or equal to n. For example, The value of 0! is 1 Factorial function is defined as

Department of ECE, VKCET

Page 39

08.607 MICROCONTROLLER LAB


Flowcharts a) Sorting numbers in ascending order
Start DPTR 4201H Counter1, R0 n-1 and counter2 R1 n-1 R2 [DPTR] DPTR = DPTR + 1 A [DPTR] No If R2 > A ? Yes [DPTR] R2 DPTR = DPTR - 1 [DPTR] A DPTR = DPTR + 1 Counter1, R0 = R0 -1 Yes If R0 > 0 ? No Counter2, R1 = R1 -1 Yes If R1 > 0 ? No Department of ECE, VKCET Stop Page 40

08.607 MICROCONTROLLER LAB


b) Factorial
Start R0 n R1 1 Factorial R2 1 Yes If R0 = 0 or 1? No A R0 B R1 Multiply A and B R2 A R0 A R1 = R1 + 1 No If R1= R0? Yes Save R2 content as result Stop

Department of ECE, VKCET

Page 41

08.607 MICROCONTROLLER LAB


Programs: a) Sorting numbers ADDRESS 4100 4103 4104 4105 4106 4107 4108 4109 410A 410B 410C 410D 410E 4110 4111 4112 4113 4115 4116 4117 4118 411A 411C STOP SWAPING LABEL MAIN OPCODE & OPERANDS 90 E0 14 F8 F9 A3 E0 FA A3 E0 C3 9A 50 E0 CA F0 15 EA F0 A3 D8 D9 80 08 42 00 MNEMONICS MOV DPTR,#4200 MOVX A,@DPTR DEC A MOV R0,A MOV R1,A INC DPTR MOVX A,@DPTR MOV R2,A INC DPTR MOVX A,@DPTR CLR C SUBB A,R2 JNC NOSWAP MOVX A,@DPTR XCH A,R2 MOVX @DPTR,A DEC DPL MOV A,R2 MOVX @DPTR,A INC DPTR DJNZ R0,NEXT DJNZ R1,MAIN SJMP STOP COMMENTS DPTR 4200H A [DPTR], n An1 R0 n 1, counter1 R1 n 1, counter2 DPTR = DPTR + 1 A [DPTR] R2 A DPTR DPTR + 1 A [DPTR] CY 0 A A R2 - CY If no carry, go to label NOSWAP A [DPTR] A R2 [DPTR] A DPL = DPL -1 A R2 [DPTR] A DPTR = DPTR + 1 R0 = R0 -1 and if R0 0 go to label NEXT for sorting R1 = R1 1 and if R1 0 go to label MAIN for next pass Stop sorting

NEXT

82

NOSWAP

EE E4 FE

Department of ECE, VKCET

Page 42

08.607 MICROCONTROLLER LAB


b) Factorial of a number ADDRESS 4100 4103 4104 4105 4107 4109 410B 410E 4110 4113 4115 4116 4118 4119 411A 411B 411C 411D 4120 4121 4122 4123 RESULT CONTINUE NEXT LABEL MAIN OPCODE & OPERANDS 90 E0 F8 F5 79 7A B4 80 B4 80 E8 89 A4 FA F8 09 E9 B5 A3 EA F0 80 30 01 01 00 10 01 0B 02 02 44 00 MNEMONICS MOV DPTR,#4400 MOVX A,@DPTR MOV R0,A MOV 30,A MOV R1,#01 MOV R2,#01 CJNE A,#00, NEXT SJMP RESULT CJNE A,#01,CONTINUE SJMP RESULT MOV A,R0 MOV B,R1 MUL AB MOV R2,A MOV R0,A INC R1 MOV A,R1 F5 CJNE A,30,CONTINUE INC DPTR MOV A,R2 MOVX @DPTR,A SJMP STOP COMMENTS DPTR 4400H, address of number n A [DPTR] R0 A, save the number [30H] A, save the number to compare R1 01 R2 01 If A 0, go to the label NEXT Go to the label RESULT to store the result If A 1, go to the label CONTINUE Go to the label RESULT to store the result A R0 B R1 BA A x B R2 A R0 A R1 = R1 +1 A R1 If A n go to label continue DPTR = DPTR +1 A R2 [DPTR] A Stop the process

F0

30

STOP

FE

Department of ECE, VKCET

Page 43

08.607 MICROCONTROLLER LAB


Procedure: a) Sorting: Steps 1. 2. 3. 4.

Enter the program (starting address 4100H) Terminate from program enter mode Enter the total numbers (in 4200H) and numbers (from 4201H) Terminate from edit mode 5. Execute the program 6. Reset the kit 7. Verify the result b) Factorial: Steps 1. Enter the program (starting address 4100H) 2. Terminate from program enter mode 3. Enter the number (in 4400H) 4. Terminate from edit mode 5. Execute the program 6. Reset the kit 7. Verify the result

Department of ECE, VKCET

Page 44

08.607 MICROCONTROLLER LAB


Expt. No.8 INTERFACING WITH 8-BIT ADC Objective: To interface with 8-bit ADC and display digital data from ADC using LEDs Equipments Required: 1. Microcontroller Kit 2. ADC 0809 interfacing card 3. Flat ribbon cable (50 pin) 4. Connecting probes 5. Multimeter Theory: ADC Interface card An analog-to-digital converter (ADC, A/D or A to D) is a device that converts a continuoustime signal to a discrete-time digital representation. Typically, an ADC is an electronic device that converts an input analog voltage or current to a digital number proportional to the magnitude of the voltage or current. A simple notation for ADC operation is shown in figure 1.

Figure 1 The interface card contains ADC0809 chip. ADC0809 Some features of the ADC0809 chip are: 6. Easy to interface 7. Successive approximation type 8. 8-analog input channels selected by 3-bit address lines 9. 0-5V input range with single 5V Vcc 10. TTL output voltage 11. 8-bit resolution 12. Maximum conversion time is 100s
Department of ECE, VKCET Page 45

08.607 MICROCONTROLLER LAB


Pinout diagram

Pins and functions Pins IN0-IN7 2-8 2-1 ADD C, ADD B and ADD A

ALE START EOC OUTPUT ENABLE CLOCK Vcc GND Vref(+) Vref(-)

Functions 8 analog input channels 8-bit digital data output lines 3-bit address lines ADD C ADD B ADD A Selected analog lines 0 0 0 IN0 0 0 1 IN1 0 1 0 IN2 0 1 1 IN3 1 0 0 IN4 1 0 1 IN5 1 1 0 IN6 1 1 1 IN7 Address latch enable, if it is a HIGH to LOW signal valid address in the address bits are latched to the ADC Start of conversion, input signal to ADC. If it is a high signal starts ADC End of conversion, output signal from ADC. If it is a high signal conversion is over and data bus has valid data Input signal to ADC, to enable ADC output data Clock signal, its frequency determines sampling rate, maximum frequency is 1280 kHz +5V power supply Ground +ve reference voltage (Vref1) -ve reference voltage (Vref2)
Page 46

Department of ECE, VKCET

08.607 MICROCONTROLLER LAB


Timing diagram

Timing characteristics f, clock frequency: Typically 640 kHz tc, conversion time: Typically 100s tWS, start pulse width: Typically 100ns tWALE, ALE pulse width: Typically 100ns ts, address set up time: Typically 25ns tH, address hold time: Typically 25ns tEOC, EOC delay time: Typically 8+2 s clock periods Connection details and memory mapped IO address Using 50 pin FRC connect microcontroller kit to interface card as shown in figure 3:

Figure 3
Department of ECE, VKCET Page 47

08.607 MICROCONTROLLER LAB


The clock frequency to ADC is derived from master clock of kit 11.0592 MHz The memory mapped IO address for the signals of ADC0809 chip are: Pins D7-D0 ADD A, ADD B, ADD C ALE and OE SOC EOC Address FFC0H FFC8H FFD0H FFD8H

The data in Accumulator for each memory mapped IO operation: Address FFC0H FFC8H Accumulator Data ADC digital output IN0 select, ALE low 0001 0000b and OE high = 10H IN0 select, ALE high 0001 1000b and OE high = 18H IN1 select, ALE low 0001 0001b and OE high = 11H IN1 select, ALE high 0001 1001b and OE high = 19H SOC low 00H SOC high 01H EOC low 00H EOC high 01H

FFD0H FFD8H

The interface card has jumpers and its details are: Jumper 2 (J2): For selecting SOC source

Note: Place J2 in A position for SOC from software running by 8051


Department of ECE, VKCET Page 48

08.607 MICROCONTROLLER LAB


Jumper 5 (J5): To connect onboard trim pot PT1 (Potentiometer) to any of the mentioned channels:

Note: To connect trim pot variable output to channel IN0, place J5 to A position There is a header P2, in which all channels IN0 to IN7 are terminated as shown below:

Department of ECE, VKCET

Page 49

08.607 MICROCONTROLLER LAB


Flowchart For converting analog signal from channel IN0 and display digital data to LEDs (L17 to L24 in Vi89C51 kit and L12 to L19 in Micro-51 kit)
Start ALE = 0, C B A = 0 0 0, OE = 1 ALE = 1, C B A = 0 0 0, OE = 1 ALE = 0, C B A = 0 0 0, OE = 1 SOC = 1 SOC = 0 No Yes Read D0-D7, A Digital data LEDs A

If EOC = 1?

Department of ECE, VKCET

Page 50

08.607 MICROCONTROLLER LAB


Program: ADDRESS 4100H 4103 4105 4106 4108 41009 410B 410C 410F 4111 4112 4113 4114 4117 4118 411B 411E 411F 4122 4123 WAIT LABEL MAIN OPCODE & OPERANDS 90 74 F0 74 F0 74 F0 90 74 F0 E4 F0 90 E0 30 90 E0 F0 F0 80 DB FF 23 E0 FF FC C0 FF D8 FF 01 D0 18 18 FF 10 C8 MNEMONICS MOV DPTR,#FFC8 MOV A,#10 MOVX @DPTR,A MOV A,#18 MOVX @DPTR,A MOV A,#10 MOVX @DPTR,A MOV DPTR,#FFD0 MOV A,#01 MOVX @DPTR,A CLR A MOVX @DPTR,A MOV DPTR,#FFD8 MOVX A,@DPTR JNB E0, WAIT MOV DPTR,#FFC0 MOVX A,@DPTR MOV DPTR,#FF23 MOVX @DPTR,A SJMP MAIN COMMENTS DPTR FFC8H, memory mapped IO address of ALE, address bit and OE pins A 0001 0000b, OE =1 ALE = 0 and C B A = 000 [FFC8] A A 0001 1000b, OE =1 ALE = 1 and C B A = 000 [FFC8] A A 0001 0000b, OE =1 ALE = 0 and C B A = 000 [FFC8] A DPTR FFC8H, memory mapped IO address of SOC A 0000 0001b, SOC = 1 [FFD0] A A 0000 0000b, SOC = 0 [FFD0] A DPTR FFD8H, memory mapped IO address of EOC [FFD8] c A If Acc.0 (address E0) is 0, go to label WAIT DPTR FFC0H, memory mapped IO address of data D7-D0 A [FFC0] DPTR FF23H, memory mapped IO address of LEDs L17 to L24 [FF23] A Repeat the process

Department of ECE, VKCET

Page 51

08.607 MICROCONTROLLER LAB


Procedure: Steps 1. Connect the ADC interface card and Microcontroller kit using 50-pin FRC 2. Turn on power supply 3. Enter the program (starting address 4100H) 4. Terminate from program enter mode 5. Check the position of jumpers J2 and J5 in A 6. Execute the program 7. Vary the trim pot and observe the digital output in LEDs (Both interfacing card and kit) 8. Measure the varying voltage across trim pot PT1 using multimeter as shown below and observe the data

Observation table Input voltage (V) 0 1 2 3 4 5 Output data (in binary format)

Department of ECE, VKCET

Page 52

08.607 MICROCONTROLLER LAB


Expt. No.9 SQUARE, SQUARE ROOT OF A NUMBER AND FIBONACCI SERIES

Objective: a) To find square and square root of a number stored in 4200H (external RAM) and store the results in 4201H and 4202H for square and 4203H for square root b) To find first n numbers of Fibonacci series and store the result in external RAM location starting from 4300H, where n is stored in 4200H. Equipments Required: Microcontroller Kit Theory: Square and square root: Square of a number can be obtained by multiplying with the same number. Square root can be obtained by dividing the number with non zero positive integers, until the quotient and divisor is equal. If quotient is less than divisor, the quotient is the fractional part of the square root of the number. Fibonacci series: Fibonacci numbers are the numbers in the following integer sequence: By definition, the first two numbers in the Fibonacci sequence are 0 and 1, and each subsequent number is the sum of the previous two. In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence relation with seed values Thus Fibonacci numbers are the series of numbers F0, F1, ..., given by

Department of ECE, VKCET

Page 53

08.607 MICROCONTROLLER LAB


Flowcharts a) Square and square root of a number Start DPTR 4200H A [DPTR], B A, and R0 A, number Perform A x B [DPTR] A, least byte of square DPTR = DPTR + 1 R2 01, divisor for square root A R0, number as dividend B R2 Perform A / B R3 A, quotient as square
Yes

If A > R2
No Yes

If A = R2 ?
No

DPTR = DPTR +1 [DPTR] R3, result Stop

R2 = R2 +1

Department of ECE, VKCET

Page 54

08.607 MICROCONTROLLER LAB


b) Fibonacci series Start DPTR = 4200H R0 [DPTR], length of sequence as counter
DPTR = 4300H [DPTR] 0 DPTR = DPTR +1 R0 = R0 - 1

[DPTR] 1 R0 = R0 - 1
DPTR = DPTR 1 R1 [DPTR]

DPTR = DPTR + 1 A [DPTR] A A + R1 DPTR = DPTR + 1 [DPTR] A R0 = R0 - 1


No

If R0 = 0?
Yes

Stop

Department of ECE, VKCET

Page 55

08.607 MICROCONTROLLER LAB


Programs: a) Square and square root of a number ADDRESS 4100 4103 4104 4106 4107 4108 4109 410A 410C 410D 410E 4110 4111 4113 4114 4115 4116 4117 4119 411A 411B 411C 411E 4120 4121 4123 STOP RESULT REPEAT LABEL MAIN OPCODE & OPERANDS 90 E0 F5 F8 A4 A3 F0 E5 A3 F0 7A E8 8A 84 FB C3 9A 50 A3 EB F0 80 60 0A 80 80 ED FE 05 01 F0 F0 F0 42 00 MNEMONICS MOV DPTR,#4200 MOVX A,@DPTR MOV B,A MOV R0,A MUL AB INC DPTR MOVX @DPTR,A MOV A,B INC DPTR MOVX @DPTR,A MOV R2,#01 MOV A,R0 MOV B,R2 DIV AB MOV R3,A CLR C SUBB A,R2 JNC NEXT INC DPTR MOV A,R3 MOVX @DPTR,A SJMP STOP JZ RESULT INC R2 SJMP REPEAT SJMP STOP COMMENTS DPTR 4200H, pointer for number A [DPTR] BA R0 A BA A x B DPTR DPTR + 1 [DPTR] A, least byte of square AB DPTR DPTR + 1 [DPTR] A, most byte of square R2 01, divisor for finding square root A R0, number B R2, divisor A / B, quotient in A and reminder in B R3 A, quotient as square root Clear CY flag A A R2, comparing quotient and divisor If A > R2, go to label NEXT, else next instruction DPTR DPTR +1 A R3, quotient is the result [DPTR] A, save the result Go to label STOP If A = R2, go to label RESULT, else next instruction R2 R2 + 1, divisor is incremented by one Go to label REPEAT for next checking Stop the process
Page 56

05 F9

NEXT

Department of ECE, VKCET

08.607 MICROCONTROLLER LAB


b) Fibonacci series ADDRESS 4100 4103 4104 4105 4107 410A 410B 410C 410D 410E 410F 4110 4112 4113 4114 4115 4116 4117 4118 4119 411B NEXT LABEL MAIN OPCODE & OPERANDS 90 E0 F8 74 90 F0 18 A3 04 F0 18 15 E0 F9 A3 E0 29 A3 F0 D8 80 F5 FE 82 00 43 00 42 00 MNEMONICS MOV DPTR,#4200 MOVX A,@DPTR MOV R0,A MOV A,#00 MOV DPTR,#4300 MOVX @DPTR,A DEC R0 INC DPTR INC A MOVX @DPTR,A DEC R0 DEC DPL MOVX A,@DPTR MOV R1,A INC DPTR MOVX A,@DPTR ADD A,R1 INC DPTR MOVX @DPTR,A DJNZ R0,NEXT SJMP STOP COMMENTS DPTR 4200H, pointer to the length of the series A [DPTR] R0 A, length as A 0, first number DPTR 4300H, pointer to the starting address of destination [DPTR] A R0 R0 1 , decrement counter by one DPTR DPTR + 1 A A + 1, second number [DPTR] A R0 R0 1 , decrement counter by one DPTR DPTR - 1 A [DPTR], previous number R1 A DPTR DPTR + 1 A [DPTR], current number A A + R1, next number DPTR DPTR + 1 [DPTR] A Decrement counter by one and if it is 0, go to label NEXT, else next instruction Stop process

Department of ECE, VKCET

Page 57

08.607 MICROCONTROLLER LAB


Procedure: a) Square and square root: Steps 1. Enter the program (starting address 4100H) 2. Terminate from program enter mode 3. Enter the number (in 4200H) 4. Terminate from edit mode 5. Execute the program 6. Reset the kit 7. Verify the result in 4201H and 4202H for square, and 4203H for square root b) Fibonacci series: Steps 1. Enter the program (starting address 4100H) 2. Terminate from program enter mode 3. Enter the length of the series (in 4200H) 4. Terminate from edit mode 5. Execute the program 6. Reset the kit 7. Verify the result in 4300H onwards

Department of ECE, VKCET

Page 58

08.607 MICROCONTROLLER LAB


Expt. No.10 8-BIT DAC INTERFACE

Objective: To interface 8-bit DAC and generate square, rectangular, saw tooth, and triangular waves. Equipments Required: Microcontroller Kit DAC 0800 Interface card 1 No. Flat ribbon cable (51 pin) 1 No. CRO/DSO 1 No. CRO probe 1 No. Theory: DAC interface card DAC is the counterpart of ADC that converts a digital (usually binary) code to an analog signal. It converts a fixed point binary into corresponding voltage or current. The simplified functional unit of 8-bit DAC is shown in figure 1.

Figure 1 Two types of DAC are binary weighted and R/2R ladder type and output may be voltage or current. In both cases output is a function of reference voltage/current and digital input code. An ideally sampled signal and DAC output corresponding to digital code of each sample is shown in figure 2 and figure 3.

Ideally sampled signal


Department of ECE, VKCET Page 59

08.607 MICROCONTROLLER LAB


Figure 2

DAC output Figure 3 DAC0800 Some features of DAC0800 chip are: 1. 8-bit R/2R ladder, current output type fast DAC 2. Interface directly with TTL, CMOS, etc type devices 3. Complementary current outputs 4. Wide power supply range: 4.5V to 18V 5. Low power consumption: 33 mW at 5V 6. Low cost Pin out diagram

Figure 4

Department of ECE, VKCET

Page 60

08.607 MICROCONTROLLER LAB


Pins and functions Pins VLC Iout and

Functions Threshold control, usually connected to ground

Non-inverting and inverting output current B1(MSB) to B8(LSB) V+ and VVref(+) and Vref(-) Compensation Output current Where Digital input Positive and negative power supplies Reference voltages which determines the full scale output current Compensation capacitor

DAC0800 interface card contains two DACs and I-V converters to convert the current output to voltage and DAC-1 circuit is shown in figure 5

Figure 5
Department of ECE, VKCET Page 61

08.607 MICROCONTROLLER LAB


From the circuit, Iref = Vref / R14 = (5.6 + 3.9+0.7) / 5.1k = 2mA Then the full scale output current Iout = 2xIref x 255/256 = 3.984mA Full scale output voltage at DAC1 is Vout = Iout x R5 8.76 V Digital inputs and corresponding output current (approximate) is shown in the table:

Connection details and memory mapped IO address

The memory mapped IO addresses for DAC interface card are: DAC 1 data bus: FFC0H DAC 2 data bus: FFC8H

Department of ECE, VKCET

Page 62

08.607 MICROCONTROLLER LAB


Details of header P2: To take output from DAC1 and DAC2

Wave form generation: Square wave and rectangular generation: To generate square wave using DAC is by sending a data corresponding to peak amplitude and zero continuously. This method gives square wave with maximum frequency. We can reduce the frequency by including delay program and also rectangular wave is generated by different on and on periods. Saw tooth wave generation: To generate saw tooth wave using DAC is by sending data from 00H to FFH continuously with one step size. The frequency and peak amplitude of the signal are maximum value. Frequency can be changed by including required delay between each step. Peak voltage of the wave is reduced by changing the maximum data. Triangular wave generation: To generate triangular wave send data from 00H to FFH for rising and FFH to 00H for falling with one step size. Similar to other waves, the frequency and amplitude can change. Delay program The simple delay program is software delay, which is the program to kill the time using counters and decrement count value. Consider the following program sub-program: DELAY: MOV R7, #N ; Take 1MC WAIT: DJNZ R7, WAIT ; Take 2MC x N RET ; Take 2MC Total time to execute this subprogram is Tdelay = 1MC + 2MC x N + 2MC = 3MC + 2MC x N The kit has 11.0592 MHz crystal frequency for microcontroller, then 1MC = 12 x T = 1.0851s Then maximum delay by the program is by N=255 and is 556.65 s For a standard delay program of 500s, put Tdelay = 500 s, then N = 228.89 228 = E4H Similarly any standard delay program can be implemented.

Department of ECE, VKCET

Page 63

08.607 MICROCONTROLLER LAB


Flowcharts a) Square wave generation of frequency 5 kHz and peak voltage 5V For square wave with 5 kHz, TON = TOFF = 100s. Then count value for delay program is N = (100 s (3x 1.0851s)) / (2x1.0851s) = 44.57 44 = 2CH Iref = 1.86mA and required peak voltage Vout = 4V The digital input for 5V = 255, then B1 to B8 = FFH and for 0V, digital input data = 127 = 7FH
Start DPTR FFC0H A = 7FH [DPTR] A Wait 100s A = FFH [DPTR] A Wait 100s

Department of ECE, VKCET

Page 64

08.607 MICROCONTROLLER LAB


b) Rectangular wave of frequency 10 kHz, duty cycle 65% and peak amplitude 2V For 10 kHz, T = 100s and 65% duty cycle T on = 0.65 T = 65s and Toff = 0.35 T = 35s. Then count value for delay programs are: Non = (65s (3 x 1.0851s)) / (2 x 1.0851s) = 28.45 1CH Noff = (35s (3 x 1.0851s)) / (2 x 1.0851s) = 14.6 0EH For peak amplitude 2V, 7FH + (FFH-7FH)/2 = BFH
Start DPTR FFC0H A = 7FH [DPTR] A Wait 35s A = BFH [DPTR] A Wait 65s

c) Saw tooth wave generation for maximum frequency and amplitude


Start DPTR FFC0H A = 00H [DPTR] A A=A+1

Department of ECE, VKCET

Page 65

08.607 MICROCONTROLLER LAB


d) Triangular wave form with maximum frequency and amplitude
Start DPTR FFC0H A = 00H [DPTR] A A=A+1 No If A = FFH ? Yes [DPTR] A A=A-1 No If A = 00H ? Yes

Department of ECE, VKCET

Page 66

08.607 MICROCONTROLLER LAB


Programs: a) Square wave of 5kHz, 4V peak generation OPCODE & ADDRESS LABEL MNEMONICS OPERANDS 4100 MAIN REPEAT MOV DPTR,#FFC0 MOV A,#7F MOVX @DPTR,A ACALL DELAY MOV A,#FF MOVX @DPTR,A ACALL DELAY SJMP REPEAT SUBPROGRAM FOR DELAY MOV R0,#2C DJNZ R0,WAIT RET

COMMENTS DPTR FFC0H, memory mapped IO address of DAC1 data bus A 7FH, data for 0V [DPTR] A Wait 100s A FFH, data for 5V [DPTR] A Wait 100s Repeat the process R0 Count value for delay 100s R0 R0 1, if R0 0 go to label WAIT Return to main program

4200

DELAY WAIT

Department of ECE, VKCET

Page 67

08.607 MICROCONTROLLER LAB


b) Rectangular wave of frequency 10 kHz, duty cycle 65% and peak amplitude 2V OPCODE & ADDRESS LABEL MNEMONICS COMMENTS OPERANDS DPTR FFC0H, memory 4100 MAIN MOV DPTR,#FFC0 mapped IO address of DAC1 data bus REPEAT MOV A,#7F A 7FH, data for 0V MOVX @DPTR,A [DPTR] A ACALL DELAY_T1 Wait 35s MOV A,#BF A FFH, data for 5V MOVX @DPTR,A [DPTR] A ACALL DELAY_T2 Wait 65s SJMP REPEAT Repeat the process SUBPROGRAM FOR DELAY R0 Count value for delay 4200 DELAY_T1 MOV R0,#0E 35s R0 R0 1, if R0 0 go WAIT_T1 DJNZ R0,WAIT_T1 to label WAIT RET Return to main program R0 Count value for delay 4200 DELAY_T2 MOV R0,#1C 65s R0 R0 1, if R0 0 go WAIT_T2 DJNZ R0,WAIT_T2 to label WAIT RET Return to main program c) Sawtooth wave generation for maximum frequency and amplitude OPCODE & ADDRESS LABEL MNEMONICS OPERANDS 4100 MAIN MOV DPTR,#FFC0 CLR A MOVX @DPTR,A INC A SJMP REPEAT Observred output wave form have Vpp = 9.1V and f = 720HZ REPEAT

COMMENTS DPTR FFC0H, memory mapped IO address of DAC1 data bus A 0 for minimum output voltage [DPTR] A A A+ 1 Repeat the process

Department of ECE, VKCET

Page 68

08.607 MICROCONTROLLER LAB


d) Triangular wave form with maximum frequency and amplitude ADDR OPCODE & LABEL MNEMONICS ESS OPERANDS 4100 MAIN MOV DPTR,#FFC0 CLR A MOVX @DPTR, A INC A CJNE A, #FF, INCREMENT DECREMENT MOVX @DPTR, A DEC A CJNE A,#00, DECREMENT

COMMENTS DPTR FFC0H, memory mapped IO address of DAC1 data bus A0 [DPTR] A A A+ 1 If A = FFH (Maximum), go to label INCREMENT, else next step [DPTR] A AA-1 If A = FFH (Minimum), go to label DECREMENT, else next step Repeat the process

INCREMENT

SJMP INCREMENT Observed output wave form have Vpp = 9V and f = 360Hz Procedure: Steps (Common for all programs): 1. Connect the DAC interface card and Microcontroller kit using 50-pin FRC 2. Connect DSO/CRO to the DAC output using header P2 using probe 3. Turn on power supply 4. Enter the program (starting address 4100H for main program) 5. Terminate from program enter mode 6. Execute the program 7. Observe the wave forms in CRO and measure the frequency and amplitude of the signal

Department of ECE, VKCET

Page 69

08.607 MICROCONTROLLER LAB


Expt. No.11 WAVE FORM GENERATION USING LOOK-UP TABLE

Objective: To generate sine wave using look-up table Equipments Required: Microcontroller Kit DAC 0800 Interface card 1 No. Flat ribbon cable (50 pin) 1 No. CRO/DSO 1 No. CRO probe 1 No. Theory: To generate a sine wave, first need a look-up table whose values represent the magnitude of the sine of angles between 0 and 360 degrees. The values for the sine function vary from -1.0 to +1.0 for 0- to 360-degree angles. Therefore, the table values must be integer numbers representing the voltage magnitude for the sin . This method ensures that only integer numbers are output to the DAC by the 8051microcontroller. To generate Table 1, we assumed the full-scale voltage of 10 V for DAC output. Full-scale output of the DAC is achieved when all the data inputs of the DAC are high. Therefore, to achieve the required full-scale output, we use the following equation

for in degree varies between 0o and 360o Where Vout is output voltage and is used to create data for look-up table , Vfs is full scale voltage and for the given DAC card it is 9V and is angle. Look-up table data (data input to DAC) can be implemented by Vout / step size, where step size = Vfs/256 Let step-angle = 20o and Vfs = 10V look-up table data are shown in table 1

Department of ECE, VKCET

Page 70

08.607 MICROCONTROLLER LAB


Look-up Table for sine wave with Vfs = 10V and step angle 20o Vout (V) Data input to DAC (rounded value) 128 171 210 238 253 253 238 210 171 128 84 45 17 1 1 17 45 84 128 Data in hex

(in degree) 0 20 40 60 80 100 120 140 160 180 200 220 240 260 280 300 320 340 360

5 6.7 8.21 9.33 9.92 9.92 9.33 8.21 6.7 5 3.29 1.79 0.67 0.076 0.076 0.67 1.79 3.29 5

80H ABH D2H EEH FDH FDH EEH D2H ABH 80H 54H 2DH 11H 01H 01H 11H 2DH 54H 80H

Department of ECE, VKCET

Page 71

08.607 MICROCONTROLLER LAB


Flowcharts
Start R0 = 12H, count value for LUT DPTR = 4200H, starting address of LUT A [DPTR] DPTR = DPTR +1 Push DPTR to stack memory DPTR = FFC0H, address of DAC1 [DPTR] A Pop stack memory content to DPTR R0 = R0 - 1 No

If R0 = 0 ? Yes

Department of ECE, VKCET

Page 72

08.607 MICROCONTROLLER LAB


Program: ADDRESS 4100 LABEL MAIN OPCODE & OPERANDS MNEMONICS MOV R0,#12 COMMENTS

NEXT

4200

LUT

Counter = 12H, length of LUT DPTR = 4200H, LUT starting MOV DPTR,#4200 address MOVX A,@DPTR A [DPTR] INC DPTR DPTR DPTR + 1 Save DPH content to default PUSH DPH stack PUSH DPL Save DPL content to stack MOV DPTR FFC0H, memrory DPTR,#FFC0 mapped IO address of DAC1 [DPTR] A, LUT element to MOVX @DPTR,A DAC POP DPL Load DPL content from stack POP DPH Load DPH content from stack R0 = R0 1, if R0 > 0, go to DJNZ R0, NEXT label NEXT, else next step SJMP MAIN Repeat the process Look-up Table 80 AB D2 EE FD FD EE D2 AB 80 54 2D 11 01 01 11 2D 54 80
Page 73

Department of ECE, VKCET

08.607 MICROCONTROLLER LAB


Procedure: Steps : 8. Connect the DAC interface card and Microcontroller kit using 50-pin FRC 9. Connect DSO/CRO to the DAC output using header P3 using probe 10. Turn on power supply 11. Enter the program (starting address 4100H for main program) 12. Terminate from program enter mode 13. Execute the program 14. Observe the wave forms in CRO and measure the frequency and amplitude of the signal

Department of ECE, VKCET

Page 74

08.607 MICROCONTROLLER LAB


Expt. No.12 MATRIX ADDITION

Objective: To add two m x n matrices stored in 4300H and 4400H onwards in row-wise order and store the sum in 4500H onwards. Where m and n values are stored in 4200H and 4201H respectively. (All locations are external RAM) Equipments Required: Microcontroller Kit Theory: Matrix addition between two matrices A and B of dimension m x n can be performed as follows:

In 8051 based system matrix elements can be enter in data memory either row-wised or column wised. A row-wised entry to the RAM locations is as follows. Let

For matrix A 4300H 4301H 4302H 4303H 4304H 4305H 4306H 4307H 4309H 0 1 2 1 0 3 9 6 4 4400H 4401H 4402H 4403H 4404H 4405H 4406H 4407H 4409H

For matrix B 3 1 0 1 0 6 1 5 8
Page 75

Department of ECE, VKCET

08.607 MICROCONTROLLER LAB


Matrix addition can be performed by accessing each location, adding the contents and saving the result to destination. Flow chart
Start DPTR 4200H, address of m A [DPTR] BA DPTR = DPTR +1, pointer to n A [DPTR] R0 A x B, perform m x n DPL = 00H, lower byte of memory address DPL = 43H, upper byte of memory address for matrix A A [DPTR] R1 A, element of matrix A DPL = 44H, upper byte of memory address for matrix B A [DPTR], element of matrix B A = A + R1, matrix addition A Department of ECE, VKCET Page 76

08.607 MICROCONTROLLER LAB


A DPL = 45H, upper byte of memory address for result matrix C [DPTR] A, element of matrix C DPTR = DPTR +1, pointer to next matrix elements R0 = R0 - 1 No If R0 = 0? Yes Stop B

Department of ECE, VKCET

Page 77

08.607 MICROCONTROLLER LAB


Program: ADDRESS 4100 LABEL MAIN OPCODE & OPERANDS MNEMONICS MOV DPTR,#4200 MOVX A,@DPTR MOV B,A INC DPTR MOVX A,@DPTR MUL AB MOV R0,A MOV DPL,#00 NEXT MOV DPH,#43 MOVX A,@DPTR MOV R1,A MOV DPH,#44 MOVX A,@DPTR ADD A,R1 MOV DPH,#45 MOVX @DPTR,A INC DPTR DJNZ R0,NEXT STOP SJMP STOP COMMENTS DPTR 4200H, address of m A [DPTR], m to A BA DPTR DPTR + 1, address of n A [DPTR], n to A Amxn R0 m x n DPL 00, for lower byte address of matrix elements DPH 43H, for higher byte address of matrix A A [DPTR], matrix A element R1 A DPH 44H, for higher byte address of matrix B A [DPTR], matrix B element A A + R1, addition of matrix elements DPH 45H, for higher byte address of result matrix [DPTR] A DPTR DPTR + 1, next matrix element R0 = R0 1, if R0 > 0 go to label NEXT Stop process

Department of ECE, VKCET

Page 78

08.607 MICROCONTROLLER LAB


Procedure: Steps: 1. Enter the program (starting address 4100H) 2. Terminate from program enter mode 3. Enter the dimension m x n of matrices in 4200H and 4201H 4. Enter the elements of matrix A in row-wise from 4300H onwards 5. Enter the elements of matrix B in row-wise from 4400H onwards 6. Terminate from edit mode 7. Execute the program 8. Reset the kit 9. Verify the result in 4500H onwards

Department of ECE, VKCET

Page 79

08.607 MICROCONTROLLER LAB


Expt. No.13 DISPLAY INTERFACING

Objective: To display a message on LCD module in the microcontroller kit Equipments Required: Microcontroller Kit Theory: LCD interfacing: LCD is wide spreading display conventional to LEDs, due to: Low cost Ability to display numbers, characters and graphics Display refreshing task is not required Easy to program for characters and graphics The pins and its functions of 16 x 2 character LCD module is shown in the table 1

Department of ECE, VKCET

Page 80

08.607 MICROCONTROLLER LAB


Timing diagram:

Timing characteristics:

Department of ECE, VKCET

Page 81

08.607 MICROCONTROLLER LAB


Instruction table

Note: DDRAM Display Data RAM Where I/D = 1, increment DDRAM address, cursor moves to right I/D = 0, decrement DDRAM address, cursor moves to left SH = 1, shift entire display according to I/D SH = 1, no shift of entire display CGRAM Character Generator RAM D = 1, entire display turn on D = 0, display turn off, but display data remains in DDRAM C = 1, cursor turn on C = 0, cursor disappear, but I/D preserves its data B = 1, cursor blink on B = 0, cursor blink off

Department of ECE, VKCET

Page 82

08.607 MICROCONTROLLER LAB

AC Address Counter

DL = 1, 8-bit bus mode DL = 0, 4 bit bus mode N = 1, 2 line display mode N = 0, 1 line display mode F = 1, 5 x 10 dots font F = 0, 5 x 7 dots font Address for DDRAM: AC6 to AC0 are: The for position 1 in line 1 the command byte is 1000 0000 = 80H, for position 2 : 1000 0001 = 81H .

Memory mapped IO address of on-chip LCD pins: E and DB7-DB0 : FF04H R/W and RS : FF08H Bit orientation for FF08H : 0000 00 R/W RS Data for command register select: 00H Data for data register select: 01H

Department of ECE, VKCET

Page 83

08.607 MICROCONTROLLER LAB


Algorithm: Algorithm to display message Welcome to ECE Dept. VKCET stored in external data memory LUT from 4200H and end character is ASCII for NULL- 00H Assumptions: a) Function set: 2 lines, 8-bit and 5 x 7 dots b) Entry mode: Left entry (first character of your message should be in left most display) c) Cursor blinking Main program Steps: 1. Start 2. Write command byte to LCD for function setting. Command byte: 0011 1000 = 38H 3. Wait minimum 40s 4. Write command byte to LCD for function setting. Command byte: 0000 0001 = 01H 5. Wait minimum 1.64ms 6. Write command byte to LCD for display on and cursor blinking. Command byte: 0000 1111 = 0FH 7. Wait minimum 40s 8. Write command byte to LCD for entry mode: left entry (increment cursor to right). Command byte: 0000 0110 = 06H 9. Write command byte to LCD for DDRAM address. Command byte: 1000 0000 = 80H 10. Wait minimum 40 s 11. Point starting address of LUT by DPTR, DPTR = 4200H 12. A [DPTR], ASCII of character for message to A 13. Push DPTR to stack 14. Write data byte (ASCII) to LCD 15. Wait minimum 40s 16. Pop DPTR from stack 17. DPTR = DPTR +1 18. If A 0 (ASCII of NULL character) go to step 10 19. Stop LCD command write Steps: 1. Start 2. R7 A, save command byte 3. DPTR = FF08H, memory mapped IO address of RW and RS pins of LCD 4. A = 00H, RW = 0 (LCD write operation) and RS = 0 (LCD command register selected) 5. [DPTR] A
Department of ECE, VKCET Page 84

08.607 MICROCONTROLLER LAB


6. DPTR = FF04H, memory mapped IO address of E and DB0-DB7 pins of LCD 7. A R7, command byte 8. [DPTR] A, (E pin set to 1 by hard ware) 9. Stop LCD data write Steps: 1. Start 2. R7 A, save data byte 3. DPTR = FF08H , memory mapped IO address of RW and RS pins of LCD 4. A = 01H, RW = 0 (LCD write operation) and RS = 1 (LCD data register selected) 5. [DPTR] A 6. DPTR = FF04H, memory mapped IO address of E and DB0-DB7 pins of LCD 7. A R7, data byte 8. [DPTR] A, (E pin set to 1 by hard ware) 9. Stop

Program Main program MAIN: ORG 4100H MOV A,#38H LCALL CMD_WRITE LCALL DELAY MOV A,#01H LCALL CMD_WRITE LCALL DELAY MOV A,#0FH LCALL CMD_WRITE LCALL DELAY MOV A,#06H LCALL CMD_WRITE LCALL DELAY MOV A,#80H LCALL CMD_WRITE LCALL DELAY MOV DPTR,#4200H MOV X A,@DPTR ;Function set command byte, 0011 1000b to A ;Write command to LCD ;Wait minimum 40s ;Clear display command byte, 0000 0001b to A ;Write command byte to LCD ;Wait minimum 2ms ;Display/cursor on/off command byte, 0000 1111b to A ;Write command byte to LCD ;Wait minimum 40s ;Entry mode command byte 0000 0110b to A ;Write command byte to LCD ;Wait minimum 40s ;DDRAM address command byte 1000 0000b to A ;Write command byte to LCD ;Wait minimum 40s ;Starting address of LUT of the message to DPTR ;A [DPTR]

NEXT:

Department of ECE, VKCET

Page 85

08.607 MICROCONTROLLER LAB


CJNE A,#0,DISPLAY STOP: DISPLAY: SJMP STOP INC DPTR PUSH DPH PUSH DPL LCALL DATA_WRITE LCALL DELAY POP DPL POP DPH SJMP NEXT ;If A = 0, ASCII for NULL character, go to ;label DISPLAY ;Stop process ;DPTR = DPTR + 1 ;DPH to stack ;DPL to stack ;Write data byte to LCD ;Wait 40s ;DPL from stack ;DPH from stack ;Go to label NEXT

Sub-programs ;LCD command write CMD_WRITE: ORG 4150H MOV R7,A MOV A,#00 MOV DPTR,#0FF08H MOV X @DPTR,A MOV A,R7 MOV DPTR,#0FF04H MOV X @DPTR,A RET ;LCD data write DATA_WRITE: ORG 4170H MOV R7,A MOV A,#01 MOV DPTR,#0FF08H MOV X @DPTR,A MOV A,R7 MOV DPTR,#0FF04H MOV X @DPTR,A RET ;R7 A, save data byte ;A = 01, RS = 1 and R/W = 0 ;DPTR = FF08H, memory mapped IO address ;of RS and R/W pins ;[DPTR] A ;A R7 ;DPTR = FF04H, memory mapped IO address ;of DB0-DB7 and EN pin set to high ;[DPTR] A ;R7 A, save command byte ;A = 0, for RS = 0 and R/W = 0 ;DPTR = FF08H, memory mapped IO address ;of RS and R/W pins ;[DPTR] A ;A R7 ;DPTR = FF04, memory mapped IO address of ;DB0-DB7 and EN goes to high ;[DPTR] A ;Return to main program

Department of ECE, VKCET

Page 86

08.607 MICROCONTROLLER LAB


2ms delay sub-program ORG 4190H DELAY: MOV R6,#05H LOOP1: MOV R5,#0FFH LOOP2: DJNZ R5, LOOP2 DJNZ R6,LOOP1 RET END Look Up Table 4200H: 4AH 43H 20H 4CH 61H 62H 20H 45H 43H 45H 20H 44H 65H 70H 74H 420FH: 00H

;M ;C ; ;L ;a ;b ; , :E :C ;E ; ;D ;e ;p ;t :NULL

Department of ECE, VKCET

Page 87

08.607 MICROCONTROLLER LAB


Expt. No.14 PWM SIGNAL GENERATION

Objective: To generate a PWM signal using 8051 timers Equipments Required: Microcontroller Kit Flat Ribbon Cable (10 pin) CRO/DSO CRO probe Theory: PWM signals are signals with variable duty cycle. Using microcontrollers that signals can be used to control the speed of the dc motors, since the average current of PWM signal is the function of duty cycle. PWM signal

The duty cycle of the signal varies linearly and the time period is always constant. Using Timer/Counter in 8051 the delay can be provided for varying duty cycle. Header details

Department of ECE, VKCET

Page 88

08.607 MICROCONTROLLER LAB


FRC connecter details

Algorithm Algorithm to generate a PWM signal of 10 kHz (Crystal frequency in kit is 11.0592MHz) on 8051 pin P1.0 using timer/timers. Duty cycle varying from 90%, 60%, 30 as shown below:

Main program 1. Start 2. Initialize timer as 16-bit timer 3. Load count value to timer for 90s 4. Set P1.0 5. Call delay sub-routine 6. Load count value to timer for 10s 7. Clear P1.0 8. Call delay sub-routine 9. Load count value to timer for 60s 10. Set P1.0 11. Call delay sub-routine 12. Load count value to timer for 40s 13. Clear P1.0
Department of ECE, VKCET Page 89

08.607 MICROCONTROLLER LAB


14. Call delay sub-routine 15. Load count value to timer for 30s 16. Set P1.0 17. Call delay sub-routine 18. Load count value to timer for 70s 19. Clear P1.0 20. Call delay sub-routine 21. Go to step 3 Sub-routine 1. Start timer 2. If timer flag is 0, wait here else go to next step 3. Stop timer 4. Clear timer flag 5. Return to main program Delay calculations (Timer in mode 1 16 bit timer) 1. 10s delay T N =(65536 d ) Tt For 11.0592MHz, timer clock period Tt = 1.0851s 10 N =65536 =65526.78 1.0851 65526 = FFF6H 2. 30s delay 30 N =65536 =65508.35 1.0851 65508 = FFE4H 3. 40s delay 40 =65499.14 1.0851 65499= FFDBH 4. 60s delay 60 N =65536 =65480.71 1.0851 65480 = FFC8H 5. 70s delay 70 N =65536 =65471.49 1.0851 65471 = FFBFH N =65536
Department of ECE, VKCET Page 90

08.607 MICROCONTROLLER LAB


6. 90s delay 90 =65453.06 1.0851 65453 = FFADH N =65536 Program MAIN: REPEAT: ORG 4100H MOV TMOD,#01H MOV TH0,#0FFH MOV TL0,#0ADH SETB P1.0 ACALL DELAY MOV TH0,#0FFH MOV TL0,#0F6H CLR P1.0 ACALL DELAY MOV TH0,#0FFH MOV TL0,#0C8H SETB P1.0 ACALL DELAY MOV TH0,#0FFH MOV TL0,#0DBH CLR P1.0 ACALL DELAY MOV TH0,#0FFH MOV TL0,#0E4H SETB P1.0 ACALL DELAY MOV TH0,#0FFH MOV TL0,#0BFH CLR P1.0 ACALL DELAY SJMP REPEAT ; Timer 0 , mode 1 ; Count value for 90s ;P1.0 = 1 ;Wait 90s ;Count value for 10s ;P1.0 = 0 ;Wait 10s ;Count value for 60s ;P1.0 = 1 ;Wait 60s ;Count value for 40s ;P1.0 = 0 ;Wait 40s ;Count value for 30s ;P1.0 = 1 ;Wait 30s ;Count value for 70s ;P1.0 = 0 ;Wait 70s ;Repeat the process ;Start timer 0 by setting TR0 in TCON ;Wait for timer flag TF0 in TCON to set ;Stop timer by clearing TR0 ;Clear timer flag TF0

;Delay sub-program DELAY: SETB TR0 WAIT: JNB TF0,WAIT CLR TR0 CLR TF0 RET END
Department of ECE, VKCET

Page 91

08.607 MICROCONTROLLER LAB


Procedure Steps: 1. 2. 3. 4. 5. 6. 7.

Connect FRC on header P17 Connect CRO probes across P1.0 and GND of FRC connector Turn on the kit and CRO/DSO Enter the program (from 4100H) Terminate from program enter mode Run the program Observe the wave form on CRO/DSO

Department of ECE, VKCET

Page 92

08.607 MICROCONTROLLER LAB


Expt. No.15 Objective: step) continuously. c) To rotate stepper motor in clock wise direction at 4 RPM when P1.0 is 1 and 10 RPM when P1.0 is 0 Equipments Required: Microcontroller Kit (Vi89c51) Stepper motor 10 pin FRC Theory: Stepper motor translates electrical pulses into mechanical motion. It is possible to control speed and position without feedback sensors. Some applications are computer peripherals (Hard disk, CD, FD, Printer, Plotter etc.), business machines (Card reader, Type writer, Copy machine, etc.), control systems and machine tools (Milling machines, drilling machines, etc) Stepper motor consists rotor (permanent magnet) and stator (coil winding) as shown in figure-1 STEPPER MOTOR INTERFACE a) To rotate stepper motor in clock wise direction continuously (full step and half b) To rotate stepper motor in 180o clock wise and 360o anti clock wise direction

Figure 1 According to the sequence of pulses applied to each stator winding, the rotor will rotate. Rotor of conventional motors runs freely, but stepper motors rotor moves in a fixed repeatable increment, allows a precise position because of tooth in the rotor. For stepper motors relationship between steps per second and rpm is Steps per second = rpm x steps per revolution / 60
Department of ECE, VKCET Page 93

08.607 MICROCONTROLLER LAB


Standard step angles and steps per revolution (for 360o rotation) are: 0.72 500 1.8 200 2.0 180 5.0 72 7.5 48 15 24 Step sequence table 1. Full step 4-step 2-phase or normal drive sequence For clock wise rotation Steps 1 2 3 4 Winding Winding Winding Winding A B C D 1 0 0 1 0 1 1 0 0 0 1 1 1 1 0 0

Note: Following figures shows the motor with step-angle of 90 o, thus after one complete step sequence motor rotates 360o. Step 1 Step 2 Step 3 Step 4

Step sequence waves:

Department of ECE, VKCET

Page 94

08.607 MICROCONTROLLER LAB


For anti-clock wise rotation Steps 1 2 3 4 Step 1 Step 2 Winding Winding Winding Winding D A B C 1 0 0 1 0 1 1 0 Step 3 1 1 0 0 0 0 1 1 Step 4

Step sequence waves:

2. Full step 4-step wave drive sequence For clock wise rotation Steps 1 2 3 4 Winding Winding Winding Winding D A B C 1 0 0 0 0 0 1 0 0 0 0 1 0 1 0 0

Department of ECE, VKCET

Page 95

08.607 MICROCONTROLLER LAB


Step 1 Step 2 Step 3 Step 4

Step sequence waves:

For anti-clock wise rotation Steps 1 2 3 4 Step 1 Step 2 Winding Winding Winding Winding D A B C 0 0 0 1 0 1 0 0 Step 3 1 0 0 0 0 0 1 0 Step 4

Step sequence waves:

Department of ECE, VKCET

Page 96

08.607 MICROCONTROLLER LAB


3. Half step 8-step mixed drive sequence For clock wise rotation Steps 1 2 3 4 5 6 7 8 Step 1 Step 2 Winding Winding Winding Winding D A B C 1 1 0 0 0 0 0 1 0 0 0 1 1 1 0 0 0 0 0 0 0 1 1 1 Step 3 0 1 1 1 0 0 0 0 Step 4

Step 5

Step 6

Step 7

Step 8

Step sequence waves:

Department of ECE, VKCET

Page 97

08.607 MICROCONTROLLER LAB


For anti-clock wise rotation Steps 1 2 3 4 5 6 7 8 Step 1 Step 2 Winding Winding Winding Winding D A B C 1 1 0 0 0 0 0 1 0 0 0 1 1 1 0 0 0 1 1 1 0 0 0 0 Step 3 0 0 0 0 0 1 1 1 Step 4

Step 5

Step 6

Step 7

Step 8

Department of ECE, VKCET

Page 98

08.607 MICROCONTROLLER LAB


Memory mapped IO address and Stepper motor details of Vi89c51 kit Memory mapped IO address of stepper motor connected to header P4 is: FF11H Stepper motor driver circuit in the kit is shown in the figure 2

Figure 2 When the memory mapped IO address FF11H is placed in DPTR and loading A register results the following bit movement: Acc.0 M1 Acc.1 M2 Acc.2 M3 Acc.3 M4 Where M1, M2, M3 and M4 are stepper motor driver IC L298N inputs. L298N is high voltage, high current dual full-bridge driver designed to accept standard TTL logic levels and drive inductive loads such as relays, solenoids, DC and stepper motors. The stepper motor provided with the kit can be connecting to header P4 and the colour code of the stepper motor (Unipolar 6 leads) are: Brown Winding A Red Winding B Orange Winding C Yellow Winding D Black Winding A/C common White Winding B/D common
Department of ECE, VKCET Page 99

08.607 MICROCONTROLLER LAB


In the kit motor leads connections are: Acc.0 M1 Yellow Winding D Acc.1 M2 Orange Winding C Acc.2 M3 Red Winding B Acc.3 M4 Brown Winding A The step sequence can be given in the order shown in the tables. The specifications of the available stepper motor are: Voltage: 12V Torque: 2kg Step angle: 1.8o Algorithm a) To rotate stepper motor in clock wise direction continuously (full step and half step) Main program 1. Start 2. Initialize timer 0 for delay 3. DPTR = 4200H, pointer for LUT of step sequence 4. Counter R0 = 04 for full step sequence or 08 for half step sequence 5. A [DPTR] 6. Save DPTR content to stack memory 7. DPTR = FF11H, pointer to memory mapped IO address of stepper motor 8. [DPTR] A, step sequence to motor 9. Call delay program 10. Load stack memory content to DPTR 11. Increment DPTR content 12. Decrement counter R0 = R0 - 1, if R0 0 go to step 5, else next step 13. Go to step 3 for continuous rotation

Department of ECE, VKCET

Page 100

08.607 MICROCONTROLLER LAB


Delay program 1. Start 2. Load count value for required delay to basic registers of timer 0 3. Start timer by setting TR0 = 1 4. If timer flag TF0 1, wait here, else go to next step 5. Stop timer by clearing TR0 = 0 6. Clear timer flag TF0 7. Return to main program b) To rotate stepper motor in 180o clock wise and 360o anti clock wise direction continuously. Main program 1. Start 2. Initialize timer 0 for delay 3. Counter R1 = 25 for 180o rotation (180/(4 x 1.8) = 25) 4. DPTR = 4200H, pointer for LUT of clock wise step sequence 5. Counter R0 = 04 for full step sequence or 08 for half step sequence 6. A [DPTR] 7. Save DPTR content to stack memory 8. DPTR = FF11H, pointer to memory mapped IO address of stepper motor 9. [DPTR] A, step sequence to motor 10. Call delay program 11. Load stack memory content to DPTR 12. Increment DPTR content 13. Decrement counter R0 = R0 - 1, if R0 0 go to step 6, else next step 14. Decrement counter R1 = R0 - 1, if R0 0 go to step 4, else next step 15. Counter R1 = 50 for 360o rotation (360/(4 x 1.8) = 50) 16. DPTR = 4300H, pointer for LUT of anti-clock wise step sequence 17. Counter R0 = 04 for full step sequence or 08 for half step sequence 18. A [DPTR] 19. Save DPTR content to stack memory 20. DPTR = FF11H, pointer to memory mapped IO address of stepper motor 21. [DPTR] A, step sequence to motor 22. Call delay program 23. Load stack memory content to DPTR 24. Increment DPTR content 25. Decrement counter R0 = R0 - 1, if R0 0 go to step 18, else next step 26. Decrement counter R1 = R0 - 1, if R0 0 go to step 16, else next step 27. Go to step 3 for continuous rotation
Department of ECE, VKCET Page 101

08.607 MICROCONTROLLER LAB


c) To rotate stepper motor in clock wise direction at 4 RPM when P1.0 is 1 and 10 RPM when P1.0 is 0 Main program 1. Start 2. Initialize timer 0 for delay 3. Set P1.0 as input 4. DPTR = 4200H, pointer for LUT of clock wise step sequence 5. Counter R0 = 04 for full step sequence or 08 for half step sequence 6. A [DPTR] 7. Save DPTR content to stack memory 8. DPTR = FF11H, pointer to memory mapped IO address of stepper motor 9. If P1.0 is 1, go to step 13, else next step 10. [DPTR] A, step sequence to motor 11. Call 10 RPM delay program 12. Go to step 13. [DPTR] A, step sequence to motor 14. Call 4 RPM delay program 15. Load stack memory content to DPTR 16. Increment DPTR content 17. Decrement counter R0 = R0 - 1, if R0 0 go to step 6, else next step 18. Go to step 3 for continuous rotation Delay calculation for 4RPM RPM X steps per revolution steps per second = 60 360 360 steps per revolution= = =200 step angle 1.8 4 X 200 steps per second = =13.33 60 1 time required for one step= =75ms steps per second Delay calculation for 10RPM 10 X 200 steps per second = =33.33 60 1 time required for one step= =30ms steps per second Delay calculations Use 25ms instead of 30ms delay and call it three times for 75ms delay. 3 25 X 10 =42496.65 Count value for 25ms delay N =65536 6 1.0851 X 10 42496 = A600H
Department of ECE, VKCET Page 102

08.607 MICROCONTROLLER LAB

Programs a) MAIN: REPEAT: NEXT: ORG 4100H MOV TMOD ,#01 ;Timer 0 in mode 1 MOV DPTR,#4200H ;LUT starting address of step sequence data to DPTR MOV R0,#04 ;Counter for full step sequence data MOVX A,@DPTR ;A [DPTR] PUSH DPH ;DPH to stack PUSH DPL ;DPL to stack MOV DPTR,#0FF11H ;DPTR = FF11H, memory mapped IO address of ;stepper motor MOVX @DPTR,A ;[DPTR] A ACALL DELAY ;Wait 50ms POP DPL ;DPL from stack POP DPH ;DPH from stack INC DPTR ;DPTR = DPTR + 1 DJNZ R0,NEXT ;R0 = R0 1, if R0 > 0 go to label NEXT SJMP REPEAT ;Repeat the process

;Delay sub-program ORG 4150H DELAY: MOV TH0,#40H MOV TL0,#00 SETB TR0 WAIT: JNB TF0,WAIT CLR TR0 CLR TF0 RET

;Count value for 50ms delay

;LUT at 4200H FS_2PHASE: DB 09H,05H,06H,0AH HS_MIXED: DB 08H,09H,01H,05H,04H,06H,02H,0AH ;Use counter value R0 = 08H END b) ORG 4100H MAIN: MOV TMOD,#01H ;Timer 0 in mode 1 CONTINUE: MOV R0,#19H ;Counter R0 = 19H, for 180o rotation

Department of ECE, VKCET

Page 103

08.607 MICROCONTROLLER LAB


REPEAT1: MOV DPTR,#4200H ;LUT starting address of step sequence (CW) data to ;DPTR MOV R1,#04 ;Counter for full step sequence data MOVX A,@DPTR ;A [DPTR] PUSH DPH ;DPH to stack PUSH DPL ;DPL to stack MOV DPTR,#0FF11H ;DPTR = FF11H, memory mapped IO address of ;stepper motor MOVX @DPTR,A ;[DPTR] A ACALL DELAY ;Wait 50ms POP DPL ;DPL from stack POP DPH ;DPH from stack INC DPTR ;DPTR = DPTR + 1 DJNZ R1,NEXT1 ;R1 = R1 1, if R1 > 0 go to label NEXT1 DJNZ R0,REPEAT1 ;R0 = R0 1, if R0 > 0 go to label REPEAT1 MOV R0,#32H ;Counter R0 = 32H, for 360o rotation MOV DPTR,#4300H ;LUT starting address of step sequence (ACW) data to ;DPTR MOV R1,#04 ;Counter for full step sequence data MOVX A,@DPTR ;A [DPTR] PUSH DPH ;DPH to stack PUSH DPL ;DPL to stack MOV DPTR,#0FF11H ;DPTR = FF11H, memory mapped IO address of ;stepper motor MOVX @DPTR,A ;[DPTR] A ACALL DELAY ;Wait 50ms POP DPL ;DPL from stack POP DPH ;DPH from stack INC DPTR ;DPTR = DPTR + 1 DJNZ R1,NEXT2 ;R1 = R1 1, if R1 > 0 go to label NEXT2 DJNZ R0,REPEAT2 ;R0 = R0 1, if R0 > 0 go to label REPEAT2 SJMP CONTINUE ;Repeat the process

NEXT1:

REPEAT2:

NEXT2:

Department of ECE, VKCET

Page 104

08.607 MICROCONTROLLER LAB


;Delay sub-program ORG 4150H DELAY: MOV TH0,#40H MOV TL0,#01H SETB TR0 WAIT: JNB TF0,WAIT CLR TR0 CLR TF0 RET ;LUT for CW rotation ORG 4200H FS_CW: DB 08H,01H,04H,02H ;LUT for ACW rotation ORG 4300H FS_ACW: DB 02H,04H,01H,08H END c) MAIN: REPEAT: NEXT: ORG 4100H MOV TMOD,#01H SETB P1.0 MOV DPTR,#4200H MOV R0,#04 MOVX A,@DPTR PUSH DPH PUSH DPL MOV DPTR,#0FF11H JB P1.0, RPM_4 MOVX @DPTR,A ACALL DELAY SJMP DEC_COUNT MOVX @DPTR,A ACALL DELAY ACALL DELAY ACALL DELAY ;Timer 0 in mode 1 ;P1.0 as input ;Counter for full step sequence data ;LUT starting address of step sequence ;(CW) data to DPTR ;DPH to stack ;DPL to stack ;DPTR = FF11H, memory mapped IO ;address of stepper motor ;If P1.0 is set go to label RPM_4 ; [DPTR] A ;Wait 25ms ;Go to label DEC_COUNT ; [DPTR] A ;Wait 75ms

RPM_10:

RPM_4:

Department of ECE, VKCET

Page 105

08.607 MICROCONTROLLER LAB


DEC_COUNT:POP DPL POP DPH INC DPTR DJNZ R0,NEXT SJMP REPEAT DELAY: MOV TH0,#0A6H MOV TL0,#0 SETB TR0 JNB TF0,WAIT CLR TR0 CLR TF0 RET ;DPL from stack ;DPH from stack ;DPTR = DPTR + 1 ;R0 = R0 1, if R0 > 0 go to label NEXT ;Repeat the process ;Count value for 30ms

WAIT:

LUT: FS_CW:

DB 09H,05,06,0AH END Procedure (Common for all programs) Steps: 1. Connect stepper motor on header P4 2. Connect FRC between port 1(header P17) and header P18, for program c 3. Turn on the kit 4. Enter the program (from 4100H) 5. Terminate from program enter mode 6. Run the program 7. Observe the rotation of motor

Department of ECE, VKCET

Page 106

08.607 MICROCONTROLLER LAB


Expt. No.16 FREQUENCY MEASUREMENT BY COUNTING THE NUMBER OF PULSES IN A FIXED AMOUNT OF TIME

Objective: To measure the frequency of pulse applied to P3.4 (T0) or P3.5 (T1) pin by counting the number of pulses in a fixed amount of time. Equipments Required: Microcontroller Kit (Vi89c51) Function generator CRO/DSO Probes Flat Ribbon Cable (50 pin) Theory: Frequency of a pulses can be measured by configuring 8051 timer/counter as event counter. Such a case, the frequency for the timer is frequency of the pulse applied to the T0 (P3.4) or T1 (P3.5) pin. Then starting timer and count value after 1 second gives the frequency of pulse. Timer as a counter is shown in figure 1.

Figure 1

Department of ECE, VKCET

Page 107

08.607 MICROCONTROLLER LAB


Header and Connection details: Vi89c51 kit has VPIC bus and 50-pin header, in which all 40 pins of 89c51 microcontroller are connected. The details of VPIC bus is shown in figure 2.

Figure 2 Using 50 pin FRC, we can use 89c51 pins. The details of FRC after connecting to VPIC bus is shown in figure 3.

Figure 3 (Note: T0 and T1 pins are also terminated using header P7 in the kit)
Department of ECE, VKCET Page 108

08.607 MICROCONTROLLER LAB


Algorithm Main program 1. Start 2. Initialize timer 0 as 16-bit counter and timer 1 as 16-bit timer for delay 3. Initialize LCD to display the frequency 4. Set the DDRAM address to display 5. Clear TL0 for initial count (maximum frequency measured will be 255) 6. Start timer 0 7. Wait 1 second 8. Load TL0 to A 9. Convert hexa decimal data in A to BCD 10. Convert BCD digits to ASCII 11. Display the ASCII for BCD digits to LCD 12. Stop the timer 0 13. Go to step 5 LCD initialize program 1. Start 2. Write command byte to LCD for function setting. Command byte: 0011 1000 = 38H 3. Wait minimum 40s 4. Write command byte to LCD for function setting. Command byte: 0000 0001 = 01H 5. Wait minimum 1.64ms 6. Write command byte to LCD for display on and cursor off. Command byte: 0000 1100 = 0CH 7. Wait minimum 40s 8. Write command byte to LCD for entry mode: left entry (increment cursor to right). Command byte: 0000 0110 = 06H 9. Return LCD command write 1. Start 2. R7 A, save command byte 3. DPTR = FF08H, memory mapped IO address of RW and RS pins of LCD 4. A = 00H, RW = 0 (LCD write operation) and RS = 0 (LCD command register selected) 5. [DPTR] A 6. DPTR = FF04H, memory mapped IO address of E and DB0-DB7 pins of LCD 7. A R7, command byte 8. [DPTR] A, (E pin set to 1 by hard ware) 9. Return

Department of ECE, VKCET

Page 109

08.607 MICROCONTROLLER LAB


LCD data write 1. Start 2. R7 A, save data byte 3. DPTR = FF08H , memory mapped IO address of RW and RS pins of LCD 4. A = 01H, RW = 0 (LCD write operation) and RS = 1 (LCD data register selected) 5. [DPTR] A 6. DPTR = FF04H, memory mapped IO address of E and DB0-DB7 pins of LCD 7. A R7, data byte 8. [DPTR] A, (E pin set to 1 by hard ware) 9. Return Hexa decimal to BCD 1. Start 2. B 100, to get hundredth position digit 3. Divide A by B 4. R7 A, hundredth position digit 5. A B, reminder 6. B 10, to get tenth position digit 7. Divide A by B 8. R6 A, tenth position digit 9. R5 B, reminder and it is unit position digit 10. Return BCD to ASCII 1. Start 2. A A + 30H 3. Return DELAY for 40s 1. Start 2. Load count value FFDBH to T1 timer register, ie TH1 = FFH and TL1 = DBH 3. Start timer 1 by TR1 = 1 4. If timer 1 flag TF1 0, stay here, else go to next step 5. Stop timer 1 by TR1 =0 6. Clear timer 1 flag TF1 =0 7. Return

Department of ECE, VKCET

Page 110

08.607 MICROCONTROLLER LAB


DELAY for 2ms 1. Start 2. Load count value F8CCH to T1 timer register, ie TH1 = F8H and TL1 = CCH 3. Start timer 1 by TR1 = 1 4. If timer 1 flag TF1 0, stay here, else go to next step 5. Stop timer 1 by TR1 =0 6. Clear timer 1 flag TF1 =0 7. Return DELAY for 50ms 1. Start 2. Load count value 4C01H to T1 timer register, ie TH1 = 4CH and TL1 = 01H 3. Start timer 1 by TR1 = 1 4. If timer 1 flag TF1 0, stay here, else go to next step 5. Stop timer 1 by TR1 =0 6. Clear timer 1 flag TF1 =0 7. Return DELAY for 1 second 1. Start 2. Initialize counter R0 = 20 3. Wait 50ms 4. R0 = R0-1, if R0 0 go to step 3, else next step 5. Return Program ORG 4100H MOV TMOD,#00010101B LCALL LCD_INIT MOV A,#88H LCALL CMD_WRITE LCALL DELAY_40u MOV TL0,#00 SETB TR0 LCALL DELAY_1S MOV A,TL0 LCALL HEX_2_BCD MOV A,R7

MAIN:

;Timer 1 as 16 bit timer and timer 0 as 16 bit ;counter ;DDRAM address of LCD

CONTINUE:

;Initial count (using 8 bit only) ;Start counter ;Wait 1 second ;Load count value to A ;Convert hex value to BCD ;Hundredth digit to ASCII
Page 111

Department of ECE, VKCET

08.607 MICROCONTROLLER LAB


LCALL BCD_2_ASCII LCALL LCD_DISPLAY MOV A,R6 LCALL BCD_2_ASCII LCALL LCD_DISPLAY MOV A,R5 LCALL BCD_2_ASCII LCALL LCD_DISPLAY CLR TR0 MOV A,#H LCALL LCD_DISPLAY MOV A,#z LCALL LCD_DISPLAY SJMP CONTINUE ORG 4150H MOV A,#38H LCALL CMD_WRITE LCALL DELAY_40u MOV A,#01H LCALL CMD_WRITE LCALL DELAY_2m MOV A,#0CH LCALL CMD_WRITE LCALL DELAY_40u MOV A,#06H LCALL CMD_WRITE LCALL DELAY_40u RET ORG 4180H MOV R4,A MOV A,#00 MOV DPTR,#0FF08H MOVX @DPTR,A MOV A,R4 MOV DPTR,#0FF04H MOVX @DPTR,A RET
Department of ECE, VKCET Page 112

;BCD to ASCII conversion ;Display hundredth digit ;Tenth digit to ASCII ;Display tenth digit ;Unit digit to ASCII ;Display unit digit ;Stop timer 0 ;H , ASCII of H ;z, ASCII of z ;Repeat process ;LCD initialize subroutine ;Function set

LCD_INIT:

;Clear display

;Display on, cursor off

;Entry mode

CMD_WRITE:

;RW = 0, RS = 0 ;Memory mapped IO address of RW and RS ;Command ;Memory mapped IO address of DB7-DB0 ;and E =1

08.607 MICROCONTROLLER LAB


ORG 4190H MOV R4,A MOV A,#01H MOV DPTR,#0FF08H MOVX @DPTR,A MOV A,R4 MOV DPTR,#0FF04H MOVX @DPTR,A RET ORG 41A0H LCALL DATA_WRITE LCALL DELAY_40u RET ORG 41B0H MOV B,#64H DIV AB MOV R7,A MOV A,B MOV B,#0AH DIV AB MOV R6,A MOV R5,B RET ORG 41C0H ADD A,#30H RET ORG 41D0H MOV TH1,#0FFH MOV TL1,#0DBH LCALL DELAY RET

DATA_WRITE:

;RW = 0, RS = 1;

LCD_DISPLAY:

HEX_2_BCD:

BCD_2_ASCII:

DELAY_40u:

Department of ECE, VKCET

Page 113

08.607 MICROCONTROLLER LAB


ORG 41E0H MOV TH1,#0F8H MOV TL1,#0CCH LCALL DELAY RET ORG 41F0H MOV TH1,#4CH MOV TL1,#01H LCALL DELAY RET ORG 4200H MOV R0,#14H LCALL DELAY_50m DJNZ R0,WAIT_1S RET ORG 4210H SETB TR1 JNB TF1,WAIT CLR TR1 CLR TF1 RET END

DELAY_2m:

DELAY_50m:

DELAY_1S: WAIT_1S:

DELAY: WAIT:

Procedure Steps: 1. Connect 50 pin FRC to VPIC Bus 2. Set function generator to square wave signal output and 5V peak amplitude. (Ensure it using CRO/DSO) 3. Connect function generator to VPIC pin 42 (P3.4) and pin 50 (GND) 4. Turn on the kit 5. Enter the program 6. Terminate from program enter mode 7. Execute the program 8. Observe the frequency displayed on LCD by changing input pulse frequency from 0 to 255Hz

Department of ECE, VKCET

Page 114

08.607 MICROCONTROLLER LAB


BIBILIOGRAPHY 1. Muhammad Ali Mazidi, The 8051 microcontroller and Embedded System 2. Kenneth Ayala, The 8051 Microcontroller 3. Scott, The_8051_Microcontroller Websites: www.8051projects.info www.engineersgarage.com www.mikroe.com www.8052.com For development tools: www.keil.com www.atmel.com

Department of ECE, VKCET

Page 115

You might also like