The 8051 Microcontroller And Embedded

Transcription

The 8051 Microcontroller and EmbeddedSystemsUsing Assembly and CSecond EditionMuhammad Ali MazidiJanice Gillispie MazidiRolin D. McKinlayCONTENTS Introduction to ComputingThe 8051 Microcontrollers8051 Assembly Language ProgrammingBranch InstructionsI/O Port Programming8051 Addressing ModesArithmetic & Logic Instructions And Programs8051 Programming in C8051 Hardware Connection and Hex File8051 Timer/Counter Programming in Assembly and C8051 Serial Port Programming in Assembly and CInterrupts Programming in Assembly and C8051 Interfacing to External Memory8051 Real World Interfacing I: LCD,ADC ANDSENSORS LCD and Keyboard Interfacing 8051 Interfacing with 8255

INTRODUCTION TOCOMPUTINGThe 8051 Microcontroller and EmbeddedSystems: Using Assembly and CMazidi, Mazidi and McKinlayChung-Ping Young楊中平Home Automation, Networking, and Entertainment LabDept. of Computer Science and Information EngineeringNational Cheng Kung University, TAIWAN

OUTLINES HANELNumbering and coding systemsDigital primerInside the computerDepartment of Computer Science and Information EngineeringNational Cheng Kung University, TAIWAN2

NUMBERINGAND CODINGSYSTEMSDecimal andBinary NumberSystemsHANEL Human beings use base 10 (decimal)arithmetic¾ There are 10 distinct symbols, 0, 1, 2, ,9Computers use base 2 (binary) system¾¾There are only 0 and 1These two binary digits are commonlyreferred to as bitsDepartment of Computer Science and Information EngineeringNational Cheng Kung University, TAIWAN3

NUMBERINGAND CODINGSYSTEMSConvertingfrom Decimalto Binary Divide the decimal number by 2repeatedlyKeep track of the remaindersContinue this process until the quotientbecomes zeroWrite the remainders in reverse orderto obtain the binary numberEx. Convert 2510 to binaryQuotientRemainder25/2 121LSB (least significant bit)12/2 606/2 303/2 111/2 01MSB (most significant bit)Therefore 2510 110012HANELDepartment of Computer Science and Information EngineeringNational Cheng Kung University, TAIWAN4

NUMBERINGAND CODINGSYSTEMS Know the weight of each bit in a binarynumberAdd them together to get its decimalequivalentConvertingEx. Convert 110012 to decimalfrom Binary toWeight:242322DecimalDigits:110Sum: 16 8 0 2120010 1 2510Use the concept of weight to convert adecimal number to a binary directlyEx. Convert 3910 to binary32 0 0 4 2 1 39Therefore, 3910 1001112HANELDepartment of Computer Science and Information EngineeringNational Cheng Kung University, TAIWAN5

NUMBERINGAND CODINGSYSTEMSHexadecimalSystem Base 16, thehexadecimal system,is used as aconvenientrepresentation ofbinary numbers¾ex.It is much easier torepresent a string of 0sand 1s such as100010010110 as itshexadecimal equivalent 0111101111Hex0123456789ABCDEFDepartment of Computer Science and Information EngineeringNational Cheng Kung University, TAIWAN6

NUMBERINGAND CODINGSYSTEMS To represent a binary number as itsequivalent hexadecimal number¾Start from the right and group 4 bits at atime, replacing each 4-bit binary numberwith its hex equivalentConvertingbetween Binary Ex. Represent binary 100111110101 in hex1001 1111 0101and Hex 9F5To convert from hex to binary¾Each hex digit is replaced with its 4-bitbinary equivalentEx. Convert hex 29B to binary HANEL29B001010011011Department of Computer Science and Information EngineeringNational Cheng Kung University, TAIWAN7

NUMBERINGAND CODINGSYSTEMSConvertingfrom Decimalto Hex Convert to binary first and thenconvert to hexConvert directly from decimal to hexby repeated division, keeping track ofthe remaindersEx. Convert 4510 to hex3216842110110132 8 4 1 454510 0010 11012 2D16Ex. Convert 62910 to hex512 256 128 64 32 16 8 4 2 11001110 1 0 162910 512 64 32 16 4 1 0010 0111 01012 27516HANELDepartment of Computer Science and Information EngineeringNational Cheng Kung University, TAIWAN8

