08.607 Microcontroller Lab Manual
-
Upload
assini-hussain -
Category
Documents
-
view
4.445 -
download
46
description
Transcript of 08.607 Microcontroller Lab Manual
08.607 MICROCONTROLLER LAB
Department of ECE, VKCET Page 1
08.607MicrocontrollerLab Manual
Prepared byAssini H
08.607 MICROCONTROLLER LAB
SyllabusL-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 80512. To study interfacing of various peripherals using 80513. To design and develop applications using 8051Outcomes:1. Students should develop a system using 8051 microcontroller
Instructions to StudentsThese are the instructions for the students attending the lab course 08.607 Microcontroller Lab1. 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 INTRODUCTION TO 8051 MICROCONTROLLER KIT
Objective: 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 : 0000H to 3FFFH and C000H to FFFFH
System RAM location : 4000H to BFFFH Additional RAM : 0000H to 3FFFH and
C000H to FEFFH (For Micro-51 LC Kit), 0000H to 3FFFH and C000H to FF00H (For Vi89C51 kit)
Monitor Buffer : 4000H to 40FFH User Program/Data RAM location : 4100H to BFFFH User Data RAM location : 0000H to 3FFFH and
C000H to FEFFH (For Micro-51 LC Kit), 0000H to 3FFFH and C000H to FF00H (For Vi89C51 kit)
Memory mapped IO : FF00H to FF2FH and FFC0H to FFFFH
Memory mapped IO expansion : 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 interface8051 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 CommandSyntax: #sp <Address> <Enter> : For program memory#sd <Address> <Enter> : For data memory
b) Register View/ Modify CommandSyntax: #r <Enter>
c) Execute CommandSyntax: #go <Address> <Enter>
d) Execute with break point commandSyntax: #go <Start address> <Space> <Enter>
e) Assembler commandSyntax: #a <Enter>
f) Disassembler commandSyntax: #u <Enter>
g) Internal RAM commandSyntax: #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
C000HEPROM Expansion area
FFFFH
4000H
User Program Memory Combined with Data
Memory
BFFFH
0000HMonitor EPROM area
3FFFH
External Data Memory
FF00HMemory Mapped IO
FFFFH
C000HUser Data Memory
FEFFH
4000H
User Data Memory Combined with Program
Memory
BFFFH
0000HUser Data Memory
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:
FFC0HUsed for Add-on
Application BoardsFFFFH
FF24HNot Used
FFBFH
FF23HDigital Output
FF22HDigital Input
FF20HNot Used
FF21H
FF18HIBM Keyboard Select
FF1FH
FF14HNot Used
FF17H
FF0CHParallel Ports-I & II
(8255)FF13H
FF08H174 Latch Select
FF0BH
FF04HLCD Chip Select
FF07H
FF00HNot Used
FF03H
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 locations2. Display and modify the registers of the 80513. Enter and initiate execution of your own program4. Assemble and disassemble
Command line editor features:Single line editor can process up to 40 charactersValid key functions are:
EnterTo validate an entryTo increment memory locationTo select from memory
-To decrement memory locationsTo select from menu
.To terminate a command
BackspaceTo delete a character and comeback one position
Left arrowTo come back one position without deleting a character
Right arrowTo move right one position without deleting a character
Space barTo provide space in a command or data
DelTo delete a character in a command line
0 to 9Numeric character
a-zAlphabetic character
ShiftNon-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 connectorSignal description
Pin Details1 GND2 -12V3 +12V4 No connection5 Vcc (+5V)
2. CPU Bus Connector (U6)40 pin IDC male connector20 pins arranged in two rows
3. ADC & DAC Connector (P11 & P13)Screw type J801 Connector
Format for assembly language program:
ADDRESS LABEL OPCODE & OPERANDS MNEMONICS COMMENTS
4100 MAIN 74 23 MOV A,#23 Load accumulator by 23H
4102 24 22 ADD A,#22 Add accumulator with 22H and store result in accumulator
4104 90 42 00 MOV DPTR,#4200 Load data pointer by external memory address 4200H
4107 F0 MOVX @DPTR,AMove accumulator content to external memory location pointed by data pointer
4108 50 04 JNC LOOP1 If any carry in addition, go to loop1 else next address
410A 74 01 MOV A,#01 Load accumulator by 1H410C 80 01 SJMP LOOP2 Go to loop2410E LOOP1 E4 CLR A Clear accumulator410F LOOP2 A3 INC DPTR Increment data pointer by one
4110 F0 MOVX @DPTR,AMove accumulator content to external memory location pointed by data pointer
4111 STOP 80 FE SJMP STOP Halt
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 memory4100 00-
Enter the opcode Edit Program memory4100 00-74 <CR>
Enter 74H
Edit Program memory4101 00-22 <CR>
Enter 22HEdit Program memory4102 00-. <CR>
After entering the program came back to command prompt by entering ‘.’
Micro-51#_
2. To enter/view data to data memoryThe 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 memory4400 00-
Enter the opcode
Department of ECE, VKCET Page 11
08.607 MICROCONTROLLER LAB
Edit Program memory4400 00-74 <CR>
Enter 74HEdit Program memory4401 00-. <CR>
After entering the program came back to command prompt by entering ‘.’Micro-51#_
3. To enter data/modify to 8051 registersThe procedure is as follows:
Micro-51#R
Enter register view/modify commandInitially register A is displayed
Register ViewA = 80 - _
Press enter or ‘-‘ key to subsequent or the previous registerChange the data to any register if desired and enterTo terminate enter ‘.’ And return command prompt
4. To view 8051 internal RAMMicro-51#ir 40
Enter internal RAM commandINTERNAL RAM VIEWADDRESS – 40 - 18
Enter to get command prompt
5. To enter assembly language program by assembler Micro-51#a <CR>
Enter assembler commandORIGIN :
Origin is displayedORIGIN : 4100 <CR>
Enter the origin of program
Department of ECE, VKCET Page 12
08.607 MICROCONTROLLER LAB
4100
Address is displayed4100MOV A,#23 <CR>
Enter the instruction4100 74 23MOV A,#23
Assembler gives the opcode and displayed in the LCDEnter for next instruction
Enter ‘.’ For return to command prompt
6. To execute a programMicro-51#go 4100 <CR>
Go command followed by 16 bit address (Program memory) and enterIf 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 locations2. 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 RAM5. 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.
ADDRESS LABEL OPCODE & OPERANDS MNEMONICS COMMENTS
4100 MAIN 78 11 MOV R0,#11 R0 = data 14102 79 FF MOV R1,#FF R1 = data 24104 E8 MOV A,R0 A = R04105 29 ADD A,R1 A = A + R14106 FA MOV R2,A R2 = A4107 HALT 80 FE SJMP HALT Halt the program
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 LABEL OPCODE & OPERANDS MNEMONICS COMMENTS
4100 MAIN 75 D0 08 MOV PSW,#08 PSW= 0000 1000 , Where RS1=0 and RS0=1 for bank 1
4103 79 FF MOV R1,#FF R1=data 14105 7A 10 MOV R2,#10 R2 =data 24107 E9 MOV A,R1 A=R14108 9A SUBB A,R2 A = A – R24109 F8 MOV R0,A R0=A410A HALT 80 FE SJMP HALT Halt the program
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 MNEMONICS COMMENTS
4100 MAIN 90 FF 22 MOV DPTR,#FF22
DPTR=FF22H, IO mapping address of digital input switches connected to port 0
4103 E0 MOVX A,@DPTR Reading the Port 0 switches and A=[DPTR]
4104 A3 INC DPTR
Increment DPTR to FF23H, IO mapping address of digital output LEDs connected to port 0
4105 23 RL A Rotate A to left
4106 F0 MOVX @DPTR, ASend the status of switches to LEDs connected to Port 0
4107 80 F8 SJMP MAIN Repeat the program
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 KitTheory:
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 F1HData 2 = F1 10HPerform 8 bit additionResult 1 = Add lower bytes = F1 + 10 = 01HResult 2 = Add upper bytes with carry of previous addition = 10 + F1 + 01 = 02HSum = 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 F1HData 2 = F1 10HPerform 8 bit subtractionResult 1 = Subtract lower bytes = F1 - 10 = E1HResult 2 = Subtract upper bytes with borrow of previous subtraction = 10 - F1 - 00 =
1FHSum = 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
Department of ECE, VKCET Page 17
Yes
If carry
Start
Move lower order byte of data 1 to AccumulatorMove lower order byte of data 2 to an internal RAM register
Add Accumulator and register contentMove 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 additionMove result in A, which is upper byte of the sum to internal RAM register
Save all the results to destination
Stop
Write a 1 to Accumulator Clear Accumulator
No
08.607 MICROCONTROLLER LAB
b) 16 bit subtraction
Department of ECE, VKCET Page 18
No
Yes
Start
Move lower order byte of data 1 to AccumulatorMove lower order byte of data 2 to an internal RAM register
Subtract Accumulator and register contentMove 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 additionMove result in A, which is upper byte of the difference to internal RAM register
Save all the results to destination
Stop
If borrow
Write a FF to Accumulator Clear Accumulator
08.607 MICROCONTROLLER LAB
Programs:1. 16 bit addition
ADDRESS LABEL OPCODE & OPERANDS MNEMONICS COMMENTS
4100 MAIN 90 42 00 MOV DPTR,#4200Load starting address of external RAM where data are saved to data pointer
4103 E0 MOVX A,@DPTR Move lower byte of data 1 to A4104 F8 MOV R0,A Save A content to R0 4105 A3 INC DPTR Increment dptr to point next byte
4106 E0 MOVX A, @DPTR Move upper byte of data 1 to A
4107 F9 MOV R1,A Save A content to R14108 A3 INC DPTR Increment dptr to point next byte4109 E0 MOVX A, @DPTR Move lower byte of data 2 to A410A FA MOV R2,A Save A content to R2410B A3 INC DPTR Increment dptr to point next byte410C E0 MOVX A, @DPTR Move upper byte of data 2 to A410D FB MOV R3,A Save A content to R1410E E8 MOV A,R0 Move lower byte of data 1 to A410F 2A ADD A,R2 Add lower bytes of data
4110 A3 INC DPTR Increment dptr to point RAM location to store result
4111 F0 MOVX @DPTR,A Store lower byte of the result4112 E9 MOV A,R1 Move upper byte of data 1 to A
4113 3B ADDC A,R3 Add upper bytes with carry of previous operation
4114 A3 INC DPTR Increment dptr to point RAM location to store result
4115 F0 MOVX @DPTR,A Store lower byte of the result
4116 50 04 JNC LOOP1 If no carry in addition, go to LOOP1, else next instruction
4118 74 01 MOV A,#01 Load 1 to A411A 80 01 SJMP LOOP2 Go to LOOP2411C LOOP1 E4 CLR A Clear A
411D LOOP2 A3 INC DPTR Increment dptr to point RAM location to store carry
411E F0 MOVX @DPTR,A Store carry 411F STOP 80 FE SJMP STOP Halt the program
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 = 042. 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 2’s complement form)2. 16 bit subtraction
ADDRESS LABEL OPCODE & OPERANDS MNEMONICS COMMENTS
4100 MAIN D2 D0 SETB PSW.3 Set RS0 bit of PSW, then PSW= 0000 1000, to select bank 1
4102 78 F1 MOV R0,#F1 Load lower byte of data 1 to R0
4104 79 10 MOV R1,#10 Load upper byte of data 1 to R14106 7A 10 MOV R2,#10 Load lower byte of data 2 to R2
4108 7B F1 MOV R3,#F1 Load upper byte of data 2 to R3410A E8 MOV A,R0 Move lower byte of data 1 to A410B 9A SUBB A,R2 Subtract lower bytes
410C 90 42 00 MOV DPTR,#4200
Load starting address of external RAM to store the result in dptr
410F F0 MOVX @DPTR,A Store the lower byte of the result
4110 E9 MOV A,R1 Move upper byte of the data 1 to A
4111 9B SUBB A,R3 Subtract upper bytes with the borrow of the previous subtraction
4112 A3 INC DPTR Increment DPTR to point next external RAM location
4113 F0 MOVX @DPTR,A Store upper byte of the result
4114 40 03 JC LOOP1 If borrow go to LOOP1, else next instruction
4116 E4 CLR A Clear A4117 80 02 SJMP LOOP2 Go to LOOP24119 LOOP1 74 FF MOV A,#FF Load FF to A
411B LOOP2 A3 INC DPTR Increment dptr to point next external RAM location
411C F0 MOVX @DPTR,A Store borrow
411D STOP 80 FE SJMP STOP Halt the program
Department of ECE, VKCET Page 20
08.607 MICROCONTROLLER LAB
Relative address calculation:1. For LOOP1, 4119 – 4116 = 032. For LOOP2, 411B – 4119 = 023. For STOP, 411D – 411F = FEProcedure:
1. 16 bit additionSteps
1. Enter the program (starting address 4100H)2. Reset the kit3. Enter the input (starting address 4200H)4. Reset the kit5. Execute the program6. Reset the kit7. Observe the output
2. 16 bit subtractionSteps
1. Enter the program (starting address 4100H)2. Reset the kit3. Execute the program4. Reset the kit5. 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 onwardsEquipments Required:
Microcontroller KitTheory:
To perform 8-bit multiplication and division, the instructions MUL AB and DIV AB can be used.Flowchart: a) 8-bit multiplication
b) 8-bit division
Department of ECE, VKCET Page 22
Start
Move multiplicand to AMove 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
Start
Move dividend to AMove divisor to B
Divide A and B content
Store the quotient in A and reminder in B to the destination
Stop
08.607 MICROCONTROLLER LAB
Program:
ADDRESS LABEL OPCODE & OPERANDS MNEMONICS COMMENTS
4100 MAIN 90 42 00 MOV DPTR,#4200
Load starting address of external RAM where data are saved to data pointer
4103 E0 MOVX A,@DPTR Move multiplicand/dividend to A4104 F9 MOV R1,A Save multiplicand/dividend to R1
4105 A3 INC DPTR Increment dptr to point multiplier/divisor
4106 E0 MOVX A, @DPTR Move multiplier/divisor to A
4107 F5 F0 MOV B,A Move multiplier/divisor to B4109 FA MOV R2,A Save multiplier/divisor to R2410A E9 MOV A,R1 Move saved multiplicand to A
410B A4 MUL ABPerform multiplication, results A = Lower byte of product and B = Upper byte of the result
410C 78 30 MOV R0,#30 Load internal RAM scratch pad address to R0 as data pointer
410E F6 MOV @R0,A Save lower byte of the product to internal RAM
410F 08 INC R0 Increment R0 to point next location
4110 A6 F0 MOV @R0,B Save upper byte of the product to internal RAM
4112 E9 MOV A,R1 Move dividend to A4113 8A F0 MOV B,R2 Move divisor to B
4115 84 DIV AB Perform division, results A = quotient and B = reminder
4116 08 INC R0 Increment R0 to point next location to store result
4117 F6 MOV @R0,A Store quotient
4118 08 INC R0 Increment R0 to point next location to store result
4119 A6 F0 MOV @R0,B Store reminder411B STOP 80 FE SJMP STOP Halt the program
Department of ECE, VKCET Page 23
08.607 MICROCONTROLLER LAB
Procedure:Steps
1. Enter the program (starting address 4100H)2. Reset the kit3. Enter the input (starting address 4200H)4. Reset the kit5. Execute the program6. Reset the kit7. 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 KitTheory:
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:
Department of ECE, VKCET Page 26
Yes
No
Count = Count -1
If count = 0
Store the product as result
Stop
Start
Assign a register for 8-bit product = 0Assign a register for counter = 8 (for shifting)Store multiplicand to a register, where upper nibble as 0Store multiplier to two registers, where upper nibble as 0
pr
Shift product to left by 1 bit Shift multiplier to left by 1 bit
Yes
NoIs carry for multiplier?
Product = Product + Multiplicand
08.607 MICROCONTROLLER LAB
Program:
ADDRESS LABEL OPCODE & OPERANDS MNEMONICS COMMENTS
4100 MAIN 78 00 MOV R0,#00 Clear product register
4102 7B 08 MOV R3,#08 Set counter = 8
4104 90 42 00 MOV DPTR,#4200 Point external memory for multiplicand
4107 E0 MOVX A,@DPTR Load multiplicand to Acc.
4108 54 0F ANL A,#0F Mask upper nibble to ensure 4-bit multiplicand
410A F9 MOV R1,A Store multiplicand
410B A3 INC DPTR Point multiplier in external memory
410C E0 MOVX A,@DPTR Load multiplier to Acc.
410D 54 0F ANL A,#0F Mask upper nibble to ensure 4-bit multiplier
410F FA MOV R2,A Store multiplier 4110 NEXT E8 MOV A,R0 Load product to Acc.4111 23 RL A Rotate product left by one bit
4112 C2 E0 CLR ACC.0 Clear LSB of product (for shifting operation)
4114 F8 MOV R0,A Store shifted product4115 EA MOV A,R2 Load multiplier to Acc.4116 33 RLC A Rotate multiplier through carry
4117 C2 E0 CLR ACC.0 Clear LSB of multiplier (for shifting operation)
4119 FA MOV R2,A Store shifted multiplier
411A 50 03 JNC NOADDIf no carry in the shifting operation skip addition, else add product and multiplicand
411C E8 MOV A,R0 Load product to Acc.
411D 29 ADD A,R1 Add product and multiplicand (8-bit addition)
411E F8 MOV R0,A Store result to product
411F NOADD DB EF DJNZ R3,NEXTDecrement counter by one and if not equal to zero go to next shift-add operation, else store the result
4121 A3 INC DPTR Increment pointer to store result4122 E8 MOV A,R0 Load the product to Acc.4123 F0 MOVX @DPTR,A Store product to external memory4124 HERE 80 FE SJMP HERE Halt
Department of ECE, VKCET Page 27
08.607 MICROCONTROLLER LAB
Procedure:Steps
1. Enter the program (starting address 4100H)2. Reset the kit3. Enter the input (starting address 4200H)4. Reset the kit5. Execute the program6. Reset the kit7. 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 4203HEquipments Required:
Microcontroller KitTheory:
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 remainderAn 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:
Department of ECE, VKCET Page 30
No
Yes
Yes
LCM = number1 + LCM
No
Divide number1 by number2
Is reminder = 0?
Store the results LCM and HCF
Stop
Assign a register for HCF
number1 = HCFnumber2 = reminder
HCF = number2
Is reminder = 0?
Start
Assign a register for number1Assign a register for number2
Divide LCM by number2
Assign a register for LCM and LCM = number1
08.607 MICROCONTROLLER LAB
Program:ADDRE
SS LABEL OPCODE & OPERANDS MNEMONICS COMMENTS
4100 MAIN 90 42 00 MOV DPTR,#4200 Point external memory for inputs
4103 E0 MOVX A,@DPTR Load number1 to Acc.4104 F8 MOV R0,A Save number1 to R04105 A3 INC DPTR Point number24106 E0 MOVX A,@DPTR Load number2 to Acc.4107 F9 MOV R1,A Save number2 to R14108 E8 MOV A,R0 Load number1 to Acc.4109 FA MOV R2,A Set R2 for LCM and set number1 as LCM410A LCM 89 F0 MOV B,R1 Load number2 to B410C 84 DIV AB Divide number1 by number2410D E5 F0 MOV A,B Move B (reminder) to Acc.410F 60 05 JZ HCF If Acc = 0, go to find HCF, else next step 4111 NEXT EA MOV A,R2 Move LCM to Acc.4112 28 ADD A,R0 Acc = number1 + LCM4113 FA MOV R2,A Store Acc to R2 (as LCM)4114 80 F4 SJMP LCM Go to next check4116 HCF E9 MOV A,R1 Move number2 to Acc.4117 FB MOV R3,A Set R3 for HCF and set number2 as HCF4118 89 F0 MOV B,R1 Load number2 to B411A E8 MOV A,R0 Move number1 to Acc.411B 84 DIV AB Divide number1 by number2411C E5 F0 MOV A,B Move B (reminder) to Acc.411E 60 05 JZ RESULT If Acc. = 0, go to store result, else next step4120 F9 MOV R1,A number2 = reminder4121 EB MOV A,R3 Move HCF to Acc.4122 F8 MOV R0,A number1 = HCF4123 80 F1 SJMP HCF Go to next check4125 RESULT EA MOV A,R2 Load LCM to Acc.
4126 A3 INC DPTR Point the external memory location for storing LCM
4127 F0 MOVX @DPTR,A Store LCM
4128 A3 INC DPTR Point the external memory location for storing HCF
4129 EB MOV A,R3 Load HCF to Acc.412A F0 MOVX @DPTR,A Store HCF412B STOP 80 FE SJMP STOP Halt the program
Department of ECE, VKCET Page 31
08.607 MICROCONTROLLER LAB
Procedure:Steps
1. Enter the program (starting address 4100H)2. Reset the kit3. Enter the inputs (starting address 4200H)4. Reset the kit5. Execute the program6. Reset the kit7. 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 portsEquipments 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-termsAn 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.7Then we can assign the input variables as switches; SW15 as d, SW14 as c, SW13 as b,
SW12 as aMemory 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 Output
a b c d f0 0 0 0 10 0 0 1 00 0 1 0 00 0 1 1 00 1 0 0 00 1 0 1 00 1 1 0 00 1 1 1 01 0 0 0 01 0 0 1 11 0 1 0 01 0 1 1 01 1 0 0 01 1 0 1 01 1 1 0 01 1 1 1 1
Department of ECE, VKCET Page 34
08.607 MICROCONTROLLER LAB
Flowchart:
Department of ECE, VKCET Page 35
Point digital output LEDs, DPTR ← FF23H
Perform a’b’c’d’, ab’c’d ,and ab’cd’ and save the result in bit addressable RAM area
Perform a’b’c’d’ + ab’c’d + ab’cd’ and save the status
Start
Point digital output LEDs, DPTR ← FF23H, and clear L24
Read P1.0, P1.1, P1.2 and P1.3
Send the status of Boolean expression to digital output LED
Set Port 1 as input
08.607 MICROCONTROLLER LAB
Program:
ADDRESS LABEL OPCODE & OPERANDS MNEMONICS COMMENTS
4100 MAIN 90 FF 23 MOV DPTR,#FF22Point the address of memory mapped IO digital output LED L24, DPTR ← FF23H
4103 E4 CLR A A ← 004104 F0 MOVX @DPTR,A [DPTR] ← A
4105 REPEAT 75 90 FF MOV P1,#FF P1 ← FFH , to set as all pins as input
4108 A2 90 MOV C,90 CY ← d, where 90 is direct address of P1.0
410A B3 CPL C CY ← d’
410B B0 91 ANL C,/91 CY ← c’d’, where 91 is direct address of P1.1
410D B0 92 ANL C,/92 CY ← b’c’d’, where 92 is direct address of P1.2
410F B0 93 ANL C,/93 CY ← a’b’c’d’, where 93 is direct address of P1.3
4111 92 00 MOV 00,C [00]← a’b’c’d’4113 A2 90 MOV C,90 CY ← d4115 B0 91 ANL C,/91 CY ← c’d4117 B0 92 ANL C,/92 CY ← b’c’d4119 82 93 ANL C,93 CY ← ab’c’d411B 92 01 MOV 01,C [01] ← ab’c’d411D 82 90 MOV C,90 CY ← d411F B3 CPL C CY ← d’4120 82 91 ANL C,91 CY ← cd’4122 B0 92 ANL C,/92 CY ← b’cd’4124 82 93 ANL C,93 CY ← ab’cd’4126 72 01 ORL C,01 CY ← ab’c’d + ab’cd’
4128 72 00 ORL C,00 CY ← a’b’c’d’ + ab’c’d + ab’cd’
412A E4 CLR A A ← 00
412B 33 RLC A Acc.0 ← a’b’c’d’ + ab’c’d + ab’cd’
412C F0 MOVX @DPTR,A [DPTR] ← A412D 80 SJMP REPEAT Repeat the process
Department of ECE, VKCET Page 36
08.607 MICROCONTROLLER LAB
Procedure:Steps
1. Connect P17 and P18 using FRC 2. Switch on the system3. Enter the program (starting address 4100H)4. Terminate from program edit mode5. Execute program6. Give the inputs a, b, c and d using switches SW15, SW14, SW13 and SW12
respectively7. 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 KitTheory:
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:( 5 4 2 8 1 ) ( 4 5 2 8 1 ), Here, algorithm compares the first two elements, and swaps them.( 4 5 2 8 1 ) ( 4 2 5 8 1), Swap since 5 > 2( 4 2 5 8 1 ) ( 4 2 5 8 1 ),No Swap since 8 > 5( 4 2 5 8 1 ) ( 4 2 5 1 8 ), Swap since 8>1Second Pass:( 4 2 5 1 8 ) ( 2 4 5 1 8 ), Swap since 4 > 2( 2 4 5 1 8 ) ( 2 4 5 1 8 ), No swap since 5 > 4( 2 4 5 1 8 ) ( 2 4 1 5 8 ), Swap since 5 > 1( 2 4 1 5 8 ) ( 2 4 1 5 8 ), No swap since 8 > 5Third Pass:( 2 4 1 5 8 ) ( 2 4 1 5 8 ), No swap since 4 > 2( 2 4 1 5 8 ) ( 2 1 4 5 8 ), Swap since 4 > 1( 2 1 4 5 8 ) ( 2 1 4 5 8 ), No swap since 5 > 4( 2 1 4 5 8 ) ( 2 1 4 5 8 ), No swap since 8 > 5Fourth Pass:( 2 1 4 5 8 ) ( 1 2 4 5 8 ), Swap since 2 > 1( 1 2 4 5 8 ) ( 1 2 4 5 8 ), No swap( 1 2 4 5 8 ) ( 1 2 4 5 8 )( 1 2 4 5 8 ) ( 1 2 4 5 8 )
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
Department of ECE, VKCET Page 40
Start
DPTR ← 4201HCounter1, R0 ← n-1 and counter2 R1← n-1
R2 ← [DPTR]
DPTR = DPTR + 1
A ← [DPTR]
If R2 > A ?
Yes
No
If R0 > 0 ?
Yes
No
[DPTR] ← R2
DPTR = DPTR - 1
[DPTR] ← A
DPTR = DPTR + 1
Counter1, R0 = R0 -1
Counter2, R1 = R1 -1
If R1 > 0 ?
No
Stop
Yes
08.607 MICROCONTROLLER LAB
b) Factorial
Department of ECE, VKCET Page 41
Start
R0 ← n R1 ← 1
Factorial R2 ← 1
If R0 = 0 or 1?Yes
No
Multiply A and B
A ← R0B ← R1
R2 ← AR0 ← A
R1 = R1 + 1
If R1= R0?No
YesSave R2 content as
result
Stop
08.607 MICROCONTROLLER LAB
Programs:a) Sorting numbers
ADDRESS LABEL OPCODE & OPERANDS MNEMONICS COMMENTS
4100 MAIN 90 42 00 MOV DPTR,#4200 DPTR ← 4200H4103 E0 MOVX A,@DPTR A ← [DPTR], n4104 14 DEC A A ← n – 14105 F8 MOV R0,A R0 ← n – 1, counter14106 F9 MOV R1,A R1 ← n – 1, counter24107 A3 INC DPTR DPTR = DPTR + 14108 NEXT E0 MOVX A,@DPTR A ← [DPTR]4109 FA MOV R2,A R2 ← A410A A3 INC DPTR DPTR ← DPTR + 1410B E0 MOVX A,@DPTR A ← [DPTR]410C C3 CLR C CY ← 0410D 9A SUBB A,R2 A ← A – R2 - CY
410E 50 08 JNC NOSWAP If no carry, go to label NOSWAP
4110 E0 MOVX A,@DPTR A ← [DPTR]4111 SWAPING CA XCH A,R2 A ↔ R24112 F0 MOVX @DPTR,A [DPTR] ← A4113 15 82 DEC DPL DPL = DPL -14115 EA MOV A,R2 A ← R24116 F0 MOVX @DPTR,A [DPTR] ← A4117 A3 INC DPTR DPTR = DPTR + 1
4118 NOSWAP D8 EE DJNZ R0,NEXT R0 = R0 -1 and if R0 ≠ 0 go to label NEXT for sorting
411A D9 E4 DJNZ R1,MAIN R1 = R1 – 1 and if R1 ≠ 0 go to label MAIN for next pass
411C STOP 80 FE SJMP STOP Stop sorting
Department of ECE, VKCET Page 42
08.607 MICROCONTROLLER LAB
b) Factorial of a number
ADDRESS LABEL OPCODE & OPERANDS MNEMONICS COMMENTS
4100 MAIN 90 44 00 MOV DPTR,#4400 DPTR ← 4400H, address of number n
4103 E0 MOVX A,@DPTR A ← [DPTR]
4104 F8 MOV R0,A R0 ← A, save the number
4105 F5 30 MOV 30,A [30H] ← A, save the number to compare
4107 79 01 MOV R1,#01 R1 ← 014109 7A 01 MOV R2,#01 R2 ← 01
410B B4 00 02 CJNE A,#00, NEXT If A ≠ 0, go to the label NEXT
410E 80 10 SJMP RESULTGo to the label RESULT to store the result
4110 NEXT B4 01 02 CJNE A,#01,CONTINUE If A ≠ 1, go to the label CONTINUE
4113 80 0B SJMP RESULTGo to the label RESULT to store the result
4115 CONTINUE E8 MOV A,R0 A ← R04116 89 F0 MOV B,R1 B ← R14118 A4 MUL AB BA ← A x B4119 FA MOV R2,A R2 ← A411A F8 MOV R0,A R0 ← A411B 09 INC R1 R1 = R1 +1411C E9 MOV A,R1 A ← R1
411D B5 30 F5 CJNE A,30,CONTINUE If A ≠ n go to label continue
4120 RESULT A3 INC DPTR DPTR = DPTR +14121 EA MOV A,R2 A ← R24122 F0 MOVX @DPTR,A [DPTR] ← A4123 STOP 80 FE SJMP STOP Stop the process
Department of ECE, VKCET Page 43
08.607 MICROCONTROLLER LAB
Procedure:a) Sorting:Steps
1. Enter the program (starting address 4100H)2. Terminate from program enter mode3. Enter the total numbers (in 4200H) and numbers (from 4201H)4. Terminate from edit mode 5. Execute the program6. Reset the kit7. Verify the result
b) Factorial:Steps
1. Enter the program (starting address 4100H)2. Terminate from program enter mode3. Enter the number (in 4400H) 4. Terminate from edit mode 5. Execute the program6. Reset the kit7. Verify the result
Department of ECE, VKCET Page 44
08.607 MICROCONTROLLER LAB
Expt. No.8 INTERFACING WITH 8-BIT ADCObjective: To interface with 8-bit ADC and display digital data from ADC using LEDsEquipments Required:
1. Microcontroller Kit2. ADC 0809 interfacing card3. Flat ribbon cable (50 pin)4. Connecting probes5. Multimeter
Theory:ADC Interface cardAn analog-to-digital converter (ADC, A/D or A to D) is a device that converts a continuous-
time 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 1The interface card contains ADC0809 chip.
ADC0809Some features of the ADC0809 chip are:
6. Easy to interface7. Successive approximation type8. 8-analog input channels selected by 3-bit address lines9. 0-5V input range with single 5V Vcc10. TTL output voltage11. 8-bit resolution12. Maximum conversion time is 100µs
Department of ECE, VKCET Page 45
08.607 MICROCONTROLLER LAB
Pinout diagram
Pins and functions Pins Functions
IN0-IN7 8 analog input channels2-8 – 2-1 8-bit digital data output lines
ADD C, ADD B and ADD A
3-bit address linesADD C ADD B ADD A Selected analog lines
0 0 0 IN00 0 1 IN10 1 0 IN20 1 1 IN31 0 0 IN41 0 1 IN51 1 0 IN61 1 1 IN7
ALE Address latch enable, if it is a HIGH to LOW signal valid address in the address bits are latched to the ADC
START Start of conversion, input signal to ADC. If it is a high signal starts ADC
EOC End of conversion, output signal from ADC. If it is a ‘high’ signal conversion is over and data bus has valid data
OUTPUT ENABLE Input signal to ADC, to enable ADC output dataCLOCK Clock signal, its frequency determines sampling rate, maximum
frequency is 1280 kHzVcc +5V power supply
GND GroundVref(+) +ve reference voltage (Vref1)Vref(-) -ve reference voltage (Vref2)
Department of ECE, VKCET Page 46
08.607 MICROCONTROLLER LAB
Timing diagram
Timing characteristicsf, clock frequency: Typically 640 kHztc, conversion time: Typically 100µstWS, start pulse width: Typically 100nstWALE, ALE pulse width: Typically 100nsts, address set up time: Typically 25nstH, address hold time: Typically 25nstEOC, EOC delay time: Typically 8+2 µs clock periods
Connection details and memory mapped IO addressUsing 50 pin FRC connect microcontroller kit to interface card as shown in figure 3:
Figure 3Department of ECE, VKCET Page 47
08.607 MICROCONTROLLER LAB
The clock frequency to ADC is derived from master clock of kit 11.0592 MHzThe memory mapped IO address for the signals of ADC0809 chip are:
Pins AddressD7-D0 FFC0H
ADD A, ADD B, ADD C ALE and OE
FFC8H
SOC FFD0HEOC FFD8H
The data in Accumulator for each memory mapped IO operation:
Address Accumulator DataFFC0H ADC digital output FFC8H IN0 select, ALE low
and OE high0001 0000b
= 10HIN0 select, ALE high
and OE high0001 1000b
= 18HIN1 select, ALE low
and OE high0001 0001b
= 11HIN1 select, ALE high
and OE high0001 1001b
= 19HFFD0H SOC low 00H
SOC high 01HFFD8H EOC low 00H
EOC high 01H
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 positionThere 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
FlowchartFor 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)
Department of ECE, VKCET Page 50
Start
ALE = 0, C B A = 0 0 0, OE = 1
ALE = 1, C B A = 0 0 0, OE = 1
SOC = 1
SOC = 0
Read D0-D7, A← Digital data
No
LEDs ← A
If EOC = 1?
Yes
ALE = 0, C B A = 0 0 0, OE = 1
08.607 MICROCONTROLLER LAB
Program:
ADDRESS LABEL OPCODE & OPERANDS MNEMONICS COMMENTS
4100H MAIN 90 FF C8 MOV DPTR,#FFC8DPTR ← FFC8H, memory mapped IO address of ALE, address bit and OE pins
4103 74 10 MOV A,#10 A ← 0001 0000b, OE =1 ALE = 0 and C B A = 000
4105 F0 MOVX @DPTR,A [FFC8] ← A
4106 74 18 MOV A,#18 A ← 0001 1000b, OE =1 ALE = 1 and C B A = 000
4108 F0 MOVX @DPTR,A [FFC8] ← A
41009 74 18 MOV A,#10 A ← 0001 0000b, OE =1 ALE = 0 and C B A = 000
410B F0 MOVX @DPTR,A [FFC8] ← A
410C 90 FF D0 MOV DPTR,#FFD0 DPTR ← FFC8H, memory mapped IO address of SOC
410F 74 01 MOV A,#01 A ← 0000 0001b, SOC = 1
4111 F0 MOVX @DPTR,A [FFD0] ← A
4112 E4 CLR A A ← 0000 0000b, SOC = 0
4113 F0 MOVX @DPTR,A [FFD0] ← A
4114 90 FF D8 MOV DPTR,#FFD8 DPTR ← FFD8H, memory mapped IO address of EOC
4117 WAIT E0 MOVX A,@DPTR [FFD8] c A
4118 30 E0 FC JNB E0, WAIT If Acc.0 (address E0) is 0, go to label WAIT
411B 90 FF C0 MOV DPTR,#FFC0DPTR ← FFC0H, memory mapped IO address of data D7-D0
411E E0 MOVX A,@DPTR A← [FFC0]
411F F0 FF 23 MOV DPTR,#FF23DPTR ← FF23H, memory mapped IO address of LEDs L17 to L24
4122 F0 MOVX @DPTR,A [FF23] ← A4123 80 DB SJMP MAIN 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 FRC2. Turn on power supply3. Enter the program (starting address 4100H)4. Terminate from program enter mode5. Check the position of jumpers J2 and J5 in A6. Execute the program7. 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)
Output data (in binary format)
012345
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 KitTheory:
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
Department of ECE, VKCET Page 54
Start
DPTR ← 4200H
A ← [DPTR], B ← A, and R0 ← A, number
Perform A x B
[DPTR] ← A, least byte of square
DPTR = DPTR + 1
If A = R2 ?Yes
No
If A > R2
No
R2 ← 01, divisor for square root
A ← R0, number as dividendB ← R2
Perform A / B
R3 ← A, quotient as square
R2 = R2 +1DPTR = DPTR +1
[DPTR] ← R3, result
Stop
Yes
08.607 MICROCONTROLLER LAB
b) Fibonacci series
Department of ECE, VKCET Page 55
Start
DPTR = 4200HR0 ← [DPTR], length of sequence as counter
If R0 = 0?
Yes
No
R0 = R0 - 1
DPTR = 4300H[DPTR] ← 0
DPTR = DPTR + 1A ← [DPTR]
DPTR = DPTR – 1R1 ← [DPTR]
A ← A + R1
Stop
DPTR = DPTR +1R0 = R0 - 1
[DPTR] ← 1
DPTR = DPTR + 1[DPTR] ← A
R0 = R0 - 1
08.607 MICROCONTROLLER LAB
Programs:a) Square and square root of a number
ADDRESS LABEL OPCODE & OPERANDS MNEMONICS COMMENTS
4100 MAIN 90 42 00 MOV DPTR,#4200 DPTR ← 4200H, pointer for number
4103 E0 MOVX A,@DPTR A ← [DPTR]4104 F5 F0 MOV B,A B ← A4106 F8 MOV R0,A R0 ← A4107 A4 MUL AB BA ← A x B4108 A3 INC DPTR DPTR ← DPTR + 1
4109 F0 MOVX @DPTR,A [DPTR] ← A, least byte of square
410A E5 F0 MOV A,B A ← B410C A3 INC DPTR DPTR ← DPTR + 1
410D F0 MOVX @DPTR,A [DPTR] ← A, most byte of square
410E 7A 01 MOV R2,#01 R2 ← 01, divisor for finding square root
4110 REPEAT E8 MOV A,R0 A ← R0, number4111 8A F0 MOV B,R2 B ← R2, divisor
4113 84 DIV AB A / B, quotient in A and reminder in B
4114 FB MOV R3,A R3 ← A, quotient as square root
4115 C3 CLR C Clear CY flag
4116 9A SUBB A,R2 A ← A – R2, comparing quotient and divisor
4117 50 05 JNC NEXT If A > R2, go to label NEXT, else next instruction
4119 RESULT A3 INC DPTR DPTR ← DPTR +1411A EB MOV A,R3 A ← R3, quotient is the result411B F0 MOVX @DPTR,A [DPTR] ← A, save the result411C 80 05 SJMP STOP Go to label STOP
411E NEXT 60 F9 JZ RESULT If A = R2, go to label RESULT, else next instruction
4120 0A INC R2 R2 ← R2 + 1, divisor is incremented by one
4121 80 ED SJMP REPEAT Go to label REPEAT for next checking
4123 STOP 80 FE SJMP STOP Stop the process
Department of ECE, VKCET Page 56
08.607 MICROCONTROLLER LAB
b) Fibonacci series
ADDRESS LABEL OPCODE & OPERANDS MNEMONICS COMMENTS
4100 MAIN 90 42 00 MOV DPTR,#4200 DPTR ← 4200H, pointer to the length of the series
4103 E0 MOVX A,@DPTR A ← [DPTR]4104 F8 MOV R0,A R0 ← A, length as 4105 74 00 MOV A,#00 A ← 0, first number
4107 90 43 00 MOV DPTR,#4300DPTR ← 4300H, pointer to the starting address of destination
410A F0 MOVX @DPTR,A [DPTR] ← A
410B 18 DEC R0 R0 ← R0 – 1 , decrement counter by one
410C A3 INC DPTR DPTR ← DPTR + 1
410D 04 INC A A ← A + 1, second number
410E F0 MOVX @DPTR,A [DPTR] ← A
410F 18 DEC R0 R0 ← R0 – 1 , decrement counter by one
4110 NEXT 15 82 DEC DPL DPTR ← DPTR - 1
4112 E0 MOVX A,@DPTR A ← [DPTR], previous number
4113 F9 MOV R1,A R1 ← A4114 A3 INC DPTR DPTR ← DPTR + 1
4115 E0 MOVX A,@DPTR A ← [DPTR], current number
4116 29 ADD A,R1 A ← A + R1, next number4117 A3 INC DPTR DPTR ← DPTR + 14118 F0 MOVX @DPTR,A [DPTR] ← A
4119 D8 F5 DJNZ R0,NEXT
Decrement counter by one and if it is ≠ 0, go to label NEXT, else next instruction
411B 80 FE SJMP STOP 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 mode3. Enter the number (in 4200H) 4. Terminate from edit mode 5. Execute the program6. Reset the kit7. 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 mode3. Enter the length of the series (in 4200H) 4. Terminate from edit mode 5. Execute the program6. Reset the kit7. 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 KitDAC 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 1Two 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 outputFigure 3
DAC0800Some features of DAC0800 chip are:
1. 8-bit R/2R ladder, current output type fast DAC2. Interface directly with TTL, CMOS, etc type devices3. Complementary current outputs4. Wide power supply range: ±4.5V to ±18V5. Low power consumption: 33 mW at ±5V6. Low cost
Pin out diagram
Figure 4
Department of ECE, VKCET Page 60
08.607 MICROCONTROLLER LAB
Pins and functionsPins FunctionsVLC Threshold control, usually connected to ground
Iout and
Non-inverting and inverting output current
B1(MSB) to B8(LSB) Digital input
V+ and V- Positive and negative power suppliesVref(+) and
Vref(-)Reference voltages which determines the full scale output current
Compensation Compensation capacitor
Output current
Where
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: FFC0HDAC 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 1MCWAIT: DJNZ R7, WAIT ; Take 2MC x N
RET ; Take 2MCTotal time to execute this subprogram is Tdelay = 1MC + 2MC x N + 2MC
= 3MC + 2MC x NThe kit has 11.0592 MHz crystal frequency for microcontroller, then 1MC = 12 x T = 1.0851µsThen maximum delay by the program is by N=255 and is 556.65 µsFor a standard delay program of 500µs, put Tdelay = 500 µs, then N = 228.89 ≈ 228 = E4HSimilarly 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 = 100µs.
Then count value for delay program is N = (100 µs –(3x 1.0851µs)) / (2x1.0851µs) = 44.57 ≈ 44 = 2CHIref = 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
Department of ECE, VKCET Page 64
Start
DPTR ← FFC0H
A = 7FH
Wait 100µs
[DPTR] ← A
A = FFH
[DPTR] ← A
Wait 100µs
08.607 MICROCONTROLLER LAB
b) Rectangular wave of frequency 10 kHz, duty cycle 65% and peak amplitude 2VFor 10 kHz, T = 100µs and 65% duty cycle Ton = 0.65 T = 65µs and Toff = 0.35 T = 35µs.
Then count value for delay programs are: Non = (65µs – (3 x 1.0851µs)) / (2 x 1.0851µs) = 28.45 ≈ 1CH
Noff = (35µs – (3 x 1.0851µs)) / (2 x 1.0851µs) = 14.6 ≈ 0EHFor peak amplitude 2V, 7FH + (FFH-7FH)/2 = BFH
c) Saw tooth wave generation for maximum frequency and amplitude
Department of ECE, VKCET Page 65
Start
Start
DPTR ← FFC0H
A = 00H
[DPTR] ← A
A = A + 1
[DPTR] ← A
Wait 35µs
A = 7FH
DPTR ← FFC0H
Wait 65µs
[DPTR] ← A
A = BFH
08.607 MICROCONTROLLER LAB
d) Triangular wave form with maximum frequency and amplitude
Department of ECE, VKCET Page 66
Start
DPTR ← FFC0H
A = 00H
[DPTR] ← A
A = A + 1
If A = FFH ?
Yes
No
[DPTR] ← A
A = A - 1
If A = 00H ?No Yes
08.607 MICROCONTROLLER LAB
Programs:a) Square wave of 5kHz, ≈4V peak generation
ADDRESS LABEL OPCODE & OPERANDS MNEMONICS COMMENTS
4100 MAIN MOV DPTR,#FFC0DPTR ← FFC0H, memory mapped IO address of DAC1 data bus
REPEAT MOV A,#7F A ← 7FH, data for 0V MOVX @DPTR,A [DPTR] ← A ACALL DELAY Wait 100µs MOV A,#FF A ← FFH, data for ≈5V MOVX @DPTR,A [DPTR] ← A ACALL DELAY Wait 100µs SJMP REPEAT Repeat the process
SUBPROGRAM FOR DELAY
4200 DELAY MOV R0,#2C R0 ← Count value for delay 100µs
WAIT DJNZ R0,WAIT R0 ← R0 – 1, if R0 ≠ 0 go to label WAIT
RET Return to main program
Department of ECE, VKCET Page 67
08.607 MICROCONTROLLER LAB
b) Rectangular wave of frequency 10 kHz, duty cycle 65% and peak amplitude 2V
ADDRESS LABEL OPCODE & OPERANDS MNEMONICS COMMENTS
4100 MAIN MOV DPTR,#FFC0DPTR ← FFC0H, memory 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 35µs MOV A,#BF A ← FFH, data for ≈5V MOVX @DPTR,A [DPTR] ← A ACALL DELAY_T2 Wait 65µs SJMP REPEAT Repeat the process
SUBPROGRAM FOR DELAY
4200 DELAY_T1 MOV R0,#0E R0 ← Count value for delay 35µs
WAIT_T1 DJNZ R0,WAIT_T1 R0 ← R0 – 1, if R0 ≠ 0 go to label WAIT
RET Return to main program
4200 DELAY_T2 MOV R0,#1C R0 ← Count value for delay 65µs
WAIT_T2 DJNZ R0,WAIT_T2 R0 ← R0 – 1, if R0 ≠ 0 go to label WAIT
RET Return to main program
c) Sawtooth wave generation for maximum frequency and amplitude
ADDRESS LABEL OPCODE & OPERANDS MNEMONICS COMMENTS
4100 MAIN MOV DPTR,#FFC0DPTR ← FFC0H, memory mapped IO address of DAC1 data bus
CLR A A ← 0 for minimum output voltage
REPEAT MOVX @DPTR,A [DPTR] ← A INC A A ← A+ 1 SJMP REPEAT Repeat the process
Observred output wave form have Vpp = 9.1V and f = 720HZ
Department of ECE, VKCET Page 68
08.607 MICROCONTROLLER LAB
d) Triangular wave form with maximum frequency and amplitude ADDR
ESS LABEL OPCODE & OPERANDS MNEMONICS COMMENTS
4100 MAIN MOV DPTR,#FFC0
DPTR ← FFC0H, memory mapped IO address of DAC1 data bus
CLR A A ← 0 INCREMENT MOVX @DPTR, A [DPTR] ← A INC A A ← A+ 1
CJNE A, #FF, INCREMENT
If A = FFH (Maximum), go to label INCREMENT, else next step
DECREMENT MOVX @DPTR, A [DPTR] ← ADEC A A ← A - 1
CJNE A,#00, DECREMENT
If A = FFH (Minimum), go to label DECREMENT, else next step
SJMP INCREMENT Repeat the processObserved 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 FRC2. Connect DSO/CRO to the DAC output using header P2 using probe3. Turn on power supply4. Enter the program (starting address 4100H for main program)5. Terminate from program enter mode6. Execute the program7. 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 tableEquipments Required:
Microcontroller KitDAC 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 20 o
(in degree)
Vout
(V)Data input to
DAC (rounded value)
Data in hex
0 5 128 80H
20 6.7 171 ABH
40 8.21 210 D2H
60 9.33 238 EEH
80 9.92 253 FDH
100 9.92 253 FDH
120 9.33 238 EEH
140 8.21 210 D2H
160 6.7 171 ABH
180 5 128 80H
200 3.29 84 54H
220 1.79 45 2DH
240 0.67 17 11H
260 0.076 1 01H
280 0.076 1 01H
300 0.67 17 11H
320 1.79 45 2DH
340 3.29 84 54H
360 5 128 80H
Department of ECE, VKCET Page 71
08.607 MICROCONTROLLER LAB
Flowcharts
Department of ECE, VKCET Page 72
Start
R0 = 12H, count value for LUT
DPTR = 4200H, starting address of LUT
DPTR = DPTR +1
A ← [DPTR]
Push DPTR to stack memory
DPTR = FFC0H, address of DAC1
[DPTR] ← A
Pop stack memory content to DPTR
R0 = R0 - 1
If R0 = 0 ?
Yes
No
08.607 MICROCONTROLLER LAB
Program:
ADDRESS LABEL OPCODE & OPERANDS MNEMONICS COMMENTS
4100 MAIN MOV R0,#12 Counter = 12H, length of LUT
MOV DPTR,#4200 DPTR = 4200H, LUT starting address
NEXT MOVX A,@DPTR A ← [DPTR] INC DPTR DPTR ← DPTR + 1
PUSH DPH Save DPH content to default stack
PUSH DPL Save DPL content to stack
MOV DPTR,#FFC0
DPTR ← FFC0H, memrory mapped IO address of DAC1
MOVX @DPTR,A [DPTR] ← A, LUT element to DAC
POP DPL Load DPL content from stack POP DPH Load DPH content from stack
DJNZ R0, NEXT R0 = R0 – 1, if R0 > 0, go to label NEXT, else next step
SJMP MAIN Repeat the processLook-up Table
4200 LUT 80ABD2EEFDFDEED2AB80542D110101112D5480
Department of ECE, VKCET Page 73
08.607 MICROCONTROLLER LAB
Procedure:Steps :
8. Connect the DAC interface card and Microcontroller kit using 50-pin FRC9. Connect DSO/CRO to the DAC output using header P3 using probe10. Turn on power supply11. Enter the program (starting address 4100H for main program)12. Terminate from program enter mode13. Execute the program14. 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 KitTheory:
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 - For matrix B -
4300H 0 4400H 34301H 1 4401H 14302H 2 4402H 04303H 1 4403H 14304H 0 4404H 04305H 3 4405H 64306H 9 4406H 14307H 6 4407H 54309H 4 4409H 8
Department of ECE, VKCET Page 75
08.607 MICROCONTROLLER LAB
Matrix addition can be performed by accessing each location, adding the contents and saving the result to destination.
Flow chart
Department of ECE, VKCET Page 76
Start
DPTR ← 4200H, address of m
A ← [DPTR]
DPTR = DPTR +1, pointer to n
B ← A
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
B
08.607 MICROCONTROLLER LAB
Department of ECE, VKCET Page 77
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
If R0 = 0?
Yes
No
B
Stop
08.607 MICROCONTROLLER LAB
Program:
ADDRESS LABEL OPCODE & OPERANDS MNEMONICS COMMENTS
4100 MAIN MOV DPTR,#4200 DPTR ← 4200H, address of mMOVX A,@DPTR A ← [DPTR], m to AMOV B,A B ← AINC DPTR DPTR ← DPTR + 1, address of nMOVX A,@DPTR A ← [DPTR], n to AMUL AB A ← m x nMOV R0,A R0 ← m x n
MOV DPL,#00 DPL ← 00, for lower byte address of matrix elements
NEXT MOV DPH,#43 DPH ← 43H, for higher byte address of matrix A
MOVX A,@DPTR A ← [DPTR], matrix A elementMOV R1,A R1 ← A
MOV DPH,#44 DPH ← 44H, for higher byte address of matrix B
MOVX A,@DPTR A ← [DPTR], matrix B element
ADD A,R1 A ← A + R1, addition of matrix elements
MOV DPH,#45 DPH ← 45H, for higher byte address of result matrix
MOVX @DPTR,A [DPTR] ← A
INC DPTR DPTR ← DPTR + 1, next matrix element
DJNZ R0,NEXT R0 = R0 – 1, if R0 > 0 go to label NEXT
STOP SJMP STOP 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 mode3. Enter the dimension m x n of matrices in 4200H and 4201H 4. Enter the elements of matrix A in row-wise from 4300H onwards5. Enter the elements of matrix B in row-wise from 4400H onwards 6. Terminate from edit mode7. Execute the program8. Reset the kit9. 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 kitEquipments Required:
Microcontroller KitTheory: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
WhereI/D = 1, increment DDRAM address, cursor moves to rightI/D = 0, decrement DDRAM address, cursor moves to leftSH = 1, shift entire display according to I/DSH = 1, no shift of entire displayCGRAM – Character Generator RAMD = 1, entire display turn onD = 0, display turn off, but display data remains in DDRAMC = 1, cursor turn onC = 0, cursor disappear, but I/D preserves its dataB = 1, cursor blink onB = 0, cursor blink off
Department of ECE, VKCET Page 82
08.607 MICROCONTROLLER LAB
AC – Address Counter
DL = 1, 8-bit bus modeDL = 0, 4 bit bus modeN = 1, 2 line display modeN = 0, 1 line display modeF = 1, 5 x 10 dots fontF = 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 : FF04HR/W and RS : FF08H
Bit orientation for FF08H : 0000 00 R/W RSData 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 programSteps:
1. Start2. Write command byte to LCD for function setting. Command byte: 0011 1000 = 38H3. Wait minimum 40µs4. Write command byte to LCD for function setting. Command byte: 0000 0001 = 01H5. Wait minimum 1.64ms6. Write command byte to LCD for display on and cursor blinking. Command byte:
0000 1111 = 0FH7. Wait minimum 40µs8. Write command byte to LCD for entry mode: left entry (increment cursor to right).
Command byte: 0000 0110 = 06H9. Write command byte to LCD for DDRAM address. Command byte: 1000 0000 =
80H10. Wait minimum 40 µs11. Point starting address of LUT by DPTR, DPTR = 4200H12. A ← [DPTR], ASCII of character for message to A13. Push DPTR to stack14. Write data byte (ASCII) to LCD15. Wait minimum 40µs16. Pop DPTR from stack17. DPTR = DPTR +118. If A ≠ 0 (ASCII of NULL character) go to step 1019. Stop
LCD command writeSteps:
1. Start2. R7 ← A, save command byte3. DPTR = FF08H, memory mapped IO address of RW and RS pins of LCD4. 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 LCD7. A ← R7, command byte8. [DPTR] ← A, (E pin set to 1 by hard ware)9. Stop
LCD data writeSteps:
1. Start 2. R7 ← A, save data byte 3. DPTR = FF08H , memory mapped IO address of RW and RS pins of LCD4. A = 01H, RW = 0 (LCD write operation) and RS = 1 (LCD data register
selected)5. [DPTR] ← A6. DPTR = FF04H, memory mapped IO address of E and DB0-DB7 pins of LCD7. A ← R7, data byte8. [DPTR] ← A, (E pin set to 1 by hard ware)9. Stop
ProgramMain program
ORG 4100HMAIN: MOV A,#38H ;Function set command byte, 0011 1000b to A
LCALL CMD_WRITE ;Write command to LCD LCALL DELAY ;Wait minimum 40µsMOV A,#01H ;Clear display command byte, 0000 0001b to ALCALL CMD_WRITE ;Write command byte to LCDLCALL DELAY ;Wait minimum 2msMOV A,#0FH ;Display/cursor on/off command byte, 0000 1111b to ALCALL CMD_WRITE ;Write command byte to LCDLCALL DELAY ;Wait minimum 40µsMOV A,#06H ;Entry mode command byte 0000 0110b to ALCALL CMD_WRITE ;Write command byte to LCDLCALL DELAY ;Wait minimum 40µsMOV A,#80H ;DDRAM address command byte 1000 0000b to ALCALL CMD_WRITE ;Write command byte to LCDLCALL DELAY ;Wait minimum 40µsMOV DPTR,#4200H ;Starting address of LUT of the message to DPTR
NEXT: MOV X A,@DPTR ;A ← [DPTR]
Department of ECE, VKCET Page 85
08.607 MICROCONTROLLER LAB
CJNE A,#0,DISPLAY ;If A = 0, ASCII for NULL character, go to ;label DISPLAY
STOP: SJMP STOP ;Stop processDISPLAY: INC DPTR ;DPTR = DPTR + 1
PUSH DPH ;DPH to stackPUSH DPL ;DPL to stackLCALL DATA_WRITE ;Write data byte to LCDLCALL DELAY ;Wait 40µsPOP DPL ;DPL from stackPOP DPH ;DPH from stackSJMP NEXT ;Go to label NEXT
Sub-programs;LCD command write
ORG 4150HCMD_WRITE: MOV R7,A ;R7 ← A, save command byte
MOV A,#00 ;A = 0, for RS = 0 and R/W = 0MOV DPTR,#0FF08H ;DPTR = FF08H, memory mapped IO address
;of RS and R/W pinsMOV X @DPTR,A ;[DPTR] ← AMOV A,R7 ;A ← R7MOV DPTR,#0FF04H ;DPTR = FF04, memory mapped IO address of
;DB0-DB7 and EN goes to highMOV X @DPTR,A ;[DPTR] ← ARET ;Return to main program
;LCD data writeORG 4170H
DATA_WRITE: MOV R7,A ;R7 ← A, save data byteMOV A,#01 ;A = 01, RS = 1 and R/W = 0MOV DPTR,#0FF08H ;DPTR = FF08H, memory mapped IO address
;of RS and R/W pinsMOV X @DPTR,A ;[DPTR] ← AMOV A,R7 ;A ← R7MOV DPTR,#0FF04H ;DPTR = FF04H, memory mapped IO address
;of DB0-DB7 and EN pin set to highMOV X @DPTR,A ;[DPTR] ← A RET
Department of ECE, VKCET Page 86
08.607 MICROCONTROLLER LAB
2ms delay sub-programORG 4190H
DELAY: MOV R6,#05HLOOP1: MOV R5,#0FFHLOOP2: DJNZ R5, LOOP2
DJNZ R6,LOOP1RETEND
Look Up Table4200H: 4AH ;’M’ 43H ;’C’ 20H ;’ ‘ 4CH ;’L’ 61H ;’a’ 62H ;’b’
20H ;’ ,’45H :’E’43H :’C’45H ;’E’20H ;’ ‘44H ;’D’65H ;’e’70H ;’p’74H ;’t’
420FH: 00H :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 timersEquipments Required:
Microcontroller KitFlat Ribbon Cable (10 pin)CRO/DSOCRO 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
AlgorithmAlgorithm 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 program1. Start2. Initialize timer as 16-bit timer3. Load count value to timer for 90µs4. Set P1.05. Call delay sub-routine6. Load count value to timer for 10µs7. Clear P1.08. Call delay sub-routine9. Load count value to timer for 60µs10. Set P1.011. Call delay sub-routine12. Load count value to timer for 40µs13. Clear P1.0
Department of ECE, VKCET Page 89
08.607 MICROCONTROLLER LAB
14. Call delay sub-routine15. Load count value to timer for 30µs16. Set P1.017. Call delay sub-routine18. Load count value to timer for 70µs19. Clear P1.020. Call delay sub-routine21. Go to step 3
Sub-routine1. Start timer 2. If timer flag is 0, wait here else go to next step3. Stop timer4. Clear timer flag5. Return to main program
Delay calculations (Timer in mode 1 – 16 bit timer)1. 10µs delay
N=(65536−T d
T t)
For 11.0592MHz, timer clock period Tt = 1.0851µs
N=65536− 101.0851
=65526.78
≈ 65526 = FFF6H2. 30µs delay
N=65536− 301.0851
=65508.35
≈ 65508 = FFE4H
3. 40µs delay
N=65536− 401.0851
=65499.14
≈ 65499= FFDBH4. 60µs delay
N=65536− 601.0851
=65480.71
≈ 65480 = FFC8H5. 70µs delay
N=65536− 701.0851
=65471.49
≈ 65471 = FFBFHDepartment of ECE, VKCET Page 90
08.607 MICROCONTROLLER LAB
6. 90µs delay
N=65536− 901.0851
=65453.06
≈ 65453 = FFADHProgram
ORG 4100HMAIN: MOV TMOD,#01H ; Timer 0 , mode 1REPEAT: MOV TH0,#0FFH ; Count value for 90µs
MOV TL0,#0ADHSETB P1.0 ;P1.0 = 1ACALL DELAY ;Wait 90µsMOV TH0,#0FFH ;Count value for 10µsMOV TL0,#0F6HCLR P1.0 ;P1.0 = 0ACALL DELAY ;Wait 10µsMOV TH0,#0FFH ;Count value for 60µsMOV TL0,#0C8HSETB P1.0 ;P1.0 = 1ACALL DELAY ;Wait 60µsMOV TH0,#0FFH ;Count value for 40µsMOV TL0,#0DBHCLR P1.0 ;P1.0 = 0ACALL DELAY ;Wait 40µsMOV TH0,#0FFH ;Count value for 30µsMOV TL0,#0E4HSETB P1.0 ;P1.0 = 1ACALL DELAY ;Wait 30µsMOV TH0,#0FFH ;Count value for 70µsMOV TL0,#0BFHCLR P1.0 ;P1.0 = 0ACALL DELAY ;Wait 70µsSJMP REPEAT ;Repeat the process
;Delay sub-programDELAY: SETB TR0 ;Start timer 0 by setting TR0 in TCONWAIT: JNB TF0,WAIT ;Wait for timer flag TF0 in TCON to set
CLR TR0 ;Stop timer by clearing TR0CLR TF0 ;Clear timer flag TF0RETEND
Department of ECE, VKCET Page 91
08.607 MICROCONTROLLER LAB
ProcedureSteps:
1. Connect FRC on header P172. Connect CRO probes across P1.0 and GND of FRC connector3. Turn on the kit and CRO/DSO4. Enter the program (from 4100H)5. Terminate from program enter mode6. Run the program7. Observe the wave form on CRO/DSO
Department of ECE, VKCET Page 92
08.607 MICROCONTROLLER LAB
Expt. No.15 STEPPER MOTOR INTERFACE
Objective: a) To rotate stepper motor in clock wise direction continuously (full step and half step)
b) To rotate stepper motor in 180o clock wise and 360o anti clock wise direction 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 motor10 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
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 motor’s 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 isSteps 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 - 5001.8 - 2002.0 - 1805.0 - 727.5 - 4815 - 24
Step sequence table 1. Full step 4-step 2-phase or normal drive sequence
For clock wise rotation
Steps Winding A
Winding B
Winding C
Winding D
1 1 0 0 1
2 0 1 0 1
3 0 1 1 0
4 1 0 1 0
Note: Following figures shows the motor with step-angle of 90o, 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 Winding A
Winding B
Winding C Winding D
1 1 0 1 0
2 0 1 1 0
3 0 1 0 1
4 1 0 0 1
Step 1 Step 2 Step 3 Step 4
Step sequence waves:
2. Full step 4-step wave drive sequenceFor clock wise rotation
Steps Winding A
Winding B
Winding C Winding D
1 1 0 0 0
2 0 0 0 1
3 0 1 0 0
4 0 0 1 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 Winding A
Winding B
Winding C Winding D
1 0 0 1 02 0 1 0 03 0 0 0 14 1 0 0 0
Step 1 Step 2 Step 3 Step 4
Step sequence waves:
Department of ECE, VKCET Page 96
08.607 MICROCONTROLLER LAB
3. Half step 8-step mixed drive sequenceFor clock wise rotation
Steps Winding A
Winding B
Winding C Winding D
1 1 0 0 02 1 0 0 13 0 0 0 14 0 1 0 15 0 1 0 06 0 1 1 07 0 0 1 08 1 0 1 0
Step 1 Step 2 Step 3 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 Winding A
Winding B
Winding C Winding D
1 1 0 0 02 1 0 1 03 0 0 1 04 0 1 1 05 0 1 0 06 0 1 0 17 0 0 0 18 1 0 0 1
Step 1 Step 2 Step 3 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 kitMemory mapped IO address of stepper motor connected to header P4 is: FF11HStepper motor driver circuit in the kit is shown in the figure 2
Figure 2When the memory mapped IO address FF11H is placed in DPTR and loading A register
results the following bit movement:Acc.0 → M1Acc.1 → M2Acc.2 → M3Acc.3 → M4Where 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 ARed – Winding BOrange – Winding CYellow – Winding DBlack – Winding A/C commonWhite – 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 DAcc.1 → M2 → Orange → Winding CAcc.2 → M3 → Red → Winding BAcc.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: 12VTorque: 2kgStep angle: 1.8o
Algorithma) To rotate stepper motor in clock wise direction continuously (full step and half step)
Main program1. Start2. Initialize timer 0 for delay3. DPTR = 4200H, pointer for LUT of step sequence4. Counter R0 = 04 for full step sequence or 08 for half step sequence5. A ← [DPTR]6. Save DPTR content to stack memory7. DPTR = FF11H, pointer to memory mapped IO address of stepper motor8. [DPTR] ← A, step sequence to motor9. Call delay program10. Load stack memory content to DPTR11. Increment DPTR content12. Decrement counter R0 = R0 - 1, if R0 ≠ 0 go to step 5, else next step13. Go to step 3 for continuous rotation
Department of ECE, VKCET Page 100
08.607 MICROCONTROLLER LAB
Delay program1. Start2. Load count value for required delay to basic registers of timer 03. Start timer by setting TR0 = 14. If timer flag TF0 ≠ 1, wait here, else go to next step5. Stop timer by clearing TR0 = 06. Clear timer flag TF07. Return to main program
b) To rotate stepper motor in 180o clock wise and 360o anti clock wise direction continuously.Main program
1. Start2. Initialize timer 0 for delay3. Counter R1 = 25 for 180o rotation (180/(4 x 1.8) = 25)4. DPTR = 4200H, pointer for LUT of clock wise step sequence5. Counter R0 = 04 for full step sequence or 08 for half step sequence6. A ← [DPTR]7. Save DPTR content to stack memory8. DPTR = FF11H, pointer to memory mapped IO address of stepper motor9. [DPTR] ← A, step sequence to motor10. Call delay program11. Load stack memory content to DPTR12. Increment DPTR content13. Decrement counter R0 = R0 - 1, if R0 ≠ 0 go to step 6, else next step14. Decrement counter R1 = R0 - 1, if R0 ≠ 0 go to step 4, else next step15. Counter R1 = 50 for 360o rotation (360/(4 x 1.8) = 50)16. DPTR = 4300H, pointer for LUT of anti-clock wise step sequence17. Counter R0 = 04 for full step sequence or 08 for half step sequence18. A ← [DPTR]19. Save DPTR content to stack memory20. DPTR = FF11H, pointer to memory mapped IO address of stepper motor21. [DPTR] ← A, step sequence to motor22. Call delay program23. Load stack memory content to DPTR24. Increment DPTR content25. Decrement counter R0 = R0 - 1, if R0 ≠ 0 go to step 18, else next step26. Decrement counter R1 = R0 - 1, if R0 ≠ 0 go to step 16, else next step27. 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. Start2. Initialize timer 0 for delay3. Set P1.0 as input4. DPTR = 4200H, pointer for LUT of clock wise step sequence5. Counter R0 = 04 for full step sequence or 08 for half step sequence6. A ← [DPTR]7. Save DPTR content to stack memory8. DPTR = FF11H, pointer to memory mapped IO address of stepper motor9. If P1.0 is 1, go to step 13, else next step10. [DPTR] ← A, step sequence to motor11. Call 10 RPM delay program12. Go to step 13. [DPTR] ← A, step sequence to motor14. Call 4 RPM delay program15. Load stack memory content to DPTR16. Increment DPTR content17. Decrement counter R0 = R0 - 1, if R0 ≠ 0 go to step 6, else next step18. Go to step 3 for continuous rotation
Delay calculation for 4RPM
steps per second = RPM X steps per revolution60
steps per revolution= 360step angle
=3601.8
=200
steps per second =4 X 20060
=13.33
time required for one step= 1steps per second
=75ms
Delay calculation for 10RPM
steps per second =10 X 20060
=33.33
time required for one step= 1steps per second
=30ms
Delay calculationsUse 25ms instead of 30ms delay and call it three times for 75ms delay.
Count value for 25ms delay N=65536− 25 X 10−3
1.0851 X 10−6=42496.65
≈ 42496 = A600HDepartment of ECE, VKCET Page 102
08.607 MICROCONTROLLER LAB
Programsa)
ORG 4100HMAIN: MOV TMOD ,#01 ;Timer 0 in mode 1REPEAT: MOV DPTR,#4200H ;LUT starting address of step sequence data to DPTR
MOV R0,#04 ;Counter for full step sequence dataNEXT: MOVX A,@DPTR ;A ← [DPTR]
PUSH DPH ;DPH to stackPUSH DPL ;DPL to stackMOV DPTR,#0FF11H ;DPTR = FF11H, memory mapped IO address of
;stepper motorMOVX @DPTR,A ;[DPTR] ← AACALL DELAY ;Wait 50msPOP DPL ;DPL from stackPOP DPH ;DPH from stackINC DPTR ;DPTR = DPTR + 1DJNZ R0,NEXT ;R0 = R0 – 1, if R0 > 0 go to label NEXTSJMP REPEAT ;Repeat the process
;Delay sub-programORG 4150H
DELAY: MOV TH0,#40H ;Count value for 50ms delayMOV TL0,#00SETB TR0
WAIT: JNB TF0,WAITCLR TR0CLR TF0RET
;LUT at 4200HFS_2PHASE: DB 09H,05H,06H,0AHHS_MIXED: DB 08H,09H,01H,05H,04H,06H,02H,0AH ;Use counter value R0 = 08H
ENDb)
ORG 4100HMAIN: MOV TMOD,#01H ;Timer 0 in mode 1CONTINUE: 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 dataNEXT1: MOVX A,@DPTR ;A ← [DPTR]
PUSH DPH ;DPH to stackPUSH DPL ;DPL to stackMOV DPTR,#0FF11H ;DPTR = FF11H, memory mapped IO address of
;stepper motorMOVX @DPTR,A ;[DPTR] ← AACALL DELAY ;Wait 50msPOP DPL ;DPL from stackPOP DPH ;DPH from stackINC DPTR ;DPTR = DPTR + 1DJNZ R1,NEXT1 ;R1 = R1 – 1, if R1 > 0 go to label NEXT1DJNZ R0,REPEAT1 ;R0 = R0 – 1, if R0 > 0 go to label REPEAT1MOV R0,#32H ;Counter R0 = 32H, for 360o rotation
REPEAT2: MOV DPTR,#4300H ;LUT starting address of step sequence (ACW) data to ;DPTR
MOV R1,#04 ;Counter for full step sequence dataNEXT2: MOVX A,@DPTR ;A ← [DPTR]
PUSH DPH ;DPH to stackPUSH DPL ;DPL to stackMOV DPTR,#0FF11H ;DPTR = FF11H, memory mapped IO address of
;stepper motorMOVX @DPTR,A ;[DPTR] ← AACALL DELAY ;Wait 50msPOP DPL ;DPL from stackPOP DPH ;DPH from stackINC DPTR ;DPTR = DPTR + 1DJNZ R1,NEXT2 ;R1 = R1 – 1, if R1 > 0 go to label NEXT2DJNZ R0,REPEAT2 ;R0 = R0 – 1, if R0 > 0 go to label REPEAT2SJMP CONTINUE ;Repeat the process
Department of ECE, VKCET Page 104
08.607 MICROCONTROLLER LAB
;Delay sub-programORG 4150H
DELAY: MOV TH0,#40HMOV TL0,#01HSETB TR0
WAIT: JNB TF0,WAITCLR TR0CLR TF0RET
;LUT for CW rotationORG 4200H
FS_CW: DB 08H,01H,04H,02H
;LUT for ACW rotationORG 4300H
FS_ACW: DB 02H,04H,01H,08HEND
c)ORG 4100H
MAIN: MOV TMOD,#01H ;Timer 0 in mode 1SETB P1.0 ;P1.0 as input
REPEAT: MOV DPTR,#4200HMOV R0,#04 ;Counter for full step sequence data
NEXT: MOVX A,@DPTR ;LUT starting address of step sequence ;(CW) data to DPTR
PUSH DPH ;DPH to stackPUSH DPL ;DPL to stackMOV DPTR,#0FF11H ;DPTR = FF11H, memory mapped IO
;address of stepper motorJB P1.0, RPM_4 ;If P1.0 is set go to label RPM_4
RPM_10: MOVX @DPTR,A ; [DPTR] ← A ACALL DELAY ;Wait 25msSJMP DEC_COUNT ;Go to label DEC_COUNT
RPM_4: MOVX @DPTR,A ; [DPTR] ← A ACALL DELAY ;Wait 75msACALL DELAYACALL DELAY
Department of ECE, VKCET Page 105
08.607 MICROCONTROLLER LAB
DEC_COUNT:POP DPL ;DPL from stackPOP DPH ;DPH from stackINC DPTR ;DPTR = DPTR + 1DJNZ R0,NEXT ;R0 = R0 – 1, if R0 > 0 go to label NEXTSJMP REPEAT ;Repeat the process
DELAY: MOV TH0,#0A6H ;Count value for 30msMOV TL0,#0SETB TR0
WAIT: JNB TF0,WAITCLR TR0CLR TF0RET
LUT:FS_CW: DB 09H,05,06,0AH
ENDProcedure (Common for all programs)
Steps:1. Connect stepper motor on header P42. Connect FRC between port 1(header P17) and header P18, for program c3. Turn on the kit 4. Enter the program (from 4100H)5. Terminate from program enter mode6. Run the program7. 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 generatorCRO/DSOProbesFlat 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 2Using 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
AlgorithmMain program
1. Start2. Initialize timer 0 as 16-bit counter and timer 1 as 16-bit timer for delay 3. Initialize LCD to display the frequency4. Set the DDRAM address to display5. Clear TL0 for initial count (maximum frequency measured will be 255) 6. Start timer 07. Wait 1 second8. Load TL0 to A 9. Convert hexa decimal data in A to BCD10. Convert BCD digits to ASCII11. Display the ASCII for BCD digits to LCD12. Stop the timer 013. Go to step 5
LCD initialize program1. Start2. Write command byte to LCD for function setting. Command byte: 0011 1000 = 38H3. Wait minimum 40µs4. Write command byte to LCD for function setting. Command byte: 0000 0001 = 01H5. Wait minimum 1.64ms6. Write command byte to LCD for display on and cursor off. Command byte: 0000
1100 = 0CH7. Wait minimum 40µs8. Write command byte to LCD for entry mode: left entry (increment cursor to right).
Command byte: 0000 0110 = 06H9. Return
LCD command write1. Start2. R7 ← A, save command byte3. DPTR = FF08H, memory mapped IO address of RW and RS pins of LCD4. A = 00H, RW = 0 (LCD write operation) and RS = 0 (LCD command register
selected)5. [DPTR] ← A6. DPTR = FF04H, memory mapped IO address of E and DB0-DB7 pins of LCD7. A ← R7, command byte8. [DPTR] ← A, (E pin set to 1 by hard ware)9. Return
Department of ECE, VKCET Page 109
08.607 MICROCONTROLLER LAB
LCD data write1. Start 2. R7 ← A, save data byte 3. DPTR = FF08H , memory mapped IO address of RW and RS pins of LCD4. A = 01H, RW = 0 (LCD write operation) and RS = 1 (LCD data register selected)5. [DPTR] ← A6. DPTR = FF04H, memory mapped IO address of E and DB0-DB7 pins of LCD7. A ← R7, data byte8. [DPTR] ← A, (E pin set to 1 by hard ware)9. Return
Hexa decimal to BCD1. Start 2. B ← 100, to get hundredth position digit3. Divide A by B4. R7 ← A, hundredth position digit5. A ← B, reminder6. B ← 10, to get tenth position digit7. Divide A by B8. R6 ← A, tenth position digit9. R5 ← B, reminder and it is unit position digit10. Return
BCD to ASCII1. Start 2. A ← A + 30H3. Return
DELAY for 40µs1. Start2. Load count value FFDBH to T1 timer register, ie TH1 = FFH and TL1 = DBH3. Start timer 1 by TR1 = 14. If timer 1 flag TF1 ≠ 0, stay here, else go to next step5. Stop timer 1 by TR1 =06. Clear timer 1 flag TF1 =07. Return
Department of ECE, VKCET Page 110
08.607 MICROCONTROLLER LAB
DELAY for 2ms1. Start2. Load count value F8CCH to T1 timer register, ie TH1 = F8H and TL1 = CCH3. Start timer 1 by TR1 = 14. If timer 1 flag TF1 ≠ 0, stay here, else go to next step5. Stop timer 1 by TR1 =06. Clear timer 1 flag TF1 =07. Return
DELAY for 50ms1. Start2. Load count value 4C01H to T1 timer register, ie TH1 = 4CH and TL1 = 01H3. Start timer 1 by TR1 = 14. If timer 1 flag TF1 ≠ 0, stay here, else go to next step5. Stop timer 1 by TR1 =06. Clear timer 1 flag TF1 =07. Return
DELAY for 1 second1. Start2. Initialize counter R0 = 203. Wait 50ms4. R0 = R0-1, if R0 ≠ 0 go to step 3, else next step5. Return
Program
ORG 4100HMAIN: MOV TMOD,#00010101B ;Timer 1 as 16 bit timer and timer 0 as 16 bit
;counterLCALL LCD_INIT
CONTINUE: MOV A,#88H ;DDRAM address of LCDLCALL CMD_WRITELCALL DELAY_40uMOV TL0,#00 ;Initial count (using 8 bit only)SETB TR0 ;Start counterLCALL DELAY_1S ;Wait 1 secondMOV A,TL0 ;Load count value to ALCALL HEX_2_BCD ;Convert hex value to BCDMOV A,R7 ;Hundredth digit to ASCII
Department of ECE, VKCET Page 111
08.607 MICROCONTROLLER LAB
LCALL BCD_2_ASCII ;BCD to ASCII conversionLCALL LCD_DISPLAY ;Display hundredth digitMOV A,R6 ;Tenth digit to ASCIILCALL BCD_2_ASCIILCALL LCD_DISPLAY ;Display tenth digitMOV A,R5 ;Unit digit to ASCIILCALL BCD_2_ASCII
LCALL LCD_DISPLAY ;Display unit digitCLR TR0 ;Stop timer 0MOV A,#’H’ ;’H’ , ASCII of HLCALL LCD_DISPLAYMOV A,#’z’ ;’z’, ASCII of zLCALL LCD_DISPLAYSJMP CONTINUE ;Repeat process
ORG 4150H ;LCD initialize subroutineLCD_INIT: MOV A,#38H ;Function set
LCALL CMD_WRITELCALL DELAY_40uMOV A,#01H ;Clear displayLCALL CMD_WRITELCALL DELAY_2mMOV A,#0CH ;Display on, cursor offLCALL CMD_WRITELCALL DELAY_40uMOV A,#06H ;Entry modeLCALL CMD_WRITELCALL DELAY_40uRETORG 4180H
CMD_WRITE: MOV R4,AMOV A,#00 ;RW = 0, RS = 0MOV DPTR,#0FF08H ;Memory mapped IO address of RW and RSMOVX @DPTR,AMOV A,R4 ;CommandMOV DPTR,#0FF04H ;Memory mapped IO address of DB7-DB0
;and E =1MOVX @DPTR,ARET
Department of ECE, VKCET Page 112
08.607 MICROCONTROLLER LAB
ORG 4190HDATA_WRITE: MOV R4,A
MOV A,#01H ;RW = 0, RS = 1; MOV DPTR,#0FF08HMOVX @DPTR,AMOV A,R4MOV DPTR,#0FF04HMOVX @DPTR,ARET
ORG 41A0HLCD_DISPLAY: LCALL DATA_WRITE
LCALL DELAY_40uRET
ORG 41B0HHEX_2_BCD: MOV B,#64H
DIV ABMOV R7,AMOV A,BMOV B,#0AHDIV ABMOV R6,AMOV R5,BRET
ORG 41C0HBCD_2_ASCII: ADD A,#30H
RET
ORG 41D0HDELAY_40u: MOV TH1,#0FFH
MOV TL1,#0DBHLCALL DELAYRET
Department of ECE, VKCET Page 113
08.607 MICROCONTROLLER LAB
ORG 41E0HDELAY_2m: MOV TH1,#0F8H
MOV TL1,#0CCHLCALL DELAYRETORG 41F0H
DELAY_50m: MOV TH1,#4CHMOV TL1,#01HLCALL DELAYRET
ORG 4200HDELAY_1S: MOV R0,#14HWAIT_1S: LCALL DELAY_50m
DJNZ R0,WAIT_1SRET
ORG 4210HDELAY: SETB TR1WAIT: JNB TF1,WAIT
CLR TR1CLR TF1RETEND
ProcedureSteps:
1. Connect 50 pin FRC to VPIC Bus2. 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 kit5. Enter the program6. Terminate from program enter mode7. 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
BIBILIOGRAPHY1. Muhammad Ali Mazidi, The 8051 microcontroller and Embedded System2. Kenneth Ayala, The 8051 Microcontroller3. Scott, The_8051_Microcontroller
Websites:www.8051projects.infowww.engineersgarage.comwww.mikroe.comwww.8052.com
For development tools:www.keil.comwww.atmel.com
Department of ECE, VKCET Page 115