Programming The MCU 8051 PDF
Programming The MCU 8051 PDF
Programming The MCU 8051 PDF
• Use the bit-addressable instructions SETB and CLR to access PSW.3 and PSW.4
Example:
At power up the register bank 0 is default and bits PSW.3 and PSW.4 are 0.
SETB PSW.4 ;sets RS1=1 and RS0=0 to select bank 2
ROM
Internal Memory 0000h
The on-chip memory of 8051 consists of 256 bytes of memory:
First 128 bytes: 00h to 1Fh Register Banks The first 128 bytes is also
20h to 2Fh Bit addressable RAM known as internal RAM
30h to 7Fh General purpose RAM (IRAM)
Next 128 bytes: 80h to FFh Special function
Registers
Addressing modes
The CPU can access data in various ways, which are called addressing modes
1. Immediate
2. Register
3. Register direct
4. Register indirect
5. Indexed
• DPTR can also be accessed as two 8-bit registers, the high byte DPH and low byte DPL
MOV DPL, #21H ;This is the same
MOV DPH, #45H ;as above
Examples:
MOV A, 04 ; copy contents of R4 into A
• Usually the direct addressing mode is used to access RAM locations 30 – 7FH
MOV R0, 40H ;save content of 40H in R0
MOV 56H, A ;save content of A in 56H
• The Special Function Register (SFR) can be accessed by their names or by their addresses
The SFR registers have addresses between 80H and FFH
MOV 0E0H, #55H ;is the same as
MOV A, #55h ;load 55H into A
MOV 0F0H, R0 ;is the same as
MOV B, R0 ;copy R0 into B
F0h F7 F6 F5 F4 F3 F2 F1 F0 B*
E0h E7 E6 E5 E4 E3 E2 E1 E0 A* (accumulator)
D0h D7 D6 D5 D4 D3 D2 D1 D0 PSW*
B8h -- -- -- BC BB BB B9 B8 IP*
Internal Memory
B0h B7 B6 B5 B4 B3 B2 B1 B0 Port 3 (P3*)
FFh Special
A8h AF -- -- AC AB AA A9 A8 IE* Function
Registers
A0h A7 A6 A5 A4 A3 A2 A1 A0 Port 2 (P2*) [SFR]
80h
99h SBUF 7Fh
98h 9F 9E 9D 9C 9B 9A 99 98 SCON* Internal
RAM
90h 97 96 95 94 93 92 91 90 Port 1 (P1*) [IRAM]
8Dh TH1
8Ch TH0
8Bh TL1
8Ah TL0 00h
89h TMOD
88h 8F 8E 8D 8C 8B 8A 89 88 TCON*
87h PCON
83h DPH
82h DPL
81h SP
80h 87 86 85 84 83 82 81 80 Port 0 (P0*)
* indicates the bit addressable SFR registers
• Only direct addressing mode is allowed for pushing or popping the stack
PUSH 0E0H ;Pushing the accumulator onto the stack
PUSH A ;invalid
b) To accessing externally connected RAM or on-chip code ROM, the DPTR register is used as
16-bit pointer
ORG 0
MOV DPTR, #300H ;LOAD TABLE ADDRESS
MOV A, #0FFH ;A=FF
MOV P1, A ;CONFIGURE P1 INPUT PORT
BACK: MOV A,P1 ;GET X
MOV A, @A+DPTR ;GET X SQAURE FROM TABLE
MOV P2, A ;ISSUE IT TO P2
SJMP BACK ;KEEP DOING IT
ORG 300H
XSQR_TABLE:
DB 0,1,4,9,16,25,36,49,64,81
END
Programming the MCU 8051 Page 7 of 48
Single-bit operation
Single-bit instructions allow to set, clear, move, and complement individual bits of a port,
memory, or register.
Bit address 00-7FH belong to RAM byte addresses 20-2FH
Bit address 80-F7H belong to SFR P0, P1, …
• Internal RAM locations 20-2FH are both byte-addressable and bit addressable
• The 128 bytes of RAM have the byte addresses of 00 – 7FH can be accessed in byte size
using direct and register-indirect addressing modes.
• The 16 bytes of RAM locations 20 – 2FH have bit address of 00 – 7FH can be accessed
by the single-bit instructions only which use direct addressing mode only.
• SFR registers A, B, PSW, IP, IE, ACC, SCON, TCON and all I/O ports are bit-
addressable
• Code ROM, holding program for execution, is not bit-addressable
Instructions Function
SETB bit Set the bit (bit = 1)
CLR bit Clear the bit (bit = 0)
CPL bit Complement the bit (bit = NOT bit)
JB bit, target Jump to target if bit = 1 (jump if bit)
JNB bit, target Jump to target if bit = 0 (jump if no bit)
JBC bit, target Jump to target if bit = 1, clear bit (jump if bit, then clear)
• The BIT directive is a widely used directive to assign the bit-addressable I/O and RAM
locations
Example: A switch is connected to pin P1.7 and an LED to pin P2.0. Write a
program to get the status of the switch and send it to the LED.
Programming the MCU 8051 Page 8 of 48
Solution:
LED BIT P1.7 ;assign bit
SW BIT P2.0 ;assign bit
HERE: MOV C, SW ;get the bit from the port
MOV LED, C ;send the bit to the port
SJMP HERE ;repeat forever
The SFRs are also assigned the same address space with addresses 80 – FFH .
Use direct addressing mode to access them
– MOV 90H, #55H or MOV P1, #55H
Programming the MCU 8051 Page 9 of 48
Power-on reset
RESET pin is active high .
Upon applying a high pulse to this pin, the microcontroller will reset and terminate all activitiesƒ
Activating a power-on reset will cause the following values in the registers.
Hardware features:
Port 0 –
a) P0 is an open drain, each pin must be connected externally to pull-up resistors.
Port 1 –
It can be used for input or output port.
Port 2 –
a) It can be used for input or output port.
b) Port 2 is also designated as A8 – A15, to provide upper 8 bit address for the external memory
Port 3 –
a) It can be used for input or output port.
b) It has the additional function of providing important signals
Software features:
• All the ports upon RESET are configured as input ports
• Configuration of ports
a) Output port - write 0 to the port,
b) Input port - write 1 to the port,
Programming the MCU 8051 Page 11 of 48
Example:
1) Access all bits of port
• Send out continuously to port 0 values 55H and AAH alternating
;;The entire 8 bits of Port 0 are accessed
BACK: MOV A,#55H
MOV P0, A
ACALL DELAY
MOV A, #0AAH
MOV P0, A
ACALL DELAY
SJMP BACK
MOV A, #0FFH
MOV P0, A
BACK: MOV A, P0
MOV P1,A
SJMP BACK ; keep doing it
Example
A switch is connected to pin P1.0 and an LED to pin P2.7. Write a program to get the status
of the switch and send it to the LED
Solution:
SETB P1.7 ; make P1.7 an input
AGAIN: MOV C, P1.0 ; read SW status into CF
MOV P2.7, C ; send SW status to LED
SJMP AGAIN ; keep repeating
Read-Modify-Write instructions
• read the port latch, normally read a value,
• perform an operation
• then rewrite it back to the port latch
Programming the MCU 8051 Page 13 of 48
Mnemonics Example
ANL PX ANL P1,A
ORL PX ORL P2,A
XRL PX XRL P0,A
JBC PX.Y,TARGET JBC P1.1,TARGET
CPL PX.Y CPL P1.2
INC PX INC P1
DEC PX DEC P2
DJNZ PX.Y,TARGET DJNZ P1,TARGET
MOV PX.Y,C MOV P1.2,C
CLR PX.Y CLR P2.3
SETB PX.Y SETB P2.3
Note: x is 0, 1, 2, or 3 for P0 – P3
Example:
MOV P1,#55H ;P1=01010101
AGAIN: XRL P1,#0FFH ;XOR P1 with 1111 1111
ACALL DELAY
SJMP AGAIN
Programming the MCU 8051 Page 14 of 48
b) hardware
The start and stop of the timer are controlled by an external source is achieved by making
GATE=1 in the TMOD register
Timer/counter is enabled while
• the INTx pin is high (pins P3.2 and P3.3 for timers 0 and 1) and
• the Tx control pin is set (pins P3.4 and P3.5 for timers 0 and 1).
2. Timer register
Both Timer 0 and Timer 1 are 16 bits wide, each accessed as two separate registers of low byte
and high byte.
• The low byte register is called TL0/TL1 and
• The high byte register is called TH0/TH1
Operation of mode 1:
1. It is a 16-bit timer; therefore, it allows value of 0000 to FFFFH to be loaded into the timer’s
register TL and TH
2. After TH and TL are loaded with a 16-bit initial value, the timer must be started . This is done
by SETB TR0 for timer 0 and SETB TR1 for timer 1
3. After the timer is started, it starts to count up. It counts up until it reaches its limit of FFFFH
When it rolls over from FFFFH to 0000, it sets high a flag bit called TF (timer flag)
- Each timer has its own timer flag: TF0 for timer 0, and TF1 for timer 1
- When this timer flag is raised, stop the timer with the instructions CLR TR0 or CLR TR1,
for timer 0 and timer 1, respectively
4. After the timer reaches its limit and rolls over, in order to repeat the process
- TH and TL must be reloaded with the original value, and
- TF must be reloaded to 0
Programming the MCU 8051 Page 16 of 48
Example
Create a square wave of 50% duty cycle (with equal portions high and low) on the P1.5 bit.
Timer 0 is used to generate the time delay.
Solution
MOV TMOD, #01 ;Timer 0, mode 1(16-bit mode)
HERE: MOV TL0, #0F2H ;TL0=F2H, the low byte
MOV TH0, #0FFH ;TH0=FFH, the high byte
CPL P1.5 ;toggle P1.5
ACALL DELAY
SJMP HERE
DELAY:
SETB TR0 ;start the timer 0
AGAIN: JNB TF0, AGAIN ;monitor timer flag 0 until it rolls over
CLR TR0 ;stop timer 0
CLR TF0 ;clear timer 0 flag
RET
Example
A 8051 C program to toggle only bit P1.5 continuously every 50 ms.
Use Timer 0, mode 1 (16-bit) to create the delay.
Solution
#include <reg51.h>
void T0M1Delay(void);
sbit mybit=P1^5;
void main(void){
while (1) {
mybit=~mybit;
T0M1Delay();
}
}
void T0M1Delay(void){
TMOD=0x01;
TL0=0xFD;
TH0=0x4B;
TR0=1;
while (TF0==0);
TR0=0;
TF0=0;
}
Programming the MCU 8051 Page 17 of 48
Mode 2 operation
1. It is an 8-bit timer. It allows values of 00 to FFH to be loaded into the timer’s register TH
2. After TH is loaded with the 8-bit value, the 8051 gives a copy of it to TL
3. Start the timer by the instruction SETB TRx for timers
4. After the timer is started,
• It counts up by incrementing the TL register to FFH
• then it rolls over from FFH to 00,
• sets high the TF (timer flag)
• TL is reloaded automatically with the original value in the TH register
Example
Generated square wave on pin P1.0 using 8-bit/auto reload mode 2
Solution
MOV TMOD,#20H ; T1/8-bit/auto reload
MOV TH1,#5 ; TH1 = 5
SETB TR1 ; start the timer 1
BACK:
JNB TF1,BACK ;till timer rolls over
CPL P1.0 ;P1.0 to hi, lo
CLR TF1 ;clear Timer 1 flag
SJMP BACK ;mode 2 is auto-reload
Solution:
• In Windows calculator, select decimal and enter 200.
• Then select hex, then +/- to get the TH value.
• Use only the right two digits and ignore the rest since data is 8-bit.
• The advantage of using negative values is that, there is no need to calculate the value loaded
to THx
Counter programming
• In counter mode, external pulse increments the TH, TL registers
• The timer is used as a counter by putting C/T = 1 in the TMOD registers, so that the 8051
gets its pulses from outside
• The counter counts up as pulses are fed from P3.4 and P3.5 pins , these pins are called T0
(timer 0 input) and T1 (timer 1 input)
Example: Assuming that clock pulses are fed into pin T1, write a program for counter 1 in
mode 2 to count the pulses and display the state of the TL1 count on P2, which connects to 8
LEDs.
Solution:
MOV TM0D,#01100000B ;counter 1, mode 2, C/T=1 external pulses
MOV TH1,#0 ;clear TH1
SETB P3.5 ;make T1 input
AGAIN: SETB TR1 ;start the counter
BACK: MOV A,TL1 ;get copy of TL
MOV P2,A ;display it on port 2
JNB TF1,Back ; keep doing, if TF = 0
CLR TR1 ;stop the counter 1
CLR TF1 ;make TF=0
SJMP AGAIN ;keep doing it
Note:
The timer works with a clock frequency of 1/12 of the XTAL frequency;
therefore, we have 11.0592 MHz / 12 = 921.6 kHz as the timer frequency.
As a result, each clock has a period of T = 1/921.6kHz = 1.085us.
Programming the MCU 8051 Page 19 of 48
In other words, Timer 0 counts up each 1.085 us resulting in delay = number of counts ×
1.085us.
The number of counts for the roll over is FFFFH – FFF2H = 0DH (13d). However, we add one
to 13 because of the extra clock needed when it rolls over from FFFF to 0 and raise the TF flag.
This gives 14 × 1.085us = 15.19us for half the pulse.
For the entire period it is T = 2 × 15.19us = 30.38us as the time delay generated by the timer.
Programming the MCU 8051 Page 20 of 48
TF is set to 1 every 12
ticks, so it functions as
a frequency divider
SCON register
An 8-bit register used to program the start bit, stop bit, and data bits of data framing, etc
D7 D0
SM0 SM1 SM2 REN TB8 RB8 TI RI
SM0, SM1
They determine the framing of data by specifying the number of bits per character, and the start
and stop bits
SM0 = 0, SM1 = 1 Serial Mode 1, 8-bit data, 1 stop bit, 1 start bit
TI (transmit interrupt)
When 8051 finishes the transfer of 8-bit character, it raises TI flag to indicate that it is ready to
transfer another byte.
TI bit is raised at the beginning of the stop bit
RI (receive interrupt)
When 8051 receives data serially via RxD, it gets rid of the start and stop bits and places the byte
in SBUF register. It raises the RI flag bit to indicate that a byte has been received and should be
picked up before it is lost
RI is raised halfway through the stop bit
SBUF register
An 8-bit register used for serial communication
SBUF register stores a byte data to be transferred via the TxD line,
SBUF holds the byte of data when it is received by 8051 RxD line.
3. The SCON register is loaded with the value 50H, indicating serial mode 1, where an 8-bit
data is framed with start and stop bits
4. TR1 is set to 1 to start timer 1
5. TI is cleared by CLR TI instruction
6. The character byte to be transferred serially is written into SBUF register
7. The TI flag bit is monitored with the use of instruction JNB TI,xx to see if the character has
been transferred completely
8. To transfer the next byte, go to step 5
Example
Write a program for the 8051 to transfer “YES” serially at 9600 baud, 8-bit data, 1 stop bit, do
this continuously
Solution:
MOV TMOD,#20H ;timer 1,mode 2(auto reload)
MOV TH1,#-3 ;9600 baud rate
MOV SCON,#50H ;8-bit, 1 stop, REN enabled
SETB TR1 ;start timer 1
AGAIN:
MOV A,#”Y” ;transfer “Y”
ACALL TRANS
MOV A,#”E” ;transfer “E”
ACALL TRANS
MOV A,#”S” ;transfer “S”
ACALL TRANS
SJMP AGAIN ;keep doing it
;serial data transfer subroutine
TRANS: MOV SBUF,A ;load SBUF
HERE: JNB TI,HERE ;wait for the last bit
CLR TI ;get ready for next byte
RET
Example:
Write a program for the 8051 to receive bytes of data serially, and put them in P1, set the baud
rate at 4800, 8-bit data, and 1 stop bit.
Solution:
MOV TMOD,#20H ;timer 1,mode 2(auto reload)
MOV TH1,#-6 ;4800 baud rate
MOV SCON,#50H ;8-bit, 1 stop, REN enabled
SETB TR1 ;start timer 1
HERE: JNB RI,HERE ;wait for char to come in
MOV A,SBUF ;saving incoming byte in A
MOV P1,A ;send to port 1
CLR RI ;get ready to receive next byte
SJMP HERE ;keep getting data
Example 10-5
Assume that the 8051 serial port is connected to the COM port of IBM PC, and on the PC the
terminal.exe program is used to send and receive data serially.
P1 and P2 of the 8051 are connected to LEDs and switches, respectively.
Write an 8051 program to (a) send to PC the message “We Are Ready”, (b) receive any data
send by PC and put it on LEDs connected to P1, and (c) get data on switches connected to P2
and send it to PC serially.
The program should perform part (a) once, but parts (b) and (c) continuously, use 4800 baud
rate.
Solution:
ORG 0
MOV P2,#0FFH ;make P2 an input port
MOV TMOD,#20H ;timer 1, mode 2
MOV TH1,#0FAH ;4800 baud rate
MOV SCON,#50H ;8-bit, 1 stop, REN enabled
SETB TR1 ;start timer 1
MOV DPTR,#MYDATA ;load pointer for message
H_1: CLR A
MOV A,@A+DPTR ;get the character
JZ B_1 ;if last character get out
ACALL SEND ;otherwise call transfer
INC DPTR ;next one
SJMP H_1 ;stay in loop
B_1: MOV A,P2 ;read data on P2
ACALL SEND ;transfer it serially
ACALL RECV ;get the serial data
MOV P1,A ;display it on LEDs
SJMP B_1 ;stay in loop indefinitely
Programming the MCU 8051 Page 24 of 48
Interrupt programming
Six interrupts are provided in 8051
a. Reset – power-up reset
b. Two timers interrupts for timer 0 and timer 1
c. Two hardware external interrupts INT0 and INT1 using pins P3.2 and P3.3
d. Serial communication interrupt for both receive and transfer
Note:
• Upon reset, all interrupts are disabled (masked),
• The interrupts must be enabled by software so that the microcontroller responds to them
Programming
Enable the interrupts through interrupt enable register IE for enabling (unmasking) and disabling
(masking) interrupts.
D7 D0
EA -- ET2 ES ET1 EX1 ET0 EX0
Example:
MOV IE,#10010110B ;enable serial, ;timer 0, EX1
or
SETB IE.7 ;EA=1, global enable
SETB IE.4 ;enable serial interrupt
SETB IE.1 ;enable Timer 0 interrupt
SETB IE.2 ;enable EX1
Programming the MCU 8051 Page 26 of 48
Timer interrupt
If the timer interrupt in the IE register is enabled,
• The microcontroller is interrupted , whenever the timer rolls over, TF is raised,
• The microcontroller jumps to the interrupt vector table to service the ISR,
• In the ISR there is no need of for a “CLR TFx” instruction before RETI. Since the 8051
clears the TF flag internally upon jumping to the interrupt vector table.
• The instruction RETI at the end of ISR does
a. Popping off return address from stack into program counter to resume the main program,
b. Clear the TF0, TF1 and interrupt-in-service flags IE0 (TCON.1) , IE1 (TCON.3) in the
TCON register indicating that servicing of interrupt is over and a new interrupt can be
accepted.
Example
Write a program to generate a square wave if 50Hz frequency on pin P1.2. Use an interrupt for
timer 0. Assume that XTAL=11.0592 MHz
Solution:
ORG 0
LJMP MAIN
ORG 000BH ;ISR for Timer 0
CPL P1.2
MOV TL0,#00
MOV TH0,#0DCH
RETI
ORG 30H
;--------main program for initialization
MAIN: MOV TMOD,#00000001B ;Timer 0, Mode 1
MOV TL0,#00
MOV TH0,#0DCH
MOV IE,#82H ;enable Timer 0 interrupt
SETB TR0
HERE: SJMP HERE
END
Example
Write a program to create a square wave of 200 μs period (5000 Hz) on pin P2.1. Use timer 0 to
create the square wave. Assume that XTAL = 11.0592 MHz.
Use timer 0 in mode 2 (auto reload). TH0 = 100/1.085 us = 92
Solution:
ORG 0000H
LJMP MAIN
Programming the MCU 8051 Page 27 of 48
level-triggered mode
• INT0 and INT1 pins are normally high
• A low-level signal triggers the interrupt
• The low-level signal at the INT pin must be removed before the execution of the last
instruction of the ISR, RETI; otherwise, another interrupt will be generated
Programming:
Configure IE register - enable bits EA (IE.7), EX1 (IE.2), / EX0 (IE.0)
Programming the MCU 8051 Page 28 of 48
Example:
Assume that the INT1 pin is connected to a switch that is normally high. Whenever it goes low,
it should turn on an LED. The LED is connected to P1.3 and is normally off. When it is turned
on it should stay on for a fraction of a second. As long as the switch is pressed low, the LED
should stay on.
Solution:
ORG 0000H
LJMP
;--ISR for INT1 to turn on LED
ORG 0013H ;INT1 ISR
SETB P1.3 ;turn on LED
MOV R3,#255
BACK: DJNZ R3,BACK ;keep LED on for a while
CLR P1.3 ;turn off the LED
RETI ;return from ISR Pressing the switch will cause the LED
;--MAIN program for initialization to be turned on. If it is kept activated, the
ORG 30H LED stays on.
MAIN: MOV IE,#10000100B ;enable external INT 1
HERE: SJMP HERE ;stay here until get interrupted
END
Edge-triggered mode
A falling edge (high to low) signal at pins INT0 and INT1 interrupts the micro-controller and
force to jump to location in the vector table to service the ISR.
Programming:
1. Configure IE register - enable bits EA (IE.7), EX1 (IE.2), / EX0 (IE.0)
2. Configure TCON register - enable bits IT1 (TCON.2), IT0 (TCON.0) to make an edge
triggered interrupt.
Example:
Assume that pin 3.3 (INT1) is connected to a pulse generator, write a program in which the
falling edge of the pulse will send a high to P1.3, which is connected to an LED. The LED is
turned on and off at the same rate as the pulses are applied to the INT1 pin.
Solution:
ORG 0000H
LJMP MAIN
;--ISR for hardware interrupt INT1 to turn on LED
ORG 0013H ;INT1 ISR
SETB P1.3 ;turn on LED
MOV R3,#255
BACK: DJNZ R3,BACK ;keep the LED on for a while
CLR P1.3 ;turn off the LED
RETI ;return from ISR
Programming the MCU 8051 Page 29 of 48
Programming
Example
Write a program in which the 8051 reads data from P1 and writes it to P2 continuously while
giving a copy of it to the serial COM port to be transferred serially. Assume that
XTAL=11.0592. Set the baud rate at 9600.
Solution:
ORG 0000H
LJMP MAIN
ORG 23H
LJMP SERIAL ;jump to serial int ISR
ORG 30H
MAIN: MOV P1,#0FFH ;make P1 an input port
MOV TMOD,#20H ;timer 1, auto reload
MOV TH1,#0FDH ;9600 baud rate
MOV SCON,#50H ;8-bit,1 stop, ren enabled
MOV IE,10010000B ;enable serial int.
SETB TR1 ;start timer 1
BACK: MOV A,P1 ;read data from port 1
Programming the MCU 8051 Page 30 of 48
Note:
• The moment a byte is written into SBUF it is framed and transferred serially.
• When the last bit (stop bit) is transferred the TI is raised,
• The serial interrupt is invoked if corresponding bit in the IE register is high.
• In the serial ISR, check for both TI and RI since both could have invoked interrupt
Programming the MCU 8051 Page 31 of 48
LCD Programming
LCD displays modules are the replacement for the 7 segment display due to their versatility.
LCD Connections
8051
P1.0 07 D0 LCD +5V
P1.1 08 D1 Vcc 02
P1.2 09 D2
P1.3 10 D3 Vee 03
P1.4 11 D4
P1.5 12 D5 Vss 01
P1.6 13 D6
P1.7 14 D7
RS R/W E
P2.0 4 5 6
P2.1
P2.2
Programming
Consists of
1. Configure LCD through control register,
2. Send data through data register.
Configuration of LCD
a. Typical command codes to the control register
Ser. Description Code
1. Initialize LCD mode – lines, matrix 38H - (2 lines, 5x7 matrix)
2. Display ON, cursor ON 0EH
3. Clear LCD 01H
4. Shift cursor RIGHT 06H
5. Cursor line and position 86H – (line 1, position 6)
C6H – (line 2, position 6)
Consult data sheet of LCD for complete command code
b. Procedure
• Make pin RS (register select) = 0 – select control register,
• Make pin R/W (read or write select) = 0 for write,
• Send a high-to-low pulse to the E (enable) pin (through delay) to enable the internal latch of
the LCD for write,
• Give the LCD some delay.
Send data
c. Procedure
• Send data over D7 – D0 lines
• Make pin RS (register select) = 1 – select data register,
• Make pin R/W (read or write select) = 0 for write,
Programming the MCU 8051 Page 32 of 48
• Send a high-to-low pulse to the E pin (through delay) to enable the internal latch of the LCD
for write.
Program # 1
Calls a time delay before sending next data/command
; P1.0-P1.7=D0-D7, P2.0=RS, P2.1=R/W, P2.2=E COMNWRT: ;send command to LCD
ORG 0H MOV P1,A ;copy reg A to port 1
; configure LCD CLR P2.0 ;RS=0 for command
MOV A,#38H ;init. lcd 2 lines, 5x7 matrix CLR P2.1 ;R/W=0 for write
ACALL COMNWRT ;call command subroutine SETB P2.2 ;E=1 for high pulse
ACALL DELAY ;give LCD some time ACALL DELAY ;give LCD some time
MOV A,#0EH ;display on, cursor on CLR P2.2 ;E=0 for H-to-L pulse
ACALL COMNWRT ;call command subroutine RET
ACALL DELAY ;give LCD some time
MOV A,#01 ;clear LCD DATAWRT: ;write data to LCD
ACALL COMNWRT ;call command subroutine MOV P1,A ;copy reg A to port 1
ACALL DELAY ;give LCD some time SETB P2.0 ;RS=1 for data
MOV A,#06H ;shift cursor right CLR P2.1 ;R/W=0 for write
ACALL COMNWRT ;call command subroutine SETB P2.2 ;E=1 for high pulse
ACALL DELAY ;give LCD some time ACALL DELAY ;give LCD some time
MOV A,#84H ;cursor at line 1, pos. 4 CLR P2.2 ;E=0 for H-to-L pulse
ACALL COMNWRT ;call command subroutine RET
ACALL DELAY ;give LCD some time
; send data DELAY: MOV R3,#50 ;50 or higher HERE2:
MOV A,#’N’ ;display letter N MOV R4,#255 ;R4 = 255
ACALL DATAWRT ;call display subroutine HERE: DJNZ R4,HERE ;stay until R4 becomes 0
ACALL DELAY ;give LCD some time DJNZ R3,HERE2
MOV A,#’O’ ;display letter O RET
ACALL DATAWRT ;call display subroutine END
AGAIN: SJMP AGAIN ;stay here
Programming the MCU 8051 Page 33 of 48
Program # 2
Check busy flag before sending data, command to LCD
; P1.0-P1.7=D0-D7, P2.0=RS, P2.1=R/W, P2.2=E CLR P2.2 ;E=0,latch in
ORG 0H RET
MOV A,#38H ;init. LCD 2 lines ,5x7
matrix DATA_DISPLAY:
ACALL COMMAND ;issue command ACALL READY ;is LCD ready?
MOV A,#0EH ;LCD on, cursor on MOV P1,A ;issue data
ACALL COMMAND ;issue command SETB P2.0 ;RS=1 for data
MOV A,#01H ;clear LCD command CLR P2.1 ;R/W =0 to write to LCD
ACALL COMMAND ;issue command SETB P2.2 ;E=1 for H-to-L pulse
MOV A,#06H ;shift cursor right CLR P2.2 ;E=0,latch in
ACALL COMMAND ;issue command RET
MOV A,#86H ;cursor: line 1, pos. 6
ACALL COMMAND ;command subroutine READY:
MOV A,#’N’ ;display letter N SETB P1.7 ;make P1.7 input port
ACALL DATA_DISPLAY CLR P2.0 ;RS=0 access command reg
MOV A,#’O’ ;display letter O SETB P2.1 ;R/W=1 read command reg
ACALL DATA_DISPLAY ;read command reg and check busy
HERE:SJMP HERE ;STAY HERE flag
BACK:
COMMAND: SETB P2.2 ;E=1 for H-to-L pulse
ACALL READY ;is LCD ready? CLR P2.2 ;E=0 H-to-L pulse
MOV P1,A ;issue command code JB P1.7,BACK ;stay until busy flag=0
CLR P2.0 ;RS=0 for command RET
CLR P2.1 ;R/W=0 to write to LCD END
SETB P2.2 ;E=1 for H-to-L pulse
Programming the MCU 8051 Page 34 of 48
Program # 3
Call a time delay before sending next data/command
; P1.0-P1.7=D0-D7, P2.0=RS, P2.1=R/W, P2.2=E SETB P2.2 ;E=1 for high pulse
ORG 0 ACALL DELAY ;give LCD some time
MOV DPTR,#MYCOM CLR P2.2 ;E=0 for H-to-L pulse
C1: CLR A RET
MOVC A,@A+DPTR
ACALL COMNWRT ;call command subroutine DATAWRT: ;write data to LCD
ACALL DELAY ;give LCD some time MOV P1,A ;copy reg A to port 1
INC DPTR SETB P2.0 ;RS=1 for data
JZ SEND_DAT CLR P2.1 ;R/W=0 for write
SJMP C1 SETB P2.2 ;E=1 for high pulse
SEND_DAT: ACALL DELAY ;give LCD some time
MOV DPTR,#MYDATA CLR P2.2 ;E=0 for H-to-L pulse
D1: CLR A RET
MOVC A,@A+DPTR
ACALL DATAWRT ;call command subroutine DELAY: MOV R3,#250 ;50 or higher
ACALL DELAY ;give LCD some time HERE2: MOV R4,#255 ;R4 = 255
INC DPTR HERE: DJNZ R4,HERE ;stay until R4 becomes 0
JZ AGAIN DJNZ R3,HERE2
SJMP D1 RET
AGAIN: SJMP AGAIN ;stay here
ORG 300H
COMNWRT: ;send command to LCD MYCOM: DB 38H,0EH,01,06,84H,0
MOV P1,A ;copy reg A to P1 ; commands and null
CLR P2.0 ;RS=0 for command MYDATA: DB “HELLO”,0
CLR P2.1 ;R/W=0 for write END
Programming the MCU 8051 Page 35 of 48
Example
Write an 8051 C program to send letters ‘M’, ‘D’, and ‘E’ to the LCD using the busy flag
method.
Solution:
#include <reg51.h> }
sfr ldata = 0x90; //P1=LCD data pins void lcddata(unsigned char value){
sbit rs = P2^0; lcdready(); //check the LCD busy flag
sbit rw = P2^1; ldata = value; //put the value on the pins
sbit en = P2^2; rs = 1;
sbit busy = P1^7; rw = 0;
void main(){ en = 1; //strobe the enable pin
lcdcmd(0x38); MSDelay(1);
lcdcmd(0x0E); en = 0;
lcdcmd(0x01); return;
lcdcmd(0x06); }
lcdcmd(0x86); //line 1, position 6 void lcdready(){
lcdcmd(‘M’); busy = 1; //make the busy pin at input
lcdcmd(‘D’); rs = 0;
lcdcmd(‘E’); rw = 1;
} while(busy==1){ //wait here for busy flag
void lcdcmd(unsigned char value){ en = 0; //strobe the enable pin
lcdready(); //check the LCD busy flag MSDelay(1);
ldata = value; //put the value on the pins en = 1;
rs = 0; }
rw = 0; void lcddata(unsigned int itime){
en = 1; //strobe the enable pin unsigned int i, j;
MSDelay(1); for(i=0;i<itime;i++)
en = 0; for(j=0;j<1275;j++);
return; }
Programming the MCU 8051 Page 36 of 48
Keyboard interfacing
• Keyboards are organized in a matrix of rows and columns
• The CPU accesses both rows and columns through ports
• Thus, with two 8-bit ports, an 8 x 8 matrix of keys can be connected to a microprocessor
• When a key is pressed, a row and a column make a contact. Otherwise, there is no connection
between rows and columns .
• Upon finding the row that the key press belongs to, it sets up the starting address for the
look-up table holding the scan codes (or ASCII) for that row
Program
Keyboard Program
;keyboard subroutine. This program sends the ASCII CJNE A,#00001111B,ROW_2 ;key row 2, find col.
;code for pressed key to P0.1 MOV P1,#11110111B ;ground row 3
;P1.0-P1.3 connected to rows, P2.0-P2.3 to column MOV A,P2 ;read all columns
MOV P2,#0FFH ;make P2 an input port ANL A,#00001111B ;mask unused bits
K1: CJNE A,#00001111B,ROW_3 ;key row 3, find col.
MOV P1,#0 ;ground all rows at once LJMP K2 ;if none, false input, repeat
MOV A,P2 ;read all col,(ensure keys open) ROW_0:
ANL A,00001111B ;masked unused bits MOV DPTR,#KCODE0 ;set DPTR=start of row 0
CJNE A,#00001111B,K1 ;till all keys release SJMP FIND ;find col. Key belongs to
K2: ROW_1:
ACALL DELAY ;call 20 ms delay MOV DPTR,#KCODE1 ;set DPTR=start of row
MOV A,P2 ;see if any key is pressed SJMP FIND ;find col. Key belongs to
ANL A,00001111B ;mask unused bits ROW_2:
CJNE A,#00001111B,OVER ;key pressed, find row MOV DPTR,#KCODE2 ;set DPTR=start of row 2
SJMP K2 ;check till key pressed SJMP FIND ;find col. Key belongs to
OVER: ROW_3:
ACALL DELAY ;wait 20 ms debounce time MOV DPTR,#KCODE3 ;set DPTR=start of row 3
MOV A,P2 ;check key closure FIND: RRC A ;see if any CY bit low
ANL A,00001111B ;mask unused bits JNC MATCH ;if zero, get ASCII code
CJNE A,#00001111B,OVER1 ;key pressed, find row INC DPTR ;point to next col. addr
SJMP K2 ;if none, keep polling SJMP FIND ;keep searching
OVER1: MOV P1, #11111110B ;ground row 0 MATCH: CLR A ;set A=0 (match is found)
MOV A,P2 ;read all columns MOVC A,@A+DPTR ;get ASCII from table
ANL A,#00001111B ;mask unused bits MOV P0,A ;display pressed key
CJNE A,#00001111B,ROW_0 ;key row 0, find col. LJMP K1
MOV P1,#11111101B ;ground row 1 ;ascii look-up table for each row
MOV A,P2 ;read all columns ORG 300H
ANL A,#00001111B ;mask unused bits KCODE0: DB ‘0’,’1’,’2’,’3’ ;ROW 0
CJNE A,#00001111B,ROW_1 ;key row 1, find col. KCODE1: DB ‘4’,’5’,’6’,’7’ ;ROW 1
MOV P1,#11111011B ;ground row 2 KCODE2: DB ‘8’,’9’,’A’,’B’ ;ROW 2
MOV A,P2 ;read all columns KCODE3: DB ‘C’,’D’,’E’,’F’ ;ROW 3
ANL A,#00001111B ;mask unused bits END
Programming the MCU 8051 Page 39 of 48
Tentative Projects
a. Blinking single LED
b. Blinking LEDs animation connected to a port
c. Display a character on a 7 segment display
d. Display characters of a string one by one on a 7 segment display
e. Display characters of a string on array of five 7 segment display
f. Display characters of a string scrolling on array of five 7 segment display
g. Door entry audio alarm with door number display on a 7 segment display
h. Simple octal key board
i. 3x3 matrix key board
j. Automatic water pump controller
k. Interfacing LCD display controller
l. Display characters of a string scrolling on LCD display controller
m. Serial data transmission and reception with display on LCD display controller – close loop
to itself
n. Serial communication with PC using serial to usb converter / adruino controller (MPU
removed), putty.exe as hyperterminal
Presentation:
Page 1 Name of University
Subject Name & Code
Project name
Participant name & ID
Section & group
Name of instructor
Date of submission
Page 2 Objective of project
Circuit diagram
List of components
Page 3 Flowchart of program
Printout of program
Page 4 Brief description of program
Achievements (Result)
Programming the MCU 8051 Page 41 of 48
The AT89C2051
Musical IC UM66
Programming the MCU 8051 Page 43 of 48
TYPES OF INSTRUCTIONS
1. Arithmetic Instructions
2. Branch Instructions
3. Data Transfer Instructions
4. Logic Instructions
5. Bit-oriented Instructions
1. ARITHMETIC INSTRUCTIONS
2. BRANCH INSTRUCTIONS
4. LOGIC INSTRUCTIONS
5. BIT-ORIENTED INSTRUCTIONS
Postscript
The materials included in this handout is of practical nature. Anyone can use any part of it
without restriction.