8051 Microcontroller MCA

100
June 8, 2022 1 8051 MICROCONTOLLER ARCHITECTURE DEPARTMENT OF ECE MVGR COLLEGE OF ENGINEERING CHINTALAVALASA,VIZIANAGARAM

Transcript of 8051 Microcontroller MCA

  • 8051 MICROCONTOLLER ARCHITECTURE

    DEPARTMENT OF ECEMVGR COLLEGE OF ENGINEERINGCHINTALAVALASA,VIZIANAGARAM

  • ContentsIntroduction to embedded systemsThe 8051 architectureMicro controller hardwarePorts External memoryTimers & CountersSerial port communicationInterrupts

  • Contents:Microcontrollers MicrocontrollersWhy should we study them?

    Where they are being used?

    vs. Microprocessor

    Need for them

    What is it?

  • 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

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

  • A smaller computerOn-chip RAM, ROM, I/O ports...ExampleMotorolas 6811, Intels 8051, Zilogs Z8 and PIC 16X RAM ROMI/O PortTimerSerial COM PortMicrocontrollerCPUA 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

  • 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

  • Microcontroller ArchitecturesCPU

    Program or Data

    Address BusData BusMemoryVon NeumannArchitectureCPU

    Program

    Address BusData BusHarvardArchitectureMemory

    Data

    Address BusFetch Bus0002n

  • 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 8 bit 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.

  • OscillatorXTAL Connection to 8051Using a quartz crystal oscillatorWe can observe the frequency on the XTAL2 pin.

  • 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.

    Solution:

    (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

  • Registers

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

  • RESET PinPower-On RESET Circuit30 pF30 pF8.2 K10 uF+Vcc11.0592 MHzEA/VPPX1X2RST3119189

  • 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).

  • Memory mapping in 8051

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

  • RAM memory space allocation in the 8051

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

    7F78

    1A100F080706050403020100

  • 8051 Flag bits and the PSW register PSW Register

  • Instructions that Affect Flag Bits:Note: X can be 0 or 1

  • 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

  • SPECIAL FUNCTION REGISTERS

  • SPECIAL FUNCTION REGISTERS

  • I/O PROGRAMMING

  • I/O Port Programming.Port 1 is denoted by P1.P1.0 ~ P1.7We use P1 as examples to show the operations on ports.P1 as an output port (i.e., write CPU data to the external pin)P1 as an input port (i.e., read pin data into CPU bus)Port 1pins 1-8

  • Other PinsP1, P2, and P3 have internal pull-up resisters.P1, P2, and P3 are not open drain.P0 has no internal pull-up resistors and does not connects to Vcc inside the 8051.P0 is open drain.Compare the figures of P1.X and P0.X. However, for a programmer, it is the same to program P0, P1, P2 and P3.All the ports upon RESET are configured as output.

  • Hardware Structure of I/O Pin Each pin of I/O portsInternal CPU buscommunicate with CPUA D latch store the value of this pinD latch is controlled by Write to latchWrite to latch1write data into the D latch2 Tri-state bufferTB1: controlled by Read pinRead pin1really read the data present at the pinTB2: controlled by Read latchRead latch1read value from internal latchA transistor M1 gateGate=0: openGate=1: close

  • Tri-state BufferOutputInputTri-state control (active high)LHLowHighimpedance (open-circuit)HHLH

  • A Pin of Port 1 8051 ICP0.x

  • Writing 1 to Output Pin P1.X8051 IC2. output pin is Vcc1. write a 1 to the pin10output 1TB1TB2

  • Writing 0 to Output Pin P1.X8051 IC2. output pin is ground1. write a 0 to the pin01output 0TB1TB2

  • Port 1 as OutputWrite to a PortSend data to Port 1

    MOV A,#55H BACK: MOV P1,A ACALLDELAYCPL ASJMP BACK

    Let P1 toggle.You can write to P1 directly.

  • Reading High at Input Pin8051 IC2. MOV A,P1 external pin=Highwrite a 1 to the pin MOV P1,#0FFH103. Read pin=1 Read latch=0 Write to latch=11TB1TB2

  • Reading Low at Input Pin8051 IC2. MOV A,P1external pin=Lowwrite a 1 to the pinMOV P1,#0FFH103. Read pin=1 Read latch=0 Write to latch=10TB1TB2

  • Port 1 as InputRead from PortIn order to make P1 an input, the port must be programmed by writing 1 to all the bit.

    MOV A,#0FFH ;A=11111111BMOV P1,A ;make P1 an input port BACK:MOV A,P1 ;get data from P0MOV P2,A ;send data to P2SJMP BACK

    To be an input port, P0, P1, P2 and P3 have similar methods.

  • Port 2pins 21-28Port 2 does not need any pull-up resistors since it already has pull-up resistors internally.In an 8031-based system, P2 are used to provide address A8-A15.

  • Port 3pins 10-17Port 3 does not need any pull-up resistors since it already has pull-up resistors internally.Although port 3 is configured as an output port upon reset, this is not the way it is most commonly used.Port 3 has the additional function of providing signals.Serial communications signalRxD, TxDChapter 10External interrupt/INT0, /INT1Chapter 11Timer/counterT0, T1Chapter 9External memory accesses in 8031-based system/WR, /RDChapter 14

  • Port 3 Alternate Functions

  • A Pin of Port 0 8051 IC P1.x

  • Port 0pins 32-39P0 is an open drain.Open drain is a term used for MOS chips in the same way that open collector is used for TTL chips. When P0 is used for simple data I/O we must connect it to external pull-up resistors.Each pin of P0 must be connected externally to a 10K ohm pull-up resistor.With external pull-up resistors connected upon reset, port 0 is configured as an output port.

  • Port 0 with Pull-Up Resistors

  • Dual Role of Port 0When connecting an 8051/8031 to an external memory, the 8051 uses ports to send addresses and read instructions.8031 is capable of accessing 64K bytes of external memory.16-bit addressP0 provides both address A0-A7, P2 provides address A8-A15.Also, P0 provides data lines D0-D7. When P0 is used for address/data multiplexing, it is connected to the 74LS373 to latch the address.There is no need for external pull-up resistors as shown in Chapter 14.

  • 74LS373

  • Reading ROM (1/2)1. Send address to ROM2. 74373 latches the address and send to ROMAddress

  • Reading ROM (2/2)2. 74373 latches the address and send to ROMAddress3. ROM send the instruction back

  • ALE PinThe ALE pin is used for de-multiplexing the address and data by connecting to the G pin of the 74LS373 latch.When ALE=0, P0 provides data D0-D7.When ALE=1, P0 provides address A0-A7.The reason is to allow P0 to multiplex address and data.

  • Timer/Counter Logic

  • PROGRAMMING 8051 TIMERSTimer 0 registersTL0 ( timer 0 low byte ) TH0 ( timer 0 high byte )

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

  • TMOD (timer mode) register

  • Operation of Timer on Mode-0

  • Operation of Timer in Mode 1

  • Operation of Timer in Mode 2

  • Operation of Timer in Mode 3

  • 8051 SERIAL COMMUNICATION

  • Basics of serial communication

  • Start and stop bits

  • SCON (Serial control) register

  • SM0,SM1SM0 and SM1 are D7 and D6 of the SCONSM0 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

  • Doubling the baud rate in the 8051To use a higher frequency crystalTo change a bit in the PCON registerD7D0MOV 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

    SMOD------GF1GF0PDIDL

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

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

  • INTERRUPTS PROGRAMMING

  • INTERRUPTS PROGRAMMING

  • Six interrupts in the 8051

  • Step in enabling an interrupt

  • Writing the ISRExample:Writing the ISR for Timer0 interruptORG 0000H;resetLJMP MAINORG 000BH;Timer0 entry pointT0ISR:.;Timer0 ISR begins.RETI;return to main programMAIN:.;main program..END

  • Interrupt priority upon reset

  • 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

    COUNTEQU25h..MOVR3,#COUNT

  • Structure of Assembly language and Running an 8051 program

    ORG0H MOVR5,#25H MOVR7,#34H MOVA,#0 ADDA,R5 ADDA,#12HHERE: SJMPHERE END

  • 8051 Instruction SetACALL: Absolute Call ADD, ADDC: Add Acc. (With Carry) AJMP: Absolute Jump ANL: Bitwise AND CJNE: Compare & Jump if Not Equal CLR: Clear Register CPL: Complement Register DA: Decimal Adjust DEC: Decrement Register DIV: Divide Accumulator by B DJNZ: Dec. Reg. & Jump if Not Zero INC: Increment Register JB: Jump if Bit Set JBC: Jump if Bit Set and Clear Bit JC: Jump if Carry Set JMP: Jump to Address JNB: Jump if Bit Not Set JNC: Jump if Carry Not Set JNZ: Jump if Acc. Not Zero JZ: Jump if Accumulator Zero LCALL: Long Call LJMP: Long Jump MOV: Move Memory MOVC: Move Code Memory MOVX: Move Extended Memory MUL: Multiply Accumulator by B NOP: No Operation ORL: Bitwise OR POP: Pop Value From Stack PUSH: Push Value Onto Stack RET: Return From Subroutine RETI: Return From Interrupt RL: Rotate Accumulator Left RLC: Rotate Acc. Left Through Carry RR: Rotate Accumulator Right RRC: Rotate Acc. Right Through Carry SETB: Set Bit SJMP: Short Jump SUBB: Sub. From Acc. With Borrow SWAP: Swap Accumulator Nibbles XCH: Exchange Bytes XCHD: Exchange Digits XRL: Bitwise Exclusive OR Undefined: Undefined Instruction

  • THANK YOUANY QUERIES

    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,6040versatility : any number of applications for PCProgram is to read data from P0 and then send data to P1Program is to read data from P0 and then send data to P1

    Open drain is a term used for MOS chips in the same way that open collector is used for TTL chips.