You are on page 1of 17

Lab Practical File ON

Embedded Systems
Using 8051 Microcontroller

SUBMITTED TO: Kavita Chabbra HAMDARD UNIVERSITY, NEW DELHI

SUBMITTED BY:Name: Saba Wasim Class: B.Tech (IT) University Roll No.- 2007-311-030

1.

Steps for running and assembling 8051 programs.

Step 1: To write the program using any editor and save it with extension .asm or .src depending on the assembler one is using.

Step 2: The source file is fed into assembler which converts it to machine code and gives us 2 files one with extension .lst i.e. the list file and other with extension .obj i.e. the object file.

Step 3: Old assemblers required a third step to link all object files to create one absolute file with extension .abs This file is used by 8051 trainers to monitor program.

Step 4: The abs file is fed into a program called OH (i.e Object to hex converter) which creates a file with extension .hex that is ready to be burned in the ROM of the controller.

The List file This is a very important file for programmers as it shows all opcodes and memory address as they will be stored in the controller. This makes error detection of labels and code quite easy. 2.Structure of the assembly language program with example. The assembly program begins execution at the reset interrupt. The reset interrupt is the first thing that occurs when power is given to the processor. By default in the Workbench files, the reset interrupt is loaded to send the execution of the program to the start of the written code. Until a branch is reached, the processor will execute each instruction in turn. If the program does not loop back to an earlier point to keep going, eventually the execution will reach the end of the valid instructions in memory and attempt to execute the "instructions" in the following memory addresses (which are invalid and possibly gibberish). The control of a programs execution is called control flow, and it is accomplished through branching, jumping, function calls, and interrupts. Interrupts are the subject of future labs. Branching and jumping refer to changing the next instruction from the next one sequentially to an instruction elsewhere in the program. #include "msp430x20x1.h" ;------------------------------------------------------------------------------ORG 0xF800 ; Begining PsuedoOP ;-------------------------------------------------------------------------------

RESET

mov.w #0x280,SP ; Set stackpointer mov.w #WDTPW+WDTHOLD,&WDTCTL ; Stop watchdog timer

;; **YOUR CODE GOES HERE**

;------------------------------------------------------------------------------; Interrupt Vectors ;------------------------------------------------------------------------------ORG 0xFFFE ; MSP430 RESET Vector DW RESET ; END 3.8051 data type and directives The 8051 microcontroller has only one data type. It is 8 bits, and the size of each register is also 8 bits. It is the job of the programmer to break down data larger than 8 bits (00 to FFH, or 0 to 255 in decimal) to be processed by the CPU. For examples of how to process data larger than 8 bits, see Chapter 6. The data types used by the 8051 can be positive or negative. A discussion of signed numbers is given in Chapter 6. DB (define byte) The DB directive is the most widely used data directive in the assembler. It is used to define the 8bit data. When DB is used to define data, the numbers can be in decimal, binary, hex, or ASCII formats.

Either single or double quotes can be used around ASCII strings. This can be useful for strings, which contain a single quote such as OLeary. DB is also used to allocate memory in byte -sized chunks. Assembler directives The following are some more widely used directives of the 8051. ORG (origin) The ORG directive is used to indicate the beginning of the address. The number that comes after ORG can be either in hex or in decimal. If the number is not followed by H, it is decimal and the assembler will convert it to hex. EQU (equate) This is used to define a constant without occupying a memory location. The EQU directive does not set aside storage for a data item but associates a constant value with a data label so that when the label appears in the program, itp constant value will be substituted for the label.

END directive Another important pseudocode is the END directive. This indicates to the assembler the end of the source (asm) file.

4. Rules for Lables. a. each label name must be unique b. first character must be alphabetic c. reserved words must not be used as labels

5. Explain the working of the Stack in 8051. The stack pointer (SP) is an 8-bit register at location 81H. A stack is used for temporarily storing data. It operates on the basis of last in first out (LIFO). Putting data onto the stack is called "pushing onto the stack" while taking data off the stack is called "popping the stack." The stack pointer contains the address of the item currently on top of the stack. On power-up or reset the SP is set to 07H. When pushing data onto the stack, the SP is first increased by one and the data is then placed in the location pointed to by the SP. When popping the stack, the data is taken off the stack and the SP is then decreased by one. Since reset initialises the SP to 07H, the first item pushed onto the stack is stored at 08H (remember, the SP is incremented first, then the item is placed on the stack). However, if the programmer wishes to use the register banks 1 to 3, which start at address 08H, he/she must move the stack to another part of memory. The general purpose RAM starting at address 30H is a good spot to place the stack. To do so we need to change the contents of the SP. MOV SP, #2FH.

6. Loop and Jump Instructions.

ORG 0H MOV A, #0 ; clear A MOV R1, #10 ; load counter R1 =10 AGAIN: ADD A, # 05 ; add five to register A DJNZ R1, AGAIN ; repeat until R1=0 (10 times) MOV R3, A ; save A in R3 END

