58
Micro processor And Interfacing What are microcontrollers and what are they used for? As all other good things, this powerful component is basically very simple and is obtained by uniting tested and high- quality "ingredients" (components) as per following receipt: 1. The simplest computer’s processor is used as a "brain" of the future system. 2. Depending on the taste of the producer, it is added : a bit of memory, a few A/D converters, timers, input/output lines etc. 3. It is all placed in one of standard packages. 4. A simple software that will be able to control it all and about which everyone will be able to learn has been developed. Three things have had a crucial impact on such a success of the microcontrollers: Powerful and intelligently chosen electronics embedded in the microcontrollers can via input/output devices ( switches, push buttons, sensors, LCD displays, relays…) control various processes and devices such as: industrial automatics, electric current, temperature, engine performance etc. A very low price enables them to be embedded in such devices in which, until recent time it was not worth embedding anything. Thanks to that, the world is overwhelmed today with cheap automatic devices and various “intelligent” appliences. Prior knowledge is hardly needed for programming. It is sufficient to have any kind of PC (software in use is not demanding at all and it is easy to learn to work on it) and one simple device (programmer) used for “transffering” completed programs into the microcontroller. Naveen Kumar .Auvusali –Asst.Professor EEE dept-SSR Engineering College,

UNIT I-8051.doc

Embed Size (px)

Citation preview

1

Micro processor And Interfacing

What are microcontrollers and what are they used for?

As all other good things, this powerful component is basically very simple and is obtained by uniting tested and high- quality "ingredients" (components) as per following receipt:

1. The simplest computers processor is used as a "brain" of the future system.

2. Depending on the taste of the producer, it is added : a bit of memory, a few A/D converters, timers, input/output lines etc.

3. It is all placed in one of standard packages.

4. A simple software that will be able to control it all and about which everyone will be able to learn has been developed.

Three things have had a crucial impact on such a success of the microcontrollers:

Powerful and intelligently chosen electronics embedded in the microcontrollers can via input/output devices ( switches, push buttons, sensors, LCD displays, relays) control various processes and devices such as: industrial automatics, electric current, temperature, engine performance etc.

A very low price enables them to be embedded in such devices in which, until recent time it was not worth embedding anything. Thanks to that, the world is overwhelmed today with cheap automatic devices and various intelligent appliences.

Prior knowledge is hardly needed for programming. It is sufficient to have any kind of PC (software in use is not demanding at all and it is easy to learn to work on it) and one simple device (programmer) used for transffering completed programs into the microcontroller.

Therefore, if you are infected with a virus called electronics, there is nothing left for you to do but to learn how to control its power and how to direct it at the right course.

How does microcontroller operate?Even though there is a great number of various microcontrollers and even greater number of programs designed for the microcontrollers use only, all of them have many things in common. That means that if you learn to handle one of them you will be able to handle them all. A typical scenario on whose basis it all functions is as follows:

1. Power supply is turned off and everything is so stillchip is programmed, every thing is in place, nothing indicates what is to come

2. Power supply connectors are connected to the power supply source and every thing starts to happen at high speed! The control logic registers what is going on first. It enables only quartz oscillator to operate. While the first preparations are in progress and parasite capacities are being charged, the first milliseconds go by.

3. Power supply connectors are connected to the power supply source and every thing starts to happen at high speed! The control logic registers what is going on first. It enables only quartz oscillator to work. While the first preparations are in progress and parasite capacities are being charged, the first milliseconds go by.

4. Voltage level has reached its full value and frequency of oscillator has became stable. The bits are being written to the SFRs, showing the state of all periph erals and all pins are configured as outputs. Everything occurs in harmony to the pulses rhythm and the overall electronis starts operating. Since this moment the time is measured in micro and nanoseconds.

5. Program Counter is reset to zero address of the program memory. Instruction from that address is sent to instruction decoder where its meaning is recognised and it is executed with immediate effect.

The value of the Program Counter is being incremented by 1 and the whole process is being repeated...several million times per second.

Difference

Where ever

Small size

Low cost

Low power Architecture:Harvard university

The Architecture given by Harvard University has the following advantages:

1: Data Space and Program Space are distinct

2: There is no Data corruption or loss of data

Disadvantage is:

1: The circuitry is very complex.Features of 8051

8 bit cpu

4k on chip ROM 128 Bytes on chip RAM

32 I/O

Two 16 bit timers

Full duplex UART

6 Source/5 Vector interrupts with two level priority levels

On chip clock Oscillator. Block Diagram:

The 8051 microcontroller has nothing impressive at first sight:

4 Kb program memory is not much at all.

128Kb RAM (including SFRs as well) satisfies basic needs, but it is not imposing amount.

4 ports having in total of 32 input/output lines are mostly enough to make connection to peripheral environment and are not luxury at all.

The whole configuration is obviously envisaged as such to satisfy the needs of most programmers who work on development of automation devices. One of advantages of this microcontroller is that nothing is missing and nothing is too much. In other words, it is created exactly in accordance to the average users taste and needs. The other advantage is the way RAM is organized, the way Central Processor Unit (CPU) operates and ports which maximally use all recourses and enable further upgrading.

8051 Microcontroller's pins

Pins 1-8: Port 1 Each of these pins can be configured as input or output.

Pin 9: RS Logical one on this pin stops microcontrollers operating and erases the contents of most registers. By applying logical zero to this pin, the program starts execution from the beginning. In other words, a positive voltage pulse on this pin resets the microcontroller.

Pins10-17: Port 3 Similar to port 1, each of these pins can serve as universal input or output . Besides, all of them have alternative functions:

Pin 10: RXD Serial asynchronous communication input or Serial synchronous communication output.

Pin 11: TXD Serial asynchronous communication output or Serial synchronous communication clock output.

Pin 12: INT0 Interrupt 0 input

Pin 13: INT1 Interrupt 1 input