NUMBERINGAND CODINGSYSTEMSConvertingfrom Hex toDecimal Convert from hex to binary and then todecimalConvert directly from hex to decimalby summing the weight of all digitsEx. 6B216 0110 1011 001021024 512 256 128 64 32 16 8 4 2 1110101100 1 01024 512 128 32 16 2 171410HANELDepartment of Computer Science and Information EngineeringNational Cheng Kung University, TAIWAN9

NUMBERINGAND CODINGSYSTEMSAddition of HexNumbers Adding the digits together from theleast significant digits¾¾If the result is less than 16, write that digitas the sum for that positionIf it is greater than 16, subtract 16 from itto get the digit and carry 1 to the nextdigitEx. Perform hex addition: 23D9 94BE23D9 94BEB897HANELLSD: 911MSD: 2 14 2313 11 253 4 89 B23 – 16 7 w/ carry25 – 16 9 w/ carryDepartment of Computer Science and Information EngineeringNational Cheng Kung University, TAIWAN10

NUMBERINGAND CODINGSYSTEMSSubtraction ofHex NumbersHANEL If the second digit is greater than thefirst, borrow 16 from the precedingdigitEx. Perform hex subtraction: 59F – 2B859F– 2B82E7LSD: 15 – 8 79 16 – 11 14 E165–1–2 2Department of Computer Science and Information EngineeringNational Cheng Kung University, TAIWAN11

NUMBERINGAND CODINGSYSTEMSThe ASCII (pronounced “ask-E”) codeassigns binary patterns for¾¾ASCII Code¾ Numbers 0 to 9All the letters of English alphabet,uppercase and lowercaseMany control codes and punctuationmarksThe ASCII system uses 7 bits torepresent each codeSelected ASCII 626364.797Aabcd yzDepartment of Computer Science and Information EngineeringNational Cheng Kung University, TAIWAN12

DIGITALPRIMER Binary Logic Two voltage levels can be representedas the two digits 0 and 1Signals in digital electronics have twodistinct voltage levels with built-intolerances for variations in the voltageA valid digital signal should be withineither of the two shaded areas543210HANELLogic 1Logic 0Department of Computer Science and Information EngineeringNational Cheng Kung University, TAIWAN13

DIGITALPRIMER AND gateLogic GatesComputer Science Illuminated, Dale and Lewis OR gateComputer Science Illuminated, Dale and LewisHANELDepartment of Computer Science and Information EngineeringNational Cheng Kung University, TAIWAN14

DIGITALPRIMER Tri-state bufferInverterLogic Gates(cont’)Computer Science Illuminated, Dale and Lewis XOR gateComputer Science Illuminated, Dale and LewisHANELDepartment of Computer Science and Information EngineeringNational Cheng Kung University, TAIWAN15

DIGITALPRIMER NAND gateLogic Gates(cont’)Computer Science Illuminated, Dale and Lewis NOR gateComputer Science Illuminated, Dale and LewisHANELDepartment of Computer Science and Information EngineeringNational Cheng Kung University, TAIWAN16

DIGITALPRIMERHalf adderLogic DesignUsing GatesFull adderDigital Design, ManoHANELDepartment of Computer Science and Information EngineeringNational Cheng Kung University, TAIWAN17

DIGITALPRIMER4-bit adderLogic DesignUsing Gates(cont’)Digital Design, ManoHANELDepartment of Computer Science and Information EngineeringNational Cheng Kung University, TAIWAN18

DIGITALPRIMERLogic DesignUsing Gates Decoders¾Decoders are widely used for addressdecoding in computer designAddress Decoders(cont’)HANELAddress decoder for 9 (10012)Address decoder for 5 (01012)The output will be 1 if andonly if the input is 10012The output will be 1 if andonly if the input is 01012Department of Computer Science and Information EngineeringNational Cheng Kung University, TAIWAN19

DIGITALPRIMER Flip-flops¾Flip-flops are frequently used to store dataLogic DesignUsing Gates(cont’)Digital Design, ManoHANELDepartment of Computer Science and Information EngineeringNational Cheng Kung University, TAIWAN20

