08.607 Microcontroller Lab Manual
08.607 Microcontroller Lab Manual
08.607 Microcontroller Lab Manual
Assini H
Page 1
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.
Page 2
Page 3
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
Page 4
INTRODUCTION TO 8051 MICROCONTROLLER KIT To study the basics of 8051 Microcontroller Kit
Equipments Required: Microcontroller Kit Theory: Specifications of the kit: 1. Hardware specifications: Intel 8051/89C51 at 12MHz Memory address System EPROM location C000H to FFFFH System RAM location Additional RAM C000H to FEFFH (For Micro-51 LC C000H to FF00H (For Vi89C51 kit) Monitor Buffer User Program/Data RAM location User Data RAM location C000H to FEFFH (For Micro-51 LC C000H to FF00H (For Vi89C51 kit) Memory mapped IO FFC0H to FFFFH Memory mapped IO expansion
: 0000H to 3FFFH and : 4000H to BFFFH : 0000H to 3FFFH and Kit), 0000H to 3FFFH and : 4000H to 40FFH : 4100H to BFFFH : 0000H to 3FFFH and Kit), 0000H to 3FFFH and : FF00H to FF2FH and : FF20H to FFBFH
Monitor buffer should not be accessed by the user since it is used by the monitor program. Input and Output 24 IO lines using two Programmable Peripheral Interface IC (8255) One RS232C Serial Interface using 8051 Serial port Two 16 bit timers One Centronics compatible Printer Interface through first 8255 Five interrupt sources
Department of ECE, VKCET Page 5
2. Software specifications: The Microcontroller Kit Micro-51 LC of Vi Microsystems contains a high performance 32kB monitor program. It is designed to respond to user input, RS232C serial communications, etc. Some simple commands (Not case sensitive) for the kit are: a) Substitute Memory Command Syntax: #sp <Address> <Enter> : For program memory #sd <Address> <Enter> : For data memory b) Register View/ Modify Command Syntax: #r <Enter> c) Execute Command Syntax: #go <Address> <Enter> d) Execute with break point command Syntax: #go <Start address> <Space> <Enter> e) Assembler command Syntax: #a <Enter> f) Disassembler command Syntax: #u <Enter> g) Internal RAM command Syntax: #ir <Address><Enter>
Department of ECE, VKCET Page 6
External Data Memory FF00H C000H 4000H 0000H Memory Mapped IO User Data Memory User Data Memory Combined with Program Memory User Data Memory FFFFH FEFFH BFFFH 3FFFH
Allocation of EPROM: The kit has a standard EPROM configuration of 32kB using one 27256 (32k x 8 EPROM). The address for the monitor EPROM is 0000H to 3FFFH and C000H to FFFFH. Allocation of RAM: The kit has 32kB of R/W Program/Data memory using one 61256 (32k x 8 DRAM). The address is from 4000H to BFFFH, out of which the first 256 locations are used by monitor as its data buffer area. Hence user program area starts from 4100H onwards. The kit has one more 32kB of R/W Data memory using one 61256 and the address is from 0000H to 3FFFH and C000H to FEFFH. Allocation of Memory Mapped IO:
Department of ECE, VKCET Page 7
FFC0H FF24H FF23H FF22H FF20H FF18H FF14H FF0CH FF08H FF04H FF00H
Software features: The kit accept any command related in the command prompt mode, indicated by a # symbol in the leftmost position of the second row in the LCD module as shown below: Micro-51 #_
Page 8
Page 9
LOOP1 LOOP2
Page 10
Page 13
HALT
FE
The observed result is: R2 = 10H 2. Program to subtract two 8 bit data in R1 and R2 of bank1 and store the result to R0 of bank1 ADDRESS 4100 4103 4105 4107 4108 4109 410A LABEL MAIN OPCODE & OPERANDS 75 79 7A E9 9A F8 80 D0 FF 10 08 MNEMONICS MOV PSW,#08 MOV R1,#FF MOV R2,#10 MOV A,R1 SUBB A,R2 MOV R0,A SJMP HALT COMMENTS PSW= 0000 1000 , Where RS1=0 and RS0=1 for bank 1 R1=data 1 R2 =data 2 A=R1 A = A R2 R0=A Halt the program
HALT
FE
Page 14
4100 4103
MAIN
4104
A3
INC DPTR
23 F0 80 F8
Page 15
Objective: a) To add two 16-bit numbers saved in external RAM and save the result in external RAM locations with carry b) To subtract two 16-bit numbers saved in internal bank 1 registers R0, R1, R2 and R3, and save the result in external RAM. Equipments Required: Microcontroller Kit Theory: The 8051 microcontroller has 8-bit data operation, so to perform 16-bit operation we have to perform byte operations as shown below. For 16 bit addition: Data 1 = 10 F1H Data 2 = F1 10H Perform 8 bit addition Result 1 = Add lower bytes = F1 + 10 = 01H Result 2 = Add upper bytes with carry of previous addition = 10 + F1 + 01 = 02H Sum = 010201H Given program, assume data 1 is stored in 4200H and 4201H RAM, data 2 is stored in 4202H and 4203H locations. And the results can be store in 4204H, 4205H and 4206H For 16 bit subtraction: Data 1 = 10 F1H Data 2 = F1 10H Perform 8 bit subtraction Result 1 = Subtract lower bytes = F1 - 10 = E1H Result 2 = Subtract upper bytes with borrow of previous subtraction = 10 - F1 - 00 = 1FH Sum = FF1FE1H Given program, data 1 is stored in R0 and R1; data 2 is stored in R2 and R3 internal bank 1 registers. And the results can be store in 4200H, 4201H and 4202H external RAM locations
Page 16
Add Accumulator and register content with carry of previous addition Move result in A, which is upper byte of the sum to internal RAM register
Stop
Page 17
Subtract Accumulator and register content with borrow of previous addition Move result in A, which is upper byte of the difference to internal RAM register
Stop
Page 18
MOVX A, @DPTR Move upper byte of data 1 to A MOV R1,A INC DPTR MOVX A, @DPTR MOV R2,A INC DPTR MOVX A, @DPTR MOV R3,A MOV A,R0 ADD A,R2 INC DPTR MOVX @DPTR,A MOV A,R1 ADDC A,R3 INC DPTR MOVX @DPTR,A JNC LOOP1 MOV A,#01 SJMP LOOP2 CLR A INC DPTR MOVX @DPTR,A SJMP STOP Save A content to R1 Increment dptr to point next byte Move lower byte of data 2 to A Save A content to R2 Increment dptr to point next byte Move upper byte of data 2 to A Save A content to R1 Move lower byte of data 1 to A Add lower bytes of data Increment dptr to point RAM location to store result Store lower byte of the result Move upper byte of data 1 to A Add upper bytes with carry of previous operation Increment dptr to point RAM location to store result Store lower byte of the result If no carry in addition, go to LOOP1, else next instruction Load 1 to A Go to LOOP2 Clear A Increment dptr to point RAM location to store carry Store carry Halt the program
Page 19
LABEL
MAIN
MNEMONICS
SETB PSW.3 MOV R0,#F1 MOV R1,#10 MOV R2,#10 MOV R3,#F1 MOV A,R0 SUBB A,R2 MOV DPTR,#4200 MOVX @DPTR,A MOV A,R1 SUBB A,R3 INC DPTR MOVX @DPTR,A JC LOOP1 CLR A SJMP LOOP2 MOV A,#FF INC DPTR MOVX @DPTR,A SJMP STOP
COMMENTS
Set RS0 bit of PSW, then PSW= 0000 1000, to select bank 1 Load lower byte of data 1 to R0 Load upper byte of data 1 to R1 Load lower byte of data 2 to R2 Load upper byte of data 2 to R3 Move lower byte of data 1 to A Subtract lower bytes Load starting address of external RAM to store the result in dptr Store the lower byte of the result Move upper byte of the data 1 to A Subtract upper bytes with the borrow of the previous subtraction Increment DPTR to point next external RAM location Store upper byte of the result If borrow go to LOOP1, else next instruction Clear A Go to LOOP2 Load FF to A Increment dptr to point next external RAM location Store borrow Halt the program
LOOP1 LOOP2
Page 20
Page 21
Objective: To multiply and divide two 8-bit numbers saved in 4200H and 4201H and save the results in internal RAM location 30H onwards Equipments Required: Microcontroller Kit Theory: To perform 8-bit multiplication and division, the instructions MUL AB and DIV AB can be used. Flowchart: a) 8-bit multiplication
Start Move multiplicand to A Move multiplier to B Multiply A and B content
Store the lower byte in A and upper byte in B of the product to the destination Stop
b) 8-bit division
Start Move dividend to A Move divisor to B Divide A and B content
Store the quotient in A and reminder in B to the destination Stop Department of ECE, VKCET Page 22
Load starting address of external RAM where data are saved to data pointer MOVX A,@DPTR Move multiplicand/dividend to A Save multiplicand/dividend to R1 Increment dptr to point multiplier/divisor Move multiplier/divisor to A Move multiplier/divisor to B Save multiplier/divisor to R2 Move saved multiplicand to A Perform multiplication, results A = Lower byte of product and B = Upper byte of the result Load internal RAM scratch pad address to R0 as data pointer Save lower byte of the product to internal RAM Increment R0 to point next location Save upper byte of the product to internal RAM Move dividend to A Move divisor to B Perform division, results A = quotient and B = reminder Increment R0 to point next location to store result Store quotient Increment R0 to point next location to store result Store reminder Halt the program
Page 23
Page 24
Objective: To multiply two 4-bit numbers saved in 4200H and 4201H without using MUL AB instruction and store the result in 4102H. Equipments Required: Microcontroller Kit Theory: Shift-and-add multiplication is similar to the multiplication performed by paper and pencil. This method adds the multiplicand X to itself Y times, where Y denotes the multiplier. To multiply two numbers by paper and pencil, the algorithm is to take the digits of the multiplier one at a time from right to left, multiplying the multiplicand by a single digit of the multiplier and placing the intermediate product in the appropriate positions to the left of the earlier results. As an example, consider the multiplication of two unsigned 4-bit numbers, 8 (1000b) and 9 (1001b).
Thus the multiplication can be performed by shifting and adding method. Shifting multiplier by one bit left and if the MSB is high, performs addition between product (intermediate) and multiplicand followed by shift. If MSB is low perform shifting only and the process continues for 2n times, where n is the number of bits in multiplier and multiplicand. The main advantage of this type process is its faster operation for large number of bit multiplication. In general the multiplication require n-bit multiplicand by n-bit multiplier require 2n registers to hold numbers and product. And require 2n-bit adders and shifters. An e.g. 4 bit multiplicand x 4-bit multiplier results 8-product and require 8-bit registers to hold data.
Page 25
No
pr
Page 26
HERE
FE
Page 28
Objective: To find LCM and HCF of two 8-bit numbers stored in 4200H and 4201H and store the result in 4202H and 4203H Equipments Required: Microcontroller Kit Theory: The least common multiple (also called the lowest common multiple or smallest common multiple) of two integers a and b, usually denoted by LCM (a, b), is the smallest positive integer that is a multiple of both a and b. An example: The LCM of 4 and 6: Multiples of 4 are: 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 60, 64, 68, 72, 76 And the multiples of 6 are: 6, 12, 18, 24, 30, 36, 42, 48, 54, 60, 66, 72... Common multiples of 4 and 6 are simply the numbers that are in both lists: 12, 24, 36, 48, 60, 72... So the least common multiple of 4 and 6 is the smallest one of those 12 The highest common factor (HCF), also known as the greatest common factor (GCF), or greatest common divisor (GCD), of two or more non-zero integers, is the largest positive integer that divides the numbers without a remainder An example: The number 54 can be expressed as a product of two other integers in several different ways: Thus the divisors of 54 are: Similarly the divisors of 24 are: The numbers that these two lists share in common are the common divisors of 54 and 24: The greatest of these is 6. That is the HCF of 54 and 24. One writes:
Page 29
Divide number1 by number2 No number1 = HCF number2 = reminder Yes Store the results LCM and HCF Stop Is reminder = 0?
Page 30
MNEMONICS MOV DPTR,#4200 MOVX A,@DPTR MOV R0,A INC DPTR MOVX A,@DPTR MOV R1,A MOV A,R0 MOV R2,A MOV B,R1 DIV AB MOV A,B JZ HCF MOV A,R2 ADD A,R0 MOV R2,A SJMP LCM MOV A,R1 MOV R3,A MOV B,R1 MOV A,R0 DIV AB MOV A,B JZ RESULT MOV R1,A MOV A,R3 MOV R0,A SJMP HCF MOV A,R2 INC DPTR MOVX @DPTR,A INC DPTR MOV A,R3 MOVX @DPTR,A SJMP STOP
COMMENTS Point external memory for inputs Load number1 to Acc. Save number1 to R0 Point number2 Load number2 to Acc. Save number2 to R1 Load number1 to Acc. Set R2 for LCM and set number1 as LCM Load number2 to B Divide number1 by number2 Move B (reminder) to Acc. If Acc = 0, go to find HCF, else next step Move LCM to Acc. Acc = number1 + LCM Store Acc to R2 (as LCM) Go to next check Move number2 to Acc. Set R3 for HCF and set number2 as HCF Load number2 to B Move number1 to Acc. Divide number1 by number2 Move B (reminder) to Acc. If Acc. = 0, go to store result, else next step number2 = reminder Move HCF to Acc. number1 = HCF Go to next check Load LCM to Acc. Point the external memory location for storing LCM Store LCM Point the external memory location for storing HCF Load HCF to Acc. Store HCF Halt the program
Page 31
LCM
F0 F0 05
NEXT
F4
HCF
F0
F0 05
F1
RESULT
STOP
FE
Page 32
Objective: To realize Boolean expressions using 8051 ports Equipments Required: 1. Microcontroller Kit (Vi89C51) 2. Flat Ribbon Cable (FRC) 1 No. Theory: Boolean expressions can be represented by either min-term (SOP) or max-term (POS). In general SOP form is represented as Where a,b,c, are input variables and xk.mk are min-terms An example: Four variable systems,
In Boolean expression
Using logical instructions these expressions can easily implemented. Similarly POS form can be represented as
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
d 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1
Output f 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1
Page 34
Set Port 1 as input Read P1.0, P1.1, P1.2 and P1.3 Perform abcd, abcd ,and abcd and save the result in bit addressable RAM area Perform abcd + abcd + abcd and save the status Point digital output LEDs, DPTR FF23H Send the status of Boolean expression to digital output LED
Page 35
Page 36
Procedure: Steps 1. 2. 3. 4. 5.
Connect P17 and P18 using FRC Switch on the system Enter the program (starting address 4100H) Terminate from program edit mode Execute program 6. Give the inputs a, b, c and d using switches SW15, SW14, SW13 and SW12 respectively 7. Observe the output by LED L24 and verify the truth table
Page 37
Objective: a) To sort n numbers stored in external RAM (starting address 4201H) in ascending order, were n is stored 4200H external RAM location b) To find factorial of an 8 bit number stored in 4400H (external RAM) and store 8 bit result in 4401H Equipments Required: 1. Microcontroller Kit Theory: Sorting numbers: There are different algorithms for sorting array of numbers. One simple method is Bubble sort and is a simple sorting algorithm that works by repeatedly stepping through the list to be sorted, comparing each pair of adjacent items and swapping them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted. Although the algorithm is simple, it is not efficient for sorting large lists; other algorithms like are better. A step by step example is shown below: Let us take the array of numbers "5 4 2 8 1", and sort the array from lowest number to greatest number using bubble sort algorithm. In each step, elements written in bold are being compared. To sort n numbers, n-1 passes are required First Pass: (54281) ( 4 5 2 8 1 ), Here, algorithm compares the first two elements, and swaps them. (45281) ( 4 2 5 8 1), Swap since 5 > 2 (42581) ( 4 2 5 8 1 ),No Swap since 8 > 5 (42581) ( 4 2 5 1 8 ), Swap since 8>1 Second Pass: (42518) ( 2 4 5 1 8 ), Swap since 4 > 2 (24518) ( 2 4 5 1 8 ), No swap since 5 > 4 (24518) ( 2 4 1 5 8 ), Swap since 5 > 1 (24158) ( 2 4 1 5 8 ), No swap since 8 > 5 Third Pass: (24158) ( 2 4 1 5 8 ), No swap since 4 > 2 (24158) ( 2 1 4 5 8 ), Swap since 4 > 1 (21458) ( 2 1 4 5 8 ), No swap since 5 > 4 (21458) ( 2 1 4 5 8 ), No swap since 8 > 5 Fourth Pass: (21458) ( 1 2 4 5 8 ), Swap since 2 > 1 (12458) ( 1 2 4 5 8 ), No swap (12458) (12458) (12458) (12458) The disadvantage of this sorting is all pass must perform whether elements are in order or not.
Department of ECE, VKCET Page 38
Page 39
Page 41
NEXT
82
NOSWAP
EE E4 FE
Page 42
F0
30
STOP
FE
Page 43
Enter the program (starting address 4100H) Terminate from program enter mode Enter the total numbers (in 4200H) and numbers (from 4201H) Terminate from edit mode 5. Execute the program 6. Reset the kit 7. Verify the result b) Factorial: Steps 1. Enter the program (starting address 4100H) 2. Terminate from program enter mode 3. Enter the number (in 4400H) 4. Terminate from edit mode 5. Execute the program 6. Reset the kit 7. Verify the result
Page 44
Figure 1 The interface card contains ADC0809 chip. ADC0809 Some features of the ADC0809 chip are: 6. Easy to interface 7. Successive approximation type 8. 8-analog input channels selected by 3-bit address lines 9. 0-5V input range with single 5V Vcc 10. TTL output voltage 11. 8-bit resolution 12. Maximum conversion time is 100s
Department of ECE, VKCET Page 45
Pins and functions Pins IN0-IN7 2-8 2-1 ADD C, ADD B and ADD A
ALE START EOC OUTPUT ENABLE CLOCK Vcc GND Vref(+) Vref(-)
Functions 8 analog input channels 8-bit digital data output lines 3-bit address lines ADD C ADD B ADD A Selected analog lines 0 0 0 IN0 0 0 1 IN1 0 1 0 IN2 0 1 1 IN3 1 0 0 IN4 1 0 1 IN5 1 1 0 IN6 1 1 1 IN7 Address latch enable, if it is a HIGH to LOW signal valid address in the address bits are latched to the ADC Start of conversion, input signal to ADC. If it is a high signal starts ADC End of conversion, output signal from ADC. If it is a high signal conversion is over and data bus has valid data Input signal to ADC, to enable ADC output data Clock signal, its frequency determines sampling rate, maximum frequency is 1280 kHz +5V power supply Ground +ve reference voltage (Vref1) -ve reference voltage (Vref2)
Page 46
Timing characteristics f, clock frequency: Typically 640 kHz tc, conversion time: Typically 100s tWS, start pulse width: Typically 100ns tWALE, ALE pulse width: Typically 100ns ts, address set up time: Typically 25ns tH, address hold time: Typically 25ns tEOC, EOC delay time: Typically 8+2 s clock periods Connection details and memory mapped IO address Using 50 pin FRC connect microcontroller kit to interface card as shown in figure 3:
Figure 3
Department of ECE, VKCET Page 47
The data in Accumulator for each memory mapped IO operation: Address FFC0H FFC8H Accumulator Data ADC digital output IN0 select, ALE low 0001 0000b and OE high = 10H IN0 select, ALE high 0001 1000b and OE high = 18H IN1 select, ALE low 0001 0001b and OE high = 11H IN1 select, ALE high 0001 1001b and OE high = 19H SOC low 00H SOC high 01H EOC low 00H EOC high 01H
FFD0H FFD8H
The interface card has jumpers and its details are: Jumper 2 (J2): For selecting SOC source
Note: To connect trim pot variable output to channel IN0, place J5 to A position There is a header P2, in which all channels IN0 to IN7 are terminated as shown below:
Page 49
If EOC = 1?
Page 50
Page 51
Observation table Input voltage (V) 0 1 2 3 4 5 Output data (in binary format)
Page 52
Objective: a) To find square and square root of a number stored in 4200H (external RAM) and store the results in 4201H and 4202H for square and 4203H for square root b) To find first n numbers of Fibonacci series and store the result in external RAM location starting from 4300H, where n is stored in 4200H. Equipments Required: Microcontroller Kit Theory: Square and square root: Square of a number can be obtained by multiplying with the same number. Square root can be obtained by dividing the number with non zero positive integers, until the quotient and divisor is equal. If quotient is less than divisor, the quotient is the fractional part of the square root of the number. Fibonacci series: Fibonacci numbers are the numbers in the following integer sequence: By definition, the first two numbers in the Fibonacci sequence are 0 and 1, and each subsequent number is the sum of the previous two. In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence relation with seed values Thus Fibonacci numbers are the series of numbers F0, F1, ..., given by
Page 53
If A > R2
No Yes
If A = R2 ?
No
R2 = R2 +1
Page 54
[DPTR] 1 R0 = R0 - 1
DPTR = DPTR 1 R1 [DPTR]
If R0 = 0?
Yes
Stop
Page 55
05 F9
NEXT
Page 57
Page 58
Objective: To interface 8-bit DAC and generate square, rectangular, saw tooth, and triangular waves. Equipments Required: Microcontroller Kit DAC 0800 Interface card 1 No. Flat ribbon cable (51 pin) 1 No. CRO/DSO 1 No. CRO probe 1 No. Theory: DAC interface card DAC is the counterpart of ADC that converts a digital (usually binary) code to an analog signal. It converts a fixed point binary into corresponding voltage or current. The simplified functional unit of 8-bit DAC is shown in figure 1.
Figure 1 Two types of DAC are binary weighted and R/2R ladder type and output may be voltage or current. In both cases output is a function of reference voltage/current and digital input code. An ideally sampled signal and DAC output corresponding to digital code of each sample is shown in figure 2 and figure 3.
DAC output Figure 3 DAC0800 Some features of DAC0800 chip are: 1. 8-bit R/2R ladder, current output type fast DAC 2. Interface directly with TTL, CMOS, etc type devices 3. Complementary current outputs 4. Wide power supply range: 4.5V to 18V 5. Low power consumption: 33 mW at 5V 6. Low cost Pin out diagram
Figure 4
Page 60
Non-inverting and inverting output current B1(MSB) to B8(LSB) V+ and VVref(+) and Vref(-) Compensation Output current Where Digital input Positive and negative power supplies Reference voltages which determines the full scale output current Compensation capacitor
DAC0800 interface card contains two DACs and I-V converters to convert the current output to voltage and DAC-1 circuit is shown in figure 5
Figure 5
Department of ECE, VKCET Page 61
The memory mapped IO addresses for DAC interface card are: DAC 1 data bus: FFC0H DAC 2 data bus: FFC8H
Page 62
Wave form generation: Square wave and rectangular generation: To generate square wave using DAC is by sending a data corresponding to peak amplitude and zero continuously. This method gives square wave with maximum frequency. We can reduce the frequency by including delay program and also rectangular wave is generated by different on and on periods. Saw tooth wave generation: To generate saw tooth wave using DAC is by sending data from 00H to FFH continuously with one step size. The frequency and peak amplitude of the signal are maximum value. Frequency can be changed by including required delay between each step. Peak voltage of the wave is reduced by changing the maximum data. Triangular wave generation: To generate triangular wave send data from 00H to FFH for rising and FFH to 00H for falling with one step size. Similar to other waves, the frequency and amplitude can change. Delay program The simple delay program is software delay, which is the program to kill the time using counters and decrement count value. Consider the following program sub-program: DELAY: MOV R7, #N ; Take 1MC WAIT: DJNZ R7, WAIT ; Take 2MC x N RET ; Take 2MC Total time to execute this subprogram is Tdelay = 1MC + 2MC x N + 2MC = 3MC + 2MC x N The kit has 11.0592 MHz crystal frequency for microcontroller, then 1MC = 12 x T = 1.0851s Then maximum delay by the program is by N=255 and is 556.65 s For a standard delay program of 500s, put Tdelay = 500 s, then N = 228.89 228 = E4H Similarly any standard delay program can be implemented.
Page 63
Page 64
Page 65
Page 66
COMMENTS DPTR FFC0H, memory mapped IO address of DAC1 data bus A 7FH, data for 0V [DPTR] A Wait 100s A FFH, data for 5V [DPTR] A Wait 100s Repeat the process R0 Count value for delay 100s R0 R0 1, if R0 0 go to label WAIT Return to main program
4200
DELAY WAIT
Page 67
COMMENTS DPTR FFC0H, memory mapped IO address of DAC1 data bus A 0 for minimum output voltage [DPTR] A A A+ 1 Repeat the process
Page 68
COMMENTS DPTR FFC0H, memory mapped IO address of DAC1 data bus A0 [DPTR] A A A+ 1 If A = FFH (Maximum), go to label INCREMENT, else next step [DPTR] A AA-1 If A = FFH (Minimum), go to label DECREMENT, else next step Repeat the process
INCREMENT
SJMP INCREMENT Observed output wave form have Vpp = 9V and f = 360Hz Procedure: Steps (Common for all programs): 1. Connect the DAC interface card and Microcontroller kit using 50-pin FRC 2. Connect DSO/CRO to the DAC output using header P2 using probe 3. Turn on power supply 4. Enter the program (starting address 4100H for main program) 5. Terminate from program enter mode 6. Execute the program 7. Observe the wave forms in CRO and measure the frequency and amplitude of the signal
Page 69
Objective: To generate sine wave using look-up table Equipments Required: Microcontroller Kit DAC 0800 Interface card 1 No. Flat ribbon cable (50 pin) 1 No. CRO/DSO 1 No. CRO probe 1 No. Theory: To generate a sine wave, first need a look-up table whose values represent the magnitude of the sine of angles between 0 and 360 degrees. The values for the sine function vary from -1.0 to +1.0 for 0- to 360-degree angles. Therefore, the table values must be integer numbers representing the voltage magnitude for the sin . This method ensures that only integer numbers are output to the DAC by the 8051microcontroller. To generate Table 1, we assumed the full-scale voltage of 10 V for DAC output. Full-scale output of the DAC is achieved when all the data inputs of the DAC are high. Therefore, to achieve the required full-scale output, we use the following equation
for in degree varies between 0o and 360o Where Vout is output voltage and is used to create data for look-up table , Vfs is full scale voltage and for the given DAC card it is 9V and is angle. Look-up table data (data input to DAC) can be implemented by Vout / step size, where step size = Vfs/256 Let step-angle = 20o and Vfs = 10V look-up table data are shown in table 1
Page 70
(in degree) 0 20 40 60 80 100 120 140 160 180 200 220 240 260 280 300 320 340 360
5 6.7 8.21 9.33 9.92 9.92 9.33 8.21 6.7 5 3.29 1.79 0.67 0.076 0.076 0.67 1.79 3.29 5
80H ABH D2H EEH FDH FDH EEH D2H ABH 80H 54H 2DH 11H 01H 01H 11H 2DH 54H 80H
Page 71
If R0 = 0 ? Yes
Page 72
NEXT
4200
LUT
Counter = 12H, length of LUT DPTR = 4200H, LUT starting MOV DPTR,#4200 address MOVX A,@DPTR A [DPTR] INC DPTR DPTR DPTR + 1 Save DPH content to default PUSH DPH stack PUSH DPL Save DPL content to stack MOV DPTR FFC0H, memrory DPTR,#FFC0 mapped IO address of DAC1 [DPTR] A, LUT element to MOVX @DPTR,A DAC POP DPL Load DPL content from stack POP DPH Load DPH content from stack R0 = R0 1, if R0 > 0, go to DJNZ R0, NEXT label NEXT, else next step SJMP MAIN Repeat the process Look-up Table 80 AB D2 EE FD FD EE D2 AB 80 54 2D 11 01 01 11 2D 54 80
Page 73
Page 74
Objective: To add two m x n matrices stored in 4300H and 4400H onwards in row-wise order and store the sum in 4500H onwards. Where m and n values are stored in 4200H and 4201H respectively. (All locations are external RAM) Equipments Required: Microcontroller Kit Theory: Matrix addition between two matrices A and B of dimension m x n can be performed as follows:
In 8051 based system matrix elements can be enter in data memory either row-wised or column wised. A row-wised entry to the RAM locations is as follows. Let
For matrix A 4300H 4301H 4302H 4303H 4304H 4305H 4306H 4307H 4309H 0 1 2 1 0 3 9 6 4 4400H 4401H 4402H 4403H 4404H 4405H 4406H 4407H 4409H
For matrix B 3 1 0 1 0 6 1 5 8
Page 75
Page 77
Page 78
Page 79
Objective: To display a message on LCD module in the microcontroller kit Equipments Required: Microcontroller Kit Theory: LCD interfacing: LCD is wide spreading display conventional to LEDs, due to: Low cost Ability to display numbers, characters and graphics Display refreshing task is not required Easy to program for characters and graphics The pins and its functions of 16 x 2 character LCD module is shown in the table 1
Page 80
Timing characteristics:
Page 81
Note: DDRAM Display Data RAM Where I/D = 1, increment DDRAM address, cursor moves to right I/D = 0, decrement DDRAM address, cursor moves to left SH = 1, shift entire display according to I/D SH = 1, no shift of entire display CGRAM Character Generator RAM D = 1, entire display turn on D = 0, display turn off, but display data remains in DDRAM C = 1, cursor turn on C = 0, cursor disappear, but I/D preserves its data B = 1, cursor blink on B = 0, cursor blink off
Page 82
AC Address Counter
DL = 1, 8-bit bus mode DL = 0, 4 bit bus mode N = 1, 2 line display mode N = 0, 1 line display mode F = 1, 5 x 10 dots font F = 0, 5 x 7 dots font Address for DDRAM: AC6 to AC0 are: The for position 1 in line 1 the command byte is 1000 0000 = 80H, for position 2 : 1000 0001 = 81H .
Memory mapped IO address of on-chip LCD pins: E and DB7-DB0 : FF04H R/W and RS : FF08H Bit orientation for FF08H : 0000 00 R/W RS Data for command register select: 00H Data for data register select: 01H
Page 83
Program Main program MAIN: ORG 4100H MOV A,#38H LCALL CMD_WRITE LCALL DELAY MOV A,#01H LCALL CMD_WRITE LCALL DELAY MOV A,#0FH LCALL CMD_WRITE LCALL DELAY MOV A,#06H LCALL CMD_WRITE LCALL DELAY MOV A,#80H LCALL CMD_WRITE LCALL DELAY MOV DPTR,#4200H MOV X A,@DPTR ;Function set command byte, 0011 1000b to A ;Write command to LCD ;Wait minimum 40s ;Clear display command byte, 0000 0001b to A ;Write command byte to LCD ;Wait minimum 2ms ;Display/cursor on/off command byte, 0000 1111b to A ;Write command byte to LCD ;Wait minimum 40s ;Entry mode command byte 0000 0110b to A ;Write command byte to LCD ;Wait minimum 40s ;DDRAM address command byte 1000 0000b to A ;Write command byte to LCD ;Wait minimum 40s ;Starting address of LUT of the message to DPTR ;A [DPTR]
NEXT:
Page 85
Sub-programs ;LCD command write CMD_WRITE: ORG 4150H MOV R7,A MOV A,#00 MOV DPTR,#0FF08H MOV X @DPTR,A MOV A,R7 MOV DPTR,#0FF04H MOV X @DPTR,A RET ;LCD data write DATA_WRITE: ORG 4170H MOV R7,A MOV A,#01 MOV DPTR,#0FF08H MOV X @DPTR,A MOV A,R7 MOV DPTR,#0FF04H MOV X @DPTR,A RET ;R7 A, save data byte ;A = 01, RS = 1 and R/W = 0 ;DPTR = FF08H, memory mapped IO address ;of RS and R/W pins ;[DPTR] A ;A R7 ;DPTR = FF04H, memory mapped IO address ;of DB0-DB7 and EN pin set to high ;[DPTR] A ;R7 A, save command byte ;A = 0, for RS = 0 and R/W = 0 ;DPTR = FF08H, memory mapped IO address ;of RS and R/W pins ;[DPTR] A ;A R7 ;DPTR = FF04, memory mapped IO address of ;DB0-DB7 and EN goes to high ;[DPTR] A ;Return to main program
Page 86
;M ;C ; ;L ;a ;b ; , :E :C ;E ; ;D ;e ;p ;t :NULL
Page 87
Objective: To generate a PWM signal using 8051 timers Equipments Required: Microcontroller Kit Flat Ribbon Cable (10 pin) CRO/DSO CRO probe Theory: PWM signals are signals with variable duty cycle. Using microcontrollers that signals can be used to control the speed of the dc motors, since the average current of PWM signal is the function of duty cycle. PWM signal
The duty cycle of the signal varies linearly and the time period is always constant. Using Timer/Counter in 8051 the delay can be provided for varying duty cycle. Header details
Page 88
Algorithm Algorithm to generate a PWM signal of 10 kHz (Crystal frequency in kit is 11.0592MHz) on 8051 pin P1.0 using timer/timers. Duty cycle varying from 90%, 60%, 30 as shown below:
Main program 1. Start 2. Initialize timer as 16-bit timer 3. Load count value to timer for 90s 4. Set P1.0 5. Call delay sub-routine 6. Load count value to timer for 10s 7. Clear P1.0 8. Call delay sub-routine 9. Load count value to timer for 60s 10. Set P1.0 11. Call delay sub-routine 12. Load count value to timer for 40s 13. Clear P1.0
Department of ECE, VKCET Page 89
;Delay sub-program DELAY: SETB TR0 WAIT: JNB TF0,WAIT CLR TR0 CLR TF0 RET END
Department of ECE, VKCET
Page 91
Connect FRC on header P17 Connect CRO probes across P1.0 and GND of FRC connector Turn on the kit and CRO/DSO Enter the program (from 4100H) Terminate from program enter mode Run the program Observe the wave form on CRO/DSO
Page 92
Figure 1 According to the sequence of pulses applied to each stator winding, the rotor will rotate. Rotor of conventional motors runs freely, but stepper motors rotor moves in a fixed repeatable increment, allows a precise position because of tooth in the rotor. For stepper motors relationship between steps per second and rpm is Steps per second = rpm x steps per revolution / 60
Department of ECE, VKCET Page 93
Note: Following figures shows the motor with step-angle of 90 o, thus after one complete step sequence motor rotates 360o. Step 1 Step 2 Step 3 Step 4
Page 94
2. Full step 4-step wave drive sequence For clock wise rotation Steps 1 2 3 4 Winding Winding Winding Winding D A B C 1 0 0 0 0 0 1 0 0 0 0 1 0 1 0 0
Page 95
For anti-clock wise rotation Steps 1 2 3 4 Step 1 Step 2 Winding Winding Winding Winding D A B C 0 0 0 1 0 1 0 0 Step 3 1 0 0 0 0 0 1 0 Step 4
Page 96
Step 5
Step 6
Step 7
Step 8
Page 97
Step 5
Step 6
Step 7
Step 8
Page 98
Figure 2 When the memory mapped IO address FF11H is placed in DPTR and loading A register results the following bit movement: Acc.0 M1 Acc.1 M2 Acc.2 M3 Acc.3 M4 Where M1, M2, M3 and M4 are stepper motor driver IC L298N inputs. L298N is high voltage, high current dual full-bridge driver designed to accept standard TTL logic levels and drive inductive loads such as relays, solenoids, DC and stepper motors. The stepper motor provided with the kit can be connecting to header P4 and the colour code of the stepper motor (Unipolar 6 leads) are: Brown Winding A Red Winding B Orange Winding C Yellow Winding D Black Winding A/C common White Winding B/D common
Department of ECE, VKCET Page 99
Page 100
Programs a) MAIN: REPEAT: NEXT: ORG 4100H MOV TMOD ,#01 ;Timer 0 in mode 1 MOV DPTR,#4200H ;LUT starting address of step sequence data to DPTR MOV R0,#04 ;Counter for full step sequence data MOVX A,@DPTR ;A [DPTR] PUSH DPH ;DPH to stack PUSH DPL ;DPL to stack MOV DPTR,#0FF11H ;DPTR = FF11H, memory mapped IO address of ;stepper motor MOVX @DPTR,A ;[DPTR] A ACALL DELAY ;Wait 50ms POP DPL ;DPL from stack POP DPH ;DPH from stack INC DPTR ;DPTR = DPTR + 1 DJNZ R0,NEXT ;R0 = R0 1, if R0 > 0 go to label NEXT SJMP REPEAT ;Repeat the process
;Delay sub-program ORG 4150H DELAY: MOV TH0,#40H MOV TL0,#00 SETB TR0 WAIT: JNB TF0,WAIT CLR TR0 CLR TF0 RET
;LUT at 4200H FS_2PHASE: DB 09H,05H,06H,0AH HS_MIXED: DB 08H,09H,01H,05H,04H,06H,02H,0AH ;Use counter value R0 = 08H END b) ORG 4100H MAIN: MOV TMOD,#01H ;Timer 0 in mode 1 CONTINUE: MOV R0,#19H ;Counter R0 = 19H, for 180o rotation
Page 103
NEXT1:
REPEAT2:
NEXT2:
Page 104
RPM_10:
RPM_4:
Page 105
WAIT:
LUT: FS_CW:
DB 09H,05,06,0AH END Procedure (Common for all programs) Steps: 1. Connect stepper motor on header P4 2. Connect FRC between port 1(header P17) and header P18, for program c 3. Turn on the kit 4. Enter the program (from 4100H) 5. Terminate from program enter mode 6. Run the program 7. Observe the rotation of motor
Page 106
Objective: To measure the frequency of pulse applied to P3.4 (T0) or P3.5 (T1) pin by counting the number of pulses in a fixed amount of time. Equipments Required: Microcontroller Kit (Vi89c51) Function generator CRO/DSO Probes Flat Ribbon Cable (50 pin) Theory: Frequency of a pulses can be measured by configuring 8051 timer/counter as event counter. Such a case, the frequency for the timer is frequency of the pulse applied to the T0 (P3.4) or T1 (P3.5) pin. Then starting timer and count value after 1 second gives the frequency of pulse. Timer as a counter is shown in figure 1.
Figure 1
Page 107
Figure 2 Using 50 pin FRC, we can use 89c51 pins. The details of FRC after connecting to VPIC bus is shown in figure 3.
Figure 3 (Note: T0 and T1 pins are also terminated using header P7 in the kit)
Department of ECE, VKCET Page 108
Page 109
Page 110
MAIN:
;Timer 1 as 16 bit timer and timer 0 as 16 bit ;counter ;DDRAM address of LCD
CONTINUE:
;Initial count (using 8 bit only) ;Start counter ;Wait 1 second ;Load count value to A ;Convert hex value to BCD ;Hundredth digit to ASCII
Page 111
;BCD to ASCII conversion ;Display hundredth digit ;Tenth digit to ASCII ;Display tenth digit ;Unit digit to ASCII ;Display unit digit ;Stop timer 0 ;H , ASCII of H ;z, ASCII of z ;Repeat process ;LCD initialize subroutine ;Function set
LCD_INIT:
;Clear display
;Entry mode
CMD_WRITE:
;RW = 0, RS = 0 ;Memory mapped IO address of RW and RS ;Command ;Memory mapped IO address of DB7-DB0 ;and E =1
DATA_WRITE:
;RW = 0, RS = 1;
LCD_DISPLAY:
HEX_2_BCD:
BCD_2_ASCII:
DELAY_40u:
Page 113
DELAY_2m:
DELAY_50m:
DELAY_1S: WAIT_1S:
DELAY: WAIT:
Procedure Steps: 1. Connect 50 pin FRC to VPIC Bus 2. Set function generator to square wave signal output and 5V peak amplitude. (Ensure it using CRO/DSO) 3. Connect function generator to VPIC pin 42 (P3.4) and pin 50 (GND) 4. Turn on the kit 5. Enter the program 6. Terminate from program enter mode 7. Execute the program 8. Observe the frequency displayed on LCD by changing input pulse frequency from 0 to 255Hz
Page 114
Page 115