8051 Microcontroller

275
Micro-Controller 8051 Micro-Controller 8051 Overview Overview Ritula Thakur NITTTR

Transcript of 8051 Microcontroller

Page 1: 8051 Microcontroller

Micro-Controller 8051Micro-Controller 8051

OverviewOverview

Ritula Thakur

NITTTR

Page 2: 8051 Microcontroller

Chapter 1Chapter 1The 8051 MicrocontrollersThe 8051 Microcontrollers

Page 3: 8051 Microcontroller

OutlinesOutlines

� Compare microprocessors & microcontrollers� Advantages of microcontrollers� Embedded systems� Choose a microcontroller� Speed, packaging, memory & cost per unit� Various members of 8051 family� Various manufacturers of 8051

04/07/15 Ritula Thakur 3

Page 4: 8051 Microcontroller

Microcontroller vs. Microcontroller vs. MicroprocessorsMicroprocessors

04/07/15 Ritula Thakur 4

Page 5: 8051 Microcontroller

Embedded Computing SystemsEmbedded Computing Systems

� Use a microprocessor or microcontroller to do one task only� Printer

� PC used for any number of applications� Word processor, print-server, bank teller terminal,

video game player, network server, internet terminal

� PC contains or is connected to various embedded products� Keyboard, printer, modem, disk controller, sound

card, CD-ROM driver, mouse

� X86 PC embedded applications04/07/15 Ritula Thakur 5

Page 6: 8051 Microcontroller

Embedded Products Using MicrocontrollersEmbedded Products Using Microcontrollers

� Home� Appliances, intercom, telephones, security

systems, garage door openers, answering machines, fax machines, home computers, TVs, cable TV tuner, VCR, camcorder, remote controls, video games, cellular phones, musical instruments, sewing machines, lighting control, paging, camera, pinball machines, toys, exercise equipment

04/07/15 Ritula Thakur 6

Page 7: 8051 Microcontroller

Embedded Products Using MicrocontrollersEmbedded Products Using Microcontrollers

� Office� Telephones, computers, security systems, fax

machines, microwave, copier, laser printer, color printer, paging

04/07/15 Ritula Thakur 7

Page 8: 8051 Microcontroller

Embedded Products Using MicrocontrollersEmbedded Products Using Microcontrollers

� Auto� Trip computer, engine control, air bag, ABS,

instrumentation, security system, transmission control, entertainment, climate control, cellular phone, keyless entry

04/07/15 Ritula Thakur 8

Page 9: 8051 Microcontroller

Choosing A MicrocontrollerChoosing A Microcontroller

� Computing needs� Speed, packaging, power consumption, RAM,

ROM, I/O pins, timers, upgrade to high performance or low-power versions, cost

� Software development tools� Assembler, debugger, C compiler, emulator,

technical support

� Availability & source

04/07/15 Ritula Thakur 9

Page 10: 8051 Microcontroller

Companies Producing 8051Companies Producing 8051

� Table 1-2:Some Companies Producing a Member of the 8051 Family

CompanyCompany Web SiteWeb Site

IntelIntel www.intel.com/design/mcs51www.intel.com/design/mcs51

AtmelAtmel www.atmel.comwww.atmel.com

Philips/SigneticsPhilips/Signetics www.semiconductors.philips.comwww.semiconductors.philips.com

SiemensSiemens www.sci.siemens.comwww.sci.siemens.com

Dallas SemiconductorDallas Semiconductor www.dalsemi.comwww.dalsemi.com

04/07/15 Ritula Thakur 10

Page 11: 8051 Microcontroller

Inside 8051 MicrocontrollerInside 8051 Microcontroller

� Introduced by Intel in 1981

04/07/15 Ritula Thakur 11

Page 12: 8051 Microcontroller

8051 Family8051 Family

� Table 1-4:Comparison of 8051 Family Members

FeatureFeature 80518051 80528052 8031 8031 ROM (on chip program space in bytes)ROM (on chip program space in bytes) 4K4K 8k8k 0k0k

RAM (bytes)RAM (bytes) 128128 256256 128128

TimersTimers 22 33 22

I/O pinsI/O pins 3232 3232 3232

Serial portSerial port 11 11 11

Interrupt sourcesInterrupt sources 66 88 66

04/07/15 Ritula Thakur 12

Page 13: 8051 Microcontroller

Various 8051 MicrocontrollersVarious 8051 Microcontrollers

� 8751 microcontroller� UV-EPROM

� AT89C51 from Atmel Corporation� Flash (erase before write)

� DS5000 from Dallas Semiconductor� NV-RAM (changed one byte at a time), RTC (real-

time clock)

� OTP (one-time-programmable) version of 8051

� 8051 family from Philips� AD, DA, extended I/O, OTP and flash04/07/15 Ritula Thakur 13

Page 14: 8051 Microcontroller

Chapter 2Chapter 28051 Assembly Language Programming8051 Assembly Language Programming

Page 15: 8051 Microcontroller

OutlinesOutlines

� 8051 registers� Manipulate data using registers & MOVE

instructions� Code simple assembly language instructions� Assemble and run a program� Sequence events upon power-up� Examine programs in ROM codes� ROM memory map� Execution of instructions� Data types� PSW register� RAM memory space� Stack� Register banks04/07/15 Ritula Thakur 15

Page 16: 8051 Microcontroller

8051 Registers8051 Registers

D7D7 D6D6 D5D5 D4D4 D3D3 D2D2 D1D1 D0D0

DPTR

PC PC (Program counter)

DPH DPL

Figure2-1 (a): Some 8 bit Registers of the 8051

Figure2-1 (b): Some 8051 16 bit Registers

8 bit Registers

R6R6

R5R5

R4R4

R3R3

R2R2

R1R1

R0R0

BB

AA

R7R7

04/07/15 Ritula Thakur 16

Page 17: 8051 Microcontroller

MOV InstructionMOV Instruction

� MOV destination, source ; copy source to dest.� MOV A,#55H ;load value 55H into reg. A

MOV R0,A ;copy contents of A into R0 ;(now A=R0=55H)

MOV R1,A ;copy contents of A into R1 ;(now A=R0=R1=55H)

MOV R2,A ;copy contents of A into R2 ;(now A=R0=R1=R2=55H)

MOV R3,#95H ;load value 95H into R3 ;(now R3=95H)

MOV A,R3 ;copy contents of R3 into A ;now A=R3=95H

04/07/15 Ritula Thakur 17

Page 18: 8051 Microcontroller

Notes on ProgrammingNotes on Programming