Pin 14: T0 Counter 0 clock input

Pin 15: T1 Counter 1 clock input

Pin 16: WR Signal for writing to external (additional) RAM

Pin 17: RD Signal for reading from external RAM

Pin 18, 19: X2, X1 Internal oscillator input and output. A quartz crystal which determines operating frequency is usually connected to these pins. Instead of quartz crystal, the miniature ceramics resonators can be also used for frequency stabilization. Later versions of the microcontrollers operate at a frequency of 0 Hz up to over 50 Hz.

Pin 20: GND Ground

Pin 21-28: Port 2 If there is no intention to use external memory then these port pins are configured as universal inputs/outputs. In case external memory is used then the higher address byte, i.e. addresses A8-A15 will appear on this port. It is important to know that even memory with capacity of 64Kb is not used ( i.e. note all bits on port are used for memory addressing) the rest of bits are not available as inputs or outputs.

Pin 29: PSEN If external ROM is used for storing program then it has a logic-0 value every time the microcontroller reads a byte from memory.

Pin 30: ALE Prior to each reading from external memory, the microcontroller will set the lower address byte (A0-A7) on P0 and immediately after that activates the output ALE. Upon receiving signal from the ALE pin, the external register (74HCT373 or 74HCT375 circuit is usually embedded ) memorizes the state of P0 and uses it as an address for memory chip. In the second part of the microcontrollers machine cycle, a signal on this pin stops being emitted and P0 is used now for data transmission (Data Bus). In this way, by means of only one additional (and cheap) integrated circuit, data multiplexing from the port is performed. This port at the same time used for data and address transmission.

Pin 31: EA By applying logic zero to this pin, P2 and P3 are used for data and address transmission with no regard to whether there is internal memory or not. That means that even there is a program written to the microcontroller, it will not be executed, the program written to external ROM will be used instead. Otherwise, by applying logic one to the EA pin, the microcontroller will use both memories, first internal and afterwards external (if it exists), up to end of address space.

Pin 32-39: Port 0 Similar to port 2, if external memory is not used, these pins can be used as universal inputs or outputs. Otherwise, P0 is configured as address output (A0-A7) when the ALE pin is at high level (1) and as data output (Data Bus), when logic zero (0) is applied to the ALE pin.

Pin 40: VCC Power supply +5V

Register set of 8051The AccumulatorIf youve worked with any other assembly languages you will be familiar with the concept of an Accumulator register.

The Accumulator, as its name suggests, is used as a general register to accumulate the results of a large number of instructions. It can hold an 8-bit (1-byte) value and is the most versatile register the 8051 has due to the shear number of instructions that make use of the accumulator. More than half of the 8051s 255 instructions manipulate or use the accumulator in some way.

For example, if you want to add the number 10 and 20, the resulting 30 will be stored in the Accumulator. Once you have a value in the Accumulator you may continue processing the value or you may store it in another register or in memory.

The "R" registersThe "R" registers are a set of eight registers that are named R0, R1, etc. up to and including R7.

These registers are used as auxillary registers in many operations. To continue with the above example, perhaps you are adding 10 and 20. The original number 10 may be stored in the Accumulator whereas the value 20 may be stored in, say, register R4. To process the addition you would execute the command:

ADD A,R4

After executing this instruction the Accumulator will contain the value 30.

You may think of the "R" registers as very important auxillary, or "helper", registers. The Accumulator alone would not be very useful if it were not for these "R" registers.

The "R" registers are also used to temporarily store values. For example, lets say you want to add the values in R1 and R2 together and then subtract the values of R3 and R4. One way to do this would be:

MOV A,R3 ;Move the value of R3 into the accumulatorADD A,R4 ;Add the value of R4MOV R5,A ;Store the resulting value temporarily in R5MOV A,R1 ;Move the value of R1 into the accumulatorADD A,R2 ;Add the value of R2SUBB A,R5 ;Subtract the value of R5 (which now contains R3 + R4)

As you can see, we used R5 to temporarily hold the sum of R3 and R4. Of course, this isnt the most efficient way to calculate (R1+R2) - (R3 +R4) but it does illustrate the use of the "R" registers as a way to store values temporarily.

The "B" RegisterThe "B" register is very similar to the Accumulator in the sense that it may hold an 8-bit (1-byte) value.

The "B" register is only used by two 8051 instructions: MUL AB and DIV AB. Thus, if you want to quickly and easily multiply or divide A by another number, you may store the other number in "B" and make use of these two instructions.

Aside from the MUL and DIV instructions, the "B" register is often used as yet another temporary storage register much like a ninth "R" register.

The Data Pointer (DPTR)The Data Pointer (DPTR) is the 8051s only user-accessable 16-bit (2-byte) register. The Accumulator, "R" registers, and "B" register are all 1-byte values.

DPTR, as the name suggests, is used to point to data. It is used by a number of commands which allow the 8051 to access external memory. When the 8051 accesses external memory it will access external memory at the address indicated by DPTR.

While DPTR is most often used to point to data in external memory, many programmers often take advantge of the fact that its the only true 16-bit register available. It is often used to store 2-byte values which have nothing to do with memory locations.

The Program Counter (PC)The Program Counter (PC) is a 2-byte address which tells the 8051 where the next instruction to execute is found in memory. When the 8051 is initialized PC always starts at 0000h and is incremented each time an instruction is executed. It is important to note that PC isnt always incremented by one. Since some instructions require 2 or 3 bytes the PC will be incremented by 2 or 3 in these cases.

The Program Counter is special in that there is no way to directly modify its value. That is to say, you cant do something like PC=2430h. On the other hand, if you execute LJMP 2430h youve effectively accomplished the same thing.