INSIDE THECOMPUTER The unit of data size¾ImportantTerminology¾¾¾ 0 or 1Byte : 8 bitsNibble : half of a bye, or 4 bitsWord : two bytes, or 16 bitsThe terms used to describe amounts ofmemory in IBM PCs and compatibles¾¾¾¾HANELBit : a binary digit that can have the valueKilobyte (K): 210 bytesMegabyte (M) : 220 bytes, over 1 millionGigabyte (G) : 230 bytes, over 1 billionTerabyte (T) : 240 bytes, over 1 trillionDepartment of Computer Science and Information EngineeringNational Cheng Kung University, TAIWAN21

INSIDE THECOMPUTERInternalOrganization ofComputers CPU (Central Processing Unit)¾ I/O (Input/output) devices¾ Execute information stored in memoryProvide a means of communicating withCPUMemory¾RAM (Random Access Memory) –temporary storage of programs thatcomputer is running The data is lost when computer is off¾ROM (Read Only Memory) – containsprograms and information essential tooperation of the computer The information cannot be changed by use,and is not lost when power is off– It is called nonvolatile memoryHANELDepartment of Computer Science and Information EngineeringNational Cheng Kung University, TAIWAN22

INSIDE THECOMPUTERInternalOrganization ofComputers(cont’)Address busMemoryCPU(RAM, ROM)Peripherals(monitor,printer, etc.)Data busHANELDepartment of Computer Science and Information EngineeringNational Cheng Kung University, TAIWAN23

INSIDE THECOMPUTER The CPU is connected to memory andI/O through strips of wire called a bus¾Carries information from place to place Address bus Data bus Control busInternalOrganization ofComputers(cont’)Address ardData busControl busDepartment of Computer Science and Information EngineeringNational Cheng Kung University, TAIWAN24

INSIDE THECOMPUTER Address bus¾InternalOrganization ofComputers The address assigned to a given device mustbe unique The CPU puts the address on the address bus,and the decoding circuitry finds the device(cont’) Data bus¾ The CPU either gets data from the deviceor sends data to itControl bus¾HANELFor a device (memory or I/O) to berecognized by the CPU, it must beassigned an addressProvides read or write signals to thedevice to indicate if the CPU is asking forinformation or sending it informationDepartment of Computer Science and Information EngineeringNational Cheng Kung University, TAIWAN25

INSIDE THECOMPUTER The more data buses available, thebetter the CPU¾More aboutData Bus More data buses mean a moreexpensive CPU and computer¾ HANELThe average size of data buses in CPUsvaries between 8 and 64Data buses are bidirectional¾ Think of data buses as highway lanesTo receive or send dataThe processing power of a computer isrelated to the size of its busesDepartment of Computer Science and Information EngineeringNational Cheng Kung University, TAIWAN26

INSIDE THECOMPUTERMore aboutAddress Bus The more address buses available, thelarger the number of devices that canbe addressedThe number of locations with which aCPU can communicate is always equalto 2x, where x is the address lines,regardless of the size of the data bus¾¾ HANELex. a CPU with 24 address lines and 16data lines can provide a total of 224 or 16Mbytes of addressable memoryEach location can have a maximum of 1byte of data, since all general-purposeCPUs are byte addressableThe address bus is unidirectionalDepartment of Computer Science and Information EngineeringNational Cheng Kung University, TAIWAN27

INSIDE THECOMPUTERCPU’s Relationto RAM andROM For the CPU to process information,the data must be stored in RAM orROM, which are referred to as primarymemory ROM provides information that is fixedand permanent¾ Tables or initialization programRAM stores information that is notpermanent and can change with time¾¾Various versions of OS and applicationpackagesCPU gets information to be processed first form RAM (or ROM) if it is not there, then seeks it from a massstorage device, called secondary memory, andtransfers the information to RAMHANELDepartment of Computer Science and Information EngineeringNational Cheng Kung University, TAIWAN28

INSIDE THECOMPUTER Registers¾The CPU uses registers to storeinformation temporarily Values to be processed Address of value to be fetched from memoryInside CPUs¾In general, the more and bigger theregisters, the better the CPU Registers can be 8-, 16-, 32-, or 64-bit The disadvantage of more and bigger registersis the increased cost of such a CPUHANELDepartment of Computer Science and Information EngineeringNational Cheng Kung University, TAIWAN29

Program CounterInside CPUsInstruction RegisterFlagsALUInstruction decoder,timing, and controlInternalbusesRegister ARegister BControl Bus Data Bus(cont’)Address BusINSIDE THECOMPUTERRegister CRegister DHANELDepartment of Computer Science and Information EngineeringNational Cheng Kung University, TAIWAN30

