Top Banner
Experiment No:- 1A Aim: Write an assembly language program to perform 16 bit addition of two numbers. Resources Used: TASM Theory: Instructions Used:- 1. MOV destination, source:- This Instruction transfers a byte/word from source to destination. Both the source and destination should be of the same size. 2. ADD destination, source:- This instruction adds the source with destination and stores the result in the destination. Algorithm: Step 1: Initialize data segment. Step 2: Take two variable of size word i. e. 16 bits NUM1 and NUM2 and initialize them with some value Step 3: Take other variable RESULT of size word which will store the result. Step 4: End of data segment. Step 5: Initialize code segment. Step 6: Initialize CS and DS register Step 7: Load the contents of NUM1 in Ax register. Step 8: Load the contents of NUM2 in Bx register Step 9: Add the content of Ax and Bx and store the result in Ax. Step 10: Load the Variable RESULT with contents of Ax Step 11: End of code segment. Step 12: End of program. KJ SCE/IT/ Microprocessor and Microcontroller
53

Mpmc Manual

Mar 09, 2015

Download

Documents

Anurag Bhavsar
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Mpmc Manual

Experiment No:- 1A

Aim: Write an assembly language program to perform 16 bit addition of two numbers. Resources Used: TASM Theory: Instructions Used:-

1. MOV destination, source:- This Instruction transfers a byte/word from source to destination. Both the source and destination should be of the same size.

2. ADD destination, source:- This instruction adds the source with destination and stores the result in the destination.

Algorithm: Step 1: Initialize data segment. Step 2: Take two variable of size word i. e. 16 bits NUM1 and NUM2 and initialize them with some value Step 3: Take other variable RESULT of size word which will store the result. Step 4: End of data segment. Step 5: Initialize code segment. Step 6: Initialize CS and DS register Step 7: Load the contents of NUM1 in Ax register. Step 8: Load the contents of NUM2 in Bx register Step 9: Add the content of Ax and Bx and store the result in Ax. Step 10: Load the Variable RESULT with contents of Ax Step 11: End of code segment. Step 12: End of program.

KJ SCE/IT/ Microprocessor and Microcontroller

Page 2: Mpmc Manual

Example: LET NUM1 = 4040H NUM2 = 5050H AX 4040H BX 5050H ADD AX, BX AX 9090H RESULT 9090H Conclusion: The program to perform addition of two 16 bit numbers was implemented. The instruction ADD was used to perform addition of contents of two registers respectively.

KJ SCE/IT/ Microprocessor and Microcontroller

Page 3: Mpmc Manual

Program: DATA SEGMENT NUM1 DW 1200H NUM2 DW 0012H RESULT DW ? DATA ENDS CODE SEGMENT ASUME CS:CODE, DS:DATA START: MOV AX,DATA MOV DS,AX MOV AX,NUM1 MOV BX,NUM2 ADD AX,BX MOV RESULT,AX MOV AH,4CH INT 21H CODE ENDS END START END Output: NUM1 1200H NUM2 0012H AX NUM1 BX NUM2 ADD AX,BX AX 1212H RESULT 1212H

KJ SCE/IT/ Microprocessor and Microcontroller

Page 4: Mpmc Manual

Experiment No:- 1B

Aim: Write an assembly language program to perform 16 bit multiplication. Resources Used: TASM Theory: Instructions Used:-

1. MOV destination, source:- This Instruction transfers a byte/word from source to destination. Both the source and destination should be of the same size.

2. MUL Source:- This instruction multiplies the source with the contents of the default register AX. The results (lower byte and higher byte) are stored in DX: AX register pair.

Algorithm: Step 1: Initialize DATA segment. Step 2: Take two variables NUM1 and NUM2 of size word i.e. 16-bit with some value. Step 3: Take third variable RESULT of size double word to store the result. Step 4: End of DATA segment. Step 5: Initialize CODE segment Step 6: Initialize CS and DS registers. Step 7: Load AX register with value of NUM1. Step 8: Load BX register with value of NUM2. Step 9: Initialize DX with 0000H. Step 10: Multiply AX and BX and store result in AX and DX. Step 11: Load variable RESULT with AX and DX. Step 12: End code segment. Step 13: End of program.

KJ SCE/IT/ Microprocessor and Microcontroller

Page 5: Mpmc Manual

Example: NUM1=1000H NUM2=0050H AX 1000H BX 0050H MUL BX DX.AX 0005 0000 Z 0005 0000H Conclusion: The program to perform multiplication of two 16 bit numbers was developed, The MUL instruction is used to perform multiplication of contents of registers.

KJ SCE/IT/ Microprocessor and Microcontroller

Page 6: Mpmc Manual

Program: DATA SEGMENT NUM1 DW 1234H NUM2 DW 1456H RESULT DD ? DATA ENDS CODE SEGMENT ASSUME CS:CODE, DS:DATA START: MOV AX,DATA MOV DS,AX MOV AX,NUM1 MOV BX,NUM2 MOV DX,0000H MUL BX MOV WORD PTR[RESULT],AX MOV WORD PTR[RESULT+2],DX MOV AH,4CH INT 21H CODE ENDS END START END Output: NUM1 1234H NUM2 1456H AX NUM1 BX NUM2 DX 0000H MUL BX DX 0172H AX 2D78H RESULT 01722D78H

KJ SCE/IT/ Microprocessor and Microcontroller

Page 7: Mpmc Manual

Experiment No:- 1C Aim: Write an assembly language program to perform 16 bit by 8 bit division. Resources Used: TASM Theory: Instructions Used:-

1. MOV destination, source:- This Instruction transfers a byte/word from source to destination. Both the source and destination should be of the same size.