� Value (proceeded with #) can be loaded directly to registers A, B, or R0 – R7� MOV R5, #0F9H

� If values 0 to F moved into an 8-bit register, the rest assumed all zeros� MOV A, #5

� A too large value causes an error� MOV A, #7F2H

04/07/15 Ritula Thakur 18

Page 19: 8051 Microcontroller

ADD InstructionADD Instruction

� ADD A, source ;ADD the source operand

;to the accumulator� MOV A, #25H ;load 25H into A

MOV R2,#34H ;load 34H into R2ADD A,R2 ;add R2 to accumulator

;(A = A + R2)

04/07/15 Ritula Thakur 19

Page 20: 8051 Microcontroller

Structure of Assembly LanguageStructure of Assembly Language

ORG 0H ;start (origin) at location 0MOV R5,#25H ;load 25H into R5MOV R7,#34H ;load 34H into R7MOV A,#0 ;load 0 into AADD A,R5 ;add contents of R5 to A

;now A = A + R5ADD A,R7 ;add contents of R7 to A

;now A = A + R7ADD A,#12H ;add to A value 12H

;now A = A + 12HHERE: SJMP HERE ;stay in this loop

END ;end of asm source file

Program 2-1:Sample of an Assembly Language Program

04/07/15 Ritula Thakur 20

Page 21: 8051 Microcontroller

Steps to Create a ProgramSteps to Create a Program

04/07/15 Ritula Thakur 21

Page 22: 8051 Microcontroller

8051 Program Counter & ROM 8051 Program Counter & ROM SpaceSpace

04/07/15 Ritula Thakur 22

Page 23: 8051 Microcontroller

8051 Program Counter & ROM 8051 Program Counter & ROM SpaceSpace

04/07/15 Ritula Thakur 23

Page 24: 8051 Microcontroller

8051 Program Counter & ROM 8051 Program Counter & ROM SpaceSpace

04/07/15 Ritula Thakur 24

Page 25: 8051 Microcontroller

Execute a Program Byte by ByteExecute a Program Byte by Byte

1. PC=0000: opcode 7D fetched; 25 fetched; R5←25; PC+2

2. PC=0002: opcode 7F fetched; 34 fetched; R7←34; PC+2

3. PC=0004; opcode 74 fetched; 0 fetched; A←0; PC+2

4. PC=0006; opcode 2D fetched; A←A+R5; PC+1

5. (Similarly…)

04/07/15 Ritula Thakur 25

Page 26: 8051 Microcontroller

8051 On-Chip ROM Address Range8051 On-Chip ROM Address Range

04/07/15 Ritula Thakur 26

Page 27: 8051 Microcontroller

Data Types & DirectivesData Types & Directives

ORG 500HDATA1: DB 28 ;DECIMAL (1C in Hex)

DATA2: DB 00110101B ;BINARY (35 in Hex)

DATA3: DB 39H ;HEX

ORG 510HDATA4: DB “2591” ; ASCII NUMBERS

ORG 518HDATA6: DB “My name is Joe” ;ASCII

CHARACTERS

04/07/15 Ritula Thakur 27

Page 28: 8051 Microcontroller

PSW (Flag) RegisterPSW (Flag) Register

04/07/15 Ritula Thakur 28

Page 29: 8051 Microcontroller

Instructions Affecting Flag BitsInstructions Affecting Flag Bits

04/07/15 Ritula Thakur 29

Page 30: 8051 Microcontroller

ADD Instruction and PSWADD Instruction and PSW

04/07/15 Ritula Thakur 30

Page 31: 8051 Microcontroller

ADD Instruction and PSWADD Instruction and PSW

04/07/15 Ritula Thakur 31

Page 32: 8051 Microcontroller

ADD Instruction and PSWADD Instruction and PSW

04/07/15 Ritula Thakur 32

Page 33: 8051 Microcontroller

8051 RAM Allocation8051 RAM Allocation

04/07/15 Ritula Thakur 33

Page 34: 8051 Microcontroller

8051 Register Banks8051 Register Banks

04/07/15 Ritula Thakur 34

Page 35: 8051 Microcontroller

Access RAM Locations Using Register NamesAccess RAM Locations Using Register Names

04/07/15 Ritula Thakur 35

Page 36: 8051 Microcontroller

Access RAM Locations Using Access RAM Locations Using AddressesAddresses

04/07/15 Ritula Thakur 36

Page 37: 8051 Microcontroller

Switch Register BanksSwitch Register Banks

04/07/15 Ritula Thakur 37

Page 38: 8051 Microcontroller

Switch Register BanksSwitch Register Banks

04/07/15 Ritula Thakur 38

Page 39: 8051 Microcontroller

Pushing onto StackPushing onto Stack

04/07/15 Ritula Thakur 39

Page 40: 8051 Microcontroller

Popping from StackPopping from Stack

04/07/15 Ritula Thakur 40

Page 41: 8051 Microcontroller

Stack & Bank 1 ConflictStack & Bank 1 Conflict

04/07/15 Ritula Thakur 41

Page 42: 8051 Microcontroller

Stack & Bank 1 ConflictStack & Bank 1 Conflict

04/07/15 Ritula Thakur 42

Page 43: 8051 Microcontroller

Chapter 3Chapter 3JUMP, LOOP and CALL InstructionsJUMP, LOOP and CALL Instructions

Page 44: 8051 Microcontroller

OutlinesOutlines

� Loop instructions� Conditional jump instructions� Conditions determining conditional jump� Unconditional long & short jumps� Calculate target addresses for jumps� Subroutines� Using stack in subroutines� Crystal frequency vs. machine cycle� Code programs to generate time delay04/07/15 Ritula Thakur 44

Page 45: 8051 Microcontroller

LoopingLooping

04/07/15 Ritula Thakur 45

Page 46: 8051 Microcontroller

Loop inside a Loop (Nested Loop inside a Loop (Nested Loop)Loop)

04/07/15 Ritula Thakur 46

Page 47: 8051 Microcontroller

8051 Conditional Jump 8051 Conditional Jump InstructionsInstructions

04/07/15 Ritula Thakur 47

Page 48: 8051 Microcontroller

Conditional Jump ExampleConditional Jump Example

04/07/15 Ritula Thakur 48

Page 49: 8051 Microcontroller

Conditional Jump ExampleConditional Jump Example

04/07/15 Ritula Thakur 49

Page 50: 8051 Microcontroller

Unconditional Jump InstructionsUnconditional Jump Instructions

� All conditional jumps are short jumps� Target address within -128 to +127 of PC

� LJMP (long jump): 3-byte instruction� 2-byte target address: 0000 to FFFFH� Original 8051 has only 4KB on-chip ROM

� SJMP (short jump): 2-byte instruction� 1-byte relative address: -128 to +127

04/07/15 Ritula Thakur 50

Page 51: 8051 Microcontroller

Calculating Short Jump AddressesCalculating Short Jump Addresses

04/07/15 Ritula Thakur 51

Page 52: 8051 Microcontroller

Calculating Short Jump AddressesCalculating Short Jump Addresses

04/07/15 Ritula Thakur 52

Page 53: 8051 Microcontroller

Call InstructionsCall Instructions

� LCALL (long call): 3-byte instruction� 2-byte address� Target address within 64K-byte range

� ACALL (absolute call): 2-byte instruction� 11-bit address� Target address within 2K-byte range

04/07/15 Ritula Thakur 53

Page 54: 8051 Microcontroller

LCALLLCALL

04/07/15 Ritula Thakur 54

Page 55: 8051 Microcontroller

CALL Instruction & Role of StackCALL Instruction & Role of Stack

04/07/15 Ritula Thakur 55

Page 56: 8051 Microcontroller

CALL Instruction & Role of StackCALL Instruction & Role of Stack

04/07/15 Ritula Thakur 56

Page 57: 8051 Microcontroller

Using PUSH & POP in SubroutinesUsing PUSH & POP in Subroutines

04/07/15 Ritula Thakur 57

Page 58: 8051 Microcontroller

Using PUSH & POP in SubroutinesUsing PUSH & POP in Subroutines

04/07/15 Ritula Thakur 58

Page 59: 8051 Microcontroller

Using PUSH & POP in SubroutinesUsing PUSH & POP in Subroutines

04/07/15 Ritula Thakur 59

Page 60: 8051 Microcontroller

Calling SubroutinesCalling Subroutines

04/07/15 Ritula Thakur 60

Page 61: 8051 Microcontroller

Calling SubroutinesCalling Subroutines

04/07/15 Ritula Thakur 61

Page 62: 8051 Microcontroller

ACALL (absolute call)ACALL (absolute call)

04/07/15 Ritula Thakur 62

Page 63: 8051 Microcontroller

Programming EfficientlyProgramming Efficiently

04/07/15 Ritula Thakur 63

Page 64: 8051 Microcontroller

Time Delay Generation & Time Delay Generation & CalculationCalculation

� 1 instruction = n × machine cycle� 1 machine cycle = 12 clock cycles

04/07/15 Ritula Thakur 64

Page 65: 8051 Microcontroller

Delay CalculationDelay Calculation

04/07/15 Ritula Thakur 65

Page 66: 8051 Microcontroller

Delay Calculation ExampleDelay Calculation Example

04/07/15 Ritula Thakur 66

Page 67: 8051 Microcontroller

Delay Calculation ExampleDelay Calculation Example

04/07/15 Ritula Thakur 67

Page 68: 8051 Microcontroller

Increasing Delay Using NOPIncreasing Delay Using NOP

04/07/15 Ritula Thakur 68

Page 69: 8051 Microcontroller

Large Delay Using Nested LoopLarge Delay Using Nested Loop

04/07/15 Ritula Thakur 69

Page 70: 8051 Microcontroller

Chapter 4Chapter 4I/O Port ProgrammingI/O Port Programming

Page 71: 8051 Microcontroller

OutlinesOutlines

� 8051 pin functions� 4 ports of 8051� Dual role of port 0 for data & address� Code assembly language to use ports� Use of port 3 for interrupt signals� Code 8051 instructions for I/O handling� Code bit-manipulation instructions

04/07/15 Ritula Thakur 71

Page 72: 8051 Microcontroller

8051 Pin Diagram8051 Pin Diagram

04/07/15 Ritula Thakur 72

Page 73: 8051 Microcontroller

Clock GenerationClock Generation

04/07/15 Ritula Thakur 73

Page 74: 8051 Microcontroller

Clock GenerationClock Generation

04/07/15 Ritula Thakur 74

Page 75: 8051 Microcontroller

Machine & Clock CyclesMachine & Clock Cycles

04/07/15 Ritula Thakur 75

Page 76: 8051 Microcontroller

RESET Value of RegistersRESET Value of Registers

04/07/15 Ritula Thakur 76

Page 77: 8051 Microcontroller

RESET CircuitsRESET Circuits

04/07/15 Ritula Thakur 77

Page 78: 8051 Microcontroller

RESET CircuitsRESET Circuits

04/07/15 Ritula Thakur 78

Page 79: 8051 Microcontroller

Pins for External ROM/RAMPins for External ROM/RAM

� EA (external access)� VCC: on-chip ROM

� GND: external ROM

� PSEN (program store enable)� to OE (output enable) pin of ROM

� ALE (address latch enable)� to G (enable) pin of 74LS373

� See Chapter 14

04/07/15 Ritula Thakur 79

Page 80: 8051 Microcontroller

Port 0: I/O, Open DrainPort 0: I/O, Open Drain

� Initially configured as output

MOVMOV A,#55HA,#55H

BACK:BACK: MOV MOV P0,AP0,A

ACALLACALL DELAYDELAY

CPLCPL AA

SJMPSJMP BACKBACK

04/07/15 Ritula Thakur 80

Page 81: 8051 Microcontroller

Port 0: I/O, Open DrainPort 0: I/O, Open Drain

04/07/15 Ritula Thakur 81

Page 82: 8051 Microcontroller

Port 0 as Input: Write all 1sPort 0 as Input: Write all 1s

� Dual role of port 0� Can be configured as AD0 – AD7

MOV A,#0FFH ;A = FF hex

MOV P0,A ;make P0 an input port

;by writing all 1s to it

BACK: MOV A,P0 ;get data from P0

MOV P1,A ;send it to port 1

SJMP BACK ;keep doing it

04/07/15 Ritula Thakur 82

Page 83: 8051 Microcontroller

Port 1: I/O, No Pull-up ResistorsPort 1: I/O, No Pull-up Resistors

� Initially configured as output

MOV A,#55H

BACK: MOV P1,A

ACALL DELAY

CPL A

SJMP BACK

MOV A,#0FFH ;A=FF hex

MOV P1,A ;make P1 an input port

;by writing all 1s to it

MOV A,P1 ;get data from P1

MOV R7,A ;save it in reg R7

ACALL DELAY ;wait

MOV A,P1 ;get another data fromP1

MOV R6,A ;save it in reg R6

ACALL DELAY ;wait

MOV A,P1 ;get another data from P1

MOV R5,A ;save it in reg R504/07/15 Ritula Thakur 83

Page 84: 8051 Microcontroller

Port 2: I/O, No Pull-up ResistorsPort 2: I/O, No Pull-up Resistors

� Initially configured as outputMOV A,#55H

BACK: MOV P2,AACALL DELAYCPL ASJMP BACK

MOV A,#0FFH ;A=FF hexMOV P2,A ;make P2 an input port by

;writing all 1s to itBACK: MOV A,P2 ;get data from P2

MOV P1,A ;send it to Port 1SJMP BACK ;keep doing that

♦ Dual role of port 2: A8-A15Dual role of port 2: A8-A1504/07/15 Ritula Thakur 84

Page 85: 8051 Microcontroller

Port 3: I/O, No Pull-up ResistorsPort 3: I/O, No Pull-up Resistors

04/07/15 Ritula Thakur 85

Page 86: 8051 Microcontroller

Different Ways of Accessing 8 bitsDifferent Ways of Accessing 8 bits

BACK: MOV A,#55H

MOV P1,A

ACALL DELAY

MOV A,#0AAH

MOV P1,A

ACALL DELAY

SJMP BACK

BACK:BACK: MOVMOV P1,#55HP1,#55H

ACALLACALL DELAYDELAY

MOVMOV P1,#0AAHP1,#0AAH

ACALLACALL DELAYDELAY

SJMPSJMP BACKBACK

MOV P1,#55H ;P1=01010101AGAIN: XLR P1,#0FFH ;EX-OR

;P1 with 1111 1111 ACALL DELAY SJMP AGAIN

04/07/15 Ritula Thakur 86

Page 87: 8051 Microcontroller

Single-bit Addressability of PortsSingle-bit Addressability of Ports

BACK: CPL P1.2 ;complement P1.2 only

ACALL DELAY

SJMP BACK

;another variation of the above program follows

AGAIN: SETB P1.2 ;change only P1.2=high

ACALL DELAY

CLR P1.2 ;change only P1.2=low

ACALL DELAY

SJMP AGAIN

04/07/15 Ritula Thakur 87

Page 88: 8051 Microcontroller

Single-bit Addressability of PortsSingle-bit Addressability of Ports

04/07/15 Ritula Thakur 88

Page 89: 8051 Microcontroller

Example 4-2Example 4-2

04/07/15 Ritula Thakur 89

Page 90: 8051 Microcontroller

Chapter 5Chapter 5Addressing ModesAddressing Modes

Page 91: 8051 Microcontroller

OutlinesOutlines

� Five addressing modes of 8051� Code instructions using each addressing

mode� Access RAM using various addressing

modes� SFR addresses� Access SFR� Operate stack using direct addressing mode� Code instructions to operate look-up table04/07/15 Ritula Thakur 91

Page 92: 8051 Microcontroller

Five Addressing ModesFive Addressing Modes

� Immediate� Register� Direct� Register indirect� Indexed

04/07/15 Ritula Thakur 92

Page 93: 8051 Microcontroller

Immediate Addressing ModeImmediate Addressing Mode

MOVMOV A,#25HA,#25H ;load 25H into A;load 25H into A

MOV MOV R4,#62R4,#62 ;load the decimal value 62 into R4;load the decimal value 62 into R4

MOV B,#40HMOV B,#40H ;load 40H into B;load 40H into B

MOVMOV DPTR,#4521HDPTR,#4521H ;DPTR=4521H;DPTR=4521H

MOVMOV DPTR,#2550HDPTR,#2550H

;is the same as:;is the same as:

MOVMOV DPL,#50HDPL,#50H

MOVMOV DPH,#25HDPH,#25H

04/07/15 Ritula Thakur 93

Page 94: 8051 Microcontroller

MOVMOV DPTR,#68975DPTR,#68975 ;illegal!! value > 65535 (FFFFH);illegal!! value > 65535 (FFFFH)

COUNTCOUNT EQU 30 EQU 30

…… … …

MOV MOV R4,#COUNT R4,#COUNT ;R4=1E (30=1EH) ;R4=1E (30=1EH)

MOV MOV DPTR,#MYDATA DPTR,#MYDATA ;DPTR=200H;DPTR=200H

ORGORG 200H 200H

MYDATA:MYDATA: DB “America” DB “America”

04/07/15 Ritula Thakur 94

Page 95: 8051 Microcontroller

Register Addressing ModeRegister Addressing Mode

MOVMOV A,R0A,R0 ;copy the contents of R0 into A;copy the contents of R0 into A

MOVMOV R2,AR2,A ;copy the contents of A into R2;copy the contents of A into R2

ADDADD A,R5A,R5 ;add the contents of R5 to contents of A;add the contents of R5 to contents of A

ADDADD A,R7A,R7 ;add the contents of R7 to contents of A;add the contents of R7 to contents of A

MOVMOV R6,AR6,A ;save accumulator in R6;save accumulator in R6

MOVMOV DPTR,#25F5HDPTR,#25F5H

MOVMOV R7,DPLR7,DPL

MOVMOV R6,DPHR6,DPH

04/07/15 Ritula Thakur 95

Page 96: 8051 Microcontroller

Direct Addressing ModeDirect Addressing Mode

� RAM addresses 00 to 7FH

MOVMOV R0,40HR0,40H ;save content of RAM location 40H in R0;save content of RAM location 40H in R0

MOVMOV 56H,A56H,A ;save content of A in RAM location 56H;save content of A in RAM location 56H

MOVMOV R4,7FHR4,7FH ;move contents of RAM location 7FH to ;move contents of RAM location 7FH to R4R4

MOVMOV A,4A,4 ;is same as;is same as

MOV A,R4MOV A,R4 ;which means copy R4 into A;which means copy R4 into A

MOVMOV A,7A,7 ;is same as;is same as

MOVMOV A,R7A,R7 ;which means copy R7 into A;which means copy R7 into A

04/07/15 Ritula Thakur 96

Page 97: 8051 Microcontroller

MOVMOV A,2A,2 ;is the same as;is the same as

MOVMOV A,R2A,R2 ;which means copy R2 into A;which means copy R2 into A

MOVMOV A,0A,0 ;is the same as;is the same as

MOVMOV A,R0A,R0 ;which means copy R0 into A;which means copy R0 into A

MOVMOV R2,#5R2,#5 ;R2=05;R2=05

MOVMOV A,2A,2 ;copy R2 to A (A=R2=05);copy R2 to A (A=R2=05)

MOVMOV B,2B,2 ;copy R2 to B (B=R2=05);copy R2 to B (B=R2=05)

MOVMOV 7,27,2 ;copy R2 to R7;copy R2 to R7

;since “MOV R7,R2” is invalid;since “MOV R7,R2” is invalid

04/07/15 Ritula Thakur 97

Page 98: 8051 Microcontroller

SFR Registers & Their AddressesSFR Registers & Their Addresses

MOVMOV 0E0H,#55H0E0H,#55H ;is the same as;is the same as

MOVMOV A,#55HA,#55H ;which means load 55H into A (A=55H);which means load 55H into A (A=55H)

MOVMOV 0F0H,#25H0F0H,#25H ;is the same as;is the same as

MOV B,#25HMOV B,#25H ;which means load 25H into B (B=25H);which means load 25H into B (B=25H)

MOVMOV 0E0H,R20E0H,R2 ;is the same as;is the same as

MOVMOV A,R2A,R2 ;which means copy R2 into A;which means copy R2 into A

MOVMOV 0F0H,R00F0H,R0 ;is the same as;is the same as

MOVMOV B,R0B,R0 ;which means copy R0 into B;which means copy R0 into B

04/07/15 Ritula Thakur 98

Page 99: 8051 Microcontroller

SFR Addresses ( 1 of 2 )SFR Addresses ( 1 of 2 )

04/07/15 Ritula Thakur 99

Page 100: 8051 Microcontroller

SFR Addresses ( 2 of 2 )SFR Addresses ( 2 of 2 )

04/07/15 Ritula Thakur 100

Page 101: 8051 Microcontroller

ExampleExample

04/07/15 Ritula Thakur 101

Page 102: 8051 Microcontroller

Stack and Direct Addressing ModeStack and Direct Addressing Mode

� Only direct addressing is allowed for stack

04/07/15 Ritula Thakur 102

Page 103: 8051 Microcontroller

Register Indirect Addressing ModeRegister Indirect Addressing Mode

� Only R0 & R1 can be used

MOVMOV A,@R0A,@R0 ;move contents of RAM location whose;move contents of RAM location whose

;address is held by R0 into A;address is held by R0 into A

MOVMOV @R1,B@R1,B ;move contents of B into RAM location;move contents of B into RAM location

;whose address is held by R1;whose address is held by R1

04/07/15 Ritula Thakur 103

Page 104: 8051 Microcontroller

Example ( 1 of 2 )Example ( 1 of 2 )

04/07/15 Ritula Thakur 104

Page 105: 8051 Microcontroller

Example ( 2 of 2 )Example ( 2 of 2 )

04/07/15 Ritula Thakur 105

Page 106: 8051 Microcontroller

Advantage of Register Indirect AddressingAdvantage of Register Indirect Addressing

� Looping not possible in direct addressing

04/07/15 Ritula Thakur 106

Page 107: 8051 Microcontroller

ExampleExample

04/07/15 Ritula Thakur 107

Page 108: 8051 Microcontroller

Index Addressing Mode & On-chip ROM Index Addressing Mode & On-chip ROM AccessAccess

� Limitation of register indirect addressing: 8-bit addresses (internal RAM)

� DPTR: 16 bits� MOVC A, @A+DPTR ; “C” means program

(code) space ROM

04/07/15 Ritula Thakur 108

Page 109: 8051 Microcontroller

Example ( 1 of 2 )Example ( 1 of 2 )

04/07/15 Ritula Thakur 109

Page 110: 8051 Microcontroller

Example ( 2 of 2 )Example ( 2 of 2 )

04/07/15 Ritula Thakur 110

Page 111: 8051 Microcontroller

Example ( 1 of 3 )Example ( 1 of 3 )

04/07/15 Ritula Thakur 111

Page 112: 8051 Microcontroller

Example ( 2 of 3 )Example ( 2 of 3 )

04/07/15 Ritula Thakur 112

Page 113: 8051 Microcontroller

Example ( 3 of 3 )Example ( 3 of 3 )

04/07/15 Ritula Thakur 113

Page 114: 8051 Microcontroller

Look-up Table & Indexed Look-up Table & Indexed AddressingAddressing

04/07/15 Ritula Thakur 114

Page 115: 8051 Microcontroller

ExampleExample

04/07/15 Ritula Thakur 115

Page 116: 8051 Microcontroller

Chapter 6Chapter 6Arithmetic Instructions and ProgramsArithmetic Instructions and Programs

Page 117: 8051 Microcontroller

OutlinesOutlines

� Range of numbers in 8051 unsigned data� Addition & subtraction instructions for unsigned

data� BCD system of data representation� Packed and unpacked BCD data� Addition & subtraction on BCD data� Range of numbers in 8051 signed data� Signed data arithmetic instructions� Carry & overflow problems & corrections

04/07/15 Ritula Thakur 117

Page 118: 8051 Microcontroller

Addition of Unsigned NumbersAddition of Unsigned Numbers

� ADD A, source ; A = A + source

04/07/15 Ritula Thakur 118

Page 119: 8051 Microcontroller

04/07/15 Ritula Thakur 119

Page 120: 8051 Microcontroller

Addition of Individual BytesAddition of Individual Bytes

04/07/15 Ritula Thakur 120

Page 121: 8051 Microcontroller

ADDC & Addition of 16-bit ADDC & Addition of 16-bit NumbersNumbers

13C E73B 8D78 74

+

04/07/15 Ritula Thakur 121

Page 122: 8051 Microcontroller

BCD Number SystemBCD Number System

� Unpacked BCD: 1 byte� Packed BCD: 4 bits

04/07/15 Ritula Thakur 122

Page 123: 8051 Microcontroller

Adding BCD Numbers & DA Adding BCD Numbers & DA InstructionInstruction

MOVMOV A,#17HA,#17H

ADDADD A,#28HA,#28H

MOVMOV A,#47HA,#47H ;A=47H first BCD operand;A=47H first BCD operandMOVMOV B,#25HB,#25H ;B=25 second BCD operand;B=25 second BCD operand

ADDADD A,BA,B ;hex (binary) addition (A=6CH);hex (binary) addition (A=6CH)

DADA AA ;adjust for BCD addition ;adjust for BCD addition (A=72H)(A=72H)

HEXHEX BCDBCD

2929 0010 10010010 1001

++ 1818 + +0001 10000001 1000

4141 0100 00010100 0001 AC=1AC=1

++ 6 6 + + 0110 0110

4747 0100 01110100 0111

04/07/15 Ritula Thakur 123

Page 124: 8051 Microcontroller

ExampleExample

04/07/15 Ritula Thakur 124

Page 125: 8051 Microcontroller

Subtraction of Unsigned NumbersSubtraction of Unsigned Numbers

� SUBB A, source ; A = A – source – CY� SUBB when CY = 0

� Take 2’s complement of subtraend (source)� Add it to minuend� Invert carry

04/07/15 Ritula Thakur 125

Page 126: 8051 Microcontroller

Example (Positive Result)Example (Positive Result)

04/07/15 Ritula Thakur 126

Page 127: 8051 Microcontroller

Example (Negative Result)Example (Negative Result)

04/07/15 Ritula Thakur 127

Page 128: 8051 Microcontroller

SUBB When CY = 1SUBB When CY = 1

� For multibyte numbers

04/07/15 Ritula Thakur 128

Page 129: 8051 Microcontroller

Multiplication of Unsigned Multiplication of Unsigned NumbersNumbers

� MUL AB ; A × B, place 16-bit result in B and AMOVMOV A,#25HA,#25H ;load 25H to reg. A;load 25H to reg. A

MOVMOV B,#65HB,#65H ;load 65H in reg. B;load 65H in reg. B

MULMUL ABAB ;25H * 65H = E99 where;25H * 65H = E99 where

;B = 0EH and A = 99H;B = 0EH and A = 99H

Table 6-1:Unsigned Multiplication Summary (MUL AB)Table 6-1:Unsigned Multiplication Summary (MUL AB)

MultiplicationMultiplication Operand 1Operand 1 Operand 2Operand 2 ResultResult

byte byte ×× bytebyte AA BB A=low byte,A=low byte,

B=high byteB=high byte

04/07/15 Ritula Thakur 129

Page 130: 8051 Microcontroller

Division of Unsigned NumbersDivision of Unsigned Numbers

� DIV AB ; divide A by B

MOVMOV A,#95HA,#95H ;load 95 into A;load 95 into A

MOVMOV B,#10HB,#10H ;load 10 into B;load 10 into B

DIVDIV ABAB ;now A = 09 (quotient) and ;now A = 09 (quotient) and

;B = 05 (remainder);B = 05 (remainder)

Table 6-2:Unsigned Division Summary (DIV AB)Table 6-2:Unsigned Division Summary (DIV AB)

DivisionDivision NumeratorNumerator DenominatorDenominator QuotientQuotient RemainderRemainder

byte / bytebyte / byte AA BB AA BB

04/07/15 Ritula Thakur 130

Page 131: 8051 Microcontroller

Example ( 1 of 2 )Example ( 1 of 2 )

04/07/15 Ritula Thakur 131

Page 132: 8051 Microcontroller

Example ( 2 of 2 )Example ( 2 of 2 )

04/07/15 Ritula Thakur 132

Page 133: 8051 Microcontroller

Signed 8-bit OperandsSigned 8-bit Operands

� Covert to 2’s complement� Write magnitude of number in 8-bit binary (no

sign)� Invert each bit� Add 1 to it

04/07/15 Ritula Thakur 133

Page 134: 8051 Microcontroller

ExampleExample

04/07/15 Ritula Thakur 134

Page 135: 8051 Microcontroller

ExampleExample

04/07/15 Ritula Thakur 135

Page 136: 8051 Microcontroller

ExampleExample

04/07/15 Ritula Thakur 136

Page 137: 8051 Microcontroller

Byte-sized Signed Numbers RangesByte-sized Signed Numbers Ranges

DecimalDecimal BinaryBinary HexHex

-128-128 1000 00001000 0000 8080

-127-127 1000 00011000 0001 8181

-126-126 1000 00101000 0010 8282

…….. …………………… ....

-2-2 1111 11101111 1110 FEFE

-1-1 1111 11111111 1111 FFFF

00 0000 00000000 0000 0000

+1+1 0000 00010000 0001 0101

+2+2 0000 00100000 0010 0202

…… …………………… ......

+127+127 0111 11110111 1111 7F7F

04/07/15 Ritula Thakur 137

Page 138: 8051 Microcontroller

Overflow in Signed Number Overflow in Signed Number OperationsOperations

04/07/15 Ritula Thakur 138

Page 139: 8051 Microcontroller

When Is the OV Flag Set?When Is the OV Flag Set?

� Either: there is a carry from D6 to D7 but no carry out of D7 (CY = 0)

� Or: there is a carry from D7 out (CY = 1) but no carry from D6 to D7

04/07/15 Ritula Thakur 139

Page 140: 8051 Microcontroller

ExampleExample

04/07/15 Ritula Thakur 140

Page 141: 8051 Microcontroller

ExampleExample

04/07/15 Ritula Thakur 141

Page 142: 8051 Microcontroller

ExampleExample

04/07/15 Ritula Thakur 142

Page 143: 8051 Microcontroller

Chapter 7Chapter 7LOGIC INSTRUCTIONS AND LOGIC INSTRUCTIONS AND PROGRAMSPROGRAMS

Page 144: 8051 Microcontroller

OutlinesOutlines

� Define the truth tables for logic functions AND, OR, XOR

� Code 8051 Assembly language logic function instructions

� Use 8051 logic instructions for bit manipulation� Use compare and jump instructions for program

control� Code 8051 rotate and swap instructions� Code 8051 programs for ASCII and BCD data

conversion

04/07/15 Ritula Thakur 144

Page 145: 8051 Microcontroller

ANDAND

XX YY X AND YX AND Y

00 00 00

00 11 00

11 00 00

11 11 11

ANL destination, source ;dest = dest AND source

04/07/15 Ritula Thakur 145

Page 146: 8051 Microcontroller

OROR

ORL destination, source ;dest = dest OR ORL destination, source ;dest = dest OR sourcesourceXX YY X AND YX AND Y

00 00 00

00 11 11

11 00 11

11 11 11

04/07/15 Ritula Thakur 146

Page 147: 8051 Microcontroller

XORXOR

XRL destination, source ;dest = dest XOR XRL destination, source ;dest = dest XOR sourcesourceXX YY X AND YX AND Y

00 00 00

00 11 11

11 00 11

11 11 00

XRL A,#04H ;EX-OR A with 0000 010004/07/15 Ritula Thakur 147

Page 148: 8051 Microcontroller

XORXOR

04/07/15 Ritula Thakur 148

Page 149: 8051 Microcontroller

XORXOR

04/07/15 Ritula Thakur 149

Page 150: 8051 Microcontroller

CPL (complement accumulator)CPL (complement accumulator)

MOVMOV A,#55HA,#55H

CPLCPL AA ;now A=AAH;now A=AAH

;0101 0101(55H) becomes ;0101 0101(55H) becomes ;1010 1010 (AAH);1010 1010 (AAH)

04/07/15 Ritula Thakur 150

Page 151: 8051 Microcontroller

Compare instructionCompare instruction

CJNE destination, source ,relative addressCJNE destination, source ,relative address

04/07/15 Ritula Thakur 151

Page 152: 8051 Microcontroller

Table 7-1:Carry Flag Setting For CJNE InstructionTable 7-1:Carry Flag Setting For CJNE Instruction

CompareCompare Carry FlagCarry Flag

destination > sourcedestination > source CY = 0CY = 0

destination < sourcedestination < source CY = 1CY = 1

CJNE R5,#80,NOT_EQUAL ;check R5 for 80…. ;R5=80

NOT_EQUAL: JNC NEXT ;jump if R5>80…. ;R5<80

NEXT: ….

04/07/15 Ritula Thakur 152

Page 153: 8051 Microcontroller

04/07/15 Ritula Thakur 153

Page 154: 8051 Microcontroller

04/07/15 Ritula Thakur 154

Page 155: 8051 Microcontroller

04/07/15 Ritula Thakur 155

Page 156: 8051 Microcontroller

04/07/15 Ritula Thakur 156

Page 157: 8051 Microcontroller

Rotating the bits of A right and leftRotating the bits of A right and left

RRRR AA ;rotate right A;rotate right A

MOV A,#36H ;A=0011 0110RR A ;A=0001 1011RR A ;A=1000 1101RR A ;A=1100 0110RR A ;A=0110 0011

RL A ;rotate left A

04/07/15 Ritula Thakur 157

Page 158: 8051 Microcontroller

MOV A,#72H ;A=0111 0010RL A ;A=1110 0100RL A ;A=1100 1001

04/07/15 Ritula Thakur 158

Page 159: 8051 Microcontroller

Rotating through the carryRotating through the carry

RRCRRC A A ;rotate right through carry;rotate right through carry

CLR C ;make CY=0MOV A,#26H ;A=0010 0110RRC A ;A=0001 0011 CY=0RRC A ;A=0000 1001 CY=1RRC A ;A=1000 0100 CY=1

RLC A ;rotate left through carry

04/07/15 Ritula Thakur 159

Page 160: 8051 Microcontroller

SETB C ;make CY=1MOV A,#15H ;A=0001 0101RLC A ;A=0010 1010 CY=0RLC A ;A=0101 0110 CY=0RLC A ;A=1010 1100 CY=0RLC A ;A=0101 1000 CY=1

04/07/15 Ritula Thakur 160

Page 161: 8051 Microcontroller

SWAPSWAP AA

04/07/15 Ritula Thakur 161

Page 162: 8051 Microcontroller

04/07/15 Ritula Thakur 162

Page 163: 8051 Microcontroller

RRC A ;first bit to carryMOV P1.3,C ;output carry as data bitRRC A ;second bit to carryMOV P1.3,C ;output carry as data bitRRC A ;third bit to carryMOV P1.3,C ;output carry as data bit…..

04/07/15 Ritula Thakur 163

Page 164: 8051 Microcontroller

BCD AND ASCII APPLICATION BCD AND ASCII APPLICATION PROGRAMPROGRAM

04/07/15 Ritula Thakur 164

Page 165: 8051 Microcontroller

Packed BCD to ASCII conversionPacked BCD to ASCII conversion

Packed BCDPacked BCD Unpacked BCDUnpacked BCD ASCIIASCII

29H29H 02H & 09H02H & 09H 32H & 39H32H & 39H

0010 10010010 1001 0000 0010 &0000 0010 & 0011 0010 &0011 0010 &

0000 10010000 1001 0011 10010011 1001

04/07/15 Ritula Thakur 165

Page 166: 8051 Microcontroller

ASCII to packed BCD conversionASCII to packed BCD conversion

Key ASCII Unpacked BCDKey ASCII Unpacked BCD Packed BCDPacked BCD

44 34 00000100 34 00000100

77 37 00000111 01000111 or 47H 37 00000111 01000111 or 47H

MOV A,#’4’ ;A=34H, hex for ASCII char 4MOV R1,#’7’ ;R1=37H, hex for ASCII char 7ANL A,#0FH ;mask upper nibble (A=04)ANL R1,#0FH ;mask upper nibble (R1=07)SWAP A ;A=40HORL A,R1 ;A=47H, packed BCD

04/07/15 Ritula Thakur 166

Page 167: 8051 Microcontroller

04/07/15 Ritula Thakur 167

Page 168: 8051 Microcontroller

Chapter 8Chapter 8SINGLE-BIT INSTRUCTIONS SINGLE-BIT INSTRUCTIONS AND PROGRAMMINGAND PROGRAMMING

Page 169: 8051 Microcontroller

OutlinesOutlines

� List the 8051 Assembly language instructions for bit manipulation

� Code 8051 instructions for bit manipulation of ports� Explain which 8051 registers are bit-addressable� Describe which portions of the 8051 RAM are bit-

addressable� Discuss bit manipulation of the carry flag� Describe the carry flag bit-related instructions of the

8051

04/07/15 Ritula Thakur 169

Page 170: 8051 Microcontroller

Single-bit instructionsSingle-bit instructions

04/07/15 Ritula Thakur 170

Page 171: 8051 Microcontroller

I/O ports and bit-addressabilityI/O ports and bit-addressability

The 8051 has four I/O ports, each of which is 8 bits

04/07/15 Ritula Thakur 171

Page 172: 8051 Microcontroller

04/07/15 Ritula Thakur 172

Page 173: 8051 Microcontroller

04/07/15 Ritula Thakur 173

Page 174: 8051 Microcontroller

04/07/15 Ritula Thakur 174

Page 175: 8051 Microcontroller

04/07/15 Ritula Thakur 175

Page 176: 8051 Microcontroller

Checking an input bitChecking an input bit

JNB (jump if no bit) ; JB (jump if bit = 1)

04/07/15 Ritula Thakur 176

Page 177: 8051 Microcontroller

Registers and bit-addressabilityRegisters and bit-addressability

04/07/15 Ritula Thakur 177

Page 178: 8051 Microcontroller

04/07/15 Ritula Thakur 178

Page 179: 8051 Microcontroller

Figure 8-2. Bits of the PSW Register

04/07/15 Ritula Thakur 179

Page 180: 8051 Microcontroller

04/07/15 Ritula Thakur 180

Page 181: 8051 Microcontroller

Bit-addressable RAMBit-addressable RAM

04/07/15 Ritula Thakur 181

Page 182: 8051 Microcontroller

04/07/15 Ritula Thakur 182

Page 183: 8051 Microcontroller

04/07/15 Ritula Thakur 183

Page 184: 8051 Microcontroller

Single-bit operations with CYSingle-bit operations with CY

04/07/15 Ritula Thakur 184

Page 185: 8051 Microcontroller

04/07/15 Ritula Thakur 185

Page 186: 8051 Microcontroller

04/07/15 Ritula Thakur 186

Page 187: 8051 Microcontroller

04/07/15 Ritula Thakur 187

Page 188: 8051 Microcontroller

Instructions for reading input portInstructions for reading input port

READING INPUT PINS VS. PORT LATCHREADING INPUT PINS VS. PORT LATCH

In Reading a port:1.Read the status of the input pin2.Read the internal latch of the output port

04/07/15 Ritula Thakur 188

Page 189: 8051 Microcontroller

Reading latch for output portReading latch for output port

04/07/15 Ritula Thakur 189

Page 190: 8051 Microcontroller

8051 Timers8051 Timers

� Basic Registers of the timer� Timer 0 registers� Timer 1 registers

Both the registers are 16 bits wide but can be accessed as 8 bit registers separately.

04/07/15 Ritula Thakur 190

Page 191: 8051 Microcontroller

PROGRAMMING 8051 TIMERSPROGRAMMING 8051 TIMERS

Timer 0 registersTimer 0 registersTL0 ( timer 0 low byte )TH0 ( timer 0 high byte )

04/07/15 Ritula Thakur 191

Page 192: 8051 Microcontroller

Timer 1 registersTimer 1 registersTL1 ( timer 1 low byte )TH1 ( timer 1 high byte )

04/07/15 Ritula Thakur 192

Page 193: 8051 Microcontroller

04/07/15 Ritula Thakur

Timer Interrupt & OptimizeTimer Interrupt & Optimize

TMOD Register - Operation ControlTMOD Register - Operation Control

TIMER1 TIMER2

M1 M1M0GATE C/T GATE C/T M0

(MSB) (LSB)

GATE Gating Control

When Set Timer/Counter “x”is enabled

Only while “INTx”pin is high and “TRx” control pin is set

When Cleared Timer “x” is enabled

Whenever “TRx” control bit is set

C/T Timer or Counter Selector

Cleared for Timer operation (input from internal system clock).

Set for Counter operation (input from “Tx” input pin)193

Page 194: 8051 Microcontroller

04/07/15 Ritula Thakur

Chap3. Timer Interrupt & OptimizeChap3. Timer Interrupt & Optimize

TMOD Register - Operating Mode Control (0 , 1, 2 )TMOD Register - Operating Mode Control (0 , 1, 2 )

TIMER1 TIMER2

GATE C/T GATE C/TM1 M1M0 M0

(MSB) (LSB)

8048 TIMER : ”TLx” serves as 5-bit prescaler.

16-bit Timer/Counter : “THx” and “TLx” are cascaded8-bit auto reload“THx” hold a value which is to be Reloaded into “TLx” each time it overflows

M1 M0 Function

0 0

0 1

1 0

194

Page 195: 8051 Microcontroller

� Indicate which mode and which timer are selected for each of the following:� MOV TMOD,#01H� MOV TMOD,#20H

� TMOD=00000001, mode 1 of Timer 0� TMOD=00100000, mode 2 of Timer 1

04/07/15 Ritula Thakur 195

Page 196: 8051 Microcontroller

04/07/15 Ritula Thakur 196

Page 197: 8051 Microcontroller

Clock Source for timerClock Source for timer

� The crystal frequency attached to the 8051 timer is the source of the clock for the timer.

� The frequency for the timer is always 1/12th the frequency of the crystal attached to the 8051

04/07/15 Ritula Thakur 197

Page 198: 8051 Microcontroller

Mode 1 programmingMode 1 programming

� 1. It is a 16-bit timer; so it allows values of 0000 to FFFFH to be loaded into the timer’s registers TL and TH

� After TH and TL are loaded with a 16 bit initial value, the timer must be started (SETB TR0 and SETB TR1)

� It starts counting up until it reaches its limit of FFFFH. When it rolls over from FFFFH to 0000, it sets high TF (Timer flag)

� Stop the timer (CLR TR0 & CLR TR1)� 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 reset to 0

04/07/15 Ritula Thakur 198

Page 199: 8051 Microcontroller

Mode 1 programmingMode 1 programming1.Loaded value into TL and TH2.”SETB TR0” for timer 0 ;”SETB TR1” for timer 13.If TF (timer flag) = high “CLR TR0” or “CLR TR1”4.Reloaded TH and TL value, TF reset to 0

04/07/15 Ritula Thakur 199

Page 200: 8051 Microcontroller

Steps to program in mode 1Steps to program in mode 11.Load the TMOD value2.Load registers TL and TH 3.Start the timer (SETB TR0 or SETB TR1)4.Keep monitoring the timer flag (TF)5.Stop the timer (CLR TR0 or CLR TR1)6.Clear the TF flag7.Go back to step 2

04/07/15 Ritula Thakur 200

Page 201: 8051 Microcontroller

Calculate Timer DelayCalculate Timer Delay

04/07/15 Ritula Thakur 201

Page 202: 8051 Microcontroller

04/07/15 Ritula Thakur 202

Page 203: 8051 Microcontroller

04/07/15 Ritula Thakur 203

Page 204: 8051 Microcontroller

04/07/15 Ritula Thakur 204

Page 205: 8051 Microcontroller

Finding values to be loaded into the timerFinding values to be loaded into the timerAssuming XTAL =11.0592MHz from Example 9-10

1.Divide the desired time delay by 1.085μs2.Perform 65536-n, where n is the decimal value we got in Step 13.Convert the result of Step 2 to hex, where yyxx is the initial hex value to be loaded into the timer’s registers4.Set TL = xx and TH = yy

04/07/15 Ritula Thakur 205

Page 206: 8051 Microcontroller

04/07/15 Ritula Thakur 206

Page 207: 8051 Microcontroller

04/07/15 Ritula Thakur

Chap3. Timer Interrupt & OptimizeChap3. Timer Interrupt & Optimize

osc 12÷

1/ =TC CONTROL

TL1

(8BITS)

TH1

(8BITS)TF1 INTERRUPT

GATE

PININT1

TR1

T1 PIN

0/ =TC

Timer/Counter 1 Mode 1:16-bit Counter

Timer Mode - Mode 1

207

Page 208: 8051 Microcontroller

Mode 0Mode 0

Like mode 1 except that it is a 13-bit timer

Mode 2 ProgrammingMode 2 Programming

1.Loaded value into TH (8-bit timer)2.”SETB TR0” for timer 0 ;”SETB TR1” for timer 13.If TF (timer flag) = high “CLR TR0” or “CLR TR1”4.Reloaded TL value kept by TH

04/07/15 Ritula Thakur 208

Page 209: 8051 Microcontroller

Steps to program in mode 2Steps to program in mode 21.Load the TMOD value2.Load the TH registers 3.Start the timer4.Keep monitoring the timer flag (TF)5.Clear the TF flag7.Go back to step 4

04/07/15 Ritula Thakur 209

Page 210: 8051 Microcontroller

04/07/15 Ritula Thakur 210

Page 211: 8051 Microcontroller

04/07/15 Ritula Thakur 211

Page 212: 8051 Microcontroller

8051 SERIAL COMMUNICATION8051 SERIAL COMMUNICATION

Page 213: 8051 Microcontroller

OutlinesOutlines

� Contrast and compare serial versus parallel communication

� List the advantages of serial communication over parallel

� Explain serial communication protocol� Contrast synchronous versus asynchronous

communication� Contrast half-versus full-duplex transmission

04/07/15 Ritula Thakur 213

Page 214: 8051 Microcontroller

♦ Explain the process of data framingExplain the process of data framing♦ Describe data transfer rate and bps rateDescribe data transfer rate and bps rate♦ Define the RS232 standardDefine the RS232 standard♦ Explain the use of the MAX232 and Explain the use of the MAX232 and

MAX233 chipsMAX233 chips♦ Interface the 8051 with an RS232 Interface the 8051 with an RS232

connectorconnector♦ Discuss the baud rate of the 8051Discuss the baud rate of the 8051♦ Describe serial communication features of Describe serial communication features of

the 8051the 8051♦ Program the 8051 for serial data Program the 8051 for serial data

communicationcommunication

04/07/15 Ritula Thakur 214

Page 215: 8051 Microcontroller

Basics of serial communicationBasics of serial communication

04/07/15 Ritula Thakur 215

Page 216: 8051 Microcontroller

04/07/15 Ritula Thakur 216

Page 217: 8051 Microcontroller

Start and stop bitsStart and stop bits

04/07/15 Ritula Thakur 217

Page 218: 8051 Microcontroller

RS232 pinsRS232 pins

04/07/15 Ritula Thakur 218

Page 219: 8051 Microcontroller

Data communication classificationData communication classification

04/07/15 Ritula Thakur 219

Page 220: 8051 Microcontroller

RxD and TxD pins in the 8051RxD and TxD pins in the 8051

� TxD pin 11 of the 8051 (P3.1)� RxD pin 10 of the 8051 (P3.0)

04/07/15 Ritula Thakur 220

Page 221: 8051 Microcontroller

MAX232MAX232

04/07/15 Ritula Thakur 221

Page 222: 8051 Microcontroller

MAX233MAX233

04/07/15 Ritula Thakur 222

Page 223: 8051 Microcontroller

8051 SERIAL COMMUNICATION 8051 SERIAL COMMUNICATION PROGRAMMINGPROGRAMMING

04/07/15 Ritula Thakur 223

Page 224: 8051 Microcontroller

Baud Rate in 8051Baud Rate in 8051

� The baud rate in 8051 is programmable (done with the help of timer 1)

� Relation b/w crystal frequency and baud rate� 8051 divides crystal frequency by 12 to get m/c

cycle frequency� 8051’s serial commn UART crctry divides it by

32 once more� 11.0592/12=921.6 kHz� 921.6 kHz/32=28,800 Hz

04/07/15 Ritula Thakur 224

Page 225: 8051 Microcontroller

04/07/15 Ritula Thakur 225

Page 226: 8051 Microcontroller

04/07/15 Ritula Thakur 226

Page 227: 8051 Microcontroller

SBUF registerSBUF register

MOV SBUF,#’D’ ;load SBUF=44H, ASCII for ‘D’

MOV SBUF,A ;copy accumulator into SBUF

MOV A,SBUF ;copy SBUF into accumulator

04/07/15 Ritula Thakur 227

Page 228: 8051 Microcontroller

SCON (Serial control) registerSCON (Serial control) register

04/07/15 Ritula Thakur 228

Page 229: 8051 Microcontroller

SM0,SM1SM0,SM1

SM0 and SM1 are D7 and D6 of the SCON

SM0 SM1 0 0 Serial Mode 0 0 1 Serial Mode 1,8 bit data, 1 stop bit, 1 start bit 1 0 Serial Mode 2 1 1 Serial Mode 3

04/07/15 Ritula Thakur 229

Page 230: 8051 Microcontroller

Programming the 8051 to transfer data Programming the 8051 to transfer data seriallyserially

� TMOD register is loaded with 20H to set the baud rate)� TH1 is loaded to set the baud rate for serial data transfer� SCON is loaded with 50H� TR1 is set to start the Timer 1� Character byte to be transferred is written into SBUF