INSIDE THECOMPUTERInside CPUs(cont’) ALU (arithmetic/logic unit)¾ Performs arithmetic functions such as add,subtract, multiply, and divide, and logicfunctions such as AND, OR, and NOTProgram counter¾Points to the address of the nextinstruction to be executed As each instruction is executed, the programcounter is incremented to point to the addressof the next instruction to be executed Instruction decoder¾Interprets the instruction fetched into theCPU A CPU capable of understanding moreinstructions requires more transistors to designHANELDepartment of Computer Science and Information EngineeringNational Cheng Kung University, TAIWAN31

INSIDE THECOMPUTERInternalWorking ofComputersEx. A CPU has registers A, B, C, and D and it has an 8-bitdata bus and a 16-bit address bus. The CPU can accessmemory from addresses 0000 to FFFFHAssume that the code for the CPU to move a value toregister A is B0H and the code for adding a value toregister A is 04HThe action to be performed by the CPU is to put 21H intoregister A, and then add to register A values 42H and 12H.HANELDepartment of Computer Science and Information EngineeringNational Cheng Kung University, TAIWAN32

INSIDE THECOMPUTERInternalWorking ofComputers(cont’)Ex. (cont’)ActionCodeDataMove value 21H into reg. AAdd value 42H to reg. AAdd value 12H to reg. AB0H04H04H21H42H12HMem. addr.1400140114021403140414051406Contents of memory address(B0) code for moving a value to register A(21) value to be moved(04) code for adding a value to register A(42) value to be added(04) code for adding a value to register A(12) value to be added(F4) code for halt.HANELDepartment of Computer Science and Information EngineeringNational Cheng Kung University, TAIWAN33

Ex. (cont’)The actions performed by CPU are as follows:1. The program counter is set to the value 1400H,indicating the address of the first instruction code tobe executedINSIDE THECOMPUTERInternalWorking ofComputers2.¾(cont’)The CPU puts 1400H on address bus and sends itout ¾以動畫表示The memory circuitry finds the locationThe CPU activates the READ signal, indicating tomemory that it wants the byte at location 1400H This causes the contents of memory location1400H, which is B0, to be put on the data bus andbrought into the CPU.HANELDepartment of Computer Science and Information EngineeringNational Cheng Kung University, TAIWAN34

INSIDE THECOMPUTEREx. (cont’)3.¾¾InternalWorking ofComputersThe CPU decodes the instruction B0The CPU commands its controller circuitry to bringinto register A of the CPU the byte in the nextmemory location (cont’)¾The value 21H goes into register AThe program counter points to the address of thenext instruction to be executed, which is 1402H Address 1402 is sent out on the address bus tofetch the next instruction.HANELDepartment of Computer Science and Information EngineeringNational Cheng Kung University, TAIWAN35

INSIDE THECOMPUTEREx. (cont’)4.¾¾InternalWorking ofComputers¾(cont’)From memory location 1402H it fetches code 04HAfter decoding, the CPU knows that it must add tothe contents of register A the byte sitting at thenext address (1403)After the CPU brings the value (42H), it providesthe contents of register A along with this value tothe ALU to perform the addition It then takes the result of the addition from theALU’s output and puts it in register AThe program counter becomes 1404, the addressof the next instruction.HANELDepartment of Computer Science and Information EngineeringNational Cheng Kung University, TAIWAN36

INSIDE THECOMPUTEREx. (cont’)5.¾InternalWorking ofComputers(cont’) This code is again adding a value to register AThe program counter is updated to 1406H6.¾¾HANELAddress 1404H is put on the address bus and thecode is fetched into the CPU, decoded, andexecutedThe contents of address 1406 are fetched in andexecutedThis HALT instruction tells the CPU to stopincrementing the program counter and asking forthe next instructionDepartment of Computer Science and Information EngineeringNational Cheng Kung University, TAIWAN37

8051 MICROCONTROLLERSThe 8051 Microcontroller and EmbeddedSystems: Using Assembly and CMazidi, Mazidi and McKinlayChung-Ping Young楊中平Home Automation, Networking, and Entertainment LabDept. of Computer Science and Information EngineeringNational Cheng Kung University, TAIWAN

OUTLINES HANELMicrocontrollers and embeddedprocessorsOverview of the 8051 familyDepartment of Computer Science and Information EngineeringNational Cheng Kung University, TAIWAN2