2. DIV Source:- This instruction divides the contents of mentioned register with the contents of the corresponding default register. The results (quotient and remainder) are store in accumulator.

Algorithm: Step 1: Initialize DATA segment. Step 2: Initialize variable NUM1 and NUM2 of type WORD and BYTE respectively. Step 3: Declare variables Q and R to store the quotient and remainder. Step 4: End of DATA segment. Step 5: Initialize CODE segment and CS and DS register. Step 6: Move 16-bits of NUM1 to AX Step 7: Move 8-bits of NUM2 to BH Step 8: Divide contents of AX register with the contents of BH register. Step 9: Store the contents of AH register to R and AL register to Q as a result Step 10: End code segment. Step 11: End of program.

KJ SCE/IT/ Microprocessor and Microcontroller

Page 8: Mpmc Manual

Example: NUM1 0005H NUM2 02H AFTER DIVISION AL 02H AH 01H Q 02H (QOUTIENT) R 01H (REMAINDER Conclusion: The program to perform division of a 16 bit number by 8 bit was developed, DIV instruction was used. After the division operation, the remainder was stored in AH and the quotient was stored in AL.

KJ SCE/IT/ Microprocessor and Microcontroller

Page 9: Mpmc Manual

Program: DATA SEGMENT NUM1 DW 0005H NUM2 DB 02 Q DB ? R DB ? DATA ENDS CODE SEGMENT ASSUME CS:CODE,DS:DATA START: MOV AX,DATA MOV DS,AX MOV AX,NUM1 MOV BL,NUM2 DIV BL MOV Q,AL MOV R,AH MOV AH,4CH INT 21H CODE ENDS END START END Output: NUM1 0005H NUM2 02H AX NUM1 BL NUM2 AFTER DIVISION AL 02H AH 01H Q 02H (QOUTIENT) R 01H (REMAINDER)

KJ SCE/IT/ Microprocessor and Microcontroller

Page 10: Mpmc Manual

Experiment No:- 2 Aim: To implement concept of MACRO using assembly language

programming Resources Used: TASM Theory: Instructions Used:-

1. MACRO:- A MACRO is a group of instructions that perform a task. Each time we call the macro in our program, the assembler will insert a group of defined instructions in place of call. Format:- MACRO_name MACRO ;define macro

; Body of MACRO ENDM ; End MACRO

2. MOV destination, source:- This instruction transfers a bye/word from source to destination both should be of the same size.

Algorithm: Step 1: Define a macro power with a single argument P to calculate the power of a particular no. Step 2: Move the contents of AX into BX. Step 3: Multiply contents of BX with AX. Step 4: Decrement the counter p. Step 5: Continue the loop until value of counter P is 0. Step 6: End the macro Step 7: Initialize the DATA segment. Step 8: Declare the word variable A and a Byte variable B and initialize them with some value. Step 9: End the DATA segment. Step 10: Initialize CODE segment.

KJ SCE/IT/ Microprocessor and Microcontroller

Page 11: Mpmc Manual

Step 11: Initialize the DS and CS register. Step 12: Move the contents of A into AX. Step 13: Decrement B. Step 14: call the macro Power to calculate the power of A. the argument passed is B. Step 15: End the CODE segment. Step 16: End the program. Example: A0002H B0002H AXA Power B AX0004H

Conclusion: The MACRO was developed; the power of the number is calculated by calling the MACRO.

KJ SCE/IT/ Microprocessor and Microcontroller

Page 12: Mpmc Manual

Program: POWER MACRO P MOV BX, AX BACK: MUL BX DEC P JNZ BACK ENDM DATA SEGMENT A DB 10H B DB 03H Pow dw ? DATA ENDS CODE SEGMENT ASSUME CS: CODE, DS:DATA START: MOV AX,DATA MOV DS,AX MOV AX,A DEC B POWER B Mov Pow,AX MOV AH,4CH INT 21H CODE ENDS END START END Output: A10H B03H AXA CL03H Power P AX1000H Pow AX

KJ SCE/IT/ Microprocessor and Microcontroller

Page 13: Mpmc Manual

Experiment No:- 3 Aim: To write an assembly language program to find the factorial of a number using procedure. Resources Used: TASM Theory: Instruction used:-

1. MOV Destination Source:- This instruction loads the destinations with the contents of the source. Both cannot be memory location.

2. RET:- RET instruction causes the control to return to the main program from the subroutine.

3. DEC destination:- Subtracts 1 from the specified destination. The destination can be register or memory location.

4. MUL source:- This instruction multiplies the source with the accumulator and stores the result in accumulator.

5. JNZ LABEL:- Jump to specified label till zero flag is not set.

6. PROC:-

A procedure is a group of instructions that perform a task. Each time we call the procedure in our program, the control is transferred to the procedure. For calling the procedure, the return address has to be stored on to the stack. Format: [Procedure name] PROC [attribute]; beginning of the Procedure [Procedure name]ENDP ; end of procedure

KJ SCE/IT/ Microprocessor and Microcontroller

Page 14: Mpmc Manual

Algorithm: Step 1: Initialize data segment. Step 2: Declare two variable NUM and RES and initialize them with some value Step 3: End of data segment. Step 4: Initialize code segment. Step 5: Initialize CS and DS register Step 6: Load counter CX with the value of variable NUM. Step 7: Load AX with value 0001H Step 8: Call procedure ‘FACT’. Step 9: Multiply the contents of counter CX with accumulator. Step 10: Decrement CX Step 11: Repeat steps 11 to 13 till counter CX becomes zero Step 12: Move the contents of accumulator to variable ‘RES’ Step 13: End of code segment. Step 14: End of program Example: LET A = 4H CX = 4 Ax = 1 AX=AX*CX=4 CX= 3(after decrement) AX=AX*BX=0C BX=2 (after decrement) AX=AX*BX=18 BX= 1 (after decrement) AX=AX*BX=18

KJ SCE/IT/ Microprocessor and Microcontroller

Page 15: Mpmc Manual

Conclusion: The program to find factorial of a number was developed by calling a procedure recursively. The procedure was written to perform the factorial. Extra segment was used to define the procedure.

KJ SCE/IT/ Microprocessor and Microcontroller

Page 16: Mpmc Manual

Program: DATA SEGMENT NUM DW 0005H RES DW ? DATA ENDS CODE SEGMENT ASSUME DS:DATA,CS:CODE START: MOV AX,DATA MOV DS,AX MOV AX,0001H MOV CX,NUM BACK:CALL FACT JNZ BACK MOV RES,AX MOV AH,4CH INT 21H FACT PROC NEAR MUL CX DEC CX RET FACT ENDP CODE ENDS END START END Output: NUM0005H AX0001H CXNUM CALL FACT AX0078H RES 0078H

KJ SCE/IT/ Microprocessor and Microcontroller

Page 17: Mpmc Manual

Experiment No:- 4 Aim: To write an assembly level program to check whether the string is palindrome. Resources Used: TASM Theory: Instructions Used:-

1. MOV destination, source:- This Instruction transfers a byte/word from source to destination. Both the source and destination should be of the same size.

2. CLD:-

This instruction clears the direction flag. No other flags are affected.

3. LOOP label:- This instruction decrements the contents of CX register and jumps to instruction pointed by label if ZF is1.

4. LODSB:-

This instruction is used to load AL register with a byte of data from data segment. The offset of the source is in SI register.

5. STOSB:-

This instruction is used to store AL register with a byte of data in extra segment. The offset of the source is in DI register.

6. STD:-

This instruction sets the direction flag. No other flags are affected.

7. CMPSB:-

This instruction is used to compare a byte in a data segment with a byte in an extra segment. The offset of the byte in data segment is in SI. The offset of the byte in extra segment is in DI.

KJ SCE/IT/ Microprocessor and Microcontroller

Page 18: Mpmc Manual

8. LEA register, source:- This instruction loads the effective address (offset address) of the source into given register.

Algorithm: Step 1: Start the DATA segment. Step 2: Declare a string of Byte type STR1 and initialize it to an appropriate value. Step 3: Declare Byte type variables STR3 and STR4 to display appropriate messages. Step 4: End the DATA segment. Step 5: Initialize Extra segment Step 6: Declare a Byte type string STR2. Step 7: End of Extra segment. Step 8: Initialize the CODE segment. Step 9: Initialize DS,CS and ES. Step 10: Load the effective address of STR1 into SI. Step 11: Load the effective address of ending of STR2 into DI. Step 12: Move the count value into CX. Step 13: Repeat the steps from 14 to 17 until count becomes 0. Step 14: Clear direction flag. Step 15: Load a Byte character from source string STR1 into accumulator. Step 16: Set the direction flag. Step 17: Store a Byte character from accumulator to destination string STR2. Step 18: Load effective address of STR1 into SI. Step 19: Load effective address of STR2 into DI. Step 20: Move the count value into CX. Step 21: Repeatedly compare each byte of two strings until they are equal.

KJ SCE/IT/ Microprocessor and Microcontroller

Page 19: Mpmc Manual

Step 22: If the strings are equal, display the message “IT IS A PALINDROME”. Step 23: If the strings are not equal, display the message “NOT PALINDROME”. Step 24: End of the CODE segment. Step 25: End of the program. Example: A="NITIN" then iteratively C will be C="N" C="NI" C="NIT" C="NITI" C="NITIN" Now, A & C will be compared using CMPSB NITIN is Palindrome. Conclusion: The program to check whether the string is a palindrome or not was developed by using EXTRA segment and direction flag. The LEA instruction was used for loading effective address of the string.

KJ SCE/IT/ Microprocessor and Microcontroller

Page 20: Mpmc Manual

Program: DATA SEGMENT STR1 DB"ABBA","$" STR3 DB"NOT PALLINDROME$" STR4 db “IT IS A PALINDROME$” DATA ENDS EXTRA SEGMENT STR2 DB 04 DUP(?) EXTRA ENDS CODE SEGMENT ASSUME CS:CODE,DS:DATA,ES:EXTRA START: MOV AX,DATA MOV DS,AX MOV AX, EXTRA MOV ES,AX LEA SI,STR1 LEA DI,STR2+3 MOV CX,0004H BACK:CLD LODSB STD STOSB LOOP BACK LEA SI,STR1 LEA DI,STR2 MOV CX,0004H CLD REPE CMPSB JZ UP LEA DX,STR3 MOV AH,09H INT 21H JMP SEND UP:LEA DX,STR4 MOV AH,09H INT 21H SEND:MOV AH,4CH INT 21H CODE ENDS END START END

KJ SCE/IT/ Microprocessor and Microcontroller

Page 21: Mpmc Manual

Output: STR1”ABBA” IT IS A PALINDROME

KJ SCE/IT/ Microprocessor and Microcontroller

Page 22: Mpmc Manual

Experiment No:- 5 Aim: To write an assembly level program to count number of 0’S, +ve and -ve numbers in a string. Resources Used: TASM Theory: Instructions Used:-

1. MOV destination, source:- This Instruction transfers a byte/word from source to destination. Both the source and destination should be of the same size.

2. CMP destination, source:-

This instruction compares a source with the destination, both of the same size. The source is subtracted from the destination. Only the flags are affected.

3. LEA register, source:-

This instruction loads the effective address (offset address) of the source into given register.

4. J condition (conditional jump):-

This is a conditional branch instruction which executes jump if the condition is true.

a) JZ:- It branches if zero flag is set.

