Unit 4 - 8051
Unit 4 - 8051
Unit 4 - 8051
8051
Prof. Videet Acharya
videet.acharya@gmail.com
8051 Architecture
The Key features of the 8051 Microcontroller
4 KB on-chip ROM (Program memory).
128 bytes on-chip RAM (Data memory).
The 8-bit data bus (bidirectional).
16-bit address bus (unidirectional).
Two 16-bit timers.
Instruction cycle of 1 microsecond with 12 MHz crystal.
Four 8-bit input/output ports.
128 user-defined flags.
Four register banks of 8 bit each.
16-byte bit-addressable RAM.
The general purpose registers are 32 each is 8-bit.
8051 has two external and three internal interrupts.
8051 microcontroller - some special function features - UARTs, ADC, Op-amp, etc.
It has a 16-bit program counter and data pointer.
8051 Pin Configuration
Pins 1 to 8 − known as Port 1- internally pulled up, bi-directional I/O port
Pin 9 − RESET pin, is used to reset the microcontroller to its initial values
Pins 10 to 17 − known as Port 3, functions like interrupts, timer input, control signals,
serial communication signals RxD and TxD, etc
Pins 18 & 19 − used for interfacing external crystal to get the system clock
Pin 20 − provides the power supply to the circuit(GND)
Pins 21 to 28 − known as Port 2 - I/O port. Higher order address bus signals also
multiplexed using this port
Pin 29 − PSEN - Program Store Enable. used to read signal from ext. program
memory
Pin 30 − EA - External Access input. used to enable/disable ext. memory interfacing
Pin 31 − ALE - Address Latch Enable. used to demultiplex the address-data signal of
port
Pins 32 to 39 − known as Port 0, - I/O port. Lower order address and data bus signals
are multiplexed using this port
Pin 40 − provide power supply to the circuit(VCC=+5V)
Special Function Registers (SFRs)
Special Function Registers act as a control table that monitor and
control the operation of the 8051 Microcontroller.
If you observe in Internal RAM Structure, the Address Space from
80H to FFH is allocated to SFRs
Out of 128 Memory Locations, only 21 locations - assigned to SFRs.
Each SFR has one Byte Address and unique name which specifies its
purpose.
Since SFRs - part of Internal RAM Structure, you can access SFRs as if
you access Internal RAM,
main difference is address space: first 128 Bytes (00H to 7FH) is for
regular Internal RAM and next 128 Bytes (80H to FFH) is for SFRs
As 21 of 128 SFR memory locations are assigned, it is recommended
that remaining registers or memory locations are not accessed.
List of 8051 Microcontroller Special Function Registers
Categories of 8051 Microcontroller Special
Function Registers
Each bit in these SFRs corresponds to one physical Pin in the 8051 Microcontroller.
Port Registers both Bit Addressable and Byte Addressable. Writing 1 or 0 on a Port Register Bit will
reflect as appropriate voltage (5V and 0V) on corresponding Pin.
If Port Bit is SET (1), corresponding Port Pin - Input and similarly if a Port Bit is CLEARED (0), the
corresponding Port Pin - Output. Upon reset, all the Port Bits are SET (1)
PCON (Power Control)
control Microcontroller’s Power Modes, located at 87H
two bits in PCON Register, can be set to Idle Mode and Power Down Mode
PCON register is not bit addressable
Idle Mode, will stop Clock Signal to ALU (CPU) but given to peripherals like Timer, Serial,
Interrupts, etc. to terminate Idle Mode, use Interrupt or Hardware Reset.
Power Down Mode, oscillator stopped and power reduced to 2V. To terminate Power
Down Mode, use Hardware Reset
Apart from these two, PCON Register used for,
SMOD Bit in PCON Register is used to control the Baud Rate of the Serial Port
two general purpose Flag Bits in the PCON Register
Table describes the function of each bit in PCON Register
6–4 —- —
6 — —
5 ET2 Reserved
If set to 1, Serial Port interrupt is enabled. If set to 0,
4 ES Serial Port Interrupt Enable Bit
Serial Port interrupt is disabled.
7 — —
6 — —
5 PT2 Reserved
Port 2
P2 acts similarly to P0 when external memory is used.
Pins occupy addresses intended for external memory chip.
This time it is about the higher address byte with addresses A8-A15.
When no memory is added: used as a general input/output port like P1
Port 3
can be used as general I/O
but they also have an alternative function
to use these alternative functions, logic (1) applied to appropriate bit of P3 register.
In terms of hardware, similar to P0, except that its pins have pull-up resistor built-in.
Current limitations
Immediate Addressing
Register Addressing
Direct Addressing
Register – Indirect Addressing
Indexed Addressing
Immediate Addressing
operand, which follows Opcode, is constant data of either 8 or 16 bits.
The name Immediate Addressing came from the fact that constant data to
be stored in memory immediately follows Opcode.
constant value to be stored is specified in instruction itself rather than taking
from register.
destination register to which the constant data must be copied should be the
same size as operand mentioned in instruction.
Example: MOV A, #030H
Accumulator is loaded with 30 (hexadecimal)
# in operand indicates that it is data and not address of Register.
Immediate Addressing is very fast as data given in instruction itself.
Register Addressing
INC Increment by 1
DEC Decrement by 1
MUL Multiply
DIV Divide
JBC Jump if specified Bit is Set and also clear the Bit
TMP EQU #30 ; Assigns the value #30 to the name TMP
RED_LED EQU P1.0 ; P1.0 is defined as RED_LED
Toggle the LEDs ON and OFF connected to PORT1
C/T Configure for the Counter operations Configure for the Timer operations
Gate (G) Timer0 or Timer1 will be in RunMode Timer0 or Timer1 will be in RunMode
when TRX bit of TCON register is high. when TRX bit of TCON register is high
and INT0 or INT1 is high.
Bit Details 00 01 10 11
Keep monitoring the timer flag (TF) with the “JNB TFx, target” instruction to see
if it is raised. Get out of the loop when TF becomes high.
3. To detect row key pressed belongs to, grounds one row at a time, reading columns
each time.
If all columns are high, means key press does not belong to that row. Therefore, it grounds next
row and continues until it finds row, that key pressed belongs to.
finding row that key pressed belongs to, sets up starting address for lookup table holding scan
codes for that row
4. To identify key pressed, it rotates column bits, one bit at a time, into carry flag and
checks to see if it is low.
Upon finding zero, it pulls out ASCII code for that key from look-up table.
Otherwise, it increments pointer to point to next element of look-up table.
Flowchart for Keyboard Interfacing
PROGRAM:
;keyboard subroutine. This program sends the ASCII code for pressed key to P0.1
;P1.0-P1.3 connected to rows, P2.0-P2.3 to column
MOV P2,#0FFH ;make P2 an input port
K1: MOV P1,#0 ;ground all rows at once
MOV A,P2 ;read all col
;(ensure keys open)
ANL A,00001111B ;masked unused bits
CJNE A,#00001111B,K1 ;till all keys release
K2: ACALL DELAY ;call 20 msec delay
MOV A,P2 ;see if any key is pressed
ANL A,00001111B ;mask unused bits
CJNE A,#00001111B,OVER ;key pressed, find row
SJMP K2 ;check till key pressed
OVER: ACALL DELAY ;wait 20 msec debounce time
MOV A,P2 ;check key closure
ANL A,00001111B ;mask unused bits
CJNE A,#00001111B,OVER1 ;key pressed, find row
SJMP K2 ;if none, keep polling
OVER1: MOV P1, #11111110B ;ground row 0
MOV A,P2 ;read all columns
ANL A,#00001111B ;mask unused bits
CJNE A,#00001111B,ROW_0 ;key row 0, find col.
MOV P1,#11111101B ;ground row 1
MOV A,P2 ;read all columns
ANL A,#00001111B ;mask unused bits
CJNE A,#00001111B,ROW_1 ;key row 1, find col.
MOV P1,#11111011B ;ground row 2
MOV A,P2 ;read all columns
ANL A,#00001111B ;mask unused bits
CJNE A,#00001111B,ROW_2 ;key row 2, find col.
MOV P1,#11110111B ;ground row 3
MOV A,P2 ;read all columns
ANL A,#00001111B ;mask unused bits
CJNE A,#00001111B,ROW_3 ;key row 3, find col.
LJMP K2 ;if none, false input,
ROW_0: MOV DPTR,#KCODE0 ;set DPTR=start of row 0
SJMP FIND ;find col. Key belongs to
ROW_1: MOV DPTR,#KCODE1 ;set DPTR=start of row
SJMP FIND ;find col. Key belongs to
ROW_2: MOV DPTR,#KCODE2 ;set DPTR=start of row 2
SJMP FIND ;find col. Key belongs to
ROW_3: MOV DPTR,#KCODE3 ;set DPTR=start of row 3
FIND: RRC A ;see if any CY bit low
JNC MATCH ;if zero, get ASCII code
INC DPTR ;point to next col. addr
SJMP FIND ;keep searching
MATCH: CLR A ;set A=0 (match is found)
MOVC A,@A+DPTR ;get ASCII from table
MOV P0,A ;display pressed key
LJMP K1
; ASCII LOOK-UP TABLE FOR EACH ROW
ORG 300H
KCODE0: DB ‘0’,’1’,’2’,’3’ ;ROW 0
KCODE1: DB ‘4’,’5’,’6’,’7’ ;ROW 1
KCODE2: DB ‘8’,’9’,’A’,’B’ ;ROW 2
KCODE3: DB ‘C’,’D’,’E’,’F’ ;ROW 3
EXTERNAL MEMORY
INTERFACE
EXTERNAL ROM (PROGRAM MEMORY)
INTERFACING
Port 0 used as multiplexed data & address lines
lower order (A7-A0) - 8 bit address in initial T cycle & higher order (A8-A15) - data bus
8 bit address is latched using external latch & ALE signal from 8051
Port 2 provides higher order (A15-A8) 8 bit address
PSEN used to activate output enable signal of external ROM/EPROM
EXTERNAL RAM (DATA MEMORY) INTERFACING
Port 0 used as multiplexed data & address lines
Address lines - decoded using external latch; ALE signal to provide lower order (A7-A0)
address lines
Port 2 gives higher order address lines
RD & WR signals selects memory read & memory write operations respectively
RD & WR signals: generally P3.6 & P3.7 pins of port 3 used to generate memory read and
memory write signals
Remaining pins of port 3 i.e. P3.0-P3.5 can be used for other functions
Example
Solution:
Given, Memory size- ROM : 16k i.e. we require 2n=16k :: n address lines
Here n=14 :: A0 to A13 address lines are required.
A14,A15,PSEN O Red CS when low – ROM is selected
Memory size- RAM :32k
i.e. we require 2n=32k :: n address lines
Here n=15 :: A0 to A15 address lines are required.
A15 inverted(NOT Gate) CS when high- RAM is selected.
For RAM selection
PSEN is used as chip select pin ROM.
RD is used as read control signal pin
WR is used as write control signal pin
ADC, DAC & Sensor
Interfacing
ANALOG-TO-DIGITAL CONVERTER (ADC)
INTERFACING
ADCs (Analog-to-Digital Converters) are among most widely used devices for data acquisition.
physical quantity, like temperature, pressure, humidity, and velocity, etc., is converted to electrical
(voltage, current) signals using device - transducer or sensor
need Analog-to-Digital Converter to translate analog signals to digital numbers, so microcontroller can
read and process them.
ADC has n-bit resolution where n can be 8, 10, 12, 16 or even 24 bits.
higher-resolution ADC provides smaller step size, where step size is smallest change that can be discerned
by ADC
In addition to resolution, conversion time is another major factor in judging ADC
Conversion time is defined as time it takes ADC to convert the analog input to digital (binary) number
In parallel ADC, we have 8 of more pins dedicated to bringing out binary data, but
in serial ADC we have only one pin for data out
ADC804 CHIP
ADC804 IC is an 8-bit parallel analog-to-digital converter
It works with +5 volts and has a resolution of 8bits
conversion time varies depending on clocking signals applied to
CLK R and CLK IN pins, but it cannot be faster than 110μs
CLK IN is input pin connected to external clock source when
external clock is used for timing
However, 0804 has internal clock generator.
To use internal clock generator (also called self-clocking), CLK IN
and CLK R pins are connected to capacitor and resistor and clock
frequency is determined by
Vref/2: (Pin 9)
used for reference voltage
If pin is open (not connected), analog input voltage is in range of
0 to 5 volts (same as Vcc pin)
If analog input range needs to be 0 to 4 volts, Vref/2 is connected
to 2 volts.
D0-D7 are digital data output pins
are tri-state buffered and the converted data is accessed only
when CS =0 and RD is forced low
To calculate the output voltage, use following formula
Solution:
(a) Iout = 2mA(153/256) = 1.195mA and Vout = 1.195mA * 5K =5.975V
(b) Iout = 2mA(200/256) = 1.562mA and Vout = 1.562mA * 5K =7.8125V
CONVERTING IOUT TO VOLTAGE IN DAC0808
we connect output pin lout, to resistor, convert this current to voltage, and monitor output
on scope.
this can cause inaccuracy since input resistance of load where it is connected will also
affect output voltage.
lref current output is isolated by connecting it to op-amp such as 741 with Rf = 5K ohms for
feedback resistor.
Assuming that R= 5K ohms, by changing binary i/p, o/p voltage change is shown below:
Example
to generate stair-step ramp, set up the circuit in Figure and connect output to
oscilloscope. Then write program to send data to DAC to generate stair-step ramp
CLR A
AGAIN: MOV P1,A ; SEND DATA TO DAC
INC A ; COUNT FROM 0 TO FFH
ACALL DELAY ; LET DAC RECOVER
SJMP AGAIN
Example
Write an ALP to generate a triangular waveform
SENSOR INTERFACING
The LM35 series sensors are precision integrated-circuit temperature sensors whose
output voltage is linearly proportional to the Celsius (centigrade) temperature.
The LM35 requires no external calibration since it is internally calibrated
It outputs 10mV for each degree of centigrade temperature.
The sensors of the LM34 series are precision integrated-circuit temperature sensors
whose output voltage is linearly proportional to the Fahrenheit temperature.
It outputs 10mV for each degree Fahrenheit temperature.
SIGNAL CONDITIONING AND INTERFACING
LM35 TO 8051
Signal conditioning is widely used in world of data acquisition
most common transducers produce output in form of voltage, current, charge,
capacitance, and resistance
we need to convert these signals to voltage in order to send input to A-to-D converter
This conversion (modification) is commonly called signal conditioning.
Signal conditioning can be a current-to-voltage conversion or signal amplification
E.g., thermistor changes resistance with temperature
change of resistance must be translated into voltages to be of any use to ADC
Look at case of connecting LM35 to ADC0848
ADC0848 has 8-bit resolution with maximum of 256 (28) steps and LM35 (or LM34)
produces l0 mV for every degree of temperature change, we can condition Vin of
ADC0848 to produce Vout, of 2560 mV (2.56 V) for full-scale output
in order to produce full-scale Vout of 2.56 V for ADC0848, we need to set Vref = 2.56.
This makes Vout, of ADC0848 correspond directly to temperature as monitored by LM35
Temperature vs. Vout for ADC0848
The LM336-2.5 zener diode to fix the voltage across the 10K pot at 2.5V
The use of the LM336-2.5 should overcome any fluctuations in the power supply
Connection to
ADC0848
and
Temperature sensor
Program