ervs. GeneralPurposeMicroprocessor General-purpose microprocessorscontains¾¾¾ Microcontroller has¾¾¾¾¾¾HANELNo RAMNo ROMNo I/O portsCPU (microprocessor)RAMROMI/O portsTimerADC and other peripheralsDepartment of Computer Science and Information EngineeringNational Cheng Kung University, TAIWAN3

ervs. eMicroProcessorData busRAMTimerSerialCOMPortCPUAddress SerialCOMPortDepartment of Computer Science and Information EngineeringNational Cheng Kung University, TAIWAN4

ervs. GeneralPurposeMicroprocessor General-purpose microprocessors¾¾¾ Microcontroller¾(cont’)¾HANELMust add RAM, ROM, I/O ports, andtimers externally to make them functionalMake the system bulkier and much moreexpensiveHave the advantage of versatility on theamount of RAM, ROM, and I/O portsThe fixed amount of on-chip ROM, RAM,and number of I/O ports makes them idealfor many applications in which cost andspace are criticalIn many applications, the space it takes,the power it consumes, and the price perunit are much more critical considerationsthan the computing powerDepartment of Computer Science and Information EngineeringNational Cheng Kung University, TAIWAN5

ersfor EmbeddedSystems An embedded product uses amicroprocessor (or microcontroller) todo one task and one task only¾ There is only one application software thatis typically burned into ROMA PC, in contrast with the embeddedsystem, can be used for any number ofapplications¾¾It has RAM memory and an operatingsystem that loads a variety of applicationsinto RAM and lets the CPU run themA PC contains or is connected to variousembedded products Each one peripheral has a microcontroller insideit that performs only one taskHANELDepartment of Computer Science and Information EngineeringNational Cheng Kung University, TAIWAN6

ersfor EmbeddedSystems Home¾ Office¾(cont’) Telephones, computers, security systems, faxmachines, microwave, copier, laser printer, colorprinter, pagingAuto¾HANELAppliances, intercom, telephones, security systems,garage door openers, answering machines, faxmachines, home computers, TVs, cable TV tuner,VCR, camcorder, remote controls, video games,cellular phones, musical instruments, sewingmachines, lighting control, paging, camera, pinballmachines, toys, exercise equipmentTrip computer, engine control, air bag, ABS,instrumentation, security system, transmissioncontrol, entertainment, climate control, cellularphone, keyless entryDepartment of Computer Science and Information EngineeringNational Cheng Kung University, TAIWAN7

MICROCONTROLLERSANDEMBEDDEDPROCESSORSx86 PCEmbeddedApplications ¾ HANELMany manufactures of general-purposemicroprocessors have targeted theirmicroprocessor for the high end of theembedded marketThere are times that a microcontroller isinadequate for the taskWhen a company targets a generalpurpose microprocessor for theembedded market, it optimizes theprocessor used for embedded systemsVery often the terms embeddedprocessor and microcontroller are usedinterchangeablyDepartment of Computer Science and Information EngineeringNational Cheng Kung University, TAIWAN8

MICROCONTROLLERSANDEMBEDDEDPROCESSORSx86 PCEmbeddedApplications(cont’) One of the most critical needs of anembedded system is to decreasepower consumption and spaceIn high-performance embeddedprocessors, the trend is to integratemore functions on the CPU chip and letdesigner decide which features he/shewants to useIn many cases using x86 PCs for thehigh-end embedded applications¾Saves money and shortens developmenttime A vast library of software already written Windows is a widely used and well understoodplatformHANELDepartment of Computer Science and Information EngineeringNational Cheng Kung University, TAIWAN9

MICROCONTROLLERSANDEMBEDDEDPROCESSORSChoosing aMicrocontrollerHANEL 8-bit microcontrollers¾¾¾¾ Motorola’s 6811Intel’s 8051Zilog’s Z8Microchip’s PICThere are also 16-bit and 32-bitmicrocontrollers made by various chipmakersDepartment of Computer Science and Information EngineeringNational Cheng Kung University, TAIWAN10

MICROCONTROLLERSANDEMBEDDEDPROCESSORSCriteria forChoosing aMicrocontroller Meeting the computing needs of thetask at hand efficiently and wer consumptionThe amount of RAM and ROM on chipThe number of I/O pins and the timer onchipHow easy to upgrade to higherperformance or lower power-consumptionversionsCost per unitDepartment of Computer Science and Information EngineeringNational Cheng Kung University, TAIWAN11