register� TI flag is monitored to see if the character has been

transferred completely� TI is cleared by CLR TI� To transfer the next character, go to Step 6

04/07/15 Ritula Thakur 230

Page 231: 8051 Microcontroller

Programming the 8051 to transfer Programming the 8051 to transfer data seriallydata serially

04/07/15 Ritula Thakur 231

Page 232: 8051 Microcontroller

04/07/15 Ritula Thakur 232

Page 233: 8051 Microcontroller

Programming the 8051 to receive data Programming the 8051 to receive data seriallyserially

� TMOD register is loaded with 20H to set the baud rate)� TH1 is loaded to set the baud rate for serial data transfer� SCON is loaded with 50H� TR1 is set to start the Timer 1� Character byte to be transferred is written into SBUF

register� RI flag is monitored to see if the character has been

transferred completely� RI is cleared by CLR TI

� To receive the next character, go to Step 6

04/07/15 Ritula Thakur 233

Page 234: 8051 Microcontroller

Programming the 8051 to receive Programming the 8051 to receive data seriallydata serially

04/07/15 Ritula Thakur 234

Page 235: 8051 Microcontroller

04/07/15 Ritula Thakur 235

Page 236: 8051 Microcontroller

04/07/15 Ritula Thakur 236

Page 237: 8051 Microcontroller