b) JL:- It branches if operation returns the value less than zero.

c) JG:- It branches if operation returns the value greater than zero

d) JNZ:- It branches if zero flag is not set.

KJ SCE/IT/ Microprocessor and Microcontroller

Page 23: Mpmc Manual

Algorithm: Step 1: Start the DATA segment. Step 2: Declare a array of Byte type and initialize it to an appropriate values. Initialize variables p, n, z with 00H. Step 3: End the DATA segment. Step 4: Initialize the CODE segment. Step 5: Initialize DS and CS. Step 6: Load the effective address of array in source index register[SI]. Step 7: Load the count value into CX. Step 8: Under label 'UP' perform: A) Store 0000H in AX & BX. B) Move string contents pointed by SI in BL. C) Compare BL contents with AL D) if it is equal, go 'Zero' E) if it is greater, go 'pos' F) if it is lesser, go 'nega' Step 9: Under label 'Zero' perform Increment 'Z' Go to step 12 Step 10: Under label 'pos' perform Increment 'p' Go to step 12 Step 11: Under label 'nega' perform Increment 'n' Step 12: Increament SI register contents and decrement CX register contents. Step 13: If 'Zero' flag is reset then go to label UP and execute instructions from step 8 onwards. Step 14: End of the CODE segment. Step 15: End of the program.

