0% found this document useful (0 votes)
62 views48 pages

SMTH 1

The document discusses assembly language programming for 8051 microcontrollers. It covers topics like assembler directives, examples of assembly language programs involving operations like addition, subtraction, and data transfer, and the use of indirect addressing modes.

Uploaded by

Divij DV
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views48 pages

SMTH 1

The document discusses assembly language programming for 8051 microcontrollers. It covers topics like assembler directives, examples of assembly language programs involving operations like addition, subtraction, and data transfer, and the use of indirect addressing modes.

Uploaded by

Divij DV
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 48

MICROPROCESSOR AND MICROCONTROLLER

Mr. Anand H. D.

Department of Electronics & Communication Engineering


Dr. Ambedkar Institute of Technology
Bengaluru-56
MICROPROCESSOR AND MICROCONTROLLER: 8051 PROGRAMMING
Prepared by Prof. Anand H. D., Dept. of ECE, Dr. AIT, Bengaluru-56 1
Topics to be covered
Assembler directives

Assembly language programs and Time delay calculations

Stack operations

Introduction to Embedded C

C data types

Logical operations

Programming 8051 using embedded C

MICROPROCESSOR AND MICROCONTROLLER: 8051 PROGRAMMING


Prepared by Prof. Anand H. D., Dept. of ECE, Dr. AIT, Bengaluru-56 2
Assembler directives
➢ Assembler directives tell the assembler to do something other than creating the machine code for an instruction.
➢ In assembly language programming, the assembler directives instruct the assembler to
1. Process subsequent assembly language instructions
2. Define program constants
3. Reserve space for variables

The following are the widely used 8051 assembler directives.


ORG (origin)
➢ The ORG directive is used to indicate the starting address.
➢ It can be used only when the program counter needs to be changed.
➢ The number that comes after ORG can be either in hex or in decimal.
➢ Eg: ORG 0000H ;Set PC to 0000.

EQU and SET


➢ EQU and SET directives assign numerical value or register name to the specified symbol name.
➢ EQU is used to define a constant without storing information in the memory.
➢ The symbol defined with EQU should not be redefined.
➢ SET directive allows redefinition of symbols at a later stage.
MICROPROCESSOR AND MICROCONTROLLER: 8051 PROGRAMMING
Prepared by Prof. Anand H. D., Dept. of ECE, Dr. AIT, Bengaluru-56 3
Assembler directives
DB (DEFINE BYTE)
➢ The DB directive is used to define an 8 bit data. DB directive initializes memory with 8 bit values.
➢ The numbers can be in decimal, binary, hex or in ASCII formats.
➢ For decimal, the ‘D’ after the decimal number is optional, but for binary and hexadecimal, 'B' and ‘H’ are
required.
➢ For ASCII, the number is written in quotation marks (‘LIKE This).
DATA1: DB 40H ;hex
DATA2: DB 01011100B ;b i n a r y
DATA3: DB 48 ;decimal
DATA4: D B ' H E L L O W ’ ;ASCII

END
➢ The END directive signals the end of the assembly module.
➢ It indicates the end of the program to the assembler. Any text in the assembly file that appears after the END
directive is ignored.
➢ If the END statement is missing, the assembler will generate an error message.

MICROPROCESSOR AND MICROCONTROLLER: 8051 PROGRAMMING


Prepared by Prof. Anand H. D., Dept. of ECE, Dr. AIT, Bengaluru-56 4
Assembly language programs

1. Write a program to add the values of locations 50H and 51H and store the result
in locations in 52h and 53H.

ORG 0000H ; Set program counter 0000H


MOV A,50H ; Load the contents of Memory location 50H into A
ADD A,51H ; Add the contents of memory 51H with CONTENTS A
MOV 52H,A ; Save the LS byte of the result in 52H
MOV A, #00 ; Load 00H into A
ADDC A, #00 ; Add the immediate data and carry to A
MOV 53H,A ; Save the MS byte of the result in location 53h
END

MICROPROCESSOR AND MICROCONTROLLER: 8051 PROGRAMMING


Prepared by Prof. Anand H. D., Dept. of ECE, Dr. AIT, Bengaluru-56 5
Assembly language programs

2. Write a program to store data FFH into RAM memory locations 50H to 58H
using direct addressing mode

ORG 0000H ; Set program counter 0000H


MOV A, #0FFH ; Load FFH into A
MOV 50H, A ; Store contents of A in location 58H
MOV 51H, A ; Store contents of A in location 58H
MOV 52H, A ; Store contents of A in location 58H
MOV 53H, A ; Store contents of A in location 58H
MOV 54H, A ; Store contents of A in location 58H
MOV 55H, A ; Store contents of A in location 58H
MOV 56H, A ; Store contents of A in location 58H
MOV 57H, A ; Store contents of A in location 58H
MOV 58H, A ; Store contents of A in location 58H
END

MICROPROCESSOR AND MICROCONTROLLER: 8051 PROGRAMMING


Prepared by Prof. Anand H. D., Dept. of ECE, Dr. AIT, Bengaluru-56 6
Assembly language programs

3. Write a program to subtract a 16 bit number stored at locations 51H-52H from