7. Write a program to (a) Clear ACC (b) Add 3 to accumulator ten times Solution: Mov A,#0 Mov R2,#10 ADD A,#03 Djnz R2, AGAIN Mov R5,A ;a=0 clear ACC ; load counter r2=10 ; add 03 to acc ;repeat untel r2=0 (10 times ) ; save A in R5

AGAIN:

8. Write a program to (a) load the accumulator with the value 55H (b) complement the ACC 700 times Solution: ; Since 700 is larger than 255 (the maximum capacity of any register), so we will use two registers to hold account. Mov A,#55h Mov R3,#10 NEXT : Mov R2,#70 AGAIN: CPL A DJNZ R2 , AGAIN DJNZ R3 , NEXT 9. Write a program to determine if R5 contains the value 0 .if so, put 55H in it. (jz,jnz) Solution: Mov A,R5 JNZ NEXT Mov R5,#55H NEXT: ;copy R5 to A ;jump if A is not zero ; A=55 ; r3=10 ; r2=70 ;compelement A ;repeat it 70 times (inner loop)

10. Find the sum of the values 79H,F5H and E2H.put the sum in the registers R0(low byte)and R5(high byte).(jc,jnc) Solution: Mov A,#0 Mov R5,A ADD A,#79H JNC N1 INC R5 N1: ADD A,#0F5H JNC INC N2 : N2 R5 ; clear A (A=0) ; clear R5 ; A=0+79H=79H ; if no carry ,add next number ;if cy=1,increment R5 ; A=79+F5=6E ; jump if cy=0 ; if cy=1 ,then incerement R5 ; A=6E+E2=50 and CY=1 ; jump if cy=0 ; if cy=1 ,then incerement R5 ;Now R0=50h , and R5 =02 SS

ADD A,#0E2H JNC over INC R5

OVER:

mov R0,A

11.

WAP to demonstrate CALL function. ORG 0 BACK: MOV A,#55H ;load A with 55H MOV P1,A ;send 55H to port 1 LCALL DELAY ;time delay MOV A,#0AAH ;load A with AA (in hex) MOV P1,A ;send AAH to port 1 LCALL DELAY SJMP BACK ;keep doing this indefinitely ;---------- this is delay subroutine -----------ORG 300H ;put DELAY at address 300H DELAY:MOV R5,#0FFH ;R5=255 (FF in hex), counter AGAIN:DJNZ R5,AGAIN ;stay here until R5 become 0 RET END

12. Write a program to toggle all the bits of port1 by to it the values of 55H and AAH continuously. Put time delay between each issuing data to port 1. ORG 0 MOV A,#55H BACK: MOV P1,A ACALL DELAY CPL A

SJMP BACK DELAY: MOV R5,#0FFH AGAIN: DJNZ R5,AGAIN RET END

13.WAP to show call instruction and role of stack

ORG 0 BACK: MOV A,#55H ;load A with 55H MOV P1,A ;send 55H to port 1 LCALL DELAY ;time delay MOV A,#0AAH ;load A with AA (in hex) MOV P1,A ;send AAH to port 1 LCALL DELAY SJMP BACK ;keep doing this indefinitely ;---------- this is delay subroutine -----------ORG 300H ;put DELAY at address 300H DELAY:MOV R5,#0FFH ;R5=255 (FF in hex), counter AGAIN:DJNZ R5,AGAIN ;stay here until R5 become 0 RET END

14. Rime Delay Generation. MAIN: MOV R6,#2D LOOP: ACALL DELAY DJNZ R6,LOOP SJMP MAIN DELAY: MOV TMOD,#00000001B MOV TH0,#0FCH MOV TL0,#018H SETB TR0 HERE: JNB TF0,HERE CLR TR0 CLR TF0 RET

Calculate the Amount Of time delay with frequency 11.059 Mhz.

Timer depends on 1/12 XTAL., Therefore 11.059/12 =921 Khz

T=1/F = 1/921 1.85 mSec.

15. 8051 Addressing modes An "addressing mode" refers to how you are addressing a given memory location. In summary, the addressing modes are as follows, with an example of each: Immediate Addressing MOV A,#20h Direct Addressing MOV A,30h Indirect Addressing MOV A,@R0 External Direct MOVX A,@DPTR Code Indirect MOVC A,@A+DPTR Each of these addressing modes provides important flexibility. Immediate Addressing Immediate addressing is so-named because the value to be stored in memory immediately follows the operation code in memory. That is to say, the instruction itself dictates what value will be stored in memory. For example, the instruction: MOV A,#20h This instruction uses Immediate Addressing because the Accumulator will be loaded with the value that immediately follows; in this case 20 (hexidecimal). Immediate addressing is very fast since the value to be loaded is included in the instruction. However, since the value to be loaded is fixed at compile-time it is not very flexible. Direct Addressing Direct addressing is so-named because the value to be stored in memory is obtained by directly retrieving it from another memory location. For example: MOV A,30h This instruction will read the data out of Internal RAM address 30 (hexidecimal) and store it in the Accumulator. Direct addressing is generally fast since, although the value to be loaded isnt included in the instruction, it is quickly accessable since it is stored in the 8051s Internal RAM. It is also much more flexible than Immediate Addressing since the value to be loaded is whatever is found at the given address--which may be variable. Also, it is important to note that when using direct addressing any instruction which refers to an address between 00h and 7Fh is referring to Internal Memory. Any instruction which refers to an address between 80h and FFh is referring to the SFR control registers that control the 8051 microcontroller itself.

