of 68 /68

8051 Microcontroller-day1

Embed Size (px)


Embedded systems PPT

Text of 8051 Microcontroller-day1




  • *ContentsIntroduction to microcontrollers8051 microcontroller architectureAssembly language programming of 80518051 interfacing KEIL-IDE: OverviewProgramming in KEIL-IDE Vision 3Applications

  • *ObjectivesParticipants will be able toKnow about microcontrollersUnderstand 8051 architectureProgram the 8051 MicrocontrollerDevelop software for embedded systems with the help of KEIL-IDEDesign small scale Embedded systems

  • *Plan?Day 1Session 1: Introduction to microcontrollersSession 2: 8051 architecture: an overviewSession 3: KEIL-IDE Vision 3: an overviewSession 4: LAB1: Programming Basics Day 2Session 1:Programming 8051 internalsSession2 :Interfacing 8051Session3&4 : LAB2: Programming 8051 in KEIL


  • *Contents:Microcontrollers MicrocontrollersWhy should we study them?

    Where they are being used?

    vs. Microprocessor

    Need for them

    What is it?

    Blah .blahblah

  • *Why we should study microprocessor or microcontroller?They are in curriculum They are used in personal computersIf the above are only reasons, see the statistics where they are being used

  • *They are being used in Embedded systems

    Embedded system means the processor is embedded into that application.

    An embedded product uses a microprocessor or microcontroller to do one task only.

    In an embedded system, there is only one application software that is typically burned into ROM.

    Exampleprinter, keyboard, video game player

  • *A short list of embedded systemsAnd the list goes on and onElectronic instrumentsElectronic toys/gamesFactory controlFax machinesFingerprint identifiersHome security systemsLife-support systemsMedical testing systemsModemsMPEG decodersNetwork cardsNetwork switches/routersOn-board navigationPagersPhotocopiersWashers and dryers

    Anti-lock brakesAuto-focus camerasAutomatic teller machinesAutomatic toll systemsAutomatic transmissionAvionic systemsBattery chargersCamcordersCell phonesCell-phone base stationsCordless phonesCruise controlCurbside check-in systemsDisk drivesDigital camerasElectronic card readersPortable video gamesPrintersSatellite phonesScannersSmart ovens/dishwashersSpeech recognizersStereo systemsTeleconferencing systemsTelevisionsTemperature controllersTheft tracking systemsTV set-top boxesVCRs, DVD playersVideo game consolesVideo phones

  • *

  • *CPUGeneral-Purpose Micro-processorRAMROMI/O PortTimerSerial COM PortData BusAddress BusGeneral-Purpose Microprocessor SystemCPU for ComputersNo RAM, ROM, I/O on CPU chip itselfExampleIntels x86, Motorolas 680x0Many chips on mothers boardGeneral-purpose microprocessor

  • * RAM ROMI/O PortTimerSerial COM PortMicrocontrollerCPUA smaller computerOn-chip RAM, ROM, I/O ports...ExampleMotorolas 6811, Intels 8051, Zilogs Z8 and PIC 16XA single chipMicrocontroller :

  • *Microprocessor CPU is stand-alone, RAM, ROM, I/O, timer are separatedesigner can decide on the amount of ROM, RAM and I/O ports.expensiveversatility general-purpose

    MicrocontrollerCPU, RAM, ROM, I/O and timer are all on a single chipfix amount of on-chip ROM, RAM, I/O portsfor applications in which cost, power and space are criticalsingle-purposeMicroprocessor vs. Microcontroller

  • *Need for microcontrollerfor applications in which cost, power and space are criticalExample : TV remote control

  • *What is a Microcontroller?A microcontroller is an entire computer manufactured on a single chipThey have a high concentration of on-chip facilities such as serial ports, parallel input/output ports, Timers & counters, interrupt control, analog-to-digital converters, random access memory, read only memory, etc.

  • *What are its AdvantagesCost is very lessAround Rs.100Consumes less powerTypically milliwatts while operatingNanowatts in sleeping and idle modeMaking it ideal for low power embedded systemsLow spaceNeeds no extra circuitryPossible to build handy systems

  • *Common MicrocontrollersAtmel ARM Intel 8-bit 8XC42 MCS48 MCS51 8xC251 16-bit MCS96 MXS296 National Semiconductor COP8 Microchip 12-bit instruction PIC 14-bit instruction PIC PIC16F84 16-bit instruction PIC NEC

    Motorola 8-bit 68HC05 68HC08 68HC11 16-bit 68HC12 68HC16 32-bit 683xx Texas Instruments TMS370 MSP430 Zilog Z8 Z86E02

  • *meeting the computing needs of the task efficiently and cost effectivelyspeed, the amount of ROM and RAM, the number of I/O ports and timers, size, packaging, power consumptioneasy to upgradecost per unitavailability of software development toolsassemblers, debuggers, C compilers, emulator, simulator, technical supportwide availability and reliable sources of the microcontrollers.Three criteria in Choosing a Microcontroller

  • *Microcontroller ArchitecturesCPU

    Program or Data

    Address BusData BusMemoryVon NeumannArchitectureCPU


    Address BusData BusHarvardArchitectureMemory


    Address BusFetch Bus0002n

  • *Embedded Products Using MicrocontrollersHomeAppliances, 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

  • *Embedded Products Using MicrocontrollersOfficeTelephones, computers, security systems, fax machines, microwave, copier, laser printer, color printer, paging

  • *Embedded Products Using MicrocontrollersAutoTrip computer, engine control, air bag, ABS, instrumentation, security system, transmission control, entertainment, climate control, cellular phone, keyless entry

  • *BlahblahblahHistory of microcontrollersEarly days:Transistor to microprocessorMedieval days: First microcontroller was introduced by Intel in late 1970s.ie 8051Present :more than 100 companies are producing microcontrollers in different variants Future 64/128 bit microcontrollers with more on chip circuitry with less powerSize may reduce to nano scale

  • *DAY 1End of session 1

  • *DAY 1Session 2

  • *Contents8051 architectureComparison of the 8051 Family MembersPin DescriptionSFRRAMROMAssembly language basicsInstruction set

  • *About 8051It was the first microcontroller developed by Intel in early 80sIt is an 8 bit microcontroller & a 40 pin ICNow produced by many companies in many variationsThe most popular microcontroller about 50% of market share

  • *More about 8051Harvard architecture single chip microcontroller (physically separate storage and signal pathways for their instructions and data )

    Typically contains Processor (CPU). 4K Bytes ROM128 Bytes RAMtwo timer/counters (16 bit)A serial port4 general purpose parallel input/output portInterrupt controller

    The 8051 can address 64K of external data memory and 64K of External program memory.

  • *Block DiagramCPUOn-chip RAMOn-chip ROM for program code4 I/O PortsTimer 0Serial PortOSCInterrupt ControlExternal interruptsTimer 1Timer/CounterBus ControlTxD RxDP0 P1 P2 P3Address/DataCounter Inputs

  • *Feature 8051 8052 8031ROM (program space in bytes) 4K 8K 0KRAM (bytes) 128 256 128Timers 2 3 2I/O pins 32 32 32 Serial port 1 1 1 Interrupt sources 6 8 6

    Comparison of the 8051 Family Members

  • *

  • *Pin Description of the 8051DIP

  • *Pins of 80511/4Vccpin 40Vcc provides supply voltage to the chip. The voltage source is +5V.GNDpin 20groundXTAL1 and XTAL2pins 19,18These 2 pins provide external clock.Way 1using a quartz crystal oscillator Way 2using a TTL oscillator Example 4-1 shows the relationship between XTAL and the machine cycle.

  • *Pins of 80512/4RSTpin 9resetIt is an input pin and is active highnormally low.The high pulse must be high at least 2 machine cycles.It is a power-on reset.Upon applying a high pulse to RST, the microcontroller will reset and all values in registers will be lost.Reset values of some 8051 registers Way 1Power-on reset circuit Way 2Power-on reset with debounce

  • *Pins of 80513/4EApin 31external accessThere is no on-chip ROM in 8031 and 8032 .The EA pin is connected to GND to indicate the code is stored externally.PSEN ALE are used for external ROM.For 8051, /EA pin is connected to Vcc.PSENpin 29program store enableThis is an output pin and is connected to the OE pin of the ROM.

  • *Pins of 80514/4ALEpin 30address latch enableIt is an output pin and is active high.8051 port 0 provides both address and data.The ALE pin is used for de-multiplexing the address and data by connecting to the G pin of the 74LS373 latch.I/O port pinsThe four ports P0, P1, P2, and P3.Each port uses 8 pins.All I/O pins are bi-directional.

  • *Figure 4-2 (a). XTAL Connection to 8051Using a quartz crystal oscillatorWe can observe the frequency on the XTAL2 pin.

  • *Figure 4-2 (b). XTAL Connection to an External Clock Source

    Using a TTL oscillatorXTAL2 is unconnected.

  • *Example :Find the machine cycle for(a) XTAL = 11.0592 MHz (b) XTAL = 16 MHz.


    (a) 11.0592 MHz / 12 = 921.6 kHz; machine cycle = 1 / 921.6 kHz = 1.085 s(b) 16 MHz / 12 = 1.333 MHz; machine cycle = 1 / 1.333 MHz = 0.75 s

  • *RESET Value of Some 8051 Registers:0000DPTR0007SP0000PSW0000B0000ACC0000PCReset ValueRegisterRAM are all zero.

  • *Figure 4-3 (a). Power-On RESET Circuit30 pF30 pF8.2 K10 uF+Vcc11.0592 MHzEA/VPPX1X2RST3119189

  • *Figure 4-3 (b). Power-On RESET with DebounceEA/VPPX1X2RSTVcc10 uF8.2 K30 pF931

  • *Pins of I/O PortThe 8051 has four I/O portsPort 0 pins 32-39P0P0.0P0.7Port 1pins 1-8 P1P1.0P1.7Port 2pins 21-28P2P2.0P2.7Port 3pins 10-17P3P3.0P3.7Each port has 8 pins.Named P0.X X=0,1,...,7, P1.X, P2.X, P3.XExP0.0 is the bit 0LSBof P0 ExP0.7 is the bit 7MSBof P0These 8 bits form a byte.Each port can be used as input or output (bi-direction).

  • *Registers

  • *Memory mapping in 8051

    ROM memory map in 8051 family4kDS5000-328k32kfrom Atmel Corporationfrom Dallas Semiconductor

  • *RAM memory space allocation in the 8051

  • *8051 Flag bits and the PSW register PSW Register

  • *Review Binary/HexadecimalConversions:

    1000 1110 (binary) 8 E (hex)Notations for hex:





    DecimalBinaryHexadecimal 00000 0100011200102300113401004501015601106701117810008910019101010A111011B121100C131101D141110E151111F

  • *Some Simple InstructionsMOV dest,source ; dest = source

    MOVA,#72H;A=72HMOV A, #r;A=r OR 72HMOVR4,#62H;R4=62HMOVB,0F9H;B=the content of F9th byte of RAM


    MOVP1,A;mov A to port 1

  • *Bit Addressable Memory20h 2Fh (16 locations X 8-bits = 128 bits)27262524232221202F2E2D2C2B2A2928Bit addressing:mov C, 1Ahormov C, 23h.2



  • *Addressing ModesImmediateRegisterDirectRegister IndirectIndexed

  • *Immediate Addressing ModeMOVA,#65HMOVA,#AMOVR6,#65HMOVDPTR,#2343HMOVP1,#65H

  • *Register Addressing ModeMOVRn, A;n=0,..,7ADDA, RnMOVDPL, R6


  • *Direct Addressing ModeAlthough the entire of 128 bytes of RAM can be accessed using direct addressing mode, it is most often used to access RAM loc. 30 7FH.

    MOVR0, 40HMOV56H, AMOVA, 4; MOV A, R4MOV6, 2; copy R2 to R6; MOV R6,R2 is invalid !

    SFR register and their address

    MOV0E0H, #66H; MOV A,#66HMOV0F0H, R2; MOV B, R2MOV80H,A; MOV P1,A

    Bit Addressable Page 359,360

  • *Register Indirect Addressing ModeIn this mode, register is used as a pointer to the data.

    MOVA,@Ri; move content of RAM loc. Where address is held by Ri into A ( i=0 or 1 )[email protected],B

    In other word, the content of register R0 or R1 is sources or target in MOV, ADD and SUBB instructions.Example:Write a program to copy a block of 10 bytes from RAM location starting at 37h to RAM location starting at 59h.

    Solution:MOV R0,37h; source pointerMOV R1,59h; dest pointer MOV R2,10; counterL1: MOV A,@R0MOV @R1,AINC R0INC R1DJNZ R2,L1

  • *Indexed Addressing ModeThis mode is widely used in accessing data elements of look-up table entries located in the program (code) space ROM at the 8051

    MOVCA,@A+DPTR MOVC A,@A+PC A= content of address A +DPTR (or A+PC) from ROM Note:Because the data elements are stored in the program (code ) space ROM of the 8051, it uses the instruction MOVC instead of MOV. The C means code.

    MOVC is normally used with internal or external ROM and can address 4K of internal or 64K of external code

  • *Accessing external RAMIt is possible to expand RAM by connecting it externallyOpcodes that access this external memory always use indirect addressing to specify the external memory

    MOVXA,@Rp MOVX A,@DPTR MOVX @DPTR,A [email protected],ARp can address 256 bytes;DPTR can address 64K bytesNOTE:Rp can be either R0 or R1

  • *

    ADDA, Source;A=A+SOURCE


    ADDA,R6;A=A+R6ADD A,6;A=A+[6] or A=A+R6


  • *Example:MOVA,#38HADDA,#2FH

    38 00111000+2F+00101111 ---- -------------- 67 01100111CY=0 AC=1P=1Example:MOVA,#88HADDA,#93H

    88 10001000+93+10010011 ---- -------------- 11B 00011011CY=1 AC=0P=0Example:MOVA,#9CHADDA,#64H

    9C 10011100+64+01100100 ---- -------------- 100 00000000CY=1 AC=1P=0

  • *SUBBA,source;A=A-source-CY




  • *MUL & DIVMULAB;B|A = A*BMOVA,#25HMOVB,#65HMULAB;25H*65H=0E99;B=0EH, A=99HDIVAB;A = A/B, B = A mod BMOVA,#25MOVB,#10DIVAB;A=2, B=5

  • *Stack in the 8051The 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:MOVR6,#25HMOVR1,#12HMOVR4,#0F3HPUSH6PUSH1PUSH4

  • *SETBbit; bit=1CLRbit; bit=0

    SETBC; CY=1SETBP0.0;bit 0 from port 0 =1SETBP3.7;bit 7 from port 3 =1SETBACC.2;bit 2 from ACCUMULATOR =1SETB05;set high D5 of RAM loc. 20h


    CLR instruction is as same as SETBi.e:CLRC;CY=0

    But following instruction is only for CLR:CLRA;A=0Bit Addressable Page 359,360

  • *DECbyte;byte=byte-1INCbyte;byte=byte+1

    INCR7INC40HINCDPTRDECADEC40H; [40]=[40]-1NOTE: DEC DPTR is not possible

    CPLA;1s complementExample:MOVA,#55H;A=01010101 BL01:CPLAMOVP1,AACALLDELAYSJMPL01NOP CALL

  • *Assembler DirectivesORG (origin)Is to indicate beginning of the addressORG0000H or ORG 8000HENDThis indicates the assembler the end of the source fileIt is last line of programEQU (equate)This is used to define a constant without occupying memory location


  • *Structure of Assembly language and Running an 8051 program





    ****processor Embedded system applications (game, accounting, fax, mail...)A printer is an example of embedded system since the processor inside it performs one task only.*Intels x86: 8086,8088,80386,80486, PentiumMotorolas 680x0: 68000, 68010, 68020,68030,6040*versatility : any number of applications for PC**Program is to read data from P0 and then send data to P1