55H-56H and store the result in locations 40H and 41H. Assume that the least
significant byte of data or the result is stored in low address. If the result is
positive, then store 00H, else store 01H in 42H.

ORG 0000H ; Set program counter 0000H


MOV A, 55H ; Load the contents of memory location 55 into A
CLR C ; Clear the borrow flag
SUBB A,51H ; Sub the contents of memory 51H from contents of A
MOV 40H, A ; Save the LSByte of the result in location 40H
MOV A, 56H ; Load the contents of memory location 56H into A
SUBB A, 52H ; Subtract the content of memory 52H from the content A
MOV 41H, A ; Save the MSbyte of the result in location 415.
MOV A, #00 ; Load 005 into A
ADDC A, #00 ; Add the immediate data and the carry flag to A
MOV 42H, A ; If result is positive, store00H, else store 0lH in 42H
END

MICROPROCESSOR AND MICROCONTROLLER: 8051 PROGRAMMING


Prepared by Prof. Anand H. D., Dept. of ECE, Dr. AIT, Bengaluru-56 7
Assembly language programs

4. Write a program to add two 16 bit numbers stored at locations 51H-52H and
55H-56H and store the result in locations 40H, 41H and 42H. Assume that the
least significant byte of data and the result is stored in low address and the most
significant byte of data or the result is stored in high address

ORG 0000H ; Set program counter 0000H


MOV A,51H ; Load the contents of memory location 51H into A
ADD A,55H ; Add the contents of 55H with contents of A
MOV 40H,A ; Save the LS byte of the result in location 40H
MOV A,52H ; Load the contents of 52H into A
ADDC A,56H ; Add the contents of 56H and CY flag with A
MOV 41H,A ; Save the second byte of the result in 41H
MOV A,#00 ; Load 00H into A
ADDC A,#00 ; Add the immediate data 00H and CY to A
MOV 42H,A ; Save the MS byte of the result in location 42H
END

MICROPROCESSOR AND MICROCONTROLLER: 8051 PROGRAMMING


Prepared by Prof. Anand H. D., Dept. of ECE, Dr. AIT, Bengaluru-56 8
Assembly language programs

5. Write a program to store data FFH into RAM memory locations 50H to 58H
using indirect addressing mode

ORG 0000H ; Set program counter 0000H


MOV A, #0FFH ; Load FFH into A
MOV RO, #50H ; Load pointer, R0-50H
MOV R5, #08H ; Load counter, R5-08H
Start:MOV @RO, A ; Copy contents of A to RAM pointed by R0
INC RO ; Increment pointer
DJNZ R5, start ; Repeat until R5 is zero
END

MICROPROCESSOR AND MICROCONTROLLER: 8051 PROGRAMMING


Prepared by Prof. Anand H. D., Dept. of ECE, Dr. AIT, Bengaluru-56 9
Assembly language programs

6. Write a program to add two Binary Coded Decimal (BCD) numbers stored at
locations 60H and 61H and store the result in BCD at memory locations 52H and
53H. Assume that the least significant byte of the result is stored in low address.

ORG 0000H ; Set program counter 00004


MOV A,60H ; Load the contents of memory location 6.0.H into A
ADD A,61H ; Add the contents of memory location 61H with contents of A
DA A ; Decimal adjustment of the sum in A
MOV 52H, A ; Save the least significant byte of the result in location 52H
MOV A,#00 ; Load 00H into .A
ADDC A,#00H ; Add the immediate data and the contents of carry flag to A
MOV 53H,A ; Save the most significant byte of the result in location 53:,
END

MICROPROCESSOR AND MICROCONTROLLER: 8051 PROGRAMMING


Prepared by Prof. Anand H. D., Dept. of ECE, Dr. AIT, Bengaluru-56 10
Assembly language programs
7. Write a program to clear 10 RAM locations starting at RAM address 1000H.

ORG 0000H ;Set program counter 0000H


MOV DPTR, #1000H ;Copy address 1000H to DPTR
CLR A ;Clear A
MOV R6, #0AH ;Load 0AH to R6
again: MOVX @DPTR,A ;Clear RAM location pointed by DPTR
INC DPTR ;Increment DPTR
DJNZ R6, again ;Loop until counter R6=0
END

MICROPROCESSOR AND MICROCONTROLLER: 8051 PROGRAMMING


Prepared by Prof. Anand H. D., Dept. of ECE, Dr. AIT, Bengaluru-56 11
Assembly language programs
8. Write a program to compute 1 + 2 + 3 + N (say N=15) and save the sum at 70H.

ORG 0000H ; Set program counter 0000H


N EQU 15
MOV R0,#00 ; Clear R0
CLR A ; Clear A
again: INC R0 ; Increment R0
ADD A, R0 ; Add the contents of R0 with A
CJNE R0,#N,again ; Loop until counter, R0, N
MOV 70H,A ; Save the result in location 70H
END

MICROPROCESSOR AND MICROCONTROLLER: 8051 PROGRAMMING


Prepared by Prof. Anand H. D., Dept. of ECE, Dr. AIT, Bengaluru-56 12
Assembly language programs

9. Write a program to multiply two 8 bit numbers stored at locations 70H and 71H
and store the result at memory locations 52H and 53H. Assume that the least
significant byte of the result is stored in low address