KJ SCE/IT/ Microprocessor and Microcontroller

Page 24: Mpmc Manual

Example: SI<-- ARR ARR="1", "2", "-3", "0" CX=0004H UP: AX <-- 0000H BL <-- 1 CMP BL, AL INC p CX <-- 3 SI <-- SI+1 BL <-- 2 CMP BL, AL INC p CX <-- 2 SI <-- SI+1 BL <-- -3 CMP BL, AL INC n CX <-- 1 SI <-- SI+1 BL <-- 0 CMP BL, AL INC z z=1 p=2 n=1 Conclusion: The program to count the number of 0’S, +ve and -ve numbers in an array was developed using JZ, JG, JL instructions.

KJ SCE/IT/ Microprocessor and Microcontroller

Page 25: Mpmc Manual

Program: DATA SEGMENT STR1 DB 01H, 02H,-03H, 00H N DB 00H P DB 00H Z DB 00H DATA ENDS CODE SEGMENT ASSUME CS:CODE,DS,DATA START: MOV AX,DATA MOV DS,AX MOV CX,0005H LEA SI,STR1 UP: MOV AX,0000H MOV BX,0000H MOV BL,[SI] CMP AL,BL JZ ZERO JG POS JL NEGA ZERO: INC Z JMP LAST POS: INC P JMP LAST NEGA: INC N LAST: INC SI DEC CX JNZ UP MOV AH,4CH INT 21H CODE ENDS END START END

KJ SCE/IT/ Microprocessor and Microcontroller

Page 26: Mpmc Manual

Output: ARR01H P01 N00 Z00 ARR02H P02 N00 Z00 ARR03H P02 N01 Z00 ARR02H P02 N01 Z01

KJ SCE/IT/ Microprocessor and Microcontroller

Page 27: Mpmc Manual

Experiment No:- 6 Aim: To write an assembly language program for blinking of alternate LED’s. Resources Used: KEIL, KB8051 base kit Theory: When interfacing an LED to a TTL output, the maximum current through the LED is 16 mA. The circuit below shows how to interface an LED to a microcontroller port pin. This limits the LED on current to 16 mA, which in most cases is adequate. However, if more current is required, the second circuit shown above may be used. To interface a LED to a Microcontroller

The adjoining figure shows how to interface the LED to 8051 microcontroller. As you can see the Anode is connected through a resistor to Vcc & the Cathode is connected to the Microcontroller pin. So when the Port Pin is HIGH the LED is OFF & when the Port Pin is LOW the LED is turned ON.

8051 has an internal pull-up resistor of 10kΩ. So now when the port Pin is HIGH the Anode is positive with respect to the Cathode so the LED should turn ON. But the internal pull-up resistor comes in series with the resistor thus limiting the current flowing through the LED. This current is not sufficient enough to Turn ON the LED.

KJ SCE/IT/ Microprocessor and Microcontroller

Page 28: Mpmc Manual

The circuit explains how to connect an LED to the Microcontroller.

Algorithm: Step 1: Start. Step 2: Output AAH to output port 0 to flash all odd LED’s off. Step 3: Introduce a time delay. Step 4: Output 55H to port 0 to flash all even LED’s off. Step 5: Introduce a time delay.

KJ SCE/IT/ Microprocessor and Microcontroller

Page 29: Mpmc Manual

Step 6: Go to step 2. Step 7: Stop. Conclusion: The program to blink alternate LED’s was developed in assembly language by using 8051 instructions. The corresponding HEX file generated was given as an input to 8051 kit and the output was observed on 8051.

KJ SCE/IT/ Microprocessor and Microcontroller

Page 30: Mpmc Manual

Program: org 0000h start: mov p0,#55h mov r0,#80h here1:mov r1,#0ffh here:djnz r1,here djnz r0,here1 mov p0,#0aah mov r0,#80h here2: mov r1,#0ffh here3: djnz r1,here3 djnz r0,here2 ljmp start end Output: The output was observed on 8051 controller kit. The Led’s were blinking.