The obvious question that may arise is, "If direct addressing an address from 80h through FFh refers to SFRs, how can I access the upper 128 bytes of Internal RAM that are available on the 8052?" The answer is: You cant access them using direct addressing. As stated, if you directly refer to an address of 80h through FFh you will be referring to an SFR. However, you may access the 8052s upper 128 bytes of RAM by using the next addressing mode, "indirect addressing." Indirect Addressing Indirect addressing is a very powerful addressing mode which in many cases provides an exceptional level of flexibility. Indirect addressing is also the only way to access the extra 128 bytes of Internal RAM found on an 8052. Indirect addressing appears as follows: MOV A,@R0 This instruction causes the 8051 to analyze the value of the R0 register. The 8051 will then load the accumulator with the value from Internal RAM which is found at the address indicated by R0. For example, lets say R0 holds the value 40h and Internal RAM address 40h holds the value 67h. When the above instruction is executed the 8051 will check the value of R0. Since R0 holds 40h the 8051 will get the value out of Internal RAM address 40h (which holds 67h) and store it in the Accumulator. Thus, the Accumulator ends up holding 67h. Indirect addressing always refers to Internal RAM; it never refers to an SFR. Thus, in a prior example we mentioned that SFR 99h can be used to write a value to the serial port. Thus one may think that the following would be a valid solution to write the value 1 to the serial port: MOV R0,#99h ;Load the address of the serial port MOV @R0,#01h ;Send 01 to the serial port -- WRONG!! This is not valid. Since indirect addressing always refers to Internal RAM these two instructions would write the value 01h to Internal RAM address 99h on an 8052. On an 8051 these two instructions would produce an undefined result since the 8051 only has 128 bytes of Internal RAM. External Direct External Memory is accessed using a suite of instructions which use what I call "External Direct" addressing. I call it this because it appears to be direct addressing, but it is used to access external memory rather than internal memory. There are only two commands that use External Direct addressing mode: MOVX A,@DPTR MOVX @DPTR,A As you can see, both commands utilize DPTR. In these instructions, DPTR must first be loaded with the address of external memory that you wish to read or write. Once DPTR holds the correct external memory

address, the first command will move the contents of that external memory address into the Accumulator. The second command will do the opposite: it will allow you to write the value of the Accumulator to the external memory address pointed to by DPTR. External Indirect External memory can also be accessed using a form of indirect addressing which I call External Indirect addressing. This form of addressing is usually only used in relatively small projects that have a very small amount of external RAM. An example of this addressing mode is: MOVX @R0,A Once again, the value of R0 is first read and the value of the Accumulator is written to that address in External RAM. Since the value of @R0 can only be 00h through FFh the project would effectively be limited to 256 bytes of External RAM. There are relatively simple hardware/software tricks that can be implemented to access more than 256 bytes of memory using External Indirect addressing; however, it is usually easier to use External Direct addressing if your project has more than 256 bytes of External RAM.

16. Arithmetic instructions a. MOV A, #0F5h ADD A, #0Bh ; A = F5h ; A = F5 + 0B = 00

b. MOV

A, #47h

; A=47h first BCD operand

MOV B, #25h ADD A, B DA A

; B=25h second BCD operand ; hex (binary) addition (A=6Ch)

; adjust for BCD addition (A=72h)

C. MOV A, #0F5h SUBB A, #0Bh d. ORG 4100 CLR C MOV A,#data1 MOV B,#data2 MUL AB

; A = F5h ; A = F5 + 0B = 00

MOV DPTR,#4500 MOVX @DPTR,A INC DPTR MOV A,B MOVX @DPTR,A HERE: SJMP HERE e. ORG 0000H MOV DPTR, #0030H MOVX A, @DPTR MOV B, A INC DPTR MOVX A, @DPTR DIV AB MOV DPTR, #0090H MOVX @DPTR, A MOV PCON, #02H

f. CLR C ;make CY=0 MOV A, #0E7H ;load the low byte now A=E7H ADD A, #8DH ;add the low byte MOV R6, A ;save the low byte sum in R6 MOV A, #3CH ;load the high byte ADDC A, #3BH ;add with the carry MOV R7, A ;save the high byte sum 17. a) AND LOGIC

MOV A,#35H ;A = 35H ANL A,#0FH ;A = A AND 0FH b)OR MOV A,#04H ;A = 04 ORL A,#68H ;A = 6C

c)XOR MOV A,#54H XRL A,#78H

d. Compare CJNE R5,#80,NOT_EQUAL ;check R5 for 80 ... ;R5 = 80 NOT_EQUAL: JNC NEXT ;jump if R5 > 80 ... ;R5 < 80 NEXT: ...

e. Compliment CPL A ;1s complement (invert) ADD A,#1 ;add 1 to make 2s comp.

You might also like