ORG 0000H
MOV A, 70H
MOV B, 71H
MUL AB
MOV 52H,A
MOV 53H,B
END

MICROPROCESSOR AND MICROCONTROLLER: 8051 PROGRAMMING


Prepared by Prof. Anand H. D., Dept. of ECE, Dr. AIT, Bengaluru-56 13
Assembly language programs

10. Ten 8 bit numbers are stored in internal data memory from location 50H.
Write a program to increment the data.

ORG 0000H
MOV R0,#50H
MOV R3,#0AH
Loopl: INC @R0
INC RO
DJNZ R3, loopl
END

MICROPROCESSOR AND MICROCONTROLLER: 8051 PROGRAMMING


Prepared by Prof. Anand H. D., Dept. of ECE, Dr. AIT, Bengaluru-56 14
Assembly language programs

11. Write a program to find the average of five 8 bit numbers. Store the result in
55H. (Assume that after adding five 8 bit numbers, the result is 8 bit only).
ORG 0000H
MOV 40H,#05H
MOV 41H,#55H
MOV 42H,#06H
MOV 43H,#1AH
MOV 44H,#09H
MOV R0,#40H
MOV R5,#05H
MOV B,R5
CLR A
Loop: ADD A,@RO
INC RO
DJNZ R5,Loop
DIV AB
MOV 55H,A
END

MICROPROCESSOR AND MICROCONTROLLER: 8051 PROGRAMMING


Prepared by Prof. Anand H. D., Dept. of ECE, Dr. AIT, Bengaluru-56 15
Assembly language programs
12. Write a program to find the cube of an 8 bit number
ORG 0000H
MOV R1,#N
MOV A,R1
MOV B,R1
MUL AB //SQUARE IS COMPUTED
MOV R2, B
MOV B, R1
MUL AB
MOV 50H,A
MOV 51H,B
MOV A,R2
MOV B, R1
MUL AB
ADD A, 51H
MOV 51H, A
MOV 52H, B
MOV A, # 00H
ADDC A, 52H
MOV 52H, A //CUBE IS STORED IN 52H,51H,50H
END

MICROPROCESSOR AND MICROCONTROLLER: 8051 PROGRAMMING


Prepared by Prof. Anand H. D., Dept. of ECE, Dr. AIT, Bengaluru-56 16
Assembly language programs

13. Write a program to exchange the lower nibble of data present in external
memory 6000H and 6001H.
ORG 0000H
MOV DPTR, #6000H
MOVX A, @DPTR
MOV R0, #45H
MOV @RO, A
INC DPL
MOVX A, @DPTR
XCHD A, @R0
MOVX @DPTR, A
DEC DPL
MOV A, @R0
MOVX @DPTR, A
END

MICROPROCESSOR AND MICROCONTROLLER: 8051 PROGRAMMING


Prepared by Prof. Anand H. D., Dept. of ECE, Dr. AIT, Bengaluru-56 17
Assembly language programs

14. Write a program to count the number of and o's of 8 bit data stored in location
6000H..
ORG 0000H
MOV DPTR, #6000h
MOVX A, @DPTR
MOV R0,#08
MOV R2,#00
MOV R3,#00
CLR C
BACK: RLC A
JC ONE
INC R2
AJMP NEXT
ONE: INC R3
NEXT: DJNZ RO,BACK
END

MICROPROCESSOR AND MICROCONTROLLER: 8051 PROGRAMMING


Prepared by Prof. Anand H. D., Dept. of ECE, Dr. AIT, Bengaluru-56 18
Assembly language programs

15. Write a program to shift a 24 bit number stored at 57H-55H to the left logically
four places. Assume that the least significant byte of data is stored in lower
address. .

ORG 0000H
MOV R1,#04
again: MOV A,55H
CLR C
RLC A
MOV 55H,A
MOV A,56H
RLC A
MOV 56H,A
MOV A,57H
RLC A
MOV 57H,A
DJNZ R1,again
END

MICROPROCESSOR AND MICROCONTROLLER: 8051 PROGRAMMING


Prepared by Prof. Anand H. D., Dept. of ECE, Dr. AIT, Bengaluru-56 19
Time delay calculations
The following shows crystal frequency for three different 8051-based systems. Find the period of
the machine cycle in each case.
(a) 11.0592 MHz (b) 16 MHz (c) 20 MHz

Solution:
(a) 11.0592 MHz/12
= 921.6 kHz;
machine cycle is 1/921.6 kHz = 1.085 µs (microsecond)

(b) 16 MHz/12
= 1.333 MHz;
machine cycle (MC) =1/1.333 MHz = 0.75 µs

(c) 20 MHz/12
= 1.66 MHz;
MC = 1/1.66 MHz = 0.60 µs

MICROPROCESSOR AND MICROCONTROLLER: 8051 PROGRAMMING


Prepared by Prof. Anand H. D., Dept. of ECE, Dr. AIT, Bengaluru-56 20
Time delay calculations
For an 8051 system of 11.0592 MHz, find how long it takes to execute each of the following instructions.
(a) MOV R3, #55 (b) DEC R3 (c) DJNZ R2, target
(d) LJMP (e) SJMP (f) NOP (no operation)
(g) MUL AB
Solution:
The machine cycle for a system of 11.0592 MHz is 1.085 µs. Therefore, we have:

Instruction Machine cycles Time to execute

(a) MOV R3, #55 1 1x1.085 µs = 1. 085 µs


(b) DEC R3 1 1x1.085 µs = 1. 085 µs
(c) DJNZ R2, target 2 2x1.085 µs = 2.17 µs
(d) LJMP 2 2x1.085 µs = 2.17 µs
(e) SJMP 2 2x1.085 µs = 2.17 µs
(f) NOP 1 1x1.085 µs = 1. 085 µs
(g) MUL AB 4 4x1.085 µs = 4.34 µs

MICROPROCESSOR AND MICROCONTROLLER: 8051 PROGRAMMING


Prepared by Prof. Anand H. D., Dept. of ECE, Dr. AIT, Bengaluru-56 21
Time delay calculations
Find the size of the delay in the following program, if the crystal frequency is 11.0592 MHz.
MOV A, #55H ; load A with 55H
AGAIN: MOV P1,A ;issue value in reg A to port 1
ACALL DELAY ;time delay
CPL A ;Complement reg A
SJMP AGAIN ;keep doing this indefinitely
-----Time delay
Instruction Machine cycles
DELAY: MOV R3, #200
MOV R3, #200 1
HERE: DJNZ R3,HERE
RET DJNZ R3,HERE 2
RET 2
Solution: Therefore, we have a time delay of

= [(200x 2) +1 +2] x 1.085 µs

= 436.255 µs

MICROPROCESSOR AND MICROCONTROLLER: 8051 PROGRAMMING


Prepared by Prof. Anand H. D., Dept. of ECE, Dr. AIT, Bengaluru-56 22
Time delay calculations

For an 8051 system of 11.0592 MHz, find the time delay for the following subroutine:
Machine cycles
If machine cycle timing is critical to your
DELAY: MOV R3, #250 1
system design, make sure that you check
HERE: NOP 1
NOP 1 the manufacture's data sheets for the
NOP 1
NOP device specification.
1
DJNZ R3, HERE 2 For example, the DS89C420 has 3

RET 2 machine cycles instead of 2 machine

cycles for the RET instruction.


Solution: The time delay inside the HERE loop is

[250(1+1+1+1+ 2)] x 1.085 µs = 1500 x 1.085 µs = 1627.5 µs.