KJ SCE/IT/ Microprocessor and Microcontroller

Page 31: Mpmc Manual

Experiment No:- 7 Aim: To write an assembly language program to interface hex keyboard with 8051. Resources Used: KEIL, KB8051 base kit Theory:

In a simple keyboard interface one input line is required to interface one key and this increases the number of keys. When a large number of keys are to be interfaced, this technique is not useful. Matrix method is used in such cases, so that the number of connections is reduced.

Fig. shows 16 keys arranged in 4 rows and 4 columns. No connections are there, when the keys are open. If a key is pressed then there is connection between corresponding rows and columns. Such a matrix requires eight lines to complete the connections.

KJ SCE/IT/ Microprocessor and Microcontroller

Page 32: Mpmc Manual

Algorithm: Step 1: Start. Step 2: Output FFH to all columns and make all rows as input. Step 3: Get contents of port 2 in accumulator. Step 4: Check if the key is pressed or not. Step 5: If the key is pressed then go to step 2 else continue. Step 6: Introduce a time delay. . Step 7: Repeat steps 2 to 4. Step 8: Initialize R2 as key counter. Step 9: Initialize R3 as column counter. Step 10: Scan 1st row of keyboard. If key is detected go to step 13. Step 11: Scan 2nd row of keyboard. If key is detected go to step 13. Step 12: Scan 3rd row of keyboard. If key is detected go to step 13. Step 13: Display the key position on port 0. Step 14: Go to step 2. Conclusion: The program to interface hex keyboard with 8051 was developed in assembly language by using 8051 instructions. The corresponding HEX file generated was given as an input to 8051 kit and the output was observed on 8051.

KJ SCE/IT/ Microprocessor and Microcontroller

Page 33: Mpmc Manual

Program: org 00h sjmp start org 30h start: mov p0,#0ffh again: acall kbd mov p0,a sjmp again kbd: mov p2,#0fh mov a,p2 cjne a,#0fh,kbd acall dly mov p2,#0fh mov a,p2 cjne a,#0fh,get_newkey cpl a mov r2,a sjmp key_found get_newkey: acall dly mov r0,#0feh mov r2,#0h back: mov p2,r0 mov a,p2 mov r1,#04h loop: rlc a jnc key_found inc r2 djnz r1,loop mov a,r0 rl a mov r0,a sjmp back key_found: mov a,r2 cpl a ret dly: mov r3,#0fh here: djnz r3,here ret end Output: The output was observed on 8051 kit. The combination of led’s were flashing after pressing the keys of hex keyboard.

KJ SCE/IT/ Microprocessor and Microcontroller

Page 34: Mpmc Manual

Experiment No:- 8 Aim: To write a program in C to interface 7 segment display with 8051. Resources Used: KEIL, KB8051 base kit Theory: Each of the segments of the display is connected to a pin on the 8051 (the

schematic shows how to do this). In order to light up a segment on the the pin

must be set to 0V. To turn a segment off the corresponding pin must be set to

5V. This is simply done by setting the pins on the 8051 to '1' or '0'.

7-SEG Display are available in two types –

1. Common anode & 2 common cathode,

but common anode display are most suitable for interfacing with 8051 since

8051 port pins can sink current better than sourcing it.

KJ SCE/IT/ Microprocessor and Microcontroller

Page 35: Mpmc Manual

Digit Seg. h Seg. g Seg. f Seg. e Seg. d Seg. c Seg. b Seg. a HEX

0 1 1 0 0 0 0 0 0 C0

1 0 0 0 0 0 1 1 0 06

2 1 0 1 0 0 1 0 0 A4 3 1 0 1 1 0 0 0 0 B0 4 1 0 0 1 1 0 0 1 99

Segment number 8051 pin number

a P1.0

b P1.1

c P1.2

d P1.3

e P1.4

f P1.5

g p1.6

h(dp) P1.7

Interfacing 8051 with 7 segment display: It is often necessary to interface a number of 7-segment displays to a microcontroller. Rather than use a separate port for each display, all of the displays are connected to the same port and other port pins are used for switching on one display at a time

KJ SCE/IT/ Microprocessor and Microcontroller

Page 36: Mpmc Manual

Algorithm: Step 1: Start Step 2: Define prototypes for initializing timer, serial port, delay and seven segment display. Step 3: call function seven segment display. Step 4: compute 1000th position by using formula “data/1000”. Step 5: Compute 100th position by using formula “(data%1000)/100”. Step 6: Compute 10th position using “((data%1000)%100)/10”. Step 7: Compute 1st position using “(data%1000)%100)%10”. Step 8: Insert delay of 20 msec. Step 9: Stop. Conclusion: The program to interface 7 segment display with 8051 was developed in C. The corresponding HEX file generated was given as an input to 8051 kit and the output was observed on 8051.

KJ SCE/IT/ Microprocessor and Microcontroller

Page 37: Mpmc Manual

Program: // 8051 Initial C Source File #include <REG51.h> #include <stdio.h> #include <conio.h> #include "sfrbit.h" /* Basic Text information for wiring details */ /* Connect TKBMC-89C51RD2 HP1 for CNMUX driver P1.4,5,6,7 */ /* Connect TKBMC-89C51RD2 HP2 for CNSEG driver */ /* Effect Running counter on 7Seg */ /* Define Prototypes */ void _InitSerialPort(void); extern void TimerInit(void); extern void Delay(unsigned int dms); extern void SevenSegDisp(int Data); /* define variables and constants */ unsigned int delay_ms; /* Main start here */ main() int i=1; _InitSerialPort(); TimerInit(); /* Initialize Timer and Serial Port */ printf("Hello World - Rolling Counter \n"); while(1) for (i = 1; i < 9999; i++) SevenSegDisp(i); Delay(delay_ms = 20); /SerCom.C #include <REG51.H> #include "SerCom.H"