04/07/15 Ritula Thakur 237

Page 238: 8051 Microcontroller

Doubling the baud rate in the Doubling the baud rate in the 80518051

1. To use a higher frequency crystal2. To change a bit in the PCON register

SMODSMOD ---- ---- ---- GF1GF1 GF0GF0 PDPD IDLIDL

D7 D0

MOV A,PCON ;place a copy of PCON in ACCSETB ACC.7 ;make D7=1 MOV PCON,A ;now SMOD=1 without ;changing any other bits

04/07/15 Ritula Thakur 238

Page 239: 8051 Microcontroller

Baud rates for SMOD=0Baud rates for SMOD=0

Machine cycle freq. = 11.0592 MHz / 12 = 921.6 kHzand921.6 kHz / 32 = 28,800 Hz since SMOD = 0

04/07/15 Ritula Thakur 239

Page 240: 8051 Microcontroller

Baud rates for SMOD=1Baud rates for SMOD=1

Machine cycle freq. = 11.0592 MHz / 12 = 921.6 kHzand921.6 kHz / 16 = 57,600 Hz since SMOD = 1

04/07/15 Ritula Thakur 240

Page 241: 8051 Microcontroller

04/07/15 Ritula Thakur 241

Page 242: 8051 Microcontroller

04/07/15 Ritula Thakur 242

