Microcontroller 7 8051 Microcontroller 2.1) Introduction The 8051 is an 8-bit Microcontroller originally developed by Intel in 1980. The block diagram of 8051 shows all the features unique to Microcontroller. Internal ROM & RAM Input &output ports with programmable pins Timer and Counter Serial data Communication The 8051 architecture consists of these specific features 8-bit CPU with Register A and B 16-bit program counter (PC) and data pointer (DPTR) 8 bit program status word (PSW) Internal ROM or EPROM Internal RAM 128 bytes In this 4 Register banks, each containing 8 registers 16 bytes which may be addressed at bit level 8 bytes of general purpose data memory • 32 input /output pins arranged as 4-8 bit ports P0-P3 • Two 16 bit timer/counters (TO-T1) • Fully duplexed serial data receiver/transmitter (SBUF) • Control register TMOD, TCON, SCOM, PCON, IP and IE • Oscillator and Clock circuit SIR M.V.I.T., DEPARTMENT OF E&C 1999-2000 Microcontroller 8 Fig 2.1: Pin description of 8051 SIR M.V.I.T., DEPARTMENT OF E&C 1999-2000 Microcontroller 9 Fig 2.2: Block diagram of 8051 2.2) Programming model The programming model of 8051 is shown in fig 2.3: It is a collection of 8 and 16 bit register and 8 bit memory locations. These registers and memory locations can be made to operate using software instructions that are incorporated as a part of the design. Each register with exception of program counter, has an internal one byte address assigned to it. Some registers such as TCON, SCON, IP, IE, A, B, PSW and ports are all bit addressable and are marked with * mark. These types have provision for reading or writing the entire byte of data and also each individual bits may be read or altered. SIR M.V.I.T., DEPARTMENT OF E&C 1999-2000 Microcontroller 10 Fig 2.3: Programming model of 8051 These operations can be done by software instructions that are generally able to specify a register by its address or its symbolic name or both. Let us consider each block separately. SIR M.V.I.T., DEPARTMENT OF E&C 1999-2000 Microcontroller 11 2.2.1) 8051 oscillator and Clocks Pins XTAL1 and XLAT2 are provided for connecting resonant network to form an oscillator. The methods used in these networks are: Ceramic resonators Quartz crystal and capacitor. Ceramic resonators The cost of this is less compared to quartz crystal resonators, but in ceramic resonators frequency stability and accuracy decreases and makes it a poor choice, if high-speed serial data communication with other systems, or critical timing is to be done. Quartz crystal and capacitor are employed as shown: Fig 2.4: Oscillator circuit & timing Choosing of crystal depends on maximum and minimum frequencies specified by 8051 manufacturers. Minimum frequency implies that some internal memories are dynamic and must always operate above a minimum frequency or data will be lost. SIR M.V.I.T., DEPARTMENT OF E&C 1999-2000 Microcontroller 12 One more aspect to be considered is serial data communication needs often dictate the frequency of the oscillator because of the requirement that internal counter must divide the basic clock rate to yield standard communication bit per second rates(baud rate). OPERATION The smallest interval of time to accomplish any simple instruction or part of a complex instruction is called machine cycle. Program instruction are may require one two or 4 machine cycles to be executed by the Microcontroller automatically beginning with the instruction located at ROM memory address 0000H at the time Microcontroller is first set / reset. Time of execution of an instruction is, Tinst =((C*12d) / crystal frequency) C = No. Of cycles required for executing a particular instruction. Two ALE pulses are produced per machine cycle. These are primarily used as a timing pulse for external memory access indicates when every instruction byte is fetched. Two bytes of single instruction may thus be fetched and executed in one machine cycle. Single byte instruction are not executed in half cycle, however single byte instruction “throw away “ the second byte. The next instruction is then fetched in the following cycle. 2.2.2) Program Counter and Data Pointer PROGRAM COUNTER Program instruction bytes are fetched from locations in memory that are present at address given by the PC. Program may be present in on-chip ROM at addresses 0000h to 0FFFh, external to the chip for addresses that exceed 0FFFh, or totally external for all addresses from 0000h to FFFFh. The PC is automatically incremented after every instruction byte is fetched. The PC is the only register that does not have an internal address. DPTR This register is made up of two 8-bit registers, named DPH and DPL, which are used to furnish memory addresses for internal and external code access and external data access. The DPTR is under the control of program instructions and can be specified by its 16-bit name, DPTR, or by each individual byte name, DPH and DPL, which also have different assigned address to it. SIR M.V.I.T., DEPARTMENT OF E&C 1999-2000 13 Microcontroller A and B CPU Registers: The A (accumulator) register is the most versatile of the two CPU registers and is used for many operations, including addition, subtraction, integer multiplication and division, and Boolean bit manipulations. The A register is also used for all data transfers between the 8051 and any external memory. The B register is used with the A register for multiplication and division operations PROGRAM STATUS WORD: [ BIT ADDRESSABLE SPECIAL FUNCTION REGISTER ] 7 CY 6 AC 5 4 FO BIT 7 6 5 4 3 2 1 0 RS1 3 RS0 SYMBOL CY AC FO RS1 RS0 RS1 RS0 0 0 0 1 1 0 1 1 OV ------P 2 OV 1 ------- 0 P FUNTION carry flag Auxiliary Carry flag User flag 0 Register bank select bit 1 Register bank select bit 2 bank selected Register bank 0 Register bank 1 Register bank 2 Register bank 3 overflow flag reserved for future use parity flag It is bit addressable as PSW.0 through PSW.1 Memories play an important role in computers and as well as in many application kits. These memories are basically classified as ROM and RAM. ROM is used mainly to store program code bytes RAM is used to store variable data that can be altered as the program runs. In Microcontroller there are 2 types of architecture used to access memory o Von Neumann architecture o Harvard architecture 8051 has Harvard architecture, which uses same address in different memories, for code and data. SIR M.V.I.T., DEPARTMENT OF E&C 1999-2000 Microcontroller 14 2.2.3) Internal ram There is 128-byte internal RAM, these are organized into three distinct areas: • Thirty-two bytes from address 00h to 1Fh that make up 32 working registers organized as four banks of eight registers each. These banks are numbered 0-3 and are made up of eight registers named R0 to R7. Each register can be addressed by name when its bank is selected or by its RAM address. Bits RS0 and RS1 in the PSW determine which bank of registers is currently in use at any time when the program is running. Register banks not selected can be used as general purpose RAM. • A bit-addressable area of 16 bytes occupies RAM byte addresses 20h to 2Fh, forming a total of 128 addressable bits. An address bit may be specified by its bit address of 00h to 7Fh, or 8 bits may form any byte address from 20h to 2Fh. • A general purpose RAM area above the bit area, from 30h to 7Fh, addressable as bytes. Fig 2.5: Internal memory model SIR M.V.I.T., DEPARTMENT OF E&C 1999-2000 Microcontroller 15 2.2.4) The Stack and the Stack Pointer . The 8-bit Stack pointer (SP) register is used by the 8051 to hold an internal RAM address that is called the top of the stack. The address held in the SP register is the location in internal RAM where the last byte of data was stored by a stack operation. When data is to be placed on the stack, the SP increments before storing the data on the stack. When the byte is read from the stack, and then the SP decrements to point to the next available byte of stored data. So normally stack is placed high in internal RAM, by an appropriate choice of the number placed in SP register to avoid conflicts with the register, bit and scratch pad internal RAM areas. 2.2.5) INTERNAL ROM The internal ROM has address from 0000h to FFFFh. The PC is ordinarily used to address program code bytes from address 0000h-FFFFh,program addresses higher than 0FFFh which exceed the internal ROM capacity will cause the 8051 to automatically address fetch code bytes from an external memory, address 0000h to FFFFh by connecting external access pin (EA=31) to ground. The detailed explanation about external memory is given in chapter 3. 2.2.6) SPECIAL FUNCTION REGISTERS In 8051 operation that do not use internal 128 bytes RAM address from 00h77h are done by a group of specific internal register each called a Special Function Register (SRF), which are address from 80h-ffh. Some of these are also bit addressable. The SFR registers are displayed in the following table. SIR M.V.I.T., DEPARTMENT OF E&C 1999-2000 Microcontroller A B DPH DPL IE IP P0 P1 P2 P3 PCON PSW SCON SBUF SP TMOD TCON TL0 TH0 TL1 Accumulator Arithmetic Addressing External memory Addressing External memory Interrupt enable Control Interrupt priority I/O port latch I/O port latch I/O port latch I/O port latch Power Control Program Status word Serial port Control Serial port data buffer Stack pointer Timer or mode control Timer/Counter Control Timer0 Low Byte Timer0 High Byte Timer1 Low Byte Timer1High Byte 16 0E0 0F0 83 82 0A8 0B8 80 90 A0 0B0 87 0D0 98 99 81 89 88 8A 8C 8B 8D TH1 2.2.7) Counters & Timers Consist of two 16 bit Counters named T0 and T1 are provided for general use of the programmer. Each may be programmed to count internal clock pulses, acting as a timer or programmed to count external pulse as a counter. The Counter are divided into two 8 byte register called the timer low (TL0,TL1) and high (TH,TH1) bytes. All counter action is controlled by bit state in the timer mode control register TMOD, the timer/counter control register (TCON) and certain program instruction. TMOD - controls 2 timers with each 4 bit controlling one Timer. TCON - has control bits and flags for timer in upper nibble and control bit and flags for external interrupts in the lower nibble. SIR M.V.I.T., DEPARTMENT OF E&C 1999-2000 Microcontroller 17 TCON TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0 TCON is Bit addressable Bit 7 SYMBOL FUNTION TF1 Timer 1 overflow flag. Set when timer rolls from all 1s to 0Cleared when processor vectors execute interrupt service routine located at program address 001Bh. 6. TR1 Timer 1 run control bit. Set to 1 by program to enable Timer to count; Cleared to 0 by program to halt timer Does not reset timer. 5 TF0 4 TR0 Timer 0 run control bit. Set to 1 by program to enable timer to count; Cleared to 0 by program to halt timer. Does not reset timer. 3 IE1 External interrupts 1Edge flag. Set to 1 when a high to low edge signal is received on port3 pin3.3 (INT1). Cleared when processor vector to interrupt service routine located at program address 0013h. Not related to timer operator program in modes 2 and 3.Stop bit in mode 1.Not used in mode 0. 2 IT1 External interrupt 1 signal type control bit. Set to 1 by program to enable external interrupt 1 to be triggered by a falling edge signal. Set to 0 by program to enable a low level signal on external interrupt 1 to generate an interrupt. 1 IE0 External interrupt 0. Edge Set to 1 when a high to low edge signal is received on port 3 pin 3.2(INT0). Cleared when processor. Vector to interrupt service routine located at program address 0003h.Not related to timer operations bit in mode 1.Not used in mode 0. Timer 0 overflow flag. Set when timer rolls from all 1s to 0s.Cleared when processor vectors to execute interrupt service routine located at program address 000Bh. SIR M.V.I.T., DEPARTMENT OF E&C 1999-2000 Microcontroller 0 IT0 18 External interrupt 0 signal type control bit. Set to 1 by program to enable external interrupt 0 to be triggered by a falling edge signal. Set to 0 by program to enable a low level signal on external interrupt 1 to generate an interrupt. M1 0 0 1 1 M0 0 1 0 1 MODE 0 1 2 3 2.2.8) Timer counter interrupts The counters can be used to interrupt the program as follows. When the program wishes to count a certain number of internal pulses or external events a number is placed in one of the counters. The number represents the maximum count less the desired count + 1. The counter increment from the initial number to maximum and rolls over to zero and final pulse and also set a timer. The flag conditions may be tested by an instruction to tell the program that the count has been accomplished or the flag may be used to interrupt the program. Timer: If the counter to be used as a timer the following conditions should be applied for basic timer control logic shown below; fig 2.6: Timer/counter control logic SIR M.V.I.T., DEPARTMENT OF E&C 1999-2000 Microcontroller 19 Fig 2.7 Timer 1 and Timer 0 modes of operation C/Ť bit in TMOD register must be set to zero Bit TRX in TCON register must be set to one and gate bit in TMOD register must be zero or external pin INT0 or INT1 must be one. Timer mode of operation There are four modes of operation specific mode is selected using bit M1 & M0 in TMOD register TIMER mode 0 TMOD register set to 00B, resulting in initializing THX register as 8-bit counter and TLX as a five bit counter. The pulse input is divided by 32D in TL so that TH counts the original oscillator frequency reduced by a total of 384D. Timer mode 1 Differs by Mode 0 as TLX is configured as a full 8-bit counter. When mode bit are set to 01B in TMOD. Timer mode 2 TMOD register is set to 10B resulting to use only the TLX counter as a 8-bit counter. THX is used to hold a value that is loaded into TLX every time, TLX overflow from FFH to 00H. The timer flag is also set when TLX overflows. SIR M.V.I.T., DEPARTMENT OF E&C 1999-2000 20 Microcontroller Timer mode 3 In this both the Timers 0 and 1cannot set to mode 3., because placing timer1 in MODE 3 causes it to stop counting. The control bit TRI and timer 1 flag TFI are used by timer 0. Timer 0 in mode 3 becomes two completely separate 8-bit counters. TL0 is controlled by the gate arrangement and sets timer flag TF0 whenever it overflows from FFh to 00h. TH0 receives the timer clock under the control of TRI only sets the TFI flag when it overflows. While timer 0 is in mode 3 with one important exception. No interrupts will be generated by the timer 1 while timer 0 is using the TFI overflow flag. Counting The only difference between counting and timing is the source of the clock pulses to the counters. When used as a timer the clock pulses are sourced from the oscillator through the divide-by-12d circuit. When used as a counter pin T0 supplies pulses to counter 0 and pin T1 to counter 1.The C/T bit in TMOD must be set to 1 to enable pulses from the TX pin to reach the control circuit . The inputs pulses on TX sampled during P2 of state 5 every machine cycle. A change on the input from high to low between samples will increment the counter. Each high and low state of the input pulses must thus be held constant for at least one machine cycle to ensure reliable counting. Since this takes 24 pulses the maximum input frequency that can be accurately counted is the oscillator frequency divided by 24. For our 6 megahertz crystal the calculation yields a maximum external frequency of 250 kilohertz. 2.2.9) Serial data input/output Computers must be able to communicate with other computers in modern multiprocessor distributed system. One cost-effective way to communicate is to send and receive data bits serially. The 8051 has a serial data communication circuit that uses register SBUF to hold data. Register SCON controls data communication register PCON controls data rates and pins RXD and TXD connect to the serial data network. SCON SM0 SM1 SM2 REN TB8 RB8 TI RI PCON SMOD -------- -------- SIR M.V.I.T., DEPARTMENT OF E&C --------- GF1 GF0 PD IDL 1999-2000 Microcontroller 21 2.2.9) INPUT/OUTPUT PINS, PORTS AND CIRCUITS The dip has 40 pins, and the success of the design in the market place was determined by the flexibility built into the use of these pins. For this reason,24 of the pins may each be used for one of two entirely different functions yielding a total pin configuration of 64.The Function a pin performs at any given instant depends first on what is physically connected to it and then on what software commands are used to "program" the pin both of these factors are under the complete control of the 8051 programmer and circuit designer. Fig 2.8: Port Pin circuits PORT 0 Port 0 pins may serve as inputs, outputs or when used together as a bidirectional low-order address and data bus for external memory. For example when a pin is to be used as an input a1 must be written to the corresponding port 2 latch by the program, thus turning both of the output transistors off, which in turn causes the pin to "float" in a high-impedance state and the pin essentially connected to the input buffer. SIR M.V.I.T., DEPARTMENT OF E&C 1999-2000 Microcontroller 22 When used as an output, the pin latches that are programmed to a 0 will turn on the lower FET grounding the pin. All latches that are programmed to a 1 still float thus external pull up resistors will be needed to supply a logic high when using port 0 as an output. PORT 1 Port 1 pins have no dual functions therefore the output latch is connected directly to the gate of the lower FET, which has an FET circuit labeled internal FET pull up as an active pull up load. Used as an input, a 1 is written to the latch, turning the lower FET off, the pin and the input to the pin buffer are pulled high by the FET load. An external circuit can overcome the high-impedance pull up and drive the pin low to input a 0 or leave the input high for a 1. If used as an output the latches containing a 1 can drive the input of an external circuit high through the pull-up. If a 0 is written to the latch the lower FET is on, the pull-up is off and the pin can drive the input of the external circuit low. To aid in speeding up switches times when the pin is used as an output the internal FET pull-up has another FET in parallel with it. The second FET is turned on for two oscillator time periods during a low-to-high transition on the pin, as shown in fig 2.7. This arrangement provides a low impedance path to the positive voltage supply to help reduce rise in times in charging any parasitic capacitance in the external circuitry. PORT 2 Port 2 may be used as an input/output port similar in operation to port 1 the alternate use of port 2 is to supply a high-order address byte in conjunction with the port 0 low-order byte to address external memory. Port 2 pins are momentarily changed by the address control signals when supplying the high byte of 16-bit address. Port 2 latches remain stable when external memory is addressed as they do not have to be turned around for data input in the case for port 0. PORT 3 Port 3 is an input/output port similar to port 1. The input and output functions can be programmed under the control of the P3 latches or under the control of various other specific function registers. The port 3 alternate uses are shown in the following table: Unlike ports 0 and 2, which can have external addressing functions and change all eight port bits when in alternate use, each pin of port 3 may be individually programmed to be used either as I/O or as one of the alternate function. SIR M.V.I.T., DEPARTMENT OF E&C 1999-2000 Microcontroller PIN P3.0-RXD P3.1-TXD P3.2-INTO P3.3- INT1 P3.4-TO P3.5-T1 P3.6 –WR P3.7-RD 23 ALTERNATE USE SFR Serial Data Input SBUF Serial Data Output SBUF ____ External Interrupt 0 TCON.1 ____ External Interrupt 1 TCON.3 External Timer 0Input TMOD External Timer 1Input TMOD ___ External memory write pulse ----___ External memory read pulse ------ 2.2.10) INTERRUPTS A computer program has 2 ways to determine the conditions that exist in internal and external circuits. One method uses software instruction that jumps to subroutine on the states of flags and port pins. The second method responds to hardware signals called interrupts that force the program to call a subroutine. Software techniques use up processor time that could be devoted to other tasks; interrupts take processor time only when action by the program is needed. Any interrupt can cause the 8051, to perform a hardware call to an interrupt handling subroutine that is located at a predetermined absolute address in program memory. Five interrupts are provided in 8051.Three of these are generated automatically by internal operation, Timer flag0, Timer flag 1 and then serial port interrupt (RI or TI).Two interrupt are triggered by external signals provided by circuitry that is connected to pins INT0 andINT1. All interrupt functions are under the control of the program. The program is able to alter control bits in the interrupt enable register (IE)the interrupt priority register(IP) and Timer control register (TCON).The program can block all or any combination of the interrupts from acting on the program by suitable setting or clearing bits in these registers. The IE and IP registers are shown below: IE 7 EA 6 ------ 5 ET2 4 ES 3 ET1 2 EX1 1 ET0 0 EX0 3 PT1 2 PX1 1 PT0 0 PX0 IP 7 ----- 6 ----- 5 PT2 SIR M.V.I.T., DEPARTMENT OF E&C 4 PS 1999-2000 Microcontroller 24 THE INTERRUPT ENABLE(IE) SPECIAL FUNCTION REGISTER Bit SYMBOL FUNTION 7 EA Enable interrupt bit. Cleared to 0 by program to disable all interrupts; Set to 1 to permit indiviual interrupts to be enable bits. 6 --- Not implemented 5 ET2 Reserved for future use. 4 ES Enable serial port interrupt.Set to 1 by program to enable serial port interrupt. Cleared to 0 to disable serial port interrupt. 3 ET1 Enable timer 1 overflow interrupt.Set to 1 by program to enable timer 1 overflow interrupt; cleared to 0 to disable timer 1 overflow interrupt. 2 EX1 Enable external interrupt 1.Set to 1 by program to enable INT1 low interrupt; cleared to disanable interrupt 1 ET0 Enable timer 0 overflow interrupt. Set to 1 by program to enable timer 0 overflow interrupt; cleared to 0 to disable timer 0 overflow interrupt. 0 EX0 Enable external interrupt 0.Set to 1 by program to enable INT0 low interrupt; cleared to disable interrupt. THE INTERRUPT PRIORITY (IP) SPECIAL FUNCTION REGISTER Bit SYMBOL FUNTION 7 --- Not implemented 6 --- Not implemented 5 PT2 Reserved for future use. 4 PS 3 PT1 2 PX1 1 PT0 Priority of serial port interrupt.Set/cleared by program Priority of timer 1 overflow interrupt 1. Set/cleared by program. Priority of external interrupt. Set/cleared by program Priority of timer 0 overflow interrupt. Set/cleared by program. 0 PX0 SIR M.V.I.T., DEPARTMENT OF E&C Priority of external interrupt 0. Set/cleared by program 1999-2000 Microcontroller 25 After the interrupt has been handled by the interrupt subroutine, which is placed by the programmer at the interrupt location in program memory the interrupt program must resume operation at the instruction where the interrupt took place. 2.2.11) RESET INTERRUPT This is ultimate interrupt because the program may not block the action of voltage on RST pin.This type of interrupt is often called nonmaskable because no combination of bits can stop or mask the register action. After the RST pin is bought high level interrupt is enabled when made low internal registers will have the values shown: Register Pc DPTR A B SP PSW P0-3 IP IE TCON TMOD THO TL0 TH1 TL1 SCON SBUF PCON value 0000 0000 0000 00 07 FF XXX0000b 0XX0000b 00 00 00 00 00 00 00 XX 0XXXXXXb 2.3) Summary This chapter gives a detailed description of the 8051 Microcontroller. The next chapter gives the design of Microcontroller systems. SIR M.V.I.T., DEPARTMENT OF E&C 1999-2000