Gopinath
Gopinath
Gopinath
Course Outcome:
At the end of the course, the student should be able to
1. Comprehend the various microprocessors including Intel Pentium Processors
2. Infer the architecture and Programming of Intel 8086 Microprocessor.
3. Comprehend the architectures and programming of 8051 microcontroller.
4. Deploy the implementation of various peripherals such as general purpose input/
output, timers, serial communication, LCD, keypad and ADC with 8051
microcontroller
5. Infer the architecture of ARM Processor
6. Develop the simple application using ARM processor.
• If EA = 1, interrupts are enabled and will be responded to, if their corresponding bits in IE
register are high;
• If EA = 0, no interrupt will be responded to, even if the associated bit in the IE register is
high.
Enable/disable Interrupts in 8051
• Pin 12 (P3.2) and pin 13 (P3.3) of the 8051, designated as INT0 and INT1, are used as
external hardware interrupts
• There are two activation levels for the external hardware interrupts (INT0 and INT1).
Level trigged - In the level-triggered mode, INT0 and INT1 pins are normally high. So,
If a low-level signal is applied to them, it triggers the interrupt.
Edge trigged – Set IE0/IE1 bit (TCON.1/TCON.3) to enable edge triggering for
INT0/INT1
8051-Interrupt Priority Upon Reset
8051- Modifying the default Interrupt Priority
Interrupt Priority (IP) Register (Bit-addressable)
D7 D0
-- -- PT2 PS PT1 PX1 PT0 PX0
• Interrupt vector table is an index of memory addresses where, the starting address of the
interrupt service routines (ISRs) are saved.
• When an interrupt request comes, microcontroller search the interrupt vector table to find
the address of the starting memory location of the required ISR.
• So that, corresponding ISR will be executed to serve the interrupt request.
Interrupt Vector Table of 8051 µC
Programming using Interrupts-Example1
Discuss what happens if interrupts INT0, TF0, and INT1 are activated at the same time.
Assume priority levels were set by the power-up reset and the external hardware interrupts
are edge triggered.
Solution:
priority levels were set by the power-up reset means default priority levels.
If these three interrupts are activated at the same time, their statuses are saved internally.
Then the 8051 checks all five interrupts according to the sequence listed in the default
priority order. If any is activated, it services it in sequence.
Therefore, when the above three interrupts are activated, INT0 (external interrupt- IE0) is
serviced first, then timer 0 (TF0), and finally INT1 (external interrupt- E1).
Programming using Interrupts-Example2
(a) Program the IP register to assign the highest priority to INT1(external interrupt 1), then
(b) discuss what happens if INT0, INT1, and TF0 are activated at the same time. Assume the
interrupts are edge-triggered.
Solution:
(a) MOV IP,#00000100B ;IP.2=1 assign INT1 higher priority. The instruction SETB IP.2 also
will do the same thing as the above line since IP is bit-addressable.
(b) The instruction in Step (a) assigned a higher priority to INT1 than the others; therefore,
when INT0, INT1, and TF0 interrupts are activated at the same time, the 8051 services INT1
first, then it services INT0, then TF0.
The instruction in Step (a) makes both the INT0 and TF0 bits in the IP register 0. As a result,
the sequence in default priority order is followed which gives a higher priority to INT0 over
TF0.
Programming using Interrupts-Example3
Assume that after reset, the interrupt priority is set by the instruction MOV IP,#00001100B.
Discuss the sequence in which the interrupts are serviced.
Solution:
The instruction “MOV IP #00001100B” (B is for binary) assigned INT1 and timer 1 (TF1) to a
higher priority level compared with the rest of the interrupts. However, INT1 and timer 1 (TF1)
both are set, they are prioritized as per the default priority order (INT1 gets highest priority and
TF1 gets second priority). The priorities of the remaining interrupts are rearranged by
following the default priority order.
Finally, the updated priorities are as follows.
External Interrupt 1 (INT1) : Highest Priority
Timer Interrupt 1 (TF1) : Second Priority
External Interrupt 0 (INT0) : rearranged as per default.
Timer Interrupt 0 (TF0) : rearranged as per default.
Lowest Priority Serial Communication (RI+TI) : rearranged as per default.
D7 D0
Interrupt priority (IP) register: -- -- PT2 PS PT1 PX1 PT0 PX0
Programming using Interrupts-Example4
Write a program using interrupts to do the following:
(a) Receive data serially and sent it to P0,
(b) Have P1 port read and transmitted serially, and a copy given to P2,
(c) Make timer 0 generate a square wave of 5kHz frequency on P0.1.
Assume that XTAL-11,0592. Set the baud rate at 4800.
Solution:
ORG 0
LJMP MAIN
ORG 000BH ;ISR for timer 0
CPL P0.1 ;toggle P0.1
RETI ;return from ISR
ORG 23H
LJMP SERIAL ;jump to serial interrupt ISR
ORG 30H
MAIN: MOV P1,#0FFH ;make P1 an input port
MOV TMOD,#22H ;timer 1,mode 2(auto reload)
Programming using Interrupts-Examples4 page2
Write a program using interrupts to do the following:
(a) Receive data serially and sent it to P0,
(b) Have P1 port read and transmitted serially, and a copy given to P2,
(c) Make timer 0 generate a square wave of 5kHz frequency on P0.1.
Assume that XTAL-11,0592. Set the baud rate at 4800.
Solution:
MOV TH1,#0F6H ;4800 baud rate
MOV SCON,#50H ;8-bit, 1 stop, ren enabled
MOV TH0,#-92 ;for 5kHZ wave
MOV IE,10010010B ;enable serial int.
SETB TR1 ;start timer 1
SETB TR0 ;start timer 0
BACK: MOV A,P1 ;read data from port 1
MOV SBUF,A ;give a copy to SBUF
MOV P2,A ;send it to P2
SJMP BACK ;stay in loop indefinitely
Programming using Interrupts-Example4 page3
Write a program using interrupts to do the following:
(a) Receive data serially and sent it to P0,
(b) Have P1 port read and transmitted serially, and a copy given to P2,
(c) Make timer 0 generate a square wave of 5kHz frequency on P0.1.
Assume that XTAL-11,0592. Set the baud rate at 4800.
Solution:
;-----------------SERIAL PORT ISR
ORG 100H
SERIAL:JB TI,TRANS ;jump if TI is high
MOV A,SBUF ;otherwise due to receive
MOV P0,A ;send serial data to P0
CLR RI ;clear RI since CPU doesn’t
RETI ;return from ISR
TRANS: CLR TI ;clear TI since CPU doesn’t
RETI ;return from ISR
END
Interrupt Vector Table for the 8051
Solution:
We will use Timer 0 in mode 2 (auto-reload). TH0 = 100/1.085 ms = 92.
ORG 0
LJMP MAIN
ORG 000BH ;ISR for Timer 0
CPL P1.2 ;complement P1.2
MOV TL0,#00 ;reload timer values
MOV TH0,#0DCH
RETI ;return from interrupt
ORG 0000H
LJMP MAIN ;bypass interrupt vector table
;--ISR for hardware interrupt INT1 to turn on the LED
ORG 0013H ;INT1 ISR
SETB P1.3 ;turn on LED
MOV R3,#255 ;load counter
BACK: DJNZ R3,BACK ;keep LED on for a while Pressing the switch will
CLR P1.3 ;turn off the LED turn the LED on. If it
RETI ;return from ISR is kept activated, the
;--MAIN program for initialization LED stays on.
ORG 30H
MAIN: MOV IE,#10000100B ;enable external INT1
HERE: SJMP HERE ;stay here until interrupted
END
Minimum Duration of the Low Level-Triggered Interrupt (XTAL = 11.0592 MHz)
Note: On RESET, IT0 (TCON.0) and IT1 (TCON.2) are both low, making
external interrupts level-triggered.
TF1 TCON.7 Timer 1 overflow flag. Set by hardware when timer/counter 1 overflows.
Cleared by hardware as the processor vectors to the interrupt service routine.
TR1 TCON.6 Timer 1 run control bit. Set/cleared by software to turn timer/counter 1 on/off.
TF0 TCON.5 Timer 0 overflow flag. Set by hardware when timer/counter 0 overflows.
Cleared by hardware as the processor vectors to the service routine.
TR0 TCON.4 Timer 0 run control bit. Set/cleared by software to turn timer/counter 0 on/off.
IE1 TCON.3 External interrupt 1 edge flag. Set by CPU when the external interrupt edge
TCON (Timer/Counter) (H-to-L transition) is detected.
Register (Bit-addressable) Cleared by CPU when the interrupt is processed.
Note: This flag does not latch low-level triggered interrupts.
IE0 TCON.1 External interrupt 0 edge flag. Set by CPU when external interrupt
(H-to-L transition) edge is detected. Cleared by CPU when interrupt is processed.
Note: This flag does not latch low-level triggered interrupts.
IT0 TCON.0 Interrupt 0 type control bit. Set/cleared by software to specify falling
edge/low-level triggered external interrupt.
Assuming 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 (or buzzer). In other words, the LED is turned on and off at the same rate
as the pulses are applied to the INT1 pin. This is an edge-triggered version of Example 11-5.
ORG 0000H
LJMP MAIN
;--ISR for hardware interrupt INT1 to turn on the LED
ORG 0013H ;INT1 ISR
SETB P1.3 ;turn on the 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
;--MAIN program for initialization
ORG 30H
MAIN: SETB TCON.2 ;make INT1 edge-trigger interrupt
MOV IE,#10000100B ;enable External INT1
HERE: SJMP HERE ;stay here until interrupted
END
Minimum Pulse Duration to Detect Edge-triggered Interrupts. XTAL = 11.0592 MHz
What is the difference between the RET and RETI instructions? Explain why we cannot use RET
instead of RETI as the last instruction of an ISR.
Both perform the same actions of popping off the top two bytes of the stack into
the program counter, and making the 8051 return to where it left off. However,
RETI also performs an additional task of clearing the interrupt-in-service flag,
indicating that the servicing of the interrupt is over and the 8051 now can accept
a new interrupt on that pin. If you use RET instead of RETI as the last instruction
of the interrupt service routine, you simply block any new interrupt on that pin
after the first interrupt, since the pin status would indicate that the interrupt is
still being serviced. In the cases of TF0, TF1, TCON.1, and TCON.3, they are
cleared by the execution of RETI.
Single Interrupt for Both TI and RI
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 MHz. Set the baud rate at 9600.
Solution:
ORG 0
LJMP MAIN
ORG 23H
LJMP SERIAL ;jump to serial interrupt ISR
ORG 30H
MAIN: MOV P1,#0FFH ;make P1 an input port
MOV TMOD,#20H ;timer 1, mode 2(auto-reload)
MOV TH1,#0FDH ;9600 baud rate
MOV SCON,#50H ;8-bit, 1 stop, REN enabled
MOV IE,#10010000B ;enable serial interrupt
SETB TR1 ;start timer 1
BACK: MOV A,P1 ;read data from port 1
MOV SBUF,A ;give a copy to SBUF
MOV P2,A ;send it to P2
SJMP BACK ;stay in loop indefinitely
;
;------------------Serial Port ISR
ORG 100H
SERIAL: JB TI,TRANS ;jump if TI is high
MOV A,SBUF ;otherwise due to receive
CLR RI ;clear RI since CPU does not
RETI ;return from ISR
TRANS: CLR TI ;clear TI since CPU does not
RETI ;return from ISR
END
In the above program notice the role of TI and RI. The moment a byte is written into SBUF it is framed
and transferred serially. As a result, when the last bit (stop bit) is transferred the TI is raised,
which causes the serial interrupt to be invoked since the corresponding bit in the IE register is high.
In the serial ISR, we check for both TI and RI since both could have invoked the interrupt. In other
words, there is only one interrupt for both transmit and receive.
Write a program in which the 8051 gets data from P1 and sends it
to P2 continuously while incoming data from the serial port is sent to P0.
Assume that XTAL = 11.0592 MHz. Set the baud rate at 9600.
Solution:
ORG 0
LJMP MAIN
ORG 23H
LJMP SERIAL ;jump to serial ISR
ORG 30H
MAIN: MOV P1,#0FFH ;make P1 an input port
MOV TMOD,#20H ;timer 1, mode 2(auto-reload)
MOV TH1,#0FDH ;9600 baud rate
MOV SCON,#50H ;8-bit,1 stop, REN enabled
MOV IE,#10010000B ;enable serial interrupt
SETB TR1 ;start Timer 1
BACK: MOV A,P1 ;read data from port 1
MOV P2,A ;send it to P2
SJMP BACK ;stay in loop indefinitely
;------------------SERIAL PORT ISR
ORG 100H
SERIAL: JB TI,TRANS ;jump if TI is high
MOV A,SBUF ;otherwise due to receive
MOV P0,A ;send incoming data to P0
CLR RI ;clear RI since CPU doesn’t
RETI ;return from ISR
TRANS: CLR TI ;clear TI since CPU doesn’t
RETI ;return from ISR
END
Interrupt Flag Bits for the 8051/52
Write a program using interrupts to do the following: (a) Receive data serially and send it to P0, (b)
Have port P1 read and transmitted serially, and a copy given to P2, (c) Make Timer 0 generate a square
wave of 5 kHz frequency on P0.1. Assume that XTAL = 11.0592 MHz. Set the baud rate at 4800.
ORG 0
LJMP MAIN
ORG 000BH ;ISR for Timer 0
CPL P0.1 ;toggle P0.1
RETI ;return from ISR
ORG 23H
LJMP SERIAL ;jump to serial int. ISR
ORG 30H
MAIN: MOV P1,#0FFH ;make P1 an input port
MOV TMOD,#22H ;timer 0&1,mode 2, auto-reload
MOV TH1,#0F6H ;4800 baud rate
MOV SCON,#50H ;8-bit, 1 stop, REN enabled
MOV TH0,#-92 ;for 5 KHz wave
MOV IE,#10010010B ;enable serial, timer 0 int.
SETB TR1 ;start timer 1
SETB TR0 ;start timer 0
BACK: MOV A,P1 ;read data from port 1
MOV SBUF,A ;give a copy to SBUF
MOV P2,A ;write it to P2
SJMP BACK ;stay in loop indefinitely
;------------------SERIAL PORT ISR
ORG 100H
SERIAL: JB TI,TRANS ;jump if TI is high
MOV A,SBUF ;otherwise due to received
MOV P0,A ;send serial data to P0
CLR RI ;clear RI since CPU does not
RETI ;return from ISR
TRANS: CLR TI ;clear TI since CPU does not
RETI ;return from ISR
END
LED INTERFACING
Interfacing comprises of hardware (Interface device) and Software
(source code to communicate, also called as the Driver).
Simply, to use an LED as the output device, LED should be connected to
Microcontroller port and the MC has to be programmed inside make
LED ON or OFF or blink.
Light Emitting Diodes or LEDs are the mostly commonly used
components in many applications, mostly used for signal transmission
/power indication purposes.
It is very cheaply and easily available in a variety of shape, color, and
size.
The LEDs are also used for design message display boards and traffic
control signal lights etc.
It has two terminals positive and negative as shown in the figure.
Observe carefully the interface LED interface 2 is in forward biased
because the input voltage of 5v connected to the positive terminal of the
LED, So here the Microcontroller pin should be at LOW level and vice
versa with the LED interface 1 connections.
LED INTERFACING WITH 8051
PROGRAM FOR BLINKING OF LEDS
ORG 00H ; Assembly Starts from 0000H.
START: MOV P1, #0FFH ; Move 11111111 to PORT1.
ACALL WAIT ; Call WAIT
MOV A, P1 ; Move P1 value to ACC
CPL A ; Complement ACC
MOV P1, A ; Move ACC value to P1
ACALL WAIT ; Call WAIT
SJMP START ; Jump to START
WAIT: MOV R2, #10 ; Load Register R2 with 10 (0x0A)
WAIT1: MOV R3, #200 ; Load Register R3 with 10 (0xC8)
WAIT2: MOV R4, #200 ; Load Register R4 with 10 (0xC8)
WAIT3: DJNZ R4, WAIT3 ; Decrement R4 till it is 0. Stay there if not 0.
DJNZ R3, WAIT2 ; Decrement R3 till it is 0. Jump to WAIT2 if not 0.
DJNZ R2, WAIT1 ; Decrement R2 till it is 0. Jump to WAIT1 if not 0.
RET ; Return to Main Program
END ; End Assembly
SEVEN SEGMENT DISPLAY
7 segment LED display is very popular and it can display digits
from 0 to 9 and quite a few characters like A, b, C, ., H, E, e, F, n,
o, t, u, y, etc.
A seven segment display consists of seven LEDs arranged in the
form of a squarish 8 slightly inclined to the right and a single
LED as the dot character.
Different characters can be displayed by selectively glowing the
required LED segments.
Seven segment displays are of two types, common cathode and
common anode.
In common cathode type, the cathode of all LEDs are tied
together to a single terminal which is usually labeled as .
In common anode type, the anode of all LEDs are tied together
as a single terminal and cathodes are left alone as individual pins.
SEVEN SEGMENT DISPLAY INTERFACING
DELAY:
MOV R0, #08H
LP2: MOV R1, #0FFH
LP1: MOV R2, #0FFH
LP3: DJNZ R2, LP3
DJNZ R1, LP1
DJNZ R0, LP2
RET
ORG 200H
DB 76H, 79H, 38H, 38H, 3FH ; Lookup table for alphabets
LCD INTERFACING
LCD is finding widespread use replacing LEDs
The declining prices of LCD
The ability to display numbers, characters, and graphics
Incorporation of a refreshing controller into the LCD,
thereby relieving the CPU of the task of refreshing the
LCD
Ease of programming for characters and graphics.
6x2 LCD is one of the most used display unit.
16x2 LCD means that there are two rows in which 16
characters can be displayed per line, and each character
takes 5X7 matrix space on LCD.
Pin Descriptions for LCD
used by the
LCD to latch
information
-Send displayed
presented to
information or
its data bus
instruction
command codes
to the LCD
-Read the
contents of the
internal
registers
LCD Command
Codes
+5V
8051 VCC
P1.0 D0
VEE 10k
POT
LCD
P1.7 D7 VSS
RS R/W E
P2.0
P2.1
P2.2
16 x 2 LCD 80 81 82 83 84 85 86 through 8F
C0 C1 C2 C3 C4 C5 C6 through CF
LCD INTERFACING
Sending Data/ Commands to LCDs w/ Time Delay
To send any of the commands to the LCD, make pin RS=0. For data, make RS=1. Then send a
high-to-low pulse to the E pin to enable the internal latch of the LCD. This is shown in the code
below.
;calls a time delay before sending next data/command
;P1.0-P1.7 are connected to LCD data pins D0-D7
;P2.0 is connected to RS pin of LCD
;P2.1 is connected to R/W pin of LCD
;P2.2 is connected to E pin of LCD
ORG 0H
MOV A,#38H ;INIT. LCD 2 LINES, 5X7 MATRIX
ACALL COMNWRT ;call command subroutine
ACALL DELAY ;give LCD some time
MOV A,#0EH ;display on, cursor blinking
ACALL COMNWRT ;call command subroutine
ACALL DELAY ;give LCD some time
MOV A,#01H ;clear LCD
ACALL COMNWRT ;call command subroutine
ACALL DELAY ;give LCD some time
MOV A,#06H ;shift cursor right
ACALL COMNWRT ;call command subroutine
ACALL DELAY ;give LCD some time
MOV A,#84H ;cursor at line 1, pos. 4
ACALL COMNWRT ;call command subroutine
ACALL DELAY ;give LCD some time
;display letter N
ACALL DATAWRT ;call display subroutine
ACALL DELAY ;give LCD some time
;display letter O
ACALL DATAWRT ;call display subroutine
AGAIN: SJMP AGAIN ;stay here
COMNWRT: ;send command to LCD
MOV P1,A ;copy reg A to port 1
CLR P2.0 ;RS=0 for command
CLR P2.1 ;R/W=0 for write
SETB P2.2 ;E=1 for high pulse ;it must be 450ns wide
ACALL DELAY ;give LCD some time
CLR P2.2 ;E=0 for H-to-L pulse
RET
DATAWRT: ;write data to LCD
MOV P1,A ;copy reg A to port 1
SETB P2.0 ;RS=1 for data
CLR P2.1 ;R/W=0 for write
SETB P2.2 ;E=1 for high pulse ;it must be 450ns wide
ACALL DELAY ;give LCD some time
CLR P2.2 ;E=0 for H-to-L pulse
RET
DELAY: MOV R3,#50 ;50 or higher for fast CPUs
HERE2: MOV R4,#255 ;R4 = 255
HERE: DJNZ R4,HERE ;stay until R4 becomes 0
DJNZ R3,HERE2
RET
END
;Check busy flag before sending data, command to LCD
;p1=data pin
;P2.0 connected to RS pin
;P2.1 connected to R/W pin
;P2.2 connected to E pin
ORG 0H
MOV A,#38H ;init. LCD 2 lines ,5x7 matrix
ACALL COMMAND ;issue command
MOV A,#0EH ;LCD on, cursor on
ACALL COMMAND ;issue command
MOV A,#01H ;clear LCD command
ACALL COMMAND ;issue command
MOV A,#06H ;shift cursor right
ACALL COMMAND ;issue command
MOV A,#84H ;cursor: line 1, pos. 6
ACALL COMMAND ;command subroutine
MOV ;display letter N
ACALL DATA_DISPLAY
MOV ;display letter O
ACALL DATA_DISPLAY
HERE:SJMP HERE ;STAY HERE
.....
.....
COMMAND:
ACALL READY ;is LCD ready?
MOV P1,A ;issue command code
CLR P2.0 ;RS=0 for command
CLR P2.1 ;R/W=0 to write to LCD
SETB P2.2 ;E=1 for H-to-L pulse
CLR P2.2 ;E=0,latch in
RET
DATA_DISPLAY:
ACALL READY ;is LCD ready?
MOV P1,A ;issue data
SETB P2.0 ;RS=1 for data
CLR P2.1 ;R/W =0 to write to LCD
SETB P2.2 ;E=1 for H-to-L pulse
CLR P2.2 ;E=0,latch in
RET To read the command register, we make
READY: R/W=1, RS=0, and a H-to-L pulse for the E pin.
SETB P1.7 ;make P1.7 input port
CLR P2.0 ;RS=0 access command reg
SETB P2.1 ;R/W=1 read command reg
;read command reg and check busy flag
BACK:SETB P2.2 ;E=1 for H-to-L pulse
CLR P2.2 ;E=0 H-to-L pulse
JB P1.7,BACK ;stay until busy flag=0
RET If bit 7 (busy flag) is high, the LCD is
END
busy and no information should be issued
to it.
Keyboard Interfacing
❑ Keyboards are organized in a matrix of rows and
columns
➢ The CPU accesses both rows and columns through ports
KEYBOARD
▪ Therefore, with two 8-bit ports, an 8 x 8 matrix of keys can be
INTERFACING 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
KEYBOARD ❑ It is the function of the microcontroller to scan the keyboard
INTERFACING continuously to detect and identify the key pressed
Grounding ❑ To detect a pressed key, the microcontroller grounds all rows by
Rows and providing 0 to the output latch, then it reads the columns
Reading
➢ If the data read from columns is D3 – D0 = 1111, no key has
Columns
been pressed and the process continues till key press is
detected
➢ If one of the column bits has a zero, this means that a key press
has occurred
▪ For example, if D3 – D0 = 1101, this means that a key in
the D1 column has been pressed
▪ After detecting a key press, microcontroller will go through
the process of identifying the key
❑ Starting with the top row, the microcontroller grounds it by
KEYBOARD
providing a low to row D0 only
INTERFACING
➢ It reads the columns, if the data read is all 1s, no key in that row is
activated and the process is moved to the next row
Grounding
Rows and
❑ It grounds the next row, reads the columns, and
Reading checks for any zero
Columns
(cont’) ➢ This process continues until the row is identified
❑ After identification of the row in which the key has
been pressed
➢ Find out which column the pressed key belongs to
Contents
• ADC
• DAC
DAC Interfacing
D/A Converter Circuits
• Binary-weighted registers
b1b2 ...bN
VREF V V
iO b1 REF b2 ... NREF
1
bN
R 2R 2 R vO iO R f
Example:
In order to generate a stair-step ramp, set up the circuit in Figure 13-18 and
connect the output to an oscilloscope. Then write a program to send data to the
DAC to generate a stair-step ramp.
Solution:
CLR A
AGAIN: MOV P1,A ;send data to DAC
INC A ;count from 0 to FFH
ACALL DELAY ;let DAC recover
SJMP AGAIN
Table: Angle vs. Voltage Magnitude for Sine Wave
Example 13-5
Verify the values given for the following angles: (a) 30° (b) 60°.
Solution:
EOR This is exclusive OR operation. Ex-OR table is followed to make the bitwise 32 bit operation. Ex:
EOR R0,R0,#1;
BIC This is bit clearing operation. This instruction is meant for getting the NOT of RHS (second
operand) and with that result AND with first operand.
Comparison operations
Instruction Description
CMP Compare instruction and the operation of the comparison is carried out by subtracting the two
operands. It does not store the result anywhere, in stead status bits will be set accordingly
based on result of the operation. Ex: CMP R0,R1; Get greater of R1, R0 in R0.
CMN Same as previous. But there will be a negation operation of the second operand (rhs) before
the comparison is being performed. Ex: CMN R1,#1; compare R1 with -1.
TST It is used to perform operand1 AND operand2. It was very similar to logical ANDing.
TEQ It is used to perform Ex-OR operation on the two operands. TEQ <lhs>,<rhs>. One important
use of TEQ is to test if two operands are equal, that too without affecting the state of the C flag
as CMP could do. After such an operation, Z = 1 if operands are equal, or 0 if not.
Ex: TEQ R0,#0; See if R0 = 0
Data Movement Operations
This is the last set of instructions which fall under data processing instructions.
Instruction Description
MOV and MOVS Move instruction moves data from the source to destination. The syntax will be: MOV
<dest>, <source> ; MOV r0, r1; Put R1 into R0
MOVS instruction operate with flags. MOVS r0,r1; Put R1 into R0, setting the flags
MVN MVN instruction moves the logically NOTed value of its operand <RHS> to the register
specified by <dest>
MVN <dest>,<Operand>
MVNS R0,R0; Invert all bits of R0, setting flags.
Conditional Execution
All ARM instructions have the ability of conditional execution.
STR and STRB • Decide what operation has to be done. Whether to read the data from the memory or storing the
data into the memory.
• STR is meant for storing the data into memory. When someone wishes to store the data onto the
memory , the location where the data has to be stored needs to be specified. One can quote the
address in 2 ways:
• Pre-indexed addressing
• Post indexed addressing
Cont.
Instruction Description
Pre-indexed Addressing
Syntax for pre-indexed addressing was:
• STR(cond.) <source>, [<base> {,<offset_value>}]
• Here, <source> is the source which holds the data to be transferred.
• <base> is the register which contains the base address of the memory location required for storage.
• <offset> is a field which is not mandatory. If it is used, then address where data has to be put =
<base>+<offset>
• Ex: STR R0, [R1,#25] - This operation will store the data available in R0 to the address obtained by adding
the base address specified by R1 + 25. Offset need not be positive always.
• STR R0, [R1,#-25] which stores R0 at R1 – 25.
• STR will help in getting the storage operations for words where STRB can be used for performing operation
based on bytes.
Cont.
Instruction Description
Post-indexed addressing:
Here offset would not be added to the base address until the instruction has been executed.
STR {cond.} <srce>, [<base>], <offset>
B It is a simple branching operation where the instruction just a single letter ‘B’. The syntax is:
B <expression> <expression> specified the address within the program to which the control has to be
transferred. Normally labels will be used for this purpose and same would be defined somewhere else in the
code.
B loop: branching to loop and the same can be defined somewhere in the code. .Loop ADD R1, R2, R3
BL • B is having one more variant of its kind. It is nothing but BL which is expanded as Branch with Link. The
instruction has to perform a link operation before the branching has to be performed.
• Current value of the R15 register should be stored in R14 and the branching action can be taken. The
address that ARM core saves in R14 is the address of the instruction that immediately follows the BL.
• After returning from subroutine the content of R14 back to R15 would do the task. MOVS R15, R14. Since ‘S’
is included, the flags will be restored automatically.
• The Syntax of BL: BL <expression>
Barrel Shifting Operation
ARM has no support for the shift instructions. Barrel shifter is used supporting the shifting
operations.
Instruction Description
LSL LSL is expanded as Logical Shift left Immediate. It takes the number of bits to be shifted as the
argument. The syntax is:
LSL #n where, n is the number of bits by which the value is shifted. After n shifts, n bits have
been shifted in on the left side and the carry bit is set to bit 32-n.
X b31 b0
This is before shifting.
b31 b30 b0 0
This was post execution for LSL #1. If the instruction is MOV r3, r4, LSL #2. Assume r3 = 5 and
r4 = 8. This instruction is executed : r3 = r4 * 2^2. Since the value 2 is specified in the LSL it has
been taken as 2 ^2. Similarly if it is LSL #n it will be taken as 2^n.
Cont.
Instruction Description
0 b31 b1 b0
X b31 b1 b0
Stack in ARM
They are LDM and STM. They can be related to PUSH and POP of 8086 processor. LDM and STM are used for
the purpose of storing and retrieving actions.
We know, stack is a memory area which can grow on addition of data to it and will shrink when data popped
out of it.
It works in the principle of LIFO.
When an item is getting added to the stack, SP will point to the new item that has been added.
Once one item is moved out of the stack, SP will point to the previous item itself.
Before PUSHing and POPing two things must be remembered.
When user try to add the item to the stack, it should not be full.
There should be enough space for holding the new entrant.
When POPing, there should be enough data to be popped out i.e. one can’t POP out of empty stack.
Cont.
STM: STM is the instructions which help in pushing items onto the stack.
The syntax of the instruction: STM <type><base>{!}, <registers>
<type> can be one of the four letters which serves as a mark of stack being full or empty with F or E. Similarly
ascending or descending mode of the stack is specified by A or D.
<BASE> register is nothing but the stack pointer.
! Option is also present in the syntax and it will cause the write back.
<registers> will specify the registers which have to be pushed onto stack.
Ex: STMED r13! {r1,r2,r5}
R1,R2 and R5 will be saved and R13 will serve as the SP. E is specified as stack empty.
Ex: STMFD R13,{R1-R15}. Here STM will store all the registers from R1 to R15, without affecting the register
R13.
Some ARM Programs
SBC It is used to subtract two numbers with carry SBC Rd, Rs SBCS Rd, Rd, Rs
STR Store word STR Rd, [Rb, R0] STR Rd, [Rb, R0]
STRB Store Byte STRB Rd, [Rb, R0] STRB Rd, [Rb, R0]
Mnemonic Instruction Syntax Arm-code development.
STRH Store Half-word STRH Rd, [Rb, R0] STRH Rd, [Rb, R0]
SWI Software Interrupt SWI values SWI values
SUB It is used to subtract two SUB Rd, Rs, Rn SUB Rd, Rs, Rn
numbers without carry
TST Test bits TST Rd, Rs TST Rd, Rs
ARM vs THUMB
ARM Code Thumb Code
ARM Divide Thumb divide
;IN: r0 (value), r1 (divisor) ;IN: r0(value), r1 (divisor)
;OUT: r2 (MODULUS), r3 (DIVIDE) ;OUT: r2 (MODulus), r3 (DIVide)
MOV r3, #0 MOV r3, #0
LOOP Loop
SUBS r0, r0, r1 ADD r3, #1
ADDGE r3,r3,#1 SUB r0,r1
BGE Loop BGE loop
ADD r2,r0, r1 SUB r3, #1
ADD r2, r0, r1
Total no of bytes: 20 bytes
Total no of bytes = 12 bytes
How to set THUMB state?
By default THUMB state is in disabled mode.
To get it enable CPSR should be accessed and T bit should be set.
Setting T bit can be done by adding 0x20. It will set the T bit and eventually the THUMB mode will be
set.
AREA RESET, CODE, READONLY
ENTRY
MRS R0, CPSR ;Copying the content of current CPSR to R0
ADD R0, R0, #0X20 ; Adding 0x20 in such a way that T bit will be set
MSR cpsr_c, r0 ;Added result should be moved to CPSR with MSR instruction
B.
Now programmer can check CPSR that T bit is got set.
1. Add the contents of Registers R1,R2 and save the result in to R3.Use
Step execution in keil, write down the contents of various flags of ARM7
$UFKLWHFWXUH
0LFURSURFHVVRU LV GLYLGHG LQWR WZR IXQFWLRQDO XQLW
X (8 ([HFXWLRQ 8QLW DQG
X %,8 %XV ,QWHUIDFH 8QLW
X &RQWDLQV FLUFXLW
SK\VLFDOIRU
DGGUHVV
DQGFDOFXODWLRQ
D SUHGHFRGLQJ
LQVWUXFWLRQ E\WH TXHXH %<7(6 /21*
X 7KH %XV ,QWHUIDFH
JHQHUDWHV
8QLW %,8
WKH ELW SK\VLFDO PHPR
DGGUHVV DQG SURYLGHV WKH LQWHUIDFH ZLWK H[WHUQDO PH
X (8 ([HFXWLRQ 8QLW
X ([HFXWLRQ XQLW JLYHV LQVWUXFWLRQV WR %,8 VWDUWLQJ I
GDWD DQG WKHQ GHFRGH DQG H[HFXWH WKRVH LQVWUXFWLRQV
FRQWURO RSHUDWLRQV RQ GDWD XVLQJ WKH LQVWUXFWLRQ GHF
X (8 KDV QR GLUHFW FRQQHFWLRQ ZLWK V\VWHP EXVHV DV VKRZQ
LW SHUIRUPV RSHUDWLRQV RYHU GDWD WKURXJK %,8
X $OX LV D IXQFWLRQDO XQLW LQ (8 $/8 KDQGOHV DOO DULW
RSHUDWLRQV OLNH î ù 25 $1' 127 RSHUDWLRQV
$UFKLWHFWXUH
%,8 %XV ,QWHUIDFH 8QLW
X %,8 WDNHV FDUH RI DOO GDWD DQG DGGUHVVHV WUDQVIHUV
OLNH VHQGLQJ DGGUHVVHV IHWFKLQJ LQVWUXFWLRQV I
GDWD IURP WKH SRUWV DQG WKH PHPRU\ DV ZHOO DV ZULWL
DQG WKH PHPRU\
X (8 KDV QR GLUHFWLRQ FRQQHFWLRQ ZLWK 6\VWHP %XVHV V
WKH %,8 (8 DQG %,8 DUH FRQQHFWHG ZLWK WKH ,QWHUQDO
X ,W KDV WKH IROORZLQJ IXQFWLRQDO SDUWV î
X ,QVWUXFWLRQ TXHXH î %,8 FRQWDLQV
JHWV
WKHXSWR
LQVWUXFWLRQ
E\WHV RI QH[W LQVWUXFWLRQV DQG VWRUHV
:KHQ
WKHP LQ WKH L
(8 H[HFXWHV LQVWUXFWLRQV DQG LV UHDG\ IRU LWV QH[
VLPSO\ UHDGV WKH LQVWUXFWLRQ IURP WKLV LQVWUXFW
LQFUHDVHG H[HFXWLRQ VSHHG
X )HWFKLQJ WKH QH[W LQVWUXFWLRQ ZKLOH WKH FXUUHQW
FDOOHG SLSHOLQLQJ
$UFKLWHFWXUH
X 7KH %XV ,QWHUIDFH
JHQHUDWHV
8QLW %,8
WKH ELW SK\VLFDO PHPRU
DQG SURYLGHV WKH LQWHUIDFH5205$0
ZLWK H[WHUQDO
PHPRU\
X KDV D VLQJOH PHPRU\ LQWHUIDFH 7R VSHHG XS WKH
LQVWUXFWLRQ DUH IHWFKHG LQ DGYDQFH DQG NHSW LQ D
ZKLOH RWKHU LQVWUXFWLRQV DUH EHLQJ H[HFXWHG LQ WKH
X +HQFH DIWHU WKH H[HFXWLRQ RI DQ LQVWUXFWLRQ WKH QH
IHWFKHG IURP WKH LQVWUXFWLRQ TXHXH ZLWKRXW KDYLQJ
PHPRU\ WR VHQG WKH LQVWUXFWLRQ
X 7KLV LV SLSHOLQLQJ
FDOOHG
DQG LV KHOSIXO IRU VSHHGLQJ XS WKH
H[HFXWLRQ SURFHVV
X V %,8 SURGXFHV
ELW SK\VLFDO
WKH PHPRU\ DGGUHVV E\ FRPE
ELW VHJPHQW DGGUHVV ZLWKD ELW RIIVHW DGGUHVV
$UFKLWHFWXUH 36: )ODJ
$UFKLWHFWXUH )ODJ5HJLV
)ODJ5HJLVWHU
X ,WLVDELWUHJLVWHUWKDWEHKDYHVOLNHDIOLSI
DFFRUGLQJWRWKHUHVXOWVWRUHGLQWKHDFFXPXODWRU
X ,WKDVIODJVDQGWKH\DUHGLYLGHGLQWRJURXSV
&RQGLWLRQDO)ODJVDQG&RQWU
)ODJV
&RQGLWLRQDO)ODJV
X ,WUHSUHVHQWVWKHUHVXOWRIWKHODVWDULWKPHWLFRU
)ROORZLQJLVWKHOLVWRIFRQGLWLRQDOIODJVî
X &DUU\IODJ
î7KLVIODJLQGLFDWHVDQRYHUIORZFRQGLWLRQI
$UFKLWHFWXUH )ODJ5HJL
X $X[LOLDU\
î :KHQ
IODJDQ RSHUDWLRQ LV SHUIRUPHG DW $/8
D FDUU\EDUURZ IURP ORZHU QLEEOH LH ' ² '
' ² ' WKHQ WKLV IODJ LV VHW LH FDUU\ JLYH
IODJ 7KH SURFHVVRU XVHV WKLV IODJ WR SHUIR
FRQYHUVLRQ
X 3DULW\î IODJ
7KLV IODJ LV XVHG WR LQGLFDWH WKH SDULW\
ZKHQ WKH ORZHU RUGHU ELWV RI WKH UHVXOW FRQW
·V WKHQ WKH 3DULW\ )ODJ LV VHW )RU RGG QXPEHU
LV UHVHW
X =HUR IODJ
î 7KLV IODJ LV VHW WR ZKHQ WKH UHVXOW RI
ORJLFDO RSHUDWLRQ LV ]HUR HOVH LW LV VHW WR
X 6LJQ î
IODJ
7KLV IODJ KROGV WKH VLJQ RI WKH UHVXOW L
RI WKH RSHUDWLRQ LV QHJDWLYH WKHQ WKH VLJQ IOD
X 2YHUIORZ
î IODJ
7KLV IODJ UHSUHVHQWV WKH UHVXOW ZKH
FDSDFLW\ LV H[FHHGHG
$UFKLWHFWXUH )ODJ5HJLV
&RQWURO )ODJV
X &RQWURO IODJV FRQWUROV WKH RSHUDWLRQV RI WKH H[HF
WKH OLVW RI FRQWURO IODJV î
X 7UDS IODJ
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GLVSODFHPHQW ZLWK VXP RI FRQWHQWV RI DQ\ RQH RI WKH EDVH
DQ\ RQH RI WKH LQGH[ UHJLVWHUV LQ D GHIDXOW VHJPHQW
X (J 029 $;+ >%;@> 6,@
X + LV GLVSODFHPHQW %; LV EDVH UHJLVWHU 6, LV LQGH[ UH
+ '6^%;@>6,@+
UHJLVWHURUJDQL]DWLRQ
3RLQWHUVDQG,QGH[LQJUHJL
UHJLVWHURUJDQL]DWLR
X 6HJPHQW UHJLVWHU
î %,8 KDV VHJPHQW EXVHV LH &6 '6 66 (6
DGGUHVVHV RI LQVWUXFWLRQV DQG GDWD LQ PHPRU\ ZKLFK DU
WR DFFHVV PHPRU\
,W
ORFDWLRQV
DOVR FRQWDLQV ,3
SRLQWHU
ZKLFK KROGV
UHJLVWHU
WKH DGGUHVV RI WKH QH[W LQVWUXFWLRQ WR H[HFXWHG E\ WKH (
X &6
î ,W VWDQGV IRU &RGH 6HJPHQW
DGGUHVVLQJ
,W LVDXVHG
PHPRU\
IRUORFDWLRQ L
WKH FRGH VHJPHQW
RI WKH PHPRU\ ZKHUH WKH H[HFXWDEOH SURJUDP L
X '6
î ,W VWDQGV IRU 'DWD 6HJPHQW
GDWD XVHG
,W E\
FRQVLVWV
WKH SURJUDP
RI DQG LV
DFFHVVHG LQ WKH GDWD
E\ DQVHJPHQW
RIIVHW DGGUHVV RU WKH FRQWHQW RI
UHJLVWHU WKDW KROGV WKH RIIVHW DGGUHVV
X 66î ,W VWDQGV IRU 6WDFN 6HJPHQW
PHPRU\ WR
,WVWRUH
KDQGOHV
GDWD DQG
DGGUHVVHV GXULQJ H[HFXWLRQ
X (6
î ,W VWDQGV IRU ([WUD 6HJPHQW
GDWD VHJPHQW
(6 LV DGGLWLRQDO
ZKLFK LV XVHG
E\ WKH VWULQJ WR KROG WKH H[WUD GHVWLQDWLRQ GDWD
X ,QVWUXFWLRQ SRLQWHU ,3 î ,W LVWKH
D ELW
DGGUHVVUHJLVWHU
RI WKH XVH
QH[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do we need more number systems?
• Humans understand decimal
• Since computers have 32, 64, and even 128 bit busses, displaying
numbers in binary is cumbersome.
Decimal10
0123456789
Weighted
Multiplication
Successive
Division
Successive Weighted
Division Multiplication
Hexadecimal16
0123456789ABCDEF
Binary2
01
2
Counting . . . 2, 8, 10, 16
Decimal Binary Octal Hexadecimal
0 00000 0 0
1 00001 1 1
2 00010 2 2
3 00011 3 3
4 00100 4 4
5 00101 5 5
6 00110 6 6
7 00111 7 7
8 01000 10 8
9 01001 11 9
10 01010 12 A
11 01011 13 B
12 01100 14 C
13 01101 15 D
14 01110 16 E
15 01111 17 F
16 10000 20 10
17 10001 21 11
18 10010 22 12 3
19 10011 23 13
Decimal ↔ Hexadecimal Conversion
The Process: Successive Division
• Divide the decimal number by 16; the remainder is the LSB of the
hexadecimal number.
• If the quotation is zero, the conversion is complete. Otherwise
repeat step (a) using the quotation as the decimal number. The
new remainder is the next most significant bit of the hexadecimal
number.
Example:
Convert the decimal number 9410 into its hexadecimal equivalent.
5
16 94 r E LSB
0
16 5 r 5 MSB
9410 = 5E16
4
Example: Dec → Hex
Example:
Convert the decimal number 42910 into its hexadecimal equivalent.
5
Example: Dec → Hex
Example:
Convert the decimal number 42910 into its hexadecimal equivalent.
Solution:
26
16 429 r D (13) LSB
1
16 26 r A (10) 42910 = 1AD16 = 1ADH
0
16 1 r 1 MSB
6
Hexadecimal ↔ Decimal Process
The Process: Weighted Multiplication
• Multiply each bit of the hexadecimal number by its corresponding
bit-weighting factor (i.e., Bit-0→160=1; Bit-1→161=16; Bit-
2→162=256; etc.).
• Sum up all of the products in step (a) to get the decimal number.
Example:
Convert the octal number 5E16 into its decimal equivalent.
5 E
161 160 5E 16 = 9410
Bit-Weighting
16 1 Factors
80 + 14 = 9410
7
Example: Hex → Dec
Example:
Convert the hexadecimal number B2EH into its decimal equivalent.
8
Example: Hex → Dec
Example:
Convert the hexadecimal number B2EH into its decimal equivalent.
Solution:
B 2 E
162 161 160
256 16 1
B2EH = 286210
2816 + 32 + 14 = 286210
9
Hex Decimal Addition
10
Hex Decimal Subtraction
11
Module:1
Overview of microprocessor
Module outline
• What is a microprocessor
• Block diagram of a computer system
• Nibble, byte, word and longword
• Internal structure and basic operation of a microprocessor
• Bus system: data bus, address bus and control bus.
• History of microprocessors : 4, 8, 16, 32 and 64 byte
2
What is a microprocessor?
ANY OF A TYPE OF MINIATURE ELECTRONIC DEVICE THAT CONTAINS THE
ARITHMETIC, LOGIC, AND CONTROL CIRCUITRY NECESSARY TO PERFORM
THE FUNCTIONS OF A MICRO COMPUTER'S CENTRAL PROCESSING UNIT
MICROPROCESSOR
MICRO +PROCESSOR
CPU OF A MICROCOMPUTER
3
DEFINITION OF MICROPROCESSOR
⚫ Microcomputer:-
It is a programmable machine.
The main characteristics of a computer are:
• Responds to a specific set of instructions in a well-defined manner
• Its main components are CPU, Input & Output devices & Memory
⚫ Microprocessor:-
It is a programmable VLSI chip which includes ALU, register circuits & control
circuits. Its main units are-
• ALU
• Registers
• Control Unit
⚫ Microcontroller:-
Silicon chip which includes microprocessor, memory & I/O in a single package
4
Microprocessor • Microprocessor (μp) – silicon chip which includes
ALU, register circuits & control circuits
and
• Microcontroller (μc) – silicon chip which includes
Microcontroller microprocessor, memory & I/O in a single package.
Microcomputer
Microprocessor Microcontroller
DIAGRAM OF A COMPUTER SYSTEM
Address bus
7
Basic components of a microcomputer
1. CPU - Central Processing Unit
• The portion of a computer system that carries out the
instructions of a computer program
• The primary element carrying out the computer's functions.
• It is the unit that reads and executes program.
• Program: Set of instructions required to perform some
desired operations.
• The data in the instruction tells the processor what to do.
9
3. I/O Unit
10
DATA SIZE
Nibble 4 bit
Byte 8 bit
Word 16 bit
11
Binary Information
Representation
12
Internal structure and basic operation of
microprocessor
13
Arithmetic and logic unit (ALU)
• The component that performs the arithmetic and logical operations
• The most important components in a microprocessor, and is
typically the part of the processor that is designed first.
• Able to perform the basic logical operations (AND, OR), including
the addition operation.
14
Control unit
• The circuitry that controls the flow of information through the
processor, and coordinates the activities of the other units within
it.
• In a way, it is the "brain within the brain", as it controls what
happens inside the processor, which in turn controls the rest of the
PC.
• On a regular processor, the control unit performs the tasks of
fetching, decoding, managing execution and then storing results.
15
Register sets
• The register section/array consists completely of circuitry used to
temporarily store data or program codes until they are sent to
the ALU or to the control section or to memory.
• The number of registers are different for any particular CPU and the
more register a CPU have will result in easier programming tasks.
16
System Bus
• Microprocessor Unit(MPU) communicates with Memory and I/O
using the System Bus
• Address bus
• Unidirectional
• Memory and I/O Addresses
• Data bus
• Bidirectional
• Transfers Binary Data and Instructions
• Control bus
• Read and Write timing signals
Data bus
• The data bus is 'bi-directional’
• Data or instruction codes from memory or input/output
are transferred into the microprocessor
• The result of an operation or computation is sent out
from the microprocessor to the memory or input/output.
• Depending on the particular microprocessor, the data bus can handle
8 bit or 16 bit data.
18
Address bus
• The address bus is 'unidirectional', over which the microprocessor
sends an address code to the memory or input/output.
• The size (width) of the address bus is specified by the number of bits
it can handle.
• The more bits there are in the address bus, the more memory
locations a microprocessor can access.
• A 16 bit address bus is capable of addressing 65,536 (64K)
addresses.
19
Control bus
• The control bus is used by the microprocessor to send out or receive
timing and control signals in order to coordinate and regulate its
operation and to communicate with other devices, i.e. memory or
input/output.
20
Timing Circuit: Micro processor clock
21
HISTORY OF MICROPROCESSORS
22
Contents
Introduction
4-Bit Microprocessors
8-Bit Microprocessors
16-Bit Microprocessors
32-Bit Microprocessors
64-Bit Microprocessors
Introduction
Introduced in 1971.
Introduced in 1974.
It was also 4-bit µP.
26
8-bit Microprocessors
Intel 8008
Introduced in 1972.
27
Intel 8080
Introduced in 1974.
It was also 8-bit µP.
Its clock speed was 2 MHz.
It had 6,000 transistors.
Was 10 times faster than 8008.
Could execute 5,00,000
instructions per second.
28
Intel 8085
Introduced in 1976.
It was also 8-bit µP.
Its clock speed was 3 MHz.
Its data bus is 8-bit and address bus
is 16-bit.
It had 6,500 transistors.
Could execute 7,69,230 instructions
per second.
It could access 64 KB of memory.
It had 246 instructions.
Over 100 million copies were sold.
29
16-bit Microprocessors
Introduced in 1978.
Intel 8086 It was first 16-bit µP.
31
Intel 80186 & 80188
Introduced in 1982.
They were 16-bit µPs.
Clock speed was 6 MHz.
80188 was a cheaper version
of 80186 with an 8-bit
external data bus.
They had additional
components like:
Interrupt Controller
Clock Generator
Local Bus Controller
Counters
32
Intel 80286
Introduced in 1982.
It was 16-bit µP.
Its clock speed was 8 MHz.
Its data bus is 16-bit and
address bus is 24-bit.
It could address 16 MB of
memory.
It had 1,34,000 transistors.
It could execute 4 million
instructions per second.
33
32-bit Microprocessors
Introduced in 1986.
Intel 80386 It was first 32-bit µP.
8 KB for data.
Intel Pentium Pro
Introduced in 1995.
It was also 32-bit µP.
It had L2 cache of 256 KB.
It had 21 million transistors.
It was primarily used in server
systems.
Cache memory:
8 KB for instructions.
8 KB for data.
Introduced in 1997.
It was also 32-bit µP.
Its clock speed was 233 MHz to
500 MHz.
Could execute 333 million
instructions per second.
MMX technology was supported.
L2 cache & processor were on
one circuit.
38
Intel Pentium II Xeon
Introduced in 1998.
Introduced in 1999.
It was also 32-bit µP.
Its clock speed varied
from 500 MHz to 1.4 GHz.
It had 9.5 million
transistors.
40
Intel Pentium IV
Introduced in 2000.
It is a 64-bit µP.
Its clock speed is from 1.2 GHz
to 3 GHz.
It has 291 million transistors.
It has 64 KB of L1 cache per
core and 4 MB of L2 cache.
It is launched in three different
versions:
Intel Core 2 Duo
Intel Core 2 Quad
Intel Core 2 Extreme
43
Intel Core i7
Introduced in 2008.
It is a 64-bit µP.
It has 4 physical cores.
Its clock speed is from 2.66 GHz
to 3.33 GHz.
It has 781 million transistors.
It has 64 KB of L1 cache per
core, 256 KB of L2 cache and 8
MB of L3 cache.
44
Intel Core i5
Introduced in 2009.
It is a 64-bit µP.
It has 4 physical cores.
Its clock speed is from 2.40
GHz to 3.60 GHz.
It has 781 million transistors.
It has 64 KB of L1 cache per
core, 256 KB of L2 cache and
8 MB of L3 cache.
45
Intel Core i3
Introduced in 2010.
It is a 64-bit µP.
It has 2 physical cores.
Its clock speed is from 2.93GHz to
3.33 GHz.
It has 781 million transistors.
It has 64 KB of L1 cache per core,
512 KB of L2 cache and 4 MB of
L3 cache.
46
Microcontroller
The microcontroller like 8051 was designed in the year 1981 by Intel.
Its foundation was on Harvard Architecture and was developed principally for bringing into play Embedded
Systems.
An Embedded system product is used to do only one task, or we can say these are the application specific
processors with memory, I/O, Timers, buses etc. on one single chip.
At first, it was created using NMOS technology but as NMOS technology needs more power to function therefore
Intel re-intended Microcontroller 8051 employing CMOS technology and a new edition came into existence with a
letter ‘C’ in the title name, for illustration: 80C51.
The most common embedded systems that we use from day to day life are Air Conditioner, Touch Screen, Anti-
Break System, Biometric Attendance System, Face Sensors etc.
1
Microprocessor Microcontroller
Key Differences Heart/Brain of the Computer System. Heart/Brain of the embedded system.
Microcontroller
The circuit is very small.
usage.
Memories like RAM and ROM are absent. Carries RAM, ROM, etc.
Example: DEC Alpha 21164, IBM RS6000, Example: Intel 8051, PIC 16x, Zilog’s Z8,
etc. Freescale’s 6811 etc.
2
Some Embedded products using 3
Microcontroller
Security systems Laser printer Garage door openers
Intel www.intel.com/design/mcs51
Atmel www.atmel.com
Philips/Signetics www.semiconductors.philips.com
Infineon www.Infineon.com
4
How to choose a microcontroller ?
5
The 8051 Microcontroller
A harvard Single chip
architecture (separate microcontroller (µc)
instruction/data
memories)
• ROM - 4K Byte
• RAM – 128 Byte
• I/O Pins – 32
• Interrupt Sources – 6
Block Diagram of 8051 • Timers – 2
• Serial port - 1
6
8051 microcontroller is designed by Intel in 1981.
It is an 8-bit microcontroller.
7
Comparison of the 8051 Family Members
Feature 8051 8052 8031
Timers 2 3 2
I/O pins 32 32 32
Serial port 1 1 1
Interrupt sources 6 8 6
8
Pin Diagram of
8051
9
Explanation of the Pins
- Pin 1 to Pin 8 (Port – 1) – Pin 1 to Pin 8 is assigned to Port 1 for simple I/O
operations. It is a bidirectional port.
- Pin 9 (RST) – It is a reset input Pin, which is used to reset the 8051
- Pin 10 to Pin 17 (Port-3) – Pin 10 to Pin 17 are assigned to Port 3. This port is
also a bidirectional I/O port like port 1. This port performs some special functions
like interrupts, control signals, timer input, serial communication etc.
- Pin 18 and Pin 19 (XTAL2 And XTAL1) –Pins 18 and 19 i.e. XTAL 2 and
XTAL 1 are the pins for interfacing external oscillator. Mostly, a Quartz Crystal
Oscillator is connected here to get the system clock.
10
Explanation of the Pins
- Pin 21 to Pin28 (Port 2) – Pin 21 to pin 28 are port 2 pins. This port is also a bidirectional I/O port. But, this
is only possible when we are not using any external memory. If we use external memory, then these pins will
work as high order address bus (A8 to A15).
- Pin 29 (PSEN) – The Pin 29 is the Program Store Enable Pin (PSEN). It is used to enable external program
memory and read a signal from the external program memory.
- Pin 30 (ALE) – Pin 30 is the Address Latch Enable Pin. This pin is used to enable or disable the external
memory interfacing.
- Pin 31 (EA) – Pin 31 is the External Access Enable (EA) Pin. This pin allows external Program Memory. It is
an input pin and connected from VCC or GND. If we want to access the program from external program
memory, it must be connected with GND. If we want to use on-chip memory, it must be high (connected with
VCC).
- Pin 32 to Pin 39 (Port 0) – Pin 32 to Pin 39 are Port 0 pins. when we don’t use any external memory, these
pins are used as a bidirectional pin like port 2 and port 3. But, when ALE or Pin 30 is at 1, then this port is
used as data bus. And when the ALE pin is at 0, then this port is used as a lower order address bus (A0 to A7).
- Pin 40 (VCC) – This pin is used to provide (+5V ) power supply to the 8051 microcontroller circuit.
11
12
- Accumulator (A register) – The Accumulator, as its name suggests, is 13
used as a general register to accumulate the results of many instructions. It
can hold an 8-bit (1-byte) value and is the most versatile register the 8051.
This register is mostly used for arithmetic operations. The accumulator
register (A or ACC) act as an operand register, in case of some
instructions.
- B register – This is an 8-bit register that is bit addressable and is used for
two instructions only like MUL AB and DIV AB.
• MOV A,#55H
• MOV R0,A MOV A,#55H
• MOV R1,#34H
MOV R0,#46H
• MOV A,R1
• MOV R2,#20 ADD A,R0
• MOV A,R2
Module 3, Microprocessor & Microcontroller, SENSE VIT Dr. Shelja, Assistant Professor
- PSW register – Program Status Word (PSW) is the flag 14
● CY, the carry flag: This flag is set whenever there is a carry out from
the D7 bit. This flag bit is affected after an 8-bit addition or subtraction.
It can also be set to 1 or 0 directly by an instruction such as “SETB C”
and “CLR C” where “SETB C” stands for “set bit carry” and “CLR C”
for “clear arry”.
● P, the parity flag: The parity flag reflects the number of 1s in the A
(accumulator) register only. If the A register contains an odd number of
1s, then P = 1, Otherwise, P = 0 if A has an even number of 1s.
● OV, the overflow flag: This flag is set whenever the result of a signed
number operation is too large, causing the high-order bit to overflow
into the sign bit. In general, The overflow flag is only used to detect
errors in signed arithmetic operations.
Note: X can be 0 or 1.
15
Example 1
Show the status of the CY, AC, and P flags after the addition of 38H and 2FH in the following
instructions.
MOV A,#38H
ADD A,#2FH ;after the addition A=67H, CY=0
Solution:
38 00111000
+ 2F 00101111
67 01100111
Show the status of the CY, AC, and P flags after the addition of 9CH and 64H in the following
instructions.
MOV A,#9CH
ADD A,#64H ;after addition A=00 and CY=1
Solution:
9C 10011100
+ 64 01100100
100 00000000
Memory Organization
ROM Memory
Map in the
8051 Family
Program Memory (ROM) is used
for permanent saving program
(CODE) being executed.
8051 memory organization
allows external program memory
to be added. memory type How
does the microcontroller handle
external memory depends on
the pin EA logical state.
RAM
ADD A, Rn A = A + Rn 1
ADD A, Rx A = A + Rx 2
ADD A, @ Ri A = A+ @Ri 1
Arithmetic Instructions
ADD A, # X A = A + Byte 2
ADDC A, Rn A = A + Rn + C 1
ADDC A , Rx A = A + Rx + C 2
Arithmetic Instructions
ADDC A, @ Ri A = A + Ri + C 1
ADDC A, # X A = A + Byte + C 2
SUBB A, Rn A = A – Rn – 1 1
Arithmetic Instructions
SUBB A, Rx A = A – Rx – 1 2
SUBB A, @ Ri A = A – Ri – 1 1
SUBB A, # X A = A – Byte – 1 2
Arithmetic Instructions
INC A A=A+1 1
INC Rn Rn = Rn + 1 1
INC Rx Rx = Rx + 1 2
Arithmetic Instructions
INC @ Ri Ri = Ri + 1 1
DEC A A=A–1 1
DEC Rn Rn = Rn – 1 1
Arithmetic Instructions
DEC Rx Rx = Rx – 1 2
DEC @ Ri Ri = Ri – 1 1
MUL AB B:A = A * B 1
DIV AB A = [A/B] 1
DA A Decimal adjustment of 1
accumulator according to BCD code
Logical Instructions
RL A (An + 1) (An) 1
(A0) (A7)
RLC (An + 1) (An) 1
(A0) (C)
(C) (A7)
Logical Instructions
RR A (An) (An + 1) 1
(A7) (A0)
CLR C (C=0) 1
SETB C (C=1) 1
Logical Instructions On Bits
CPL C (1 = 0, 0 = 1) 1
NOP No operation 1
Example 3-1: Looping in the 8051
Write a program to
(a) clear ACC, then
(b) add 3 to the accumulator ten times.
Solution:
What is the maximum number of times that the loop in Example 3-1 can be repeated?
Solution:
Write a program to (a) load the accumulator with the value 55H, and (b) complement the ACC 700
times.
Solution:
Since 700 is larger than 255 (the maximum capacity of any register), we use two registers to hold
the count. The following code shows how to use R2 and R3 for the count.
MOV A,#55H ;A=55H
MOV R3,#10 ;R3=10, the outer loop count
NEXT: MOV R2,#70 ;R2=70, the inner loop count
AGAIN: CPL A ;complement A register
DJNZ R2,AGAIN ;repeat it 70 times (inner loop)
DJNZ R3,NEXT
In this program, R2 is used to keep the inner loop count. In the instruction “DJNZ R2,AGAIN”,
whenever R2 becomes 0 it falls through and “DJNZ R3,NEXT” is executed. This instruction forces
the CPU to load R2 with the count 70 and the inner loop starts again. This process will continue
until R3 becomes zero and the outer loop is finished.
Table 3-1: 8051 Conditional Jump Instructions
Example 3-4
Write a program to determine if R5 contains the value 0. If so, put 55H in it.
Solution:
Find the sum of the values 79H, F5H, and E2H. Put the sum in registers R0 (low byte) and R5 (high
byte).
Solution:
MOV A,#0 ;clear A(A=0)
MOV R5,A ;clear R5
ADD A,#79H ;A=0+79H=79H
JNC N_1 ;if no carry, add next number
INC R5 ;if CY=1, increment R5
N_1: ADD A,#0F5H ;A=79+F5=6E and CY=1
JNC N_2 ;jump if CY=0
INC R5 ;If CY=1 then increment R5(R5=1)
N_2: ADD A,#0E2H ;A=6E+E2=50 and CY=1
JNC OVER ;jump if CY=0
INC R5 ;if CY=1, increment 5
OVER: MOV R0,A ;Now R0=50H, and R5=02
Example 3-6
Using the following list file, verify the jump forward address calculation.
Line PC Opcode Mnemonic Operand
01 0000 ORG 0000
02 0000 7800 MOV R0,#0
03 0002 7455 MOV A,#55H
04 0004 6003 JZ NEXT
05 0006 08 INC R0
06 0007 04 AGAIN: INC A
07 0008 04 INC A
08 0009 2477 NEXT: ADD A,#77h
09 000B 5005 JNC OVER
10 000D E4 CLR A
11 000E F8 MOV R0,A
12 000F F9 MOV R1,A
13 0010 FA MOV R2,A
14 0011 FB MOV R3,A
15 0012 2B OVER: ADD A,R3
16 0013 50F2 JNC AGAIN
17 0015 80FE HERE: SJMP HERE
18 0017 END
Example 3-6
Solution:
First notice that the JZ and JNC instructions both jump forward. The target
address for a forward jump is calculated by adding the PC of the following
instruction to the second byte of the short jump instruction, which is called the
relative address. In line 4 the instruction “JZ NEXT” has opcode of 60 and
operand of 03 at the addresses of 0004 and 0005. The 03 is the relative address,
relative to the address of the next instruction INC R0, which is 0006. By adding
0006 to 3, the target address of the label NEXT, which is 0009, is generated. In
the same way for line 9, the “JNC OVER” instruction has opcode and operand of
50 and 05 where 50 is the opcode and 05 the relative address. Therefore, 05 is
added to 000D, the address of instruction “CLR A”, giving 12H, the address of
label OVER.
Example 3-7
Solution:
In that program list, “JNC AGAIN” has opcode 50 and relative address F2H.
When the relative address of F2H is added to 15H, the address of the
instruction below the jump, we have 15H + F2H = 07 (the carry is dropped).
Notice that 07 is the address of label AGAIN. Look also at “SJMP HERE”, which
has 80 and FE for the opcode and relative address, respectively. The PC of the
following instruction, 0017H, is added to FEH, the relative address, to get
0015H, address of the HERE label (17H + FEH = 15H). Notice that FEH is -2 and
17H + (-2) = 15H. For further discussion of the addition of negative numbers, see
Chapter 6.
Example 3-8
Write a program to toggle all the bits of port 1 by sending to it the values 55H and AAH continuously.
Put a time delay in between each issuing of data to port 1. This program will be used to test the ports
of the 8051 in the next chapter.
Solution:
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 the delay subroutine
ORG 300H ;put time delay at address 300H
DELAY: MOV R5,#0FFH ;R5 = 255(FF in hex),the counter
AGAIN: DJNZ R5,AGAIN ;stay here until R5 becomes 0
RET ;return to caller (when R5 = 0)
END ;end of asm file
Example 3-9
Analyze the stack contents after the execution of the first LCALL in the following.
Solution:
001 0000 ORG 0
002 0000 7455 BACK: MOV A,#55H ;load A with 55H
003 0002 F590 MOV P1,A ;send 55H to port 1
004 0004 120300 LCALL DELAY ;time delay
005 0007 74AA MOV A,#0AAH ;load A with AAH
006 0009 F590 MOV P1,A ;send AAH to port 1
007 000B 120300 LCALL DELAY
008 000E 80F0 SJMP BACK ;keep doing this
009 0010
010 0010 ;————————this is the delay subroutine
011 0300 ORG 300H
012 0300 DELAY:
013 0300 7DFF MOV R5,#0FFH ;R5=255
014 0302 DDFE AGAIN: DJNZ R5,AGAIN ;stay here
015 0304 22 RET ;return to caller
016 0305 END ;end of asm file
Example 3-10
Analyze the stack for the first LCALL instruction in the following program.
01 0000 ORG 0
02 0000 7455 BACK: MOV A,#55H ;load A with 55H
03 0002 F590 MOV P1,A ;send 55H to port 1
04 0004 7C99 MOV R4,#99H
05 0006 7D67 MOV R5,#67H
06 0008 120300 LCALL DELAY ;time delay
07 000B 74AA MOV A,#0AAH ;load A with AA
08 000D F590 MOV P1,A ;send AAH to port 1
09 000F 120300 LCALL DELAY
10 0012 80EC SJMP BACK ;keep doing this
11 0014 ;————————this is the delay subroutine
12 0300 ORG 300H
13 0300 C004 DELAY: PUSH 4 ;PUSH R4
14 0302 C005 PUSH 5 ;PUSH R5
15 0304 7CFF MOV R4,#0FFH ;R4=FFH
16 0306 7DFF NEXT: MOV R5,#0FFH ;R5=255
17 0308 DDFE AGAIN: DJNZ R5,AGAIN
18 030A DCFA DJNZ R4,NEXT
19 030C D005 POP 5 ;POP into R5
20 030E D004 POP 4 ;POP into R4
21 0310 22 RET ;return to caller
22 0311 END ;end of asm file
Example 3-10
Solution:
First notice that for the PUSH and POP instructions we must specify the direct
address of the register being pushed or popped. Here is the stack frame.
;MAIN program calling subroutines
ORG 0
MAIN: LCALL SUBR_1
LCALL SUBR_2
LCALL SUBR_3
SUBR_3: ....
....
RET
;————————end of subroutine 3
END ;end of the asm file
Example 3-11
A developer is using the Atmel AT89C1051 microcontroller chip for a product. This
chip has only 1K byte of on-chip flash ROM. Which instruction, LCALL or ACALL, is
more useful in programming this chip?
Solution:
The ACALL instruction is more useful since it is a 2-byte instruction. It saves one
byte each time the call instruction is used.
Example 3-12
• TCON and TMOD registers are used for timer/counter control and mode selection.
C/T’ Configure for the Counter operations Configure for the Timer operations
Gate (G) Timer0 or Timer1 will be in RunMode when Timer0 or Timer1 will be in RunMode when
TRX bit of TCON register is high. TRX bit of TCON register is high
and INT0 or INT1 is high.
8-bit timer/counter, 16-bit 8-bit auto reload timer/counter; Split mode, Timer split
with 5-bit pre-scaler. timer/counter THx holds a value which is to be in to two separate 8-bit
(Total 13-bits in use) reloaded into TLx each time it timers.
overflows.
Timers-Counters in 8051 µC – Generating delay using Timers
DELAY generated by timer=(Max.value –Initial value+1)*(Time period of one machine cycle)
• max. value = maximum count value a timer/counter can reach (based on selected mode
in TMOD register)
• Initial value= the value we need to load into timer to the get the required delay.
• +1 is required in the above equation as timer increments one more time (to roll over to
Zero) after reaching its maximum value. After rollover the timer will stop and TF
(timer overflow) flog in TCON (Timer control) register is Set.
NOTE: 8051 timers works at a rate of 1/12 of the CLK (XTAL) frequency (equal
to machine cycle frequency).
• If XTAL frequency is 11.0592MHz, then the delay required/generated for each
count (pulse) is as follows,
(11.0592MHz/12)=926kHz=1.085µsec
Q1: Indicate which mode and which timer are selected for each of the following.
(a) MOV TMOD,#01H (b) MOV TMOD, #20H (c) MOV TMOD, #12H
A1:
(a) TMOD = 00000001, mode 1 of TIMER 0 and mode 0 of Timer 1 are selected.
(b) TMOD = 00100000, mode 2 of TIMER 1 and mode 0 of Timer 1 are selected.
(c) TMOD = 00010010, mode 2 of TIMER 0, and mode 1 of TIMER 1 are selected.
Q2: Find the timer’s clock frequency and it’s period for various 8051 based systems,
with the following crystal frequencies (a) 12MHz (b) 16 MHz (c) 11.0592 MHz
A2:
(a)
(b)
(c)
XTAL Divide by
Oscillator 12
Q3: In the following program, we 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. Analyze the program
Steps to generate a time delay using TIMERs:
1. Load the TMOD value register indicating the which timer is to be used and which
timer mode is selected.
2. Load the registers TL and TH with initial count value
3. Start the Timer
4. Keep monitoring the timer flag (TF) with JNB TFx, target instruction to see if it is raised
Get out of the loop when TF becomes high
5. Stop the timer
6. Clear the TF flag for the next round
7. Go back to Step 2 to load TH and TL again
Q4: Calculate the amount of time delay in the DELAY subroutine by the timer. Assume XTAL =
11.0592MHz.
A4:
• The timer works with a clock frequency and time period of
Solution:
• In mode 2 we do not need to reload TH since it is auto-reload.
• Now, (256 - 05) * 1.0851µs = 272.33 µs in the high portion of the pulse.
• Since it is a 50% duty cycle,
Total time period is = 2* 272.33 µs = 544.67 µs
• The frequency = 1/T = 1.83597 KHz
Q11: Assume 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.
Q12
SERIAL COMMUNICATION in 8051
Unused bits
PCON is an 8-bit special function register (SFR) accommodated in SFR memory area with an
address 87H. It is not bit addressable.
SMOD
This bit if set (1), it will double the baud rate of serial communication.
GF0 and GF1: These are general purpose flag bits.
Idle mode
• When IDL bit of the PCON register is set, the microcontroller turns off the CPU unit while
peripheral units such as serial port, timers and interrupt system continue operating normally.
• In Idle mode, the state of all registers and I/O ports remains unchanged.
• To exit the Idle mode, it is necessary to enable and execute any interrupt or reset.
• It is recommended that, first three instructions to be executed after coming out of Idle mode
are NOP instructions to stabilize the microcontroller and prevents undesired changes on the
I/O ports.
Power control register (PCON) of 8051