mc5mp - with app
DESCRIPTION
microcontoller8051TRANSCRIPT
8051 Microcontroller
The necessary tools for a microprocessor/controller
• CPU: Central Processing Unit• I/O: Input /Output• Bus: Address bus & Data bus• Memory: RAM & ROM• Timer• Interrupt• Serial Port• Parallel Port
CPU
General-Purpose Micro-processor
RAM ROM I/O Port
TimerSerial COM Port
Data Bus
Address Bus
General-Purpose Microprocessor System
Microprocessors
• CPU for Computers• No RAM, ROM, I/O port on CPU chip itself• Example : Intel’s x86, Motorola’s 680x0
Many chips on mother’s board
General-purpose microprocessor
RAM ROM
I/O Port
TimerSerial COM Port
Microcontroller
CPU
• A smaller computer• On-chip RAM, ROM, I/O ports...• Example : Motorola’s 6811, Intel’s 8051, Zilog’s Z8 and PIC 16X
A single chip
Microcontroller :
Microprocessor • CPU is stand-alone, RAM,
ROM, I/O, timer are separate• designer can decide on the
amount of ROM, RAM and I/O ports.
• Expansive
• general-purpose
• Used in desktops,laptops,notebooks etc..
Microcontroller• CPU, RAM, ROM, I/O and
timer are all on a single chip• fix amount of on-chip ROM,
RAM, I/O ports• for applications in which cost,
power and space are critical• single-purpose• Used in mobile phones, digital
cameras, microwave oven, washing machines
Microprocessor vs. Microcontroller
Pin Description of the 8051Pin Description of the 8051
1234567891011121314151617181920
4039383736353433323130292827262524232221
P1.0P1.1P1.2P1.3P1.4P1.5P1.6P1.7RST
(RXD)P3.0(TXD)P3.1
(T0)P3.4(T1)P3.5
XTAL2XTAL1
GND
(INT0)P3.2(INT1)P3.3
(RD)P3.7(WR)P3.6
VccP0.0(AD0)P0.1(AD1)P0.2(AD2)P0.3(AD3)P0.4(AD4)P0.5(AD5)P0.6(AD6)P0.7(AD7)EA/VPPALE/PROGPSENP2.7(A15)P2.6(A14)P2.5(A13)P2.4(A12)P2.3(A11)P2.2(A10)P2.1(A9)P2.0(A8)
8051
8051 Schematic
Pin out
1 2 3
8051 Architecture
IMPORTANT PINS IMPORTANT PINS
I/O Ports• One of the most useful features of the 8051 is that it contains four
I/O ports (P0 - P3)
• Port 0 Port 0 (( pins 32-39pins 32-39 ):): P0P0 (( P0.0P0.0 ~~ P0.7P0.7 ))– 8-bit R/W - General Purpose I/O8-bit R/W - General Purpose I/O– OrOr acts as a multiplexed low byte acts as a multiplexed low byte addressaddress and and datadata bus for bus for externalexternal memory design memory design
• Port 1 Port 1 (( pins 1-8pins 1-8 )) :: P1P1 (( P1.0P1.0 ~~ P1.7P1.7 ))– OnlyOnly 8-bit R/W - General Purpose I/O 8-bit R/W - General Purpose I/O
• Port 2 Port 2 (( pins 21-28pins 21-28 ):): P2P2 (( P2.0P2.0 ~~ P2.7P2.7 ))– 8-bit R/W - General Purpose I/O8-bit R/W - General Purpose I/O– OrOr highhigh byte of the byte of the addressaddress bus for external memory design bus for external memory design
• Port 3 Port 3 (( pins 10-17pins 10-17 ):): P3P3 (( P3.0P3.0 ~~ P3.7P3.7 ))– General Purpose I/OGeneral Purpose I/O– Each bit in port 3 can be programmed for alternate use.Each bit in port 3 can be programmed for alternate use.
• Each port can be used as input or output (bi-direction)
Port 3 Alternate Functions
Pins of 8051
• Vcc ( pin 40):– Vcc provides supply voltage to the chip. – The voltage source is +5V.
• GND ( pin 20 ): ground• XTAL1 and XTAL2 ( pins 19,18 ):
– These 2 pins provide clock pulses by which all internal operations can be synchronized.
– Quartz crystal oscillator is used– Typical range from 1MHz to 16 MHz.
RESET Value of Some 8051 Registers:
0000DPTR
0007SP
0000PSW
0000B
0000ACC
0000PC
Reset ValueRegister
RAM are all zero
Pins of 8051• RST ( pin 9): reset
• Upon applying a high pulse to RST, the microcontroller will reset and all values in registers will be lost.
• Register bank 0 is selected and all bits in PSW are 0
Pins of 8051
• ( pin 31 ): external access -- The (External Access) pin is used to control the
internal or external memory access. The signal 0 is for external memory access and signal 1
for internal memory access• ( pin 29 ): program store enable
If the memory access is for a byte of program code in the external ROM, the pin will go low to enable the ROM to place a byte of program code on the data bus.
PSEN
EA
EA
Pins of 8051
• ALE ( pin 30 ): address latch enable– It is an output pin and is active high.– Activates the port 0 joined with port 2 to provide 16 bit external
address bus to access the external memory. – The ALE multiplexes the P0.
1 for latching address on P0 as A0-A7 in the 16 bit address bus, 0 for latching P0 as data I/O.
On-Chip MemoryInternal RAM
Summary of the 8051 on-chip data memory(RAM)
Summary of the 8051 on-chip data memory(Special Function Registers)
TCON
TMOD• Both counters use the TMOD (Timer Mode) register for
configuration.
Stack in the 8051• The register used to access the stack is called SP (stack pointer)
register.
• The stack pointer in the 8051 is only 8 bits wide, which means that it can take value 00 to FFH. When 8051 powered up, the SP register contains value 07.
Example:MOV R6,#25HMOV R1,#12HMOV R4,#0F3HPUSH R6PUSH R1PUSH R4
0BH
0AH
09H
08H
SP=07H
0BH
0AH
09H
08H
SP=08H
0BH
0AH
09H
08H
SP=09H
0BH
0AH
09H
08H
SP=0AH
25
12
F3
25 25
12
Example:MOV R6,#25HMOV R1,#12HMOV R4,#0F3HPUSH R6PUSH R1PUSH R4POP R4POP R1POP R6
0BH
0AH
09H
08H
SP=07H
0BH
0AH
09H
08H
SP=08H
0BH
0AH
09H
08H
SP=09H
0BH
0AH
09H
08H
SP=0AH
25
12
F3
2525
12
Data Pointer Register (DPTR)
Interrupt
• Whenever any device needs its service, the device notifies the microcontroller by sending it an interrupt signal.
• Upon receiving an interrupt signal, the microcontroller interrupts whatever it is doing and serves the device.
• The program which is associated with the interrupt is called the interrupt service routine (ISR) or interrupt handler.
Interrupts
Interrupt
Program
time t
Interrupts
Program
Interrupt Service Routine
Interrupt
Program
time t
34
Interrupts
ProgramSave
Context Interrupt Service Routine
Restore Context
Interrupt
Program
time t
Interrupt Enable(IE) Register
Interrupt Priority (IP) Register
The 8051 Programming
INTRODUCTION
• An instruction is an order or command given to a processor by a computer program. All commands are known as instruction set and set of instructions is known as program.
• 8051 have in total 111 instructions, i.e. 111 different words available for program writing.
Instruction Format
• Where first part describes WHAT should be done (Opcode), while other explains HOW to do it(operand).
• Ex: MOV R6, #25H Opcode Operands• The latter part can be a data (binary number)
or the address at which the data is stored.
Addressing Modes
• Addressing modes specifies where the data (operand) is. They specify the source or destination of data (operand) in several different ways, depending upon the situation.
• Addressing modes are used to know where the operand located is.
Addressing Modes• There are 4 types of addressing modes:
1. Immediate addressing. 2. Register addressing.3. Direct addressing.4. Register indirect addressing.
Immediate Addressing Mode
• In immediate addressing mode, the data is given with the instruction itself.
• In this case; the data to be stored in memory immediately follows the opcode.
• MOV Rr, #n: copy the 8 bit number n into register Rr. • MOV A, #n: copy the 8 bit number n into accumulator.• MOV DPTR, #nn: copy the 16 bit number nn into DPTR.
Immediate Addressing Mode
• For example;1. MOV A, #25H :This instruction will move the data 25H to
accumulator.
2. MOV R4, #04H: Put the immediate 8 bit number 04H in register R4
3. MOV DPTR, #1234H: Put the immediate 16 bit number 1234H into DPTR register.
Register Addressing Mode
• In register addressing mode; the source and/or destination is a register.
• In this case data is placed in any of the 8 registers (R0-R7); in instructions it is specified with letter Rn (where n indicates 0 to 7).
Register Addressing Mode
Examples:
1. MOV A,R0 : Copy the data in register R0 to Accumulator.
2. ADD R5,A : This instruction will add the contents of register R5 with the accumulator contents.
Direct Addressing Mode
• In direct addressing mode the address of memory location containing data to be read is specified in instruction.
• In this case; address of the data is given with the instruction itself.
Direct Addressing Mode
• Examples:
Register Indirect Addressing Mode• In register indirect addressing mode; the contents of the
designated register are used as a pointer to memory.• In this case; data is placed in memory, but address of
memory location is not given directly with instruction.
Register Indirect Addressing Mode
Examples
Data Transfer Instructions
• MOV dest, source dest source• Stack instructions
PUSH byte ;increment stack pointer, ;move byte on stackPOP byte ;move from stack to byte,
;decrement stack pointer
• Exchange instructionsXCH a, byte ;exchange accumulator and byteXCHD a, byte ;exchange low nibbles of
;accumulator and byte
Data Processing Instructions
Arithmetic InstructionsLogic Instructions
Arithmetic Instructions
• Add• Subtract• Increment• Decrement• Multiply• Divide
Arithmetic Instructions
Mnemonic DescriptionADD A, byte add A to byte, put result in AADDC A, byte add with carrySUBB A, byte subtract with borrowINC A increment AINC byte increment byte in memoryINC DPTR increment data pointerDEC A decrement accumulatorDEC byte decrement byteMUL AB multiply accumulator by b registerDIV AB divide accumulator by b registerDA A decimal adjust the accumulator
ADD
Subtract
Multiply
Division
Logic InstructionsANL AND ORL OR XRL XORCPL Complement
CLR - clearRL – rotate leftRLC – rotate left through CarryRR – rotate rightRRC – rotate right through CarrySWAP – swap accumulator nibbles
Increment & Decrement
Example
8 bit addition
Initialize DPTR
Start
Bring data to registers A & B
Add data
Transfer result to external memory location
Stop
Addition programORG 8400H : Starting location of program
MOV DPTR, #8500H : Initialize the data pointer
MOV A, #14H : Data1 is copied into accumulator
ADD A, #23H : Data1 and data2 are added
MOVX @DPTR, A : Result is moved into memory location
END
8 bit substraction
Initialize DPTR
Start
Bring data to registers A & B
Subtract data
Transfer result to external memory location
Stop
Subtraction program
ORG 8400H : Starting location of program
MOV DPTR #8500H : Initialize the data pointer
MOV A, #23H : Data1 is copied into accumulator
SUBB A, #14H : Data1 and data2 are substracted.
MOVX @DPTR, A : Result is moved into memory location
END
8 bit Multiplication
Initialize DPTR
Start
Initialize accumulator with data1 & B register with data 2
Multiply the data
Transfer result to external memory location
Stop
Multiplication program• ORG 8400 H : Starting location of the program• MOV DPTR,#8800H : Initializing of the data pointer• MOV 0F0H, #02H : Data 1 is transferred into B(0F0H) • MOV A, #05H : Data 2 is transferred into A register • MUL AB : Multiplying the Data 1 and Data 2• MOVX @DPTR, A : Copy low order byte of result into 8800H• INC DPTR : Update the pointer to 8801H• MOV R0,0F0H : Copy the high order byte of result into R0• MOV A, R0 : Get high order byte of result into A register• MOVX @DPTR, A : Copy high order byte of result into 8801H• END
8 bit Division
Initialize DPTR
Start
Initialize accumulator with dividend & B register with divisor
Divide the data
Transfer result to external memory location
Stop
Division program• ORG 8400H : Starting location of program• MOV DPTR,#8800H : Initialization the data pointer• MOV A,#64H : Dividend is loaded into register A• MOV OFOH,#05 : Divisor is loaded into register B• DIV AB : Divided the 64H by 05H• MOVX @DPTR,A : Quotient is moved into 8800H location• INC DPTR : Update the pointer• MOV R0,0F0H : Copy the remainder into R0 register• MOV A,R0 : Copy the remainder into register A• MOVX @DPTR,A : Remainder is copied into 8801H• END
Applications of 8051
Analog to Digital converters (ADC)
8051
CS :Active low input (P3.2-INT0 pin) used to activate the ADC chip.
WR (start conversion): Active low input used to inform the ADC to start the conversion process. If CS = 0 when WR makes a low-to-high transition(P3.3-INT1 pin), the ADC starts converting the analog input value of Vin to an 8-bit digital number.
RD (data enable) : Active low input used to get converted data out of the ADC chip. When CS = 0, if a high-to-low pulse is applied to the RD pin (from T0 pin of port 3), the 8-bit digital output shows up at the D0-D7 data pins.
.Vref (+) and Vref(-) :These are the differential analog inputs where Vin = Vref (+) - Vref (-). Often the Vref (-) pin is connected to ground and the Vref (+) pin is used as the analog input to be converted to digital.
Vin : This is the analog input to be converted.
Digital to Analog converters (DAC)
• CS :Active low input (P3.2-INT0 pin) used to activate the DAC chip.
• WR : Active low input used to inform the DAC to get data from data pins and start the conversion process. If CS = 0 when WR makes a low-to-high transition(P3.3-INT1 pin), the DAC starts converting the digital input to an analog voltage.
Temperature control system
Temperature sensor
8051
Relay
Relay
Heater
Fan
T>23
T<23
Preset temperature=23
Temperature control system
• Temperature sensor is used for sensing the ambient temperature .
• The temperature is compared with a preset value stored by the user.
• If the room temperature goes beyond the preset temperature then the fan will be ON
• If the temperature goes below the preset temperature then heater will be ON