KJ SCE/IT/ Microprocessor and Microcontroller

Page 38: Mpmc Manual

/* Define Prototypes */ void TimerInit(void); void Delay(unsigned int dms); /* Define variables */ extern unsigned int delay_ms; extern unsigned char digitvalue[],digitpos[]; unsigned char digitptr,delay_7seg; /* timer routine this is the heart of our program */ /* Set timer 0 for keyboard and timer 1 for baudrate generation */ void Delay(unsigned int dms) delay_ms = dms; while(delay_ms > 0) /* the do nothing loop */ void TimerInit() /* we now start the timer */ delay_7seg=0; digitptr = 0; /* start with the first digit */ SCON = 0x52; /* 8-bit UART with Variable Baudrate,TI=1,RI=0 */ TMOD = 0x21; /* Timer 1 in mode 2, 8-bit auto-reload, T0 = 0X01, T1 = 0X20*/ //TH1 = 0xFD; /* Standard 8051 -- 9600 Baud @11.0592 Mhz */ //TH1 = (256 - (XTAL/(6L * 16L * BAUDRATE))); //6 clock periods per machine cycle TH1 = (256 - (XTAL/(12L * 16L * BAUDRATE))); //12 clock periods per machine cycle TL0 = 0xb0; /* load the timer low bits 3cb0 -> 50 mSec */ TH0 = 0x3c; /* load the timer high bits fc00 */ ET0 = 1; /* enable timer0 interupt */ EA = 1; /* enable master interupt */ TR1 = 0x01; /* Turn on Timer 1 */

KJ SCE/IT/ Microprocessor and Microcontroller

Page 39: Mpmc Manual

/* now we start both the timers */ TCON = 0X50; /* Start T0 for keyboard and T1 for serial */ return; //SevenSegDisp.C //initialize variables /* This is 7seg digit codes */ _code unsigned char sevensegdata[]=0x3f,0x6,0x5b,0x4f,0x66,0x6d,0x7d,0x7,0x7f ,0x6f,0x77,0x7c,0x79,0x5e,0x79,0x71; /* This is 7seg digit values */ unsigned char digitvalue[]=0x0F,0x0F,0x0F,0x0F; //unsigned char digitpos[]=0x1F,0x2F,0x4F,0x8F; // when we put 7404 unsigned char digitpos[]=0xEF,0xDF,0xBF,0x7F; // when we put 7407 void SevenSegDisp(int Data) int tempdata; unsigned char codepos; /* compute 1000 position */ tempdata = Data; codepos = ( tempdata / 1000 ); digitvalue[0] = sevensegdata[codepos]; /* compute 100 position */ tempdata = Data; codepos = ( ( tempdata % 1000 ) / 100 ); digitvalue[1] = sevensegdata[codepos]; /* compute 10 position */ tempdata = Data; codepos = ( ( ( tempdata % 1000 ) % 100 ) / 10 ); digitvalue[2] = sevensegdata[codepos]; /* compute 1 position */ tempdata = Data; codepos = ( ( ( tempdata % 1000 ) % 100 ) % 10 ); digitvalue[3] = sevensegdata[codepos]; // Serial communication variables #define BAUDRATE 9600 //#define XTAL 12000000 #define XTAL 11059200 #define XDIVIDER 12

KJ SCE/IT/ Microprocessor and Microcontroller

Page 40: Mpmc Manual

Output:

The output was observed on 7 segment LED’s of the 8051 kit

KJ SCE/IT/ Microprocessor and Microcontroller

Page 41: Mpmc Manual

Experiment No:- 9 Aim: To write a program in C to interface Relay with 8051. Resources Used: KEIL, KB8051 base kit Theory: Relays are devices which allow low power circuits to switch a relatively high Current/Voltage ON/OFF. For a relay to operate a suitable pull-in & holding current should be passed through its coil. Generally relay coils are designed to operate from a particular voltage often its 5V or 12V. The function of relay driver circuit is to provide the necessary current (typically 25 to 70ma) to energize the relay coil. Condition – Corresponding led should light up when switch pressed , i.e. if Switch at 1.0 is pressed -> LED at P0.0 should light up.

To Test TK51RD – Relay operation (LED => LPNC/LPNO) 1. Switch OFF power to TKBase. 2. Place TK51RD daughter board on TKBase. 3. Remove all connecting wires and cables. 4. Connect TKBase serial port ot PC-COM. 5. Ensure that RTS and CTS is shorted on CNSER(CN43) 6. Connect 89V51RD pin 10, 11 [Port P3.0 and P3.1] (Rx and Tx) to TKBase Rx and Tx. 7. Power ON the TKBase. 8. Run Flash Magic Programmer. 9. Select Part 89V51RD2 and appropriate com port, baud rate 9600. 10. Select Relay.Hex file from TKBMC\89V51RD2\Relay. 11. Switch Jumper on TK51RD to ISP mode (required if you have 89C51RD2) 12. Reset the CPU.

KJ SCE/IT/ Microprocessor and Microcontroller

Page 42: Mpmc Manual