MICROCONTROLLERSANDEMBEDDEDPROCESSORS Availability of software developmenttools, such as compilers, assemblers,and debuggersWide availability and reliable sourcesof the microcontroller¾Criteria forChoosing aMicrocontroller(cont’)HANELThe 8051 family has the largest number ofdiversified (multiple source) suppliers Intel (original)AtmelPhilips/SigneticsAMDInfineon (formerly Siemens)MatraDallas Semiconductor/MaximDepartment of Computer Science and Information EngineeringNational Cheng Kung University, TAIWAN12

OVERVIEW OF8051 FAMILY¾The 8051 is an 8-bit processor The CPU can work on only 8 bits of data at atime8051Microcontroller¾The 8051 had HANELIntel introduced 8051, referred as MCS51, in 1981128 bytes of RAM4K bytes of on-chip ROMTwo timersOne serial portFour I/O ports, each 8 bits wide6 interrupt sourcesThe 8051 became widely popular afterallowing other manufactures to makeand market any flavor of the 8051, butremaining code-compatibleDepartment of Computer Science and Information EngineeringNational Cheng Kung University, TAIWAN13

pROMfor codeOn-chipRAMEtc.Timer 0Timer 1BusControlI/OPortsSerialPortCounter InputsOVERVIEW OF8051 FAMILYExternalInterruptsCPUOSCP0 P1 P2 P3TXDRXDAddress/DataHANELDepartment of Computer Science and Information EngineeringNational Cheng Kung University, TAIWAN14

OVERVIEW OF8051 FAMILY The 8051 is a subset of the 8052The 8031 is a ROM-less 8051¾8051 Family¾Add external ROM to itYou lose two ports, and leave only 2 portsfor I/O operationsFeatureROM (on-chip programspace in bytes)4K8K0K128256128Timers232I/O pins323232Serial port111Interrupt sources686RAM (bytes)HANEL8051 8052 8031Department of Computer Science and Information EngineeringNational Cheng Kung University, TAIWAN15

OVERVIEW OF8051 FAMILYVarious 8051Microcontrollers 8751 microcontroller¾UV-EPROM PROM burner UV-EPROM eraser takes 20 min to erase AT89C51 from Atmel Corporation¾Flash (erase before write) ROM burner that supports flash A separate eraser is not needed DS89C4x0 from Dallas Semiconductor,now part of Maxim Corp.¾Flash Comes with on-chip loader, loading program toon-chip flash via PC COM portHANELDepartment of Computer Science and Information EngineeringNational Cheng Kung University, TAIWAN16

OVERVIEW OF8051 FAMILYVarious 8051Microcontrollers DS5000 from Dallas Semiconductor¾ Also comes with on-chip loader (cont’) OTP (one-time-programmable) versionof 80518051 family from Philips¾HANELNV-RAM (changed one byte at a time),RTC (real-time clock)ADC, DAC, extended I/O, and both OTPand flashDepartment of Computer Science and Information EngineeringNational Cheng Kung University, TAIWAN17

8051 ASSEMBLYLANGUAGEPROGRAMMINGThe 8051 Microcontroller and EmbeddedSystems: Using Assembly and CMazidi, Mazidi and McKinlayChung-Ping Young楊中平Home Automation, Networking, and Entertainment LabDept. of Computer Science and Information EngineeringNational Cheng Kung University, TAIWAN

INSIDE THE8051 RegistersRegister are used to store informationtemporarily, while the informationcould be¾¾ The vast majority of 8051 register are8-bit registers¾HANELa byte of data to be processed, oran address pointing to the data to befetchedThere is only one data type, 8 bitsDepartment of Computer Science and Information EngineeringNational Cheng Kung University, TAIWAN2

INSIDE THE8051 The 8 bits of a register are shown fromMSB D7 to the LSB D0¾Registers(cont’)With an 8-bit data type, any data largerthan 8 bits must be broken into 8-bitchunks before it is processedmostsignificant bitD7D6D5D4leastsignificant bitD3D2D1D08 bit RegistersHANELDepartment of Computer Science and Information EngineeringNational Cheng Kung University, TAIWAN3

