SMTH 1
SMTH 1
Mr. Anand H. D.
Stack operations
Introduction to Embedded C
C data types
Logical operations
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.
1. Write a program to add the values of locations 50H and 51H and store the result
in locations in 52h and 53H.
2. Write a program to store data FFH into RAM memory locations 50H to 58H
using direct addressing mode
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
5. Write a program to store data FFH into RAM memory locations 50H to 58H
using indirect addressing mode
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.
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
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
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
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
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
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
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
= 436.255 µs
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
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.
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
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
➢ 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.
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.
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
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.
} continuously.
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.
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++) ;
}
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
}
}
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