13. Programmed and verify the 89V51RD2. 14. Switch the TK51RD ISP jumper to User Program mode( Only for 89C51RD2 ). 15. Reset the CPU. 16. Connect Header H1 (Port1) to CNKY1 and PIN H4 ( P0.0 PIN) by Flying wire to CNRLY. 17. Press Matrix key K1 and K2 to toggle Relay ON and OFF and monitor the effect on LED LPNC and LED LPNO. To Test TK51RD 1. Switch OFF power to TKBase. 2. Place TK51RD daughter board on TKBase. 3. Remove all connecting wires and cables. 4. Connect TKBase serial port ot PC-COM. 5. Ensure that RTS and CTS is shorted on CNSER(CN43) 6. Connect 89V51RD pin 10, 11 [Port P3.0 and P3.1] (Rx and Tx) to TKBase Rx and Tx. 7. Power ON the TKBase. 8. Run Flash Magic Programmer. 9. Select Part 89V51RD2 and appropriate com port, baud rate 9600.

Algorithm: Step 1: Start. Step 2: Define a variable KEYDATA and a function GETKEY to initialize port 0 to 0 and 1 respectively using two cases. Step 3: Initialize port 2 to 0000H. Step 4: Call the function delay for inserting a delay of 20msec till the value of port 1 equals 000FH. Step 5: Call the function GETKEY till the value of port 1 equals 000FH. Step 6: Go to step 4. Step 7: Stop.

KJ SCE/IT/ Microprocessor and Microcontroller

Page 43: Mpmc Manual

Conclusion: The program to interface Relay with 8051 was developed in assembly language by using 8051 instructions. The corresponding HEX file generated was given as an input to 8051 kit and the output was observed on 8051.

KJ SCE/IT/ Microprocessor and Microcontroller

Page 44: Mpmc Manual

Program: // 8051 Initial C Source File #include <REG51.h> #include <stdio.h> #include <conio.h> #include "sfrbit.h" /* Basic Text information for wiring details */ /* Connect TKBMC- 89C51RD2 JP0.0 for RELAY's */ /* Connect TKBMC- 89C51RD2 JP1 for SimpleKbd (K1 & K2) */ /* During Flash programming keep JP3 empty */ /* Define Prototypes */ void _InitSerialPort(void); int GetKey(int keydata); extern void TimerInit(void); extern void Delay(unsigned int dms); /* define variables and constants */ int keydata; unsigned int delay_ms; /* Main start here */ int GetKey(int keydata) switch(keydata) case 0X0E: P0_0 = 1; break; case 0X0D: P0_0 = 0; break; main() _InitSerialPort(); /* Support from crossware */ TimerInit(); /* Initialize Timer and Serial Port */ printf("Hello World - Relay Testing on P2.0 \n");

KJ SCE/IT/ Microprocessor and Microcontroller

Page 45: Mpmc Manual

P2 = 0x00; while(1) P1 = 0x0F; if(P1 != 0x0F) keydata = P1; Delay(delay_ms = 20); /* define a delay of 20ms */ if(P1 == keydata) GetKey(keydata); //SerCom.C #include <REG51.H> #include "SerCom.H" /* Define Prototypes */ void TimerInit(void); void Delay(unsigned int dms); /* Define variables */ extern unsigned int delay_ms; extern unsigned char digitvalue[],digitpos[]; unsigned char digitptr,delay_7seg; /* timer routine this is the heart of our program */ /* Set timer 0 for keyboard and timer 1 for baudrate generation */ void Delay(unsigned int dms) delay_ms = dms; while(delay_ms > 0) /* the do nothing loop */ void TimerInit() /* we now start the timer */ delay_7seg=0; digitptr = 0; /* start with the first digit */

KJ SCE/IT/ Microprocessor and Microcontroller

Page 46: Mpmc Manual

SCON = 0x52; /* 8-bit UART with Variable Baudrate,TI=1,RI=0 */ TMOD = 0x21; /* Timer 1 in mode 2, 8-bit auto-reload, T0 = 0X01, T1 = 0X20*/ //TH1 = 0xFD; /* Standard 8051 -- 9600 Baud @11.0592 Mhz */ TH1 = (256 - (XTAL/(12L * 16L * BAUDRATE))); //6 clock periods per machine cycle TL0 = 0xb0; /* load the timer low bits 3cb0 -> 50 mSec */ TH0 = 0x3c; /* load the timer high bits fc00 */ ET0 = 1; /* enable timer0 interupt */ EA = 1; /* enable master interupt */ TR1 = 0x01; /* Turn on Timer 1 */ /* now we start both the timers */ TCON = 0X50; /* Start T0 for keyboard and T1 for serial */ return; void SysTimer() interrupt 1 using 2 /* when timer interupts we come here. */ TR0=0; /* Stop T0 timer */ TL0 = 0x18; /* Re-Load count low 12 Mhz crystal */ //TH0 = 0xfc; /* Re-Load count high 12 Mhz crystal fc18 -> 1msec*/ TH0 = 0xfe; /* Re-Load count high 12 Mhz crystal fe0b -> 0.5msec*/ /* decrement any counters */ if(delay_ms > 0) delay_ms--; TR0 = 1; /* Restart the T0 timer */ Output:

The output was observed on relay of 8051 kit. Either NC or NO led was flashing.

KJ SCE/IT/ Microprocessor and Microcontroller

Page 47: Mpmc Manual

Experiment No:- 10 Aim: To write a program in C to interface LCD with 8051. Resources Used: KEIL, KB8051 base kit Theory: Liquid Crystal Display also called as LCD is very helpful in providing user interface as well as for debugging purpose. The LCD requires 3 control lines (RS, R/W & EN) & 8 (or 4) data lines. The number on data lines depends on the mode of operation. If operated in 8-bit mode then 8 data lines + 3 control lines i.e. total 11 lines are required. And if operated in 4-bit mode then 4 data lines + 3 control lines i.e. 7 lines are required.

Pin out:

• 8 data pins D7:D0

Bi-directional data/command pins.