INSIDE THE8051Registers(cont’) The most widely used registers¾A (Accumulator) For all arithmetic and logic instructions¾¾B, R0, R1, R2, R3, R4, R5, R6, R7DPTR (data pointer), and PC LPC (Program counter)Department of Computer Science and Information EngineeringNational Cheng Kung University, TAIWAN4

INSIDE THE8051MOV destination, source¾The instruction tells the CPU to move (in reality,COPY) the source operand to the destinationoperandMOVInstructionHANEL;copy source to dest.“#” signifies that it is a R3;load value 55H into reg. A;copy contents of A into R0;(now A R0 55H);copy contents of A into R1;(now A R0 R1 55H);copy contents of A into R2;(now A R0 R1 R2 55H);load value 95H into R3;(now R3 95H);copy contents of R3 into A;now A R3 95HDepartment of Computer Science and Information EngineeringNational Cheng Kung University, TAIWAN5

INSIDE THE8051MOVInstruction(cont’) Notes on programming¾Value (proceeded with #) can be loadeddirectly to registers A, B, or R0 – R7 MOV A, #23H MOV R5, #0F9HAdd a 0 to indicate thatF is a hex number andnot a letter¾If it’s not preceded with #,it means to load from amemory locationIf values 0 to F moved into an 8-bitregister, the rest of the bits are assumedall zeros “MOV A, #5”, the result will be A 05; i.e., A 00000101 in binary¾Moving a value that is too large into aregister will cause an error MOVHANELA, #7F2H ; ILLEGAL: 7F2H 8 bits (FFH)Department of Computer Science and Information EngineeringNational Cheng Kung University, TAIWAN6

ADD A, sourceINSIDE THE8051¾ADDInstruction¾;ADD the source operand;to the accumulatorThe ADD instruction tells the CPU to add the sourcebyte to register A and put the result in register ASource operand can be either a register orimmediate data, but the destination must alwaysbe register A “ADD R4, A” and “ADD R2, #12H” are invalidsince A must be the destination of any arithmeticoperationThere are alwaysmany ways to writethe same program,depending on theregisters usedHANELMOV A, #25H;load 25H into AMOV R2, #34H;load 34H into R2ADD A, R2 ;add R2 to Accumulator;(A A R2)MOV A, #25H;load one operand;into A (A 25H)ADD A, #34H;add the second;operand 34H to ADepartment of Computer Science and Information EngineeringNational Cheng Kung University, TAIWAN7

8051ASSEMBLYPROGRAMMINGStructure ofAssemblyLanguage In the early days of the computer,programmers coded in machine language,consisting of 0s and 1s¾ Tedious, slow and prone to errorAssembly languages, which providedmnemonics for the machine code instructions,plus other features, were developed¾ An Assembly language program consist of a seriesof lines of Assembly language instructionsAssembly language is referred to as a low-level language¾HANELIt deals directly with the internal structure of theCPUDepartment of Computer Science and Information EngineeringNational Cheng Kung University, TAIWAN8

8051ASSEMBLYPROGRAMMINGStructure ofAssemblyLanguageAssembly language instruction includes¾a mnemonic (abbreviation easy to remember) the commands to the CPU, telling it what thoseto do with those items¾optionally followed by one or two operands the data items being manipulated A given Assembly language program isa series of statements, or lines¾Assembly language instructions Tell the CPU what to do¾Directives (or pseudo-instructions) Give directions to the assemblerHANELDepartment of Computer Science and Information EngineeringNational Cheng Kung University, TAIWAN9

8051ASSEMBLYPROGRAMMINGStructure ofAssemblyLanguageMnemonicsproduceopcodes An Assembly language instructionconsists of four fields:[label:] Mnemonic [operands] [;comment]ORG0MOVMOVMOVADD0HADDA, R7ADDA, #12HR5, #25HR7, #34HA, #0A, R5HERE: SJMP HEREEND;start(origin) at location;load 25H into R5;load 34H intoR7Directivesdo not;load 0 into generateAany machineused;add contentscodeofandR5aretoA;now A A onlyR5 by the assembler;add contents of R7 to A;now A A R7;add to A value 12H;now A A

The 8051 Microcontroller and Embedded Systems Using Assembly and C Second Edition Muhammad Ali Mazidi Janice Gillispie Mazidi Rolin D. McKinlay CONTENTS Introduction to Computing The 8051 Microcontrollers 8051 Assembly Language Programming Branc