The Stack Pointer (SPThe Stack Pointer, like all registers except DPTR and PC, may hold an 8-bit (1-byte) value. The Stack Pointer is used to indicate where the next value to be removed from the stack should be taken from.

When you push a value onto the stack, the 8051 first increments the value of SP and then stores the value at the resulting memory location.

When you pop a value off the stack, the 8051 returns the value from the memory location indicated by SP, and then decrements the value of SP.

This order of operation is important. When the 8051 is initialized SP will be initialized to 07h. If you immediately push a value onto the stack, the value will be stored in Internal RAM address 08h. This makes sense taking into account what was mentioned two paragraphs above: First the 8051 will increment the value of SP (from 07h to 08h) and then will store the pushed value at that memory address (08h).

SP is modified directly by the 8051 by six instructions: PUSH, POP, ACALL, LCALL, RET, and RETI. It is also used intrinsically whenever an interrupt is triggered (more on interrupts later. Dont worry about them for now!).

MEMORY ORGANISATION:

Counters and Timers

As explained in the previous chapter, the main oscillator of the microcontroller uses quartz crystal for its operating. As the frequency of this oscillator is precisely defined and very stable, these pulses are the most suitable for time measuring (such oscillators are used in quartz clocks as well). In order to measure time between two events it is only needed to count up pulses from this oscillator. That is exactly what the timer is doing. Namely, if the timer is properly programmed, the value written to the timer register will be incremented or decremented after each coming pulse, i.e. once per each machine cycle cycle. Taking into account that one instruction lasts 12 quartz oscillator periods (one machine cycle), by embedding quartz with oscillator frequency of 12MHz, a number in the timer register will be changed million times per second, i.e. each microsecond.

The 8051 microcontrollers have 2 timer counters called T0 and T1. As their names tell, their main purpose is to measure time and count external events. Besides, they can be used for generating clock pulses used in serial communication, i.e. Baud Rate.

Timer T0

As it is shown in the picture below, this timer consists of two registers TH0 and TL0. The numbers these registers include represent a lower and a higher byte of one 16-digit binary number.

This means that if the content of the timer 0 is equal to 0 (T0=0) then both registers it includes will include 0. If the same timer contains for example number 1000 (decimal) then the register TH0 (higher byte) will contain number 3, while TL0 (lower byte) will contain decimal number 232.

Formula used to calculate values in registers is very simple:TH0 256 + TL0 = TMatching the previous example it would be as follows :3 256 + 232 = 1000

Since the timers are virtually 16-bit registers, the greatest value that could be written to them is 65 535. In case of exceeding this value, the timer will be automatically reset and afterwords that counting starts from 0. It is called overflow. Two registers TMOD and TCON are closely connected to this timer and control how it operates.

TMOD Register (Timer Mode)

This register selects mode of the timers T0 and T1. As illustrated in the following picture, the lower 4 bits (bit0 - bit3) refer to the timer 0, while the higher 4 bits (bit4 - bit7) refer to the timer 1. There are in total of 4 modes and each of them is described here in this book.

Bits of this register have the following purpose:

GATE1 starts and stops Timer 1 by means of a signal provided to the pin INT1 (P3.3):

1 - Timer 1 operates only if the bit INT1 is set

0 - Timer 1 operates regardless of the state of the bit INT 1

C/T1 selects which pulses are to be counted up by the timer/counter 1:

1 - Timer counts pulses provided to the pin T1 (P3.5)

0 - Timer counts pulses from internal oscillator

T1M1,T1M0 These two bits selects the Timer 1 operating mode.

T1M1T1M0ModeDescription

00013-bit timer

01116-bit timer

1028-bit auto-reload

113Split mode

GATE0 starts and stops Timer 1, using a signal provided to the pin INT0 (P3.2):

1 - Timer 0 operates only if the bit INT0 is set

0 - Timer 0 operates regardless of the state of the bit INT0

C/T0 selects which pulses are to be counted up by the timer/counter 0:

1 - Timer counts pulses provided to the pin T0(P3.4)

0 - Timer counts pulses from internal oscillator

T0M1,T0M0 These two bits select the Timer 0 operating mode.

T0M1T0M0ModeDescription

00013-bit timer

01116-bit timer

1028-bit auto-reload

113Split mode

Timer 0 in mode 0 (13-bit timer)

This is one of the rarities being kept only for compatibility with the previuos versions of the microcontrollers. When using this mode, the higher byte TH0 and only the first 5 bits of the lower byte TL0 are in use. Being configured in this way, the Timer 0 uses only 13 of all 16 bits. How does it operate? With each new pulse coming, the state of the lower register (that one with 5 bits) is changed. After 32 pulses received it becomes full and automatically is reset, while the higher byte TH0 is incremented by 1. This action will be repeated until registers count up 8192 pulses. After that, both registers are reset and counting starts from 0.

Timer 0 in mode 1 (16-bit timer)

All bits from the registers TH0 and TL0 are used in this mode. That is why for this mode is being more commonly used. Counting is performed in the same way as in mode 0, with difference that the timer counts up to 65 536, i.e. as far as the use of 16 bits allows.

Timer 0 in mode 2 (Auto-Reload Timer)

What does auto-reload mean? Simply, it means that such timer uses only one 8-bit register for counting, but it never counts from 0 but from an arbitrary chosen value (0- 255) saved in another register.

The advantages of this way of counting are described in the following example: suppose that for any reason it is continuously needed to count up 55 pulses at a time from the clock generator.

When using mode 1 or mode 0, It is needed to write number 200 to the timer registers and check constantly afterwards whether overflow occured, i.e. whether the value 255 is reached by counting . When it has occurred, it is needed to rewrite number 200 and repeat the whole procedure. The microcontroller performs the same procedure in mode 2 automatically. Namely, in this mode it is only register TL0 operating as a timer ( normally 8-bit), while the value from which counting should start is saved in the TH0 register. Referring to the previous example, in order to register each 55th pulse, it is needed to write the number 200 to the register and configure the timer to operate in mode 2.

Timer 0 in Mode 3 (Split Timer)

By configuring Timer 0 to operate in Mode 3, the 16-bit counter consisting of two registers TH0 and TL0 is split into two independent 8-bit timers. In addition, all control bits which belonged to the initial Timer 1 (consisting of the registers TH1 and TL1), now control newly created Timer 1. This means that even though the initial Timer 1 still can be configured to operate in any mode ( mode 1, 2 or 3 ), it is no longer able to stop, simply because there is no bit to do that. Therefore, in this mode, it will uninterruptedly operate in the background .

The only application of this mode is in case two independent 'quick' timers are used and the initial Timer 1 whose operating is out of control is used as baud rate generator.

TCON - Timer Control Register

This is also one of the registers whose bits directly control timer operating.Only 4 of all 8 bits this register has are used for timer control, while others are used for interrupt control which will be discussed later.

TF1 This bit is automatically set with the Timer 1 overflow

TR1 This bit turns the Timer 1 on

1 - Timer 1 is turned on

0 - Timer 1 is turned off

TF0 This bit is automatically set with the Timer 0 overflow.

TR0 This bit turns the timer 0 on

1 - Timer 0 is turned on

0 - Timer 0 is turned off

How to start Timer 0 ?

Normally, first this timer and afterwards its mode should be selected. Bits which control that are resided in the register TMOD:

This means that timer 0 operates in mode 1 and counts pulses from internal source whose frequency is equal to 1/12 the quartz frequency.In order to enable the timer, turn it on:

Immediately upon the bit TR0 is set, the timer starts operating. Assuming that a quartz crystal with frequency of 12MHz is embedded, a number it contains will be incremented every microsecond. By counting up to 65.536 microseconds, the both registers that timer consists of will be set. The microcontroller automatically reset them and the timer keeps on repeating counting from the beginning as far as the bits value is logic one (1).

How to 'read' a timer ?

Depending on the timers application, it is needed to read a number in the timer registers or to register a moment they have been reset.

- Everything is extremely simple when it is needed to read a value of the timer which uses only one register for counting (mode 2 or Mode 3) . It is sufficient to read its state at any moment and it is it!

- It is a bit complicated to read a timers value when it operates in mode 2. Assuming that the state of the lower byte is read first (TL0) and the state of the higher byte (TH0) afterwards, the result is:

TH0 = 15 TL0 = 255

Everything seems to be in order at first sight, but the current state of register at the moment of reading was:

TH0 = 14 TL0 = 255

In case of negligence, this error in counting ( 255 pulses ) may occur for not so obvious but quite logical reason. Reading the lower byte is correct ( 255 ), but at the same time the program counter was taking a new instruction for the TH0 state reading, an overflow occurred and both registers have changed their contents ( TH0: 1415, TL0: 2550). The problem has simple solution: the state of the higher byte should be read first, then the state of the lower byte and once again the state of the higher byte. If the number stored in the higher byte is not the same both times it has been read then this sequence should be repeated ( this is a mini- loop consisting of only 3 instructions in a program).

There is another solution too. It is sufficient to simply turn timer off while reading ( the bit TR0 in the register TCON should be 0), and turn it on after that.

Detecting Timer 0 Overflow

Usually, there is no need to continuously read timer registers contents. It is sufficient to register the moment they are reset, i.e. when counting starts from 0. It is called overflow. When this has occurred, the bit TF0 from the register TCON will be automatically set. The microcontroller is waiting for that moment in a way that program will constantly check the state of this bit. Furthermore, an interrupt to stop the main program execution can be enabled. Assuming that it is needed to provide a program pause ( time the program appeared to be stopped) in duration of for example 0.05 seconds ( 50 000 machine cycles ):

First, it is needed to calculate a number that should be written to the timer registers:

This number should be written to the timer registers TH0 and TL0:

Once the timer is started it will continue counting from the written number. Program instruction checks if the bit TF0 is set, which happens at the moment of overflow, i.e. after exactly 50.000 machine cycles and 0.05 seconds respectively.

UART (Universal Asynchronous Receiver and Transmitter)

One of the features that makes this microcontroller so powerful is an integrated UART, better known as a serial port. It is a duplex port, which means that it can transmit and receive data simultaneously. Without it, serial data sending and receiving would be endlessly complicated part of the program where the pin state continuously is being changed and checked according to strictly determined rhythm. Naturally, it does not happen here because the UART resolves it in a very elegant manner. All the programmer needs to do is to simply select serial port mode and baud rate. When the programmer is such configured, serial data sending is done by writing to the register SBUF while data receiving is done by reading the same register. The microcontroller takes care of all issues necessary for not making any error during data exchange.

Serial port should be configured prior to being used. That determines how many bits one serial word contains, what the baud rate is and what the pulse source for synchronization is. All bits controlling this are stored in the SFR Register SCON (Serial Control).

SCON Register (Serial Port Control Register) SM0 - bit selects mode

SM1 - bit selects mode

SM2 - bit is used in case that several microcontrollers share the same interface. In normal circumstances this bit must be cleared in order to enable connection to function normally.

REN - bit enables data receiving via serial communication and must be set in order to enable it.

TB8 - Since all registers in microcontroller are 8-bit registers, this bit solves the problem of sending the 9th bit in modes 2 and 3. Simply, bits content is sent as the 9th bit.

RB8 - bit has the same purpose as the bit TB8 but this time on the receiver side. This means that on receiving data in 9-bit format , the value of the last ( ninth) appears on its location.

TI - bit is automatically set at the moment the last bit of one byte is sent when the USART operates as a transmitter. In that way processor knows that the line is available for sending a new byte. Bit must be clear from within the program!

RI - bit is automatically set once one byte has been received. Everything functions in the similar way as in the previous case but on the receive side. This is line a doorbell which announces that a byte has been received via serial communication. It should be read quickly prior to a new data takes its place. This bit must also be also cleared from within the program!

As seen, serial port mode is selected by combining the bits SM0 and SM2 :

SM0SM1ModeDescriptionBaud Rate

0008-bit Shift Register1/12 the quartz frequency

0118-bit UARTDetermined by the timer 1

1029-bit UART1/32 the quartz frequency (1/64 the quartz frequency)

1139-bit UARTDetermined by the timer 1

In mode 0, the data are transferred through the RXD pin, while clock pulses appear on the TXD pin. The bout rate is fixed at 1/12 the quartz oscillator frequency. On transmit, the least significant bit (LSB bit) is being sent/received first. (received).

TRANSMIT - Data transmission in form of pulse train automatically starts on the pin RXD at the moment the data has been written to the SBUF register.In fact, this process starts after any instruction being performed on this register. Upon all 8 bits have been sent, the bit TI in the SCON register is automatically set.

RECEIVE - Starts data receiving through the pin RXD once two necessary conditions are met: bit REN=1 and RI=0 (both bits reside in the SCON register). Upon 8 bits have been received, the bit RI (register SCON) is automatically set, which indicates that one byte is received.

Since, there are no START and STOP bits or any other bit except data from the SBUF register, this mode is mainly used on shorter distance where the noise level is minimal and where operating rate is important. A typical example for this is I/O port extension by adding cheap IC circuit ( shift registers 74HC595, 74HC597 and similar).

Mode 1

In Mode1 10 bits are transmitted through TXD or received through RXD in the following manner: a START bit (always 0), 8 data bits (LSB first) and a STOP bit (always 1) last. The START bit is not registered in this pulse train. Its purpose is to start data receiving mechanism. On receive the STOP bit is automatically written to the RB8 bit in the SCON register.

TRANSMIT - A sequence for data transmission via serial communication is automatically started upon the data has been written to the SBUF register. End of 1 byte transmission is indicated by setting the TI bit in the SCON register.

RECEIVE - Receiving starts as soon as the START bit (logic zero (0)) appears on the pin RXD. The condition is that bit REN=1and bit RI=0. Both of them are stored in the SCON register. The RI bit is automatically set upon receiving has been completed.

The Baud rate in this mode is determined by the timer 1 overflow time.

Mode 2

In mode 2, 11 bits are sent through TXD or received through RXD: a START bit (always 0), 8 data bits (LSB first), additional 9th data bit and a STOP bit (always 1) last. On transmit, the 9th data bit is actually the TB8 bit from the SCON register. This bit commonly has the purpose of parity bit. Upon transmission, the 9th data bit is copied to the RB8 bit in the same register ( SCON).The baud rate is either 1/32 or 1/64 the quartz oscillator frequency.

TRANSMIT - A sequence for data transmission via serial communication is automatically started upon the data has been written to the SBUF register. End of 1 byte transmission is indicated by setting the TI bit in the SCON register.

RECEIVE - Receiving starts as soon as the START bit (logic zero (0)) appears on the pin RXD. The condition is that bit REN=1and bit RI=0. Both of them are stored in the SCON register. The RI bit is automatically set upon receiving has been completed.

Mode 3

Mode 3 is the same as Mode 2 except the baud rate. In Mode 3 is variable and can be selected.

The parity bit is the bit P in the PSW register. The simplest way to check correctness of the received byte is to add this parity bit to the transmit side as additional bit. Simply, immediately before transmit, the message is stored in the accumulator and the bit P goes into the TB8 bit in order to be a part of the message. On the receive side is the opposite : received byte is stored in the accumulator and the bit P is compared with the bit RB8 ( additional bit in the message). If they are the same- everything is OK!

Baud Rate

Baud Rate is defined as a number of send/received bits per second. In case the UART is used, baud rate depends on: selected mode, oscillator frequency and in some cases on the state of the bit SMOD stored in the SCON register. All necessary formulas are specified in the table :

Baud RateBitSMOD

Mode 0Fosc. / 12

Mode 11 Fosc.16 12 (256-TH1) BitSMOD

Mode 2Fosc. / 32Fosc. / 6410

Mode 31 Fosc.16 12 (256-TH1)

Timer 1 as a baud rate generator

Timer 1 is usually used as a baud rate generator because it is easy to adjust various baud rate by the means of this timer. The whole procedure is simple:

First, Timer 1 overflow interrupt should be disabled

Timer T1 should be set in auto-reload mode

Depending on necessary baud rate, in order to obtain some of the standard values one of the numbers from the table should be selected. That number should be written to the TH1 register. That's all.

Baud RateFosc. (MHz)Bit SMOD

11.05921214.74561620

15040 h30 h00 h0

300A0 h98 h80 h75 h52 h0

600D0 hCC hC0 hBB hA9 h0

1200E8 hE6 hE0 hDE hD5 h0

2400F4 hF3 hF0 hEF hEA h0

4800F3 hEF hEF h1

4800FA hF8 hF5 h0

9600FD hFC h0

9600F5 h1

19200FD hFC h1

38400FE h1

76800FF h1

Multiprocessor Communication

As described in the previous text, modes 2 and 3 enable the additional 9th data bit to be part of message. It can be used for checking data via parity bit. Another useful application of this bit is in communication between two microcontrollers, i.e. multiprocessor communication. This feature is enabled by setting the SM2 bit in the SCON register. The consequence is the following: when the STOP bit is ready, indicating end of message, the serial port interrupt will be requested only in case the bit RB8 = 1 (the 9th bit).

The whole procedure will be performed as follows:

Suppose that there are several connected microcontrollers having to exchange data. That means that each of them must have its address. The point is that each address sent via serial communication has the 9th bit set (1), while data has it cleared (0). If the microcontroller A should send data to the microcontroller C then it at will place first send address of C and the 9th bit set to 1. That will generate interrupt and all microcontrollers will check whether they are called.

Of course, only one of them will recognize this address and immediately clear the bit SM2 in the SCON register. All following data will be normally received by that microcontroller and ignored by other microcontrollers.

8051 Microcontroller Interrupts

There are five interrupt sources for the 8051, which means that they can recognize 5 different event that can interrupt regular program execution. Each interrupt can be enabled or disabled by setting bits in the IE register. Also, as seen from the picture below the whole interrupt system can be disabled by clearing bit EA from the same register.

Now, one detail should be explained which is not completely obvious but refers to external interrupts- INT0 and INT1. Namely, if the bits IT0 and IT1 stored in the TCON register are set, program interrupt will occur on changing logic state from 1 to 0, (only at the moment). If these bits are cleared, the same signal will generate interrupt request and it will be continuously executed as far as the pins are held low.

IE Register (Interrupt Enable) EA - bit enables or disables all other interrupt sources (globally)

0 - (when cleared) any interrupt request is ignored (even if it is enabled)

1 - (when set to 1) enables all interrupts requests which are individually enabled

ES - bit enables or disables serial communication interrupt (UART)

0 - UART System can not generate interrupt

1 - UART System enables interrupt

ET1 - bit enables or disables Timer 1 interrupt

0 - Timer 1 can not generate interrupt

1 - Timer 1 enables interrupt

EX1 - bit enables or disables INT 0 pin external interrupt

0 - change of the pin INT0 logic state can not generate interrupt

1 - enables external interrupt at the moment of changing the pin INT0 state

ET0 - bit enables or disables timer 0 interrupt

0 - Timer 0 can not generate interrupt

1 - enables timer 0 interrupt

EX0 - bit enables or disables INT1 pin external interrupt

0 - change of the INT1 pin logic state can not cause interrupt

1 - enables external interrupt at the moment of changing the pin INT1 state

Interrupt Priorities

It is not possible to predict when an interrupt will be required. For that reason, if several interrupts are enabled. It can easily occur that while one of them is in progress, another one is requested. In such situation, there is a priority list making the microcontroller know whether to continue operating or meet a new interrupt request.

The priority list cosists of 3 levels:

1. Reset! The apsolute master of the situation. If an request for Reset omits, everything is stopped and the microcontroller starts operating from the beginning.

2. Interrupt priority 1 can be stopped by Reset only.

3. Interrupt priority 0 can be stopped by both Reset and interrupt priority 1.

Which one of these existing interrupt sources have higher and which one has lower priority is defined in the IP Register ( Interrupt Priority Register). It is usually done at the beginning of the program. According to that, there are several possibilities:

Once an interrupt service begins. It cannot be interrupted by another inter rupt at the same or lower priority level, but only by a higher priority interrupt.

If two interrupt requests, at different priority levels, arrive at the same time then the higher priority interrupt is serviced first.

If the both interrupt requests, at the same priority level, occur one after another , the one who came later has to wait until routine being in progress ends.

If two interrupts of equal priority requests arrive at the same time then the interrupt to be serviced is selected according to the following priority list :

1. External interrupt INT0

2. Timer 0 interrupt

3. External Interrupt INT1

4. Timer 1 interrupt

5. Serial Communication Interrupt

IP Register (Interrupt Priority)

The IP register bits specify the priority level of each interrupt (high or low priority).

PS - Serial Port Interrupt priority bit

Priority 0

Priority 1

PT1 - Timer 1 interrupt priority

Priority 0

Priority 1

PX1 - External Interrupt INT1 priority

Priority 0

Priority 1

PT0 - Timer 0 Interrupt Priority

Priority 0

Priority 1

PX0 - External Interrupt INT0 Priority

Priority 0

Priority 1

Handling Interrupt

Once some of interrupt requests arrives, everything occurs according to the following order:

1. Instruction in progress is ended

2. The address of the next instruction to execute is pushed on the stack

3. Depending on which interrupt is requested, one of 5 vectors (addresses) is written to the program counter in accordance to the following table:

Interrupt SourceVector (address)

IE03 h

TF0B h

TF11B h

RI, TI23 h

All addresses are in hexadecimal format

4. The appropriate subroutines processing interrupts should be located at these addresses. Instead of them, there are usually jump instructions indicating the location where the subroutines reside.

5. When interrupt routine is executed, the address of the next instruction to execute is poped from the stack to the program counter and interrupted program continues operating from where it left off.

From the moment an interrupt is enabled, the microcontroller is on alert all the time. When interrupt request arrives, the program execution is interrupted, electronics recognizes the cause and the program jumps to the appropriate address (see the table above ). Usually, there is a jump instruction already prepared subroutine prepared in advance. The subroutine is executed which exactly the aim- to do something when something else has happened. After that, the program continues operating from where it left off

Reset

Reset occurs when the RS pin is supplied with a positive pulse in duration of at least 2 machine cycles ( 24 clock cycles of crystal oscillator). After that, the microcontroller generates internal reset signal during which all SFRs, excluding SBUF registers, Stack Pointer and ports are reset ( the state of the first two ports is indefinite while FF value is being written to the ports configuring all pins as inputs). Depending on device purpose and environment it is in, on power-on reset it is usually push button or circuit or both connected to the RS pin. One of the most simple circuit providing secure reset at the moment of turning power on is shown on the picture.

Everything functions rather simply: upon the power is on, electrical condenser is being charged for several milliseconds through resistor connected to the ground and during this process the pin voltage supply is on. When the condenser is charged, power supply voltage is stable and the pin keeps being connected to the ground providing normal operating in that way. If later on, during the operation, manual reset button is pushed, the condenser is being temporarily discharged and the microcontroller is being reset. Upon the button release, the whole process is repeated

Through the program- step by step...

The microcontrollers normally operate at very high speed. The use of 12 Mhz quartz crystal enables 1.000.000 instructions per second to be executed! In principle, there is no need for higher operating rate. In case it is needed, it is easy to built-in crystal for high frequency. The problem comes up when it is necessary to slow down. For example, when during testing in real operating environment, several instructions should be executed step by step in order to check for logic state of I/O pins.

Interrupt system applied on the 8051 microcontrollers practically stops operating and enables instructions to be executed one at a time by pushing button. Two interrupt features enable that:

Interrupt request is ignored if an interrupt of the same priority level is being in progress.

Upon interrupt routine has been executed, a new interrupt is not executed until at least one instruction from the main program is executed.

In order to apply this in practice, the following steps should be done:

1. External interrupt sensitive to the signal level should be enabled (for example INT0).

2. Three following instructions should be entered into the program (start from address 03hex.):

What is going on? Once the pin P3.2 is set to 0 (for example, by pushing button), the microcontroller will interrupt program execution jump to the address 03hex, will be executed a mini-interrupt routine consisting of 3 instructions is located at that address.

The first instruction is being executed until the push button is pressed ( logic one (1) on the pin P3.2). The second instruction is being executed until the push button is released. Immediately after that, the instruction RETI is executed and processor continues executing the main program. After each executed instruction, the interrupt INT0 is generated and the whole procedure is repeated ( push button is still pressed). Button Press = One Instruction.

8051 Microcontroller Power Consumption Control

Conditionally said microcontroller is the most part of its lifetime is inactive for some external signal in order to takes its role in a show. It can make a great problem in case batteries are used for power supply. In extremely cases, the only solution is to put the whole electronics to sleep in order to reduce consumption to the minimum. A typical example of this is remote TV controller: it can be out of use for months but when used again it takes less than a second to send a command to TV receiver. While normally operating, the AT89S53 uses current of approximately 25mA, which shows that it is not too sparing microcontroller. Anyway, it doesnt have to be always like this, it can easily switch the operation mode in order to reduce its total consumption to approximately 40uA. Actually, there are two power-saving modes of operation: Idle and Power Down.Idle mode

Immediately upon instruction which sets the bit IDL in the PCON register, the microcontroller turns off the greatest power consumer- CPU unit while peripheral units serial port, timers and interrupt system continue operating normally consuming 6.5mA. In Idle mode, the state of all registers and I/O ports is remains unchanged.

In order to terminate the Idle mode and make the microcontroller operate normally, it is necessary to enable and execute any interrupt or reset.Then, the IDL bit is automatically cleared and the program continues executing from instruction following that instruction which has set the IDL bit. It is recommended that three first following one which set NOP instructions. They do not perform any operation but keep the microcontroller from undesired changes on the I/O ports.

Power Down mode

When the bit PD in the register PCON is set from within the program, the microcontroller is set to Powerdown mode. It and turns off its internal oscillator reducing drastically consumption in that way. In power- down mode the microcontroller can operate using only 2V power supply while the total power consumption is less than 40uA. The only way to get the microcontroller back to normal mode is reset.

During Power Down mode, the state of all SFR registers and I/O ports remains unchanged, and after the microcontroller is put get into the normal mode, the content of the SFR register is lost, but the content of internal RAM is saved. Reset signal must be long enough approximately 10mS in order to stabilize quartz oscillator operating.

PCON register The purpose of the Register PCON bits :

SMOD By setting this bit baud rate is doubled.

GF1 General-purpose bit (available for use).

GF1 General-purpose bit (available for use).

GF0 General-purpose bit (available for use).

PD By setting this bit the microcontroller is set into Power Down mode.

IDL By setting this bit the microcontroller is set into Idle mode.

Data Memory

As already mentioned, Data Memory is used for temporarily storing and keeping data and intermediate results created and used during microcontrollers operating. Besides, this microcontroller family includes many other registers such as: hardware counters and timers, input/output ports, serial data buffers etc. The previous versions have the total memory size of 256 locations, while for later models this number is incremented by additional 128 available registers. In both cases, these first 256 memory locations (addresses 0-FFh) are the base of the memory. Common to all types of the 8051 microcontrollers. Locations available to the user occupy memory space with addresses from 0 to 7Fh. First 128 registers and this part of RAM is divided in several blocks.

The first block consists of 4 banks each including 8 registers designated as R0 to R7. Prior to access them, a bank containing that register must be selected. Next memory block ( in the range of 20h to 2Fh) is bit- addressable, which means that each bit being there has its own address from 0 to 7Fh. Since there are 16 such registers, this block contains in total of 128 bits with separate addresses (The 0th bit of the 20h byte has the bit address 0 and the 7th bit of th 2Fh byte has the bit address 7Fh). The third group of registers occupy addresses 2Fh-7Fh ( in total of 80 locations) and does not have any special purpose or feature.Additional Memory Block of Data Memory

In order to satisfy the programmers permanent hunger for Data Memory, producers have embedded an additional memory block of 128 locations into the latest versions of the 8051 microcontrollers. Naturally, its not so simpleThe problem is that electronics performing addressing has 1 byte (8 bits) on disposal and due to that it can reach only the first 256 locations. In order to keep already existing 8-bit architecture and compatibility with other existing models a little trick has been used.

Using trick in this case means that additional memory block shares the same addresses with existing locations intended for the SFRs (80h- FFh). In order to differentiate between these two physically separated memory spaces, different ways of addressing are used. A direct addressing is used for all locations in the SFRs, while the locations from additional RAM are accessible using indirect addressing.

How to extend memory?

In case on-chip memory is not enough, it is possible to add two external memory chips with capacity of 64Kb each. I/O ports P2 and P3 are used for their addressing and data transmission.

From the users perspective, everything functions quite simple if properly connected because the most operations are performed by the microcontroller itself. The 8051 microcontroller has two separate reading signals RD#(P3.7) and PSEN#. The first one is activated byte from external data memory (RAM) should be read, while another one is activated to read byte from external program memory (ROM). These both signals are active at logical zero (0) level. A typical example of such memory extension using special chips for RAM and ROM, is shown on the previous picture. It is called Hardward architecture.

Even though the additional memory is rarely used with the latest versions of the microcontrollers, it will be described here in short what happens when memory chips are connected according to the previous schematic. It is important to know that the whole process is performed automatically, i.e. with no intervention in the program.

When the program during execution encounters the instruction which resides in exter nal memory (ROM), the microcontroller will activate its control output ALE and set the first 8 bits of address (A0-A7) on P0. In this way, IC circuit 74HCT573 which "lets in" the first 8 bits to memory address pins is activated.

A signal on the pin ALE closes the IC circuit 74HCT573 and immediately afterwards 8 higher bits of address (A8-A15) appear on the port. In this way, a desired location in addtional program memory is completely addressed. The only thing left over is to read its content.

Pins on P0 are configured as inputs, the pin PSEN is activated and the microcon troller reads content from memory chip. The same connections are used both for data and lower address byte.

Similar occurs when it is a needed to read some location from external Data Memory. Now, addressing is performed in the same way, while reading or writing is performed via signals which appear on the control outputs RD or WR .

Addressing Modes:An "addressing mode" refers to how you are addressing a given memory location. In summary, the addressing modes are as follows, with an example of each:

Immediate AddressingMOV A,#20h

Direct AddressingMOV A,30h

Indirect AddressingMOV A,@R0

External DirectMOVX A,@DPTR

Code IndirectMOVC A,@A+DPTR

Each of these addressing modes provides important flexibility.

Immediate AddressingImmediate addressing is so-named because the value to be stored in memory immediately follows the operation code in memory. That is to say, the instruction itself dictates what value will be stored in memory.

For example, the instruction:

MOV A,#20h

This instruction uses Immediate Addressing because the Accumulator will be loaded with the value that immediately follows; in this case 20 (hexidecimal).

Immediate addressing is very fast since the value to be loaded is included in the instruction. However, since the value to be loaded is fixed at compile-time it is not very flexible.

Direct AddressingDirect addressing is so-named because the value to be stored in memory is obtained by directly retrieving it from another memory location. For example:

MOV A,30h

This instruction will read the data out of Internal RAM address 30 (hexidecimal) and store it in the Accumulator.

Direct addressing is generally fast since, although the value to be loaded isnt included in the instruction, it is quickly accessable since it is stored in the 8051s Internal RAM. It is also much more flexible than Immediate Addressing since the value to be loaded is whatever is found at the given address--which may be variable.

Also, it is important to note that when using direct addressing any instruction which refers to an address between 00h and 7Fh is referring to Internal Memory. Any instruction which refers to an address between 80h and FFh is referring to the SFR control registers that control the 8051 microcontroller itself.

The obvious question that may arise is, "If direct addressing an address from 80h through FFh refers to SFRs, how can I access the upper 128 bytes of Internal RAM that are available on the 8052?" The answer is: You cant access them using direct addressing. As stated, if you directly refer to an address of 80h through FFh you will be referring to an SFR. However, you may access the 8052s upper 128 bytes of RAM by using the next addressing mode, "indirect addressing."

Indirect AddressingIndirect addressing is a very powerful addressing mode which in many cases provides an exceptional level of flexibility. Indirect addressing is also the only way to access the extra 128 bytes of Internal RAM found on an 8052.

Indirect addressing appears as follows:

MOV A,@R0

This instruction causes the 8051 to analyze the value of the R0 register. The 8051 will then load the accumulator with the value from Internal RAM which is found at the address indicated by R0.

For example, lets say R0 holds the value 40h and Internal RAM address 40h holds the value 67h. When the above instruction is executed the 8051 will check the value of R0. Since R0 holds 40h the 8051 will get the value out of Internal RAM address 40h (which holds 67h) and store it in the Accumulator. Thus, the Accumulator ends up holding 67h.

Indirect addressing always refers to Internal RAM; it never refers to an SFR. Thus, in a prior example we mentioned that SFR 99h can be used to write a value to the serial port. Thus one may think that the following would be a valid solution to write the value 1 to the serial port:

MOV R0,#99h ;Load the address of the serial portMOV @R0,#01h ;Send 01 to the serial port -- WRONG!!This is not valid. Since indirect addressing always refers to Internal RAM these two instructions would write the value 01h to Internal RAM address 99h on an 8052. On an 8051 these two instructions would produce an undefined result since the 8051 only has 128 bytes of Internal RAM.

External DirectExternal Memory is accessed using a suite of instructions which use what I call "External Direct" addressing. I call it this because it appears to be direct addressing, but it is used to access external memory rather than internal memory.

There are only two commands that use External Direct addressing mode:

MOVX A,@DPTRMOVX @DPTR,A

As you can see, both commands utilize DPTR. In these instructions, DPTR must first be loaded with the address of external memory that you wish to read or write. Once DPTR holds the correct external memory address, the first command will move the contents of that external memory address into the Accumulator. The second command will do the opposite: it will allow you to write the value of the Accumulator to the external memory address pointed to by DPTR.

External IndirectExternal memory can also be accessed using a form of indirect addressing which I call External Indirect addressing. This form of addressing is usually only used in relatively small projects that have a very small amount of external RAM. An example of this addressing mode is:

MOVX @R0,A

Once again, the value of R0 is first read and the value of the Accumulator is written to that address in External RAM. Since the value of @R0 can only be 00h through FFh the project would effectively be limited to 256 bytes of External RAM. There are relatively simple hardware/software tricks that can be implemented to access more than 256 bytes of memory using External Indirect addressing; however, it is usually easier to use External Direct addressing if your project has more than 256 bytes of External RAM.

Naveen Kumar .Auvusali Asst.Professor

EEE dept-SSR Engineering College,