Alphanumeric characters are sent in ASCII format.

• RS: Register Select

RS = 0 -> Command Register is selected

RS = 1 -> Data Register is selected

• R/W: Read or Write

0 -> Write, 1 -> Read

• E: Enable (Latch data)

Used to latch the data present on the data pins.

A high-to-low edge is needed to latch the data.

• VEE : contrast control

When writing to the display, data is transferred only on the high to low

transition of this signal. However, when reading from the display, data will

KJ SCE/IT/ Microprocessor and Microcontroller

Page 48: Mpmc Manual

become available shortly after the low to high transition and remain available

until the signal falls low again.

INTERFACING LCD TO 8051

The 44780 standard requires 3 control lines as well as either 4 or 8 I/O lines for the data bus. The user may select whether the LCD is to operate with a 4-bit data bus or an 8-bit data bus. If a 4-bit data bus is used, the LCD will require a total of 7 data lines. If an 8-bit data bus is used, the LCD will require a total of 11 data lines.

The three control lines are E, RS, and R/W.

The EN line must be raised/lowered before/after each instruction sent to the LCD regardless of whether that instruction is read or write, text or instruction. In short, you must always manipulate EN when communicating with the LCD. EN is the LCD's way of knowing that you are talking to it. If you don't raise/lower EN, the LCD doesn't know you're talking to it on the other lines.

When RS is low (0), the data is to be treated as a command. When RS is high (1), the data being sent is considered as text data which should be displayed on the screen.

When R/W is low (0), the information on the data bus is being written to the LCD. When RW is high (1), the program is effectively reading from the LCD. Most of the times there is no need to read from the LCD so this line can directly be connected to Gnd thus saving one controller line.

The ENABLE pin is used to latch the data present on the data pins. A HIGH - LOW signal is required to latch the data. The LCD interprets and executes our command at the instant the EN line is brought low.

KJ SCE/IT/ Microprocessor and Microcontroller

Page 49: Mpmc Manual

Algorithm: Step 1: Start. Step 2: Define a function for delay. Step 3: Define a function lcd_command() to enable the LCD. Step 4: Call a function lcd command. Step 5: Define a function LcdPrint () to display the text in terms of pages. Step 6: Introduce a delay of 1000msec. Step 7: Stop. Conclusion: The program to interface LCD with 8051 was developed in C by. The corresponding HEX file generated was given as an input to 8051 kit and the output was observed on 8051.

KJ SCE/IT/ Microprocessor and Microcontroller

Page 50: Mpmc Manual

Program: // Lcd.c - HLL lcd routines to print string //#include <string.h> #include "lcd.h" #include "lcd_routines.h" #include "SerCom.H" extern void Delay(unsigned int dms); extern unsigned int delay_ms; static void lcd_command(); static void lcd_data(); /*============================================== Function sends variable value as a command to LCD as MSB & LSB ================================================*/ void lcd_command() lcd_command_high(); lcd_enable(); lcd_command_low(); lcd_enable(); Delay(delay_ms=2); //delay of 2ms void lcd_data() lcddata(); Delay(delay_ms=50); /*=================================================== Intialization process ====================================================*/ void LcdInit() lcd_clear(); lcd_enable(); temp=0x28; /*N=1:2 line, F=0:5x7, DL=0:4 bit,*/ lcd_command(); /* Function 0,0,1,DL,N,F,x,x */ temp=0x10; /*Cursor or Display shifting*/

KJ SCE/IT/ Microprocessor and Microcontroller

Page 51: Mpmc Manual

lcd_command(); /* Function 0,0,0,1,S/C,R/L,0,0 */ temp=0x04; /*Set Entry Mode*/ lcd_command(); /* Function 0,0,0,0,0,1,I/D,SH */ temp=0x01; /*Clear Display */ lcd_command(); // repeat again temp=0x28; /*N=1:2 line, F=0:5x7, DL=0:4 bit,*/ lcd_command(); /* Function 0,0,1,DL,N,F,x,x */ temp=0x10; /*Cursor or Display shifting*/ lcd_command(); /* Function 0,0,0,1,S/C,R/L,0,0 */ temp=0x04; /*Set Entry Mode*/ lcd_command(); /* Function 0,0,0,0,0,1,I/D,SH */ temp=0x0C; /*Display On/Off control */ lcd_command(); /* Function 0,0,0,0,1,D,C,B */ temp=0x01; /*Clear Display*/ lcd_command(); /*==================================================== Clear the LCD display screen =====================================================*/ void LcdClear() temp = 0x01; lcd_command(); /*===================================================== Text displayed in the form of pages =======================================================*/ void LcdPrint(char *lcddata, unsigned char length) unsigned char count ,tcount = 0; char *pdata; pdata = lcddata; /* get the pointer address */ LcdClear();

KJ SCE/IT/ Microprocessor and Microcontroller

Page 52: Mpmc Manual

while(tcount < length) // we support multiple page display count = 0; temp = 0x80; //set address to first line lcd_command(); while(count < 16) if(tcount < length) temp = *pdata; lcd_data(); pdata++; else temp = 0x20; lcd_data(); count++; tcount++; temp = 0xc0; //set address to second line lcd_command(); while(count < 32) if(tcount < length) temp = *pdata; lcd_data(); pdata++; else temp = 0x20; lcd_data(); count++; tcount++; //we support now multiple pages with delay between pages Delay(delay_ms=1000); //while end

KJ SCE/IT/ Microprocessor and Microcontroller

Page 53: Mpmc Manual

KJ SCE/IT/ Microprocessor and Microcontroller

Output: The output was observed on the LCD of 8051 kit in terms of displayed text.