Page 243: 8051 Microcontroller

04/07/15 Ritula Thakur 243

Page 244: 8051 Microcontroller

Chapter 11Chapter 11INTERRUPTS PROGRAMMINGINTERRUPTS PROGRAMMING

Page 245: 8051 Microcontroller

OutlinesOutlines

� Contrast and compare interrupts versus polling� Explain the purpose of the ISR� List the 6 interrupts of the 8051� Explain the purpose of the interrupt vector

table� Enable or disable 8051 interrupts� Program the 8051 timers using interrupts� Describe the two external hardware interrupts

of the 805104/07/15 Ritula Thakur 245

Page 246: 8051 Microcontroller

♦ Contrast edge-triggered with level-triggered Contrast edge-triggered with level-triggered interruptsinterrupts

♦ Program the 8051 for interrupt-based serial Program the 8051 for interrupt-based serial communicationcommunication

♦ Define the interrupt priority of the 8051Define the interrupt priority of the 8051

04/07/15 Ritula Thakur 246

Page 247: 8051 Microcontroller

Six interrupts in the 8051Six interrupts in the 8051

04/07/15 Ritula Thakur 247

Page 248: 8051 Microcontroller

Step in enabling an interruptStep in enabling an interrupt

04/07/15 Ritula Thakur 248