`Adding the two instructions outside the loop we have

1627.5 µs + 3 x 1.085 µs = 1630.755 µs.

MICROPROCESSOR AND MICROCONTROLLER: 8051 PROGRAMMING


Prepared by Prof. Anand H. D., Dept. of ECE, Dr. AIT, Bengaluru-56 23
Time delay calculations
For an 89C51 with a crystal frequency of 22 MHz, generate a delay of 5 ms.

Solution: Machine cycles


However there is an extra delay corresponding
DELAY: MOV R2, #19 1
HERE: MOV R3, #255 to the instructions
1
AGAIN:DJNZ R3, AGAIN 2 MOV R3, #255 and DJNZ R2,HERE,
DJNZ R2, HERE 2
which gets repeated 19 times equaling a delay
RET 2
of 19 x 3 x0.546 = 31.12 µs, which can safely

be neglected.
For a crystal frequency of 22 MHz, one machine cycle will be 0.546 µs.
However, we should remember that in delay
The AGAIN loop takes (2 x 255 ) x 0.546 = 278 µs.
loops, the time is only approximate since we
The HERE loop repeats the AGAIN loop 19 times.
have ignored the first and last instructions in
Hence the delay is 278 x 19 = 5.29ms 5 ms
the subroutine.

MICROPROCESSOR AND MICROCONTROLLER: 8051 PROGRAMMING


Prepared by Prof. Anand H. D., Dept. of ECE, Dr. AIT, Bengaluru-56 24
Time delay calculations

Exercise:
1.Find the system frequency of an 89C51 if the machine cycle period is 0.546 µs.
2. Find the machine cycle if the crystal frequency is 18 MHz.
3. Find the machine cycle if the crystal frequency is 12 MHz.
4. Find the machine cycle if the crystal frequency is 25 MHz.
5. True or false. LJMP and SJMP instructions take the same amount of time to execute even though one is
a 3-byte instruction and the other is a 2-byte instruction.
6. Find the delay with the following program for the system specified in Problem 1.
DELAY: MOV R2, #100
HERE: MOV R3, #255
AGAIN: DJNZ R3, AGAIN
DJNZ R2, HERE
RET

MICROPROCESSOR AND MICROCONTROLLER: 8051 PROGRAMMING


Prepared by Prof. Anand H. D., Dept. of ECE, Dr. AIT, Bengaluru-56 25
Time delay calculations

7. Find the time delay for the following delay subroutine, if the system has an 8051 with frequency of
11.0592 MHz.
DELAY: MOV R3, #200
HERE: NOP
NOP
NOP
DJNZ R3, HERE
RET

8. Find the time delay generated by the following routine if the XTAL =22 MHz.
HERE: MOV R0,#200
AGAIN: DJNZ RO, AGAIN
RET

MICROPROCESSOR AND MICROCONTROLLER: 8051 PROGRAMMING


Prepared by Prof. Anand H. D., Dept. of ECE, Dr. AIT, Bengaluru-56 26
Embedded C

Why Program the 8051 in C?

➢ Compilers produce hex files that we download into the ROM of the microcontroller.
➢ The size of the hex file produced by the compiler is one of the main concerns of microcontroller programmers,
for two reasons:
• Microcontrollers have limited on-chip ROM.
• The code space for the 8051 is limited to 64K bytes.
➢ How does the choice of programming language affect the compiled program size?
• While Assembly language produces a hex file that is much smaller than C, programming in Assembly
language is tedious and time consuming.
• C programming, on the other hand, is less time consuming and much easier to write, but the hex file size
produced is much larger than if we used Assembly language.
➢ The following are some of the major reasons for Writing programs C instead of Assembly:
1. It is easier and less time consuming to write in C than Assembly.
2. C is easier to modify and update.
3. You can use code available in function libraries.
4. C code is portable to other microcontrollers with little or no modification.

MICROPROCESSOR AND MICROCONTROLLER: 8051 PROGRAMMING


Prepared by Prof. Anand H. D., Dept. of ECE, Dr. AIT, Bengaluru-56 27
DATA TYPES AND TIME DELAY IN 8051 C

C data types for the 8051


➢ Since one of the goals of 8051 C programmers is to create smaller hex files, it is worthwhile to
re-examine C data types for 8051 C.
➢ In other words, a good understanding of C data types for the 8051 can help programmers to create smaller hex
files. In this section we focus on the specific C data types that are most useful and widely used for the 8051
microcontroller.

Unsigned char
➢ Since the 8051 is an 8-bit microcontroller, the character data type is the most natural choice for many
applications.
➢ The unsigned char is an 8-bit data type that takes a value in the range of 0 - 255 (00 - FFH).
➢ It is one of the most widely used data types for the 8051. In many situations, such as setting a counter value,
where there is no need for signed data we should use the unsigned char instead of the signed char.
➢ Remember that C compilers use the signed char as the default if we do not put the keyword unsigned in
front of the char.
➢ We can also use the unsigned char data type for a string of ASCII characters, including extended ASCII
characters.

MICROPROCESSOR AND MICROCONTROLLER: 8051 PROGRAMMING


Prepared by Prof. Anand H. D., Dept. of ECE, Dr. AIT, Bengaluru-56 28
DATA TYPES AND TIME DELAY IN 8051 C
➢ In declaring variables, we must pay careful attention to the size of the data and try to use unsigned char
instead of int if possible.
➢ Because the 8051 has a limited number of registers and data RAM locations, using the int in place of the
char data type can lead to a larger size hex file.
➢ Such a misuse of the data types in compilers such as Microsoft Visual C++ for x86 IBM PCs is not a
significant issue.

Example 1
Write an 8051 C program to send values 00-FF to port P1.
Solution:
#include <reg51.h>
void main (void)
{
unsigned char z;
Run the above program on your simulator
for (z=0; z <= 255; z++)
P1= z ;
to see how P1 displays values 00- FFH in
} binary

MICROPROCESSOR AND MICROCONTROLLER: 8051 PROGRAMMING


Prepared by Prof. Anand H. D., Dept. of ECE, Dr. AIT, Bengaluru-56 29
DATA TYPES AND TIME DELAY IN 8051 C
Example 2
Write an 8051 C program to send hex values for ASCII characters of 0, 1, 2, 3, 4, 5, A, B, C, and D
to port PL.
Solution:
#include <reg51.h>
void main (void)
{
unsigned char mynum[] = "012345ABCD";
unsigned char z;
for (z=0;z<=10; z++)
P1=mymum[z] ; Run the above program on your

} simulator to see how P1 displays values


30H, 31H, 32H, 33H, 34H, 35H, 41H,
42H, 43H, and 44H, the hex values for
ASCII 0, 1, 2, and so on.

MICROPROCESSOR AND MICROCONTROLLER: 8051 PROGRAMMING


Prepared by Prof. Anand H. D., Dept. of ECE, Dr. AIT, Bengaluru-56 30
DATA TYPES AND TIME DELAY IN 8051 C
Example 3
Write an 8051 C program to toggle all the bits of P1 continuously.
Solution:
// Toggle P1 forever
#include <reg51.h>
void main (void)
{
for (;;) //repeat forever
P1 0x55; //0x indicates the data is in hex (binary)
P1-0xAA;
} Run the above program on your
simulator to see how Pl toggles
continuously.
Examine the asm code generated by
the C compiler.

MICROPROCESSOR AND MICROCONTROLLER: 8051 PROGRAMMING


Prepared by Prof. Anand H. D., Dept. of ECE, Dr. AIT, Bengaluru-56 31
DATA TYPES AND TIME DELAY IN 8051 C
Signed char
➢ The signed char is an 8-bit data type that uses the most significant bit (D7 of D7- DO) to represent the - or +
value.
➢ As a result, we have only 7 bits for the magnitude of the signed number, giving us values from -128 to +127.
➢ In situations where + and -are needed to represent a given quantity such as temperature, the use of the
signed char data type is a must.
➢ Again notice that if we do not use the keyword unsigned, the default is the signed value. For that reason we
should stick with the unsigned char unless the data needs to be represented as signed numbers.

Unsigned int
➢ The unsigned int is a 16-bit data type that takes a value in the range of 0 to 65535 (0000 - FFFFH).
➢ In the 8051 unsigned int is used to define 16-bit variables such as memory addresses.
➢ It is also used to set counter values of more than 256. Since the 8051 is an 8-bit microcontroller and the int
data type takes two bytes of RAM,
➢ We must not use the int data type unless we have to. Since registers and memory accesses are in 8-bit chunks,
the misuse of int variables will result in a larger hex file. Such misuse is not a big deal in PCs with 256
megabytes of memory, 32-bit Pentium registers and memory accesses, and a bus speed of 133 MHz.

MICROPROCESSOR AND MICROCONTROLLER: 8051 PROGRAMMING


Prepared by Prof. Anand H. D., Dept. of ECE, Dr. AIT, Bengaluru-56 32
DATA TYPES AND TIME DELAY IN 8051 C
➢ However, for 8051 programming do not use unsigned int in places where unsigned char will do the job.
➢ Of course the compiler will not generate an error for this misuse, but the overhead in hex file size is
noticeable.
➢ Also in situations where there is no need for signed data (such as setting counter values), we should use
unsigned int instead of signed int.
➢ This gives a much wider range for data declaration. Again, remember that the C compiler uses signed int as
the default if we do not use the keyword unsigned.
Signed int
➢ Signed int is a 16-bit data type that uses the most significant bit (D15 of D15 - DO) to represent the - or +
value.
➢ As a result, we have only 15 bits for the magnitude of the number, or values from-32,768 to +32,767.

Sbit (single bit)


➢ The sbit keyword is a widely used 8051 C data type designed specifically to access single-bit addressable
registers.
➢ It allows access to the single bits of the SFR registers, some of the SFRs are bit-addressable.
➢ Among the SFRs that are widely used and are also bit-addressable are ports P0 - P3. We can use sbit to access
the individual bits of the ports.
MICROPROCESSOR AND MICROCONTROLLER: 8051 PROGRAMMING
Prepared by Prof. Anand H. D., Dept. of ECE, Dr. AIT, Bengaluru-56 33
DATA TYPES AND TIME DELAY IN 8051 C
Example 4
Write an 8051 C program to send values of -4 to +4 to port Pl.
Solution:
//sign numbercs
#include <reg51.h>
void main (void)
{
char mynum [] = {+1,-1,+2,-2, +3,-3, +4,-4};
unsigned char z;
for (z=0;z<=8;z++) Run the above program on your
simulator to see how P1 displays values
P1=mynum [zl
of 1, FFH, 2, FEH, 3, FDH, 4, and
}
FCH, the hex values for +1,-1, +2,-2,
and so on.

MICROPROCESSOR AND MICROCONTROLLER: 8051 PROGRAMMING


Prepared by Prof. Anand H. D., Dept. of ECE, Dr. AIT, Bengaluru-56 34
DATA TYPES AND TIME DELAY IN 8051 C
Example 5
Write an 8051 C program to toggle bit DO of the port P1 (P1.0) 50,000 times.
Solution:
#include <reg51.h>
sbit MYBIT = P1^0; //notice that sbit is declared outside of main
void main(void)
{
unsigned int z;
for (z=0; Z<=50000; Z++)
{
MYBIT = 0; Run the above program on your

MYBIT = 1; simulator to see how P1.0 toggles

} continuously.

MICROPROCESSOR AND MICROCONTROLLER: 8051 PROGRAMMING


Prepared by Prof. Anand H. D., Dept. of ECE, Dr. AIT, Bengaluru-56 35
DATA TYPES AND TIME DELAY IN 8051 C
Bit and sfr
➢ The bit data type allows access to single bits of bit-addressable memory spaces 20 - 2FH.
➢ Notice that while the sbit data type is used for bit-addressable SFRs, the bit data type is used for
the bit-addressable section of RAM space 20 - 2FH.
➢ To access the byte-size SFR registers, we use the sfr data type.
➢ We will see the use of sbit, bit, and sfr data types in Table.

Data Type Size in Bits Data range/usage


unsigned char 8-bit 0-255
signed char 8-bit -128 to +128
unsigned int 16-bit 0 to 65535
signed int 16-bit -32768 to +32767
sbit 1-bit SFR bit addressable only
bit 1-bit RAM bit addressable only
sfr 8-bit RAM addresses 80-FFH only

MICROPROCESSOR AND MICROCONTROLLER: 8051 PROGRAMMING


Prepared by Prof. Anand H. D., Dept. of ECE, Dr. AIT, Bengaluru-56 36
TIME DELAY IN 8051 C
There are two ways to create a time delay in 8051 C:
1. Using a simple for loop
2. Using the 8051 timers
In either case, when we write a time delay we must use the oscilloscope to measure the duration of our
time delay.
In creating a time delay using a for loop, we must be mindful of three factors that can affect the
accuracy of the delay.
1. The 8051 design: Since the original 8051 was designed in 1980, both the fields of IC technology
and microprocessor architectural design have seen great advancements.
• As we saw, the number of machine cycles and the number of clock periods per machine cycle vary
among different versions of the 8051/52 microcontroller.
• While the original 8051/52 design used 12 clock periods per machine cycle, many of the newer
generations of the 8051 use fewer clocks per machine cycle.
• For example, the DS5000 uses 4 clock periods per machine cycle, while the DS89C420 uses only
one clock per machine cycle.
MICROPROCESSOR AND MICROCONTROLLER: 8051 PROGRAMMING
Prepared by Prof. Anand H. D., Dept. of ECE, Dr. AIT, Bengaluru-56 37
TIME DELAY IN 8051 C
2. The crystal frequency connected to the X1 - X2 input pins: The duration of the clock period for
the machine cycle is a function of this crystal frequency.

3. Compiler choice: The third factor that affects the time delay is the compiler used to compile the
program.
• When we program in Assembly language, we can control the exact instructions and their sequences
used in the delay subroutine.
• In the case of C programs, it is the C compiler that converts the C statements and functions to
Assembly language instructions. As a result, different compilers produce different code.
• In other words, if we compile a given 8051 C programs with different compilers, each compiler
produces different hex code.

For the above reasons, when we write time delays for C, we must use the oscilloscope to measure the
exact duration.

MICROPROCESSOR AND MICROCONTROLLER: 8051 PROGRAMMING


Prepared by Prof. Anand H. D., Dept. of ECE, Dr. AIT, Bengaluru-56 38
DATA TYPES AND TIME DELAY IN 8051 C
Example 6
Write an 8051 C program to toggle bits of P1 continuously forever with some delay.
Solution:
// Toggle Pi forever with some delay in between "on" and "off"
#include <reg51.h>
void main(void)
{
unsigned int x;
for (;;) //repeat forever
{
P1=0x55;
for (x=0; x<40000; x++); //delay size unknown
P1=0xAA;
for (x=0; x<40000; x++);
}
}

MICROPROCESSOR AND MICROCONTROLLER: 8051 PROGRAMMING


Prepared by Prof. Anand H. D., Dept. of ECE, Dr. AIT, Bengaluru-56 39
DATA TYPES AND TIME DELAY IN 8051 C
Example 7
Write an 8051 C program to toggle the bits of P1 ports continuously with a 250 ms delay.
Solution: The program below is tested for the DS89C420 with XTAL = 11.0592 MHz.
#include <reg51.h>
void MSDelay (unsigned int);
void main(void)
{
while (1) //repeat forever
{
P1=0x55;
MSDelay(250);
P1=0xAA; void MSDelay (unsigned int itime)
MSDelay (250); unsigned int i, j;
} {
} for(i=0;i<itime; i++)
for(j=0; j<1275; j++);
}

MICROPROCESSOR AND MICROCONTROLLER: 8051 PROGRAMMING


Prepared by Prof. Anand H. D., Dept. of ECE, Dr. AIT, Bengaluru-56 40
DATA TYPES AND TIME DELAY IN 8051 C
Example 8
Write a 8051 C program to toggle all the bits of P0 and P2 continuously with a 250 ms delay.
Solution: This program is tested for the DS89C420 with XTAL = 11.0592 MHz
#include <reg51.h>
void MSDelay (unsigned int);
void main(void)
{
while (1) //another way to do it forever
{
P0=0x55;
P2=0x55;
MSDelay(250); void MSDelay (unsigned int itime)
P0=0XAA; {
P2=0XAA; unsigned int i, j;
MSDelay(250); for(i=0;i<itime; i++)
} for(j=0; j<1275; j++);
} }

MICROPROCESSOR AND MICROCONTROLLER: 8051 PROGRAMMING


Prepared by Prof. Anand H. D., Dept. of ECE, Dr. AIT, Bengaluru-56 41
LOGIC OPERATIONS IN 8051 C
➢ One of the most important and powerful features of the C language is its ability to perform bit manipulation.
Because many books on C do not cover this important topic, it is appropriate to discuss it in this section.
➢ This section describes the action of bit-wise logic operators and provides some examples of how they are used.
Bit-wise operators in C
➢ While every C programmer is familiar with the logical operators AND (&&), OR (| |), and NOT (),
many C programmers are less familiar with the bitwise operators AND (&), OR (), EX-OR (^), Inverter (-),
Shift Right (>>), and Shift Left (<<).
➢ These bit-wise operators are widely used in software engineering for embedded systems and control;
consequently, understanding and mastery of them are critical in microprocessor-based system design and
interfacing
AND OR Ex-OR Inverter
A B A&B A|B A^B Y=~B
0 0 0 0 0 1
0 1 0 1 1 0
1 0 0 1 1 1
1 1 1 1 0 0

MICROPROCESSOR AND MICROCONTROLLER: 8051 PROGRAMMING


Prepared by Prof. Anand H. D., Dept. of ECE, Dr. AIT, Bengaluru-56 42
LOGIC OPERATIONS IN 8051 C
➢ The following shows some examples using the C logical operatorS
1. 0x35 & 0x0F = 0x05 /*ANDing* /
2. 0x04|0x68 = 0x6C /* ORing*/
3. 0x54^0x78 = 0x2C /*XORing*/
4. ~0x55 = 0xAA /*inverting*/

Example 9:
Run the following program on your simulator and examine the results.
Solution:
include <reg5l.h>
void main (void)
{
P0 = 0x35 & 0x0F; //ANDing
Pl = 0x04 | 0x68; //ORing
P2 = 0x54 ^ 0x78; //XORinng
P0 = ~0x55; //inversing
Pl = 0x9A >> 3; //shifting right 3 times
P2 = 0x77 >> 4; //shifting right 4 times
P0 = 0x6 << 4; //shifting left 4 times
}
MICROPROCESSOR AND MICROCONTROLLER: 8051 PROGRAMMING
Prepared by Prof. Anand H. D., Dept. of ECE, Dr. AIT, Bengaluru-56 43
LOGIC OPERATIONS IN 8051 C
Example 10:
Write an 8051 C program to toggle all the bits of PO and P2 continuously with a 250 ms delay. Use
the inverting operator
Solution:
The program below is tested for the DS89C420 with XTAL = 11.0592 MHz.
#include <reg5l.h>
void MSDelay (unsigned int);
void main (void)
{
P0=0x55;
P2=0x55;
while (1)
{
P0=~P0
P2 =~ P2
MSDelay (250) ;
}
} void MSDelay (unsigned int itime)
{
unsigned int i, j:
for (i=0; i<itime; i++)
for (j=0; j<1275; j++) ;
}
MICROPROCESSOR AND MICROCONTROLLER: 8051 PROGRAMMING
Prepared by Prof. Anand H. D., Dept. of ECE, Dr. AIT, Bengaluru-56 44
LOGIC OPERATIONS IN 8051 C
Bit-wise shift operation in C
➢ There are two bit-wise shift operators in C: (1) shift right (>>), and (2) shift left (<<).
➢ Their format in C is as follows:
#includee <reg51.h>
data >> number of bits to be shifted right void MSDelay (unsigned int);
void main (void)
data<<number of bits to be shifted left
{
The following shows some examples of shift operators in C. P0 = 0x55;
P1 = 0x55;
1. 0x9A >> 3 = 0x13 /* shifting right 3 times */
P2 = 0x55;
2. 0x77 >> 4 = 0x07 /*shifting right 4 times */ while (1)
{
3. 0x6 << 4 = 0x60 /*shifting left 4 times */
P0 = P0 ^ 0xFF;
P1 = P1 ^ 0xFF ;
Example 11 P2 = P2 ^ 0xFF;
MSDelay (250);
Write an 8051 C program to toggle all the bits of PO, P1, }
and P2 continuously with a 250 ms delay. Use the Ex- }
OX operator. void MSDelay (unsigned int itime)
Solution: {
The program below is tested for the DS89C420 with unsigned int i, j;
XTAL = 11.0592 MHz. for (i=0;i<itime;i++)
for (j=0;j<1275:j++) ;
}

MICROPROCESSOR AND MICROCONTROLLER: 8051 PROGRAMMING


Prepared by Prof. Anand H. D., Dept. of ECE, Dr. AIT, Bengaluru-56 45
LOGIC OPERATIONS IN 8051 C
Example 12:
Write an 8051 C program to get bit P1.0 and send it to P2.7 after inverting it.

Solution:
#include <reg51.h>
sbit inbit =P1 ^ 0;
sbit outbit=P2 ^ 7; //sbit is used declare port (SFR) bits
bit membit; //notice this is bit-addressable memory

void main(void)
{
while (1)
{
Membit = inbit; //get a bit from P1.0
outbit = ~membit; //invert it and send it to P2.7
}
}

MICROPROCESSOR AND MICROCONTROLLER: 8051 PROGRAMMING


Prepared by Prof. Anand H. D., Dept. of ECE, Dr. AIT, Bengaluru-56 46
LOGIC OPERATIONS IN 8051 C
Example 12:
Write an 8051 C program to read the P1.0 and P1.1 bits and issue an ASCII character to P0 according to the
following table.
P1.1 P1.0
solution: 0 0 send ‘0' to P0
#include <reg51.h>
void main(void) 0 1 send '1' to P0
{ 1 0 send '2' to P0
unsigned char z;
1 1 send '3' to P0
z=P1; //read P1
z=z&0x3; //mask the unused bits
switch(z) //make decision
{
case (0): case (2):
{ {
P0 = '0’; //issue ASCII 0 P0='2’; //issue ASCII 2
break; break;
} }
case (1): case (3):
{ {
P0='1’; //issue ASCII 1 P0='3’ ; //issue ASCII 3
break; break;
} }
}
}

MICROPROCESSOR AND MICROCONTROLLER: 8051 PROGRAMMING


Prepared by Prof. Anand H. D., Dept. of ECE, Dr. AIT, Bengaluru-56 47
Thank You

Prof. Anand H. D.
M. Tech. (PhD.)
Assistant Professor,
Department of Electronics & Communication Engineering
Dr. Ambedkar Institute of Technology, Bengaluru-56
Email: [email protected]
Phone: 9844518832
48

You might also like