Page 249: 8051 Microcontroller

04/07/15 Ritula Thakur 249

Page 250: 8051 Microcontroller

PROGRAMMING TIMER PROGRAMMING TIMER INTERRUPTSINTERRUPTS

04/07/15 Ritula Thakur 250

Page 251: 8051 Microcontroller

Roll-over timer flag and Roll-over timer flag and interruptinterruptJNB TF, target

04/07/15 Ritula Thakur 251

Page 252: 8051 Microcontroller

04/07/15 Ritula Thakur 252

Page 253: 8051 Microcontroller

04/07/15 Ritula Thakur 253

Page 254: 8051 Microcontroller

04/07/15 Ritula Thakur 254

Page 255: 8051 Microcontroller

04/07/15 Ritula Thakur 255

Page 256: 8051 Microcontroller

Programming external Programming external hardware interruptshardware interrupts

04/07/15 Ritula Thakur 256

Page 257: 8051 Microcontroller

04/07/15 Ritula Thakur 257

Page 258: 8051 Microcontroller

04/07/15 Ritula Thakur 258

Page 259: 8051 Microcontroller

Sampling the low level-Sampling the low level-triggered interrupttriggered interrupt

04/07/15 Ritula Thakur 259

Page 260: 8051 Microcontroller

Edge-triggered interruptsEdge-triggered interrupts

04/07/15 Ritula Thakur 260

Page 261: 8051 Microcontroller

04/07/15 Ritula Thakur 261

Page 262: 8051 Microcontroller

04/07/15 Ritula Thakur 262

Page 263: 8051 Microcontroller

Sampling the edge-triggered Sampling the edge-triggered interruptinterrupt

04/07/15 Ritula Thakur 263

Page 264: 8051 Microcontroller

RI and TI flags and interruptsRI and TI flags and interrupts

04/07/15 Ritula Thakur 264

Page 265: 8051 Microcontroller

Use of serial COM in the 8051Use of serial COM in the 8051

04/07/15 Ritula Thakur 265

Page 266: 8051 Microcontroller

04/07/15 Ritula Thakur 266

Page 267: 8051 Microcontroller

04/07/15 Ritula Thakur 267

Page 268: 8051 Microcontroller

Clearing RI and TI before the Clearing RI and TI before the RETI instructionRETI instructionCLR TI ; CLR RI

04/07/15 Ritula Thakur 268

Page 269: 8051 Microcontroller

04/07/15 Ritula Thakur 269

Page 270: 8051 Microcontroller

04/07/15 Ritula Thakur 270

Page 271: 8051 Microcontroller

Interrupt priority upon resetInterrupt priority upon reset

04/07/15 Ritula Thakur 271

Page 272: 8051 Microcontroller

04/07/15 Ritula Thakur 272

Page 273: 8051 Microcontroller

04/07/15 Ritula Thakur 273

Page 274: 8051 Microcontroller

Setting interrupt priority with the IP Setting interrupt priority with the IP registerregister

04/07/15 Ritula Thakur 274

Page 275: 8051 Microcontroller

04/07/15 Ritula Thakur 275