A PROJECT REPORT ON EMBEDDED SYSTEM
SUBMITTED TO
SUBMITTED BY
Sunil panjeta Electronics & Comm.Engg.
Hec jagadhri
ACKNOWLEDGEMENT
Success is a sweet fruit, which everyone strives to taste. To achieve this goal, one has to put in a lot of physical and mental efforts. Each time we write this report, gain stronger appreciation for the following fact: we couldn’t do it without the help of many talented and dedicated people. So we wish to express our appreciation to those whose help has been most valuable. Firstly, we would like like to express express our gratitude and appreciation appreciation to …… ……………………………… (Head
of
company).who
explain ained
us
everything about the training process at the company and made us familiar with the company staff. We are equally grateful to ………………
…………………. (Instructor … who sorted out many of our problems regarding the training and gave us proper material to work with. We are also grateful to ……………………….. who is always there to help us out in our problematic time. time. We
…………………..(Training and Placement are are also also gratef grateful ul to …………………..(T
Officer,HEC…………………) for arranging the six months training for us and providing us with all the necessary information about the same. Finally, we would like to say thanks to all the people of the company for their kind co-operation.
sunil panjeta
TABLE OF CONTENTS
COMPANY PROFILES
Technology changes drastically within the counts of time of time and it have become have become vicissitude of life. To survive in the air of air of cutthroat cutthroat competition, one needs to be fully cognizant with the changing trends of the of the technology, the needs of the clients and the employers, in brief, with almost all the aspects o f the industry. There is great demand for system designing engineers working on the embedded systems. This demand is going to rise further in further in the coming years. Other factor favorable for this is the incursion of foreign especially American companies in India. EEAST
is
a
complete
R & D Organization
and Advanced Software Products and Solutions
dedicated
to provide Electronics
to its Clients. Achieving the needs
of our customer and customer and converting their ideas their ideas to real models is our motto. our motto. We are working in the field of Embedded Systems, Automation and Advanced System design for the for the last four years four years with the vision of becoming of becoming a center of Excellence to provide Solutions, Services and Training in various fields of technologies. of technologies. EEAST has the distinction of being of being a pioneer among the embedded companies in India, engaged in imparting high-end training in all aspects of Embedded of Embedded Systems Design and Project development in various fields of engineering and technology for graduates, undergraduates and postgraduates and postgraduates of the of the appropriate discipline, the training at EEAST is not merely passing knowledge but build intelligence among the participants the participants to achieve goals in their life by thoroughly exposing them to industrial environment and projects. We work on work on overall
development of our of our employees employees and trainees.
EEAST is an organization providing advanced projects, complete electronic solutions in development systems like microprocessor, micro - controllers, wireless communications, communications, optical – optical – fiber fiber co comm mmun unic icat atio ions ns,,
real real time operating systems,
digital signal processing, signal processing, Embedded Systems and Micro - Sensors including software solution, solutions in C, C++, Java, .Net, Visual, C++ Visual basic, Visual basic, embedded C and Embedded LINUX. We have been have been roviding projects roviding projects and solutions professionally to various industries, academically to innumerable number of number of students. In our endeavor for endeavor for excellence excellence and manpower developments manpower developments in this field, we are providing are providing on these technologies specially customized for individual needs.
INTRODUCTION TO EMBEDDED SYSTEMS
Microcontrollers are widely used in Embedded System products. An Embedded product uses the microprocessor (or microcontroller) to do one task & one task only. A printer is an example of embedded system since the processor inside it performs one task only namely getting the data and printing it. Contrast this with Pentium based PC. A PC can be used for any no. of applications such as word processor, print server, bank teller terminal, video game player, network server or internet terminal. Software for variety of applications can be loaded and run. Of course the reason a PC can perform multiple task is that it has RAM memory and an operating system that loads the application software software into RAM & lets the CPU run it. In and Embedded system there is only one application software that is typi typica cally lly burn burn into into ROM. ROM. An x86P x86PC C Cont Contai ain n or its its conn connec ecte ted d to vari variou ous s Embedded Products such as keyboard, printer, modem, Disc controller, Sound card, CD-Rom Driver, Mouse & so on. Each one of these peripherals as a microcontroller inside it that performs only one task. For example inside every mouse there is microcontroller to perform the task of finding the mouse position and sending it to PC. Although Although microcon microcontrol troller ler are preferr preferred ed choice choice for many Embedd Embedded ed systems systems,, There are times that a microcontroller is inadequate for the task. For this reason in recent years many manufactures manufactures of general purpose microprocessors microprocessors such as INTEL, Motorolla, AMD & Cyrix have targeted their microprocessors for the high end of Embedded market. While INTEL, AMD, Cyrix push their x86 processors for both the embedded and desktop pc market, Motorolla is determined to keep the 68000 families alive by targeting it mainly for high end of embedded system. One of the most critical needs of the embedded system is to decrease power consumptions and space. This can be achieved by integrating more functions into the CPU chips. All the embedded processors based on the x86 and 680x0 have low power consumptions in additions to some forms of I/O, Com port &
ROM all on a single chip. In higher performance Embedded system the trend is to integrate more & more function on the CPU chip & let the designer decide which feature he/she wants to us. EMBEDDED SYSTEM An Embe Embedd dded ed Syste System m emplo employs ys a comb combina inatio tion n of hardw hardwar are e & softw software are (a “computational engine”) to perform a specific function; is part of a larger system that that may may not not be a “com “compu pute terr work works s in a reac reacti tive ve and and time time-c -con onst stra rain ined ed environment. environment. Software is used for providing features and flexibility Hardware = {Processors, ASICs, Memory...} is used for performance (& sometimes security
An embedded system is a special purpose system in which the computer is complet completely ely encapsu encapsulate lated d by the device device it control controls. s. Unlike Unlike a general general purpose purpose comput computer er,, such such as a PC, PC, an embed embedde ded d syste system m perfo perform rms s pred predefi efine ned d task’ task’s s usually with very specific tasks design engineers can optimize it reducing the size and cost of the product. Embedded systems are often mass produced, so the cost savings may be multiplied by million of items.
The core of any embedded system is formed by one or several microprocessor or micro controller programmed to perform a small number of tasks. In contrast to a general purpose computer, which can run any software application, the user chooses, the software on an embedded system is semi-permanent, so it is often called firmware.
EXAMPLES OF EMBEDDED SYSTEM
Automated tiller machines (ATMS).
Avio Avioni nic, c, such such as inert inertia iall guida guidanc nce e syste systems ms,, fligh flightt contr control ol hardwa hardware re / software and letter integrated system in aircraft and missile.
Cellular telephones and telephonic switches.
Computer network equipment, including routers timeservers and firewalls
Computer printers,Copiers.
Disk drives (floppy disk drive and hard disk drive)
Engine controllers and antilock brake controllers for automobiles.
Home automation products like thermostat, air conditioners sprinkles and security monitoring system.
House hold appliances including microwave ovens, washing machines, TV sets DVD players/recorders.
Medical equipment.
Measur Measurem ement ent equip equipme ment nt such such as digita digitall storag storage e oscil oscillos loscop copes es,, logic logic analyzers and spectrum analyzers.
Multimedia appliances: internet radio receivers, TV set top boxes.
Personal digital assistants (PDA’s), i.e., small hand held computer with P1M5 and other applications.
Program Programmabl mable e logic logic controll controllers ers (PLC’s (PLC’s)) for industri industrial al automat automation ion and monitoring.
Stationary video game controllers.
Wearable computers.
MICROPROCESSOR VS
MICROCONTROLLERS MICROCONTROLLER S
Products using microprocessors generally fall into two categories. The first category uses high-performance microprocessors such as the Pentium in application where system per perfo form rmanc ancee is crit critic ical al.. Howe However ver the the seco second nd cate catego gory ry of appli applicat catio ions ns in whic which, h, performance is secondary; issues of power, space and rapid development are more critical than raw processing power. The microprocessors for this category are often called a microcontroller.
MICROCONTROLLERS (MCU)
Figure shows the block diagram of a typical microcontroller, which is a true computer on a chip. chip. The design design incorpo incorporates rates all of the features features found in micro-proce micro-processor ssor CPU: ALU, PC, SP, SP, and registers. It also added the other features needed to make a complete computer: ROM, RAM, parallel I/O, serial I/O, counters, and clock circuit.
CPU
RAM
ROM
A single chip I/O Port
Serial Timer COM Port
FIG. BLOCK DIAGRAM OF A MICROCONTROLLER MICROCONTROLLER MICROPROCESSOR (MPU)
A microprocessor is a general-purpose digital computer central processing unit (CPU). Although popularly known as a “computer on a chip” is is in no sense a complete digital computer. computer. The block diagram of a microprocessor CPU is shown,
which contains an arithmetic and logical unit (ALU), program counter (PC), a stack pointer (SP),some working registers, a clock timing circuit, and interrupt circuits.
Data Bus
CPU GeneralPurpose Microrocessor
RAM
ROM
I/O Port
Timer
Serial COM Port
FIG. BLOCK DIAGRAM OF A MICROPROCESSOR
Microprocessors
1. Microprocessors contain no RAM or ROM.
2. Microprocessors do not have
Microcontrollers
1. Microcontrollers have an internal RAM and a ROM.
2. Microcontrollers have I/O ports.
any I/O ports.
3. Advantage of versatility. versatility.
3. Advantage of less power consump-
tion.
4. Expensive.
4. Cheaper in comparison.
5. With the addition of external
5. Occupies less space.
RAM and ROM, the system is more bulkier.
The prime use of microprocessor is to read data, perform extensive calculations on that data and store them in the mass storage device or display it. The prime functions of microcontroller is to read data, perform limited calculations on it, control its environment based on these data
Thus the microprocessor is said to be general-purpose digital computers whereas the microcontroller are intend to be special purpose digital controller. controller.
Microprocessor need many opcodes for moving data from the external memory to the CPU, microcontroller may require just one or two, also microprocessor may have one or two types of bit handling instructions whereas microcontrollers hav e many.
Thus microprocessor is concerned with the rapid movement of the code and data from the externa externall address addresses es to the chip, chip, micro microcont control roller ler is concer concerned ned with with the rapid rapid movement of the bits within the chip.
Lastly, Lastly, the microprocessor design accomplishes the goal g oal of flexibility in the hardware configuration by enabling large amounts of memory and I/O that could be connected to the address and data pins on the IC package. The microcontroller design uses much more limited set of single and double b yte instructions to move code and data from internal
LER VAR IOUS MICROCONTROL ROLLE R S
First microcontroller •
is ‘8031’
FEATURES (i) It is Intel’s product. Intel’s product. Neither Neither a a microprocessor nor microprocessor nor a a microcontroller. (ii) It is a 8-bit controller. (iii) Internally no ROM is provided is provided i.e. code is outside the chip.
Second microcontro ller is ‘8051’ •
FEATURES (i) It is a first complete 8-bit microcontroller. (ii) It is a name of a of a family. In which the instruction set, pin set, pin configuration, architecture are same, only memory storage capacity is different. (iii) Internally PROM (programmable read only memory) is provided so it called one time programmable time programmable (OTP).
Third microcontroller •
is ‘A ‘AT89C51’
FEATURES (i) It is a similar to similar to 8051 microcontroller i.e. microcontroller i.e. having same instruction set, pin configuration, architecture. (ii) It is a also 8-bit microcontroller . It’s cost is only Rs10 more than 8051. (iii) It uses EPROM (erasable programmable (erasable programmable read only memory) or FLASH memory. (iv) it is Multiple time programmable time programmable (MTP)i.e. 1000 times. So it is better is better than 8051.
ATMEL 89C51 It is a low-power, high-performance CMOS 8-bit microcomputer with microcomputer with 4K bytes bytes of Flash programmable Flash programmable and erasable read only memory (PEROM). The device is manufactured using Atmel’s high-density nonvolatile memory technology and is compatible with the industry- standard MCS-51 instruction set and pin and pin out. The on-chip Flash allows the program the program memory to be to be reprogrammed in system or by by a conventional nonvolatile memory programmer. memory programmer. By combining a versatile 8- bit 8- bit CPU with Flash on a monolithic chip, the Atmel AT89C51 is a powerful microcomputer, which provides which provides a highly flexible and cost-effective solution to many embedded control applications
THE
LER ROLLE R 8051 MICROCONTROL
The 8051 provides 8051 provides the following standard features: 4Kbytes of ROM, of ROM, 128 bytes of RAM of RAM, 32 I/O lines, two 16-bit timer/counters, a five vector two-level vector two-level interrupt architecture, a full duplex serial port, serial port, on-chip oscillator and oscillator and clock circuitry. In addition, the 8051 is designed designed with static logic for operation down to zero frequency and supports two software selectable power saving power saving modes. The Idle Mode stops the CPU while allowing the RAM, timer/counters, serial port and interrupt system to continue functioning. The Power Down Power Down Mode saves the RAM contents but freezes the oscillator disabling oscillator disabling all other chip other chip functions until the next hardware reset.
The 8051 8051 Micr ocontr oller ’s Ar chitectur e consists of these specific features
•
Eight-bit CPU with registers A (the accumulator) & B.
•
Sixteen-bit program Sixteen-bit program counter (PC) counter (PC) and data pointer data pointer (DPTR). (DPTR).
•
Eight-bit program Eight-bit program status word (PSW).
•
Eight-bit stack pointer (SP). pointer (SP).
•
Internal ROM or EPROM or EPROM (8751) of 0(8031) of 0(8031) to 4K (8051). 4K (8051).
•
Internal RAM of 128 of 128 bytes. bytes.
Four register Four register banks, banks, each containing eight registers.
Sixteen bytes, Sixteen bytes, which may be may be addressed at the bit the bit level.
Eight bytes Eight bytes of general-purpose of general-purpose data memory.
•
Thirty two I/O pins I/O pins arranged as four-bit ports four-bit ports P0 – P – P3.
•
Two 16-bit Timer/Counters T0 and T1.
•
Full duplex serial data receiver/transmitter (SBUF). receiver/transmitter (SBUF).
•
Control registers TCON, TMOD, SCON, PCON, IP and IE.
•
Two external and three internal
interrupt sources. Oscillator and Oscillator and clock circuits clock circuits
What is 8051 Standard? Microcontrollers’ producers have been struggling for a long time for attracting more and more choosy customers. Every couple of days a new chip with a higher operating frequency, frequency, more memory memor y and more high-quality A/D converters comes o n the market. Nevertheless, by analyzing their structure it is concluded that most of them have the same (or (or at leas leastt very very simi simila lar) r) arch archit itec ectu ture re know known n in the the prod produc uctt cata catalo logs gs as “805 “8051 1 compatible”. What is all this about? The The whol wholee story tory bega began n in the far far 80s 80s when when Intel ntel launc aunche hed d its serie eriess of the microcontrollers labelled with MCS 051. Although, several circuits belonging to this series had quite modest features in comparison to the new ones, they took over the world very fast and became a standard for what nowadays is ment by a word microcontroller. microcontroller. The reason for success and such a big popularity is a skillfully chosen configuration which satisfies needs of a great number of the users allowing at the same time stable expanding ( refers to the new types of the microcontrollers ). Besides, since a great deal of software has been developed in the meantime, it simply was not profitable to change anything in the microcontroller’s basic core. That is the reason for having a great number of various microcontrollers which actually are solely upgraded versions of the 8051 family. What is it what makes this microcontroller so special and universal so that almost all the world producers manufacture it today toda y under different name ?
As shown on the previous picture, 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.
As it is shown on the previous picture, the 8051 microcontroller have nothing impressive at first sight: 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 user‘s taste and needs. need s. The other advantage is the way wa y RAM is organized, the way Central Processor Unit (CPU) operates and ports which maximally use all recourses and enable further upgrading.
2.2 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 microcontroller’s 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. 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, 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 microcontroller’s 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. da ta and address Pin 31: EA By applying logic zero to this pin, P2 and P3 are used for data 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
I/O PORTS
2.4 8051 Microcontroller Memory Organisation
The microcontroller memory is divided into Program Memory and Data Memory. Memory. Program Memory (ROM) is used for permanent saving program being executed, while Data Memory (RAM) is used for temporarily storing and keeping intermediate results and variables. Depending on the model in use ( still referring to the whole 8051 microcontroller family) at most a few Kb of ROM and 128 or 256 bytes of RAM can be used. All 8051 microcontrollers have 16-bit addressing bus and can address 64 kb memory me mory.. It is neither a mistake nor a big ambition of engineers who were working on basic core development. It is a matter of very ver y clever memory organization which makes these controllers a real “ programmers’ tidbit“ . Program Memory
The oldest models of the 8051 805 1 microcontroller family did not have internal program memory . It was added from outside as a separate chip. These models are recognizable by their label beginning with 803 ( for ex. 8031 or 8032 ). All All later models have a few Kbytes ROM embedded, Even though thoug h it is enough for writing most of the programs, there are situations when additional memory is necessary. necessary. A typical typical example of o f it is the use of so called lookup tables. They are used in cases when something is too complicated or when there is no time for solving equations describing some process. The example of it can be totally exotic (an estimate of o f self-guided rockets’ meeting point) or totally common( measuring of temperature using non-linear thermo element or asynchronous motor speed control). In those cases all needed estimates and approximates are executed in advance and the final results are put in the tables ( similar to logarithmic tables ).
How does the microcontroller handle external memory depends on the pin EA logic state:
EA=0 In this case, internal program memory is completely ignored, only a p rogram
stored in external memory is to be executed. EA=1 In this case, a program from builtin ROM is to be executed first ( to the last
location). Afterwards, Afterwards, the execution e xecution is continued by reading additional memory memor y. in both cases, P0 and P2 are not available to the user because they are used for data nd address transmission. Besides, the pins ALE and PSEN are used too.
Data Memory
As already mentioned, Data Memory is used for temporarily storing and keep ing data and intermediate results created and used during microcontroller’s 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 n umber 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 c ontaining 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 permanent hunger for Data Memory, producers have embedded an additional memory block of 128 locations into the latest versions of the 8051 microcontrollers. Naturally, it’s it’s not so simple…The 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 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, eno ugh, 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 prope rly connected because the most operations are performed b y 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 happe ns 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, 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 Me mory.. Now, addressing is performed in the same way, way, while reading or writing is performed via signals which appear on the control outputs RD or WR .
SPECIAL FUNCTION REGISTERS The 8051 operations that do not use the internal 128-byte Ram addresses from 00h to 7Fh are done by a group of specific specific internal internal register register,, each called a special special function function register (SFR). SFRs are a kind of control table used for running and monitoring microcontroller’s operating. Each of these registers, even each bit they include, has its name, address in the scope of RAM and clearly defined purpose ( for example: timer control, interrupt, serial connection etc.). Even though there are 128 free memory locations intended for their storage, the basic core, shared by all types of 8051 controllers, has only 21 such registers. Rest of locations are intensionally left free in order to enable the producers to further improved models keeping at the same time compatibility with the previous versions. It also enables the use of programs written written a long time ago for the microcontroller microcontrollerss which are out of production now.
.1 A Register (Accumulator)
This is a general-purpose register which serves for storing intermediate results during operating. A number (an operand) should be added to the accumulator prior p rior to execute an instruction upon it. Once an arithmetical operation is preformed by the ALU, the result is placed into the accumulator. If a data should be transferred from o ne register to another, it must go through accumulator. For such universal purpose, this is the most commonly used register that none microcontroller can be imagined without (more than a half 8051 8 051 microcontroller's instructions used use the accumulator in some way).
.2 B Register B register is used during multiply and divide operations which can be performed only upon numbers stored in the A and B registers. All other instructions in the program can use this register as a spare accumulator (A).
NOTE
During programming, each of registers is called b y name so that their exact address is not so important for the user. During compiling into machine code (series of hexadecimal numbers recognized as instructions by the microcontroller), PC will automatically, instead of registers’ name, write necessary addresses into the microcontroller
.3 R Registers (R0-R7)
This is a common name for the total 8 generalpurpose registers (R0, R1, R2 ...R7). Even they are not true SFRs, they deserve to be discussed here because of their purpose. The bank is active when the R registers it includes are in use. Similar to the accu mulator, they are used for temporary storing variables and intermediate results. Which of the banks will be active depends on two bits included in the PSW Register. These registers are stored in four banks in the scope of RAM. The following example best illustrates the useful purpose of these registers. Suppose that mathematical operations on numbers previously stored in the R registers should be performed: (R1+R2) - (R3+R4). Obviously Obv iously,, a register for temporary storing results of addition is needed. Everything is quite simple and the program is as follows : MOV A,R3; Means: move number from R3 into accumulator ADD A,R4; Means: add number from R4 to accumulator (result
remains in accumulator) MOV R5,A; Means: temporarily move the result from
accumulator into R5 MOV A,R1; Means: move number from R1 into accumulator ADD A,R2; Means: add number from R2 to accumulator
SUBB A,R5; Means: subtract number from R5 ( there are R3+R4
)
.4 PSW Register (Program Status Word)
This is one of the most important SFRs. The Program Status Word Word (PSW) contains several status bits that reflect the current state of the CPU. This register contains: Carry bit, Auxiliary Carry, Carry, two register bank ba nk select bits, Overflow flag, parity bit, and userdefinable status flag. The ALU automatically changes some of register’s bits, which is usually used in regulation of the program performing. p erforming. P - Parity bit. If a number in accumulator is even then this bit will be automatically set
(1), otherwise it will be cleared (0). It is mainly used during data transmission and receiving via serial communication. - Bit 1. This bit is intended for the future versions of the microcontrollers, so it is not
supposed to be here. OV Overflow occurs when the result of arithmetical operation is greater than 255 (deci
mal), so that it can not be stored in one register. In that case, this bit will be set (1). If there is no overflow, this bit will be cleared (0). RS0, RS1 - Register bank select bits. These two bits are used to select one of the four
register banks in RAM. By writing zeroes and one s to these bits, a group of registers R0R7 is stored in one of four banks in RAM.
RS1
RS2
Space in RAM
0
0
Bank0 00h-07h
0
1
Bank1 08h-0Fh
1
0
Bank2 10h-17h
1
1
Bank3 18h-1Fh
user. F0 - Flag 0. This is a general-purpose bit available to the user. AC - Auxiliary Carry Flag is used for BCD operations only. CY - Carry Flag is the (ninth) auxiliary bit used for all arithmetical operations and shift
instructions. .5 DPTR Register (Data Pointer)
These registers are not true ones because they do not physically exist. They consist of two separate registers: DPH (Data Pointer High) and (Data Pointer Low). Their 16 bits are used for external memory addressing. They may be handled as a 16-bit register or as two independet 8-bit registers.Besides, the DPTR Register is usually used for storing data and intermediate results which have nothing to do with memory locations.
.6 SP Register (Stack Pointer)
A value of the Stack Pointer Pointer ensures that the Stack Pointer Pointer will point to valid RAM and permits Stack availability. By starting each subprogram, the value in the Stack Pointer is incremented by 1. In the same manner, by ending subprogram, this value is decremented by 1. After any reset, the value 7 is written to the Stack Pointer, which means that the space of RAM reserved for the Stack starts from this location. If another value is written to this register then the entire Stack is moved to a new location in the memory. memory.
P0, P1, P2, P3 - Input/Output Registers
In case that external memory and serial communication system are not in use then, 4 ports with in total of 32 input-output lines are available to the user for connection to peripheral environment. Each bit inside these ports coresponds to the appropriate pin on the microcontroller. This means that logic state written to these ports appears as a voltage on the pin ( 0 or 5 V). Naturally, while reading, the opposite occurs – voltage on some input pins is reflected in the appropriate port b it. The state of a port bit, besides being reflected in the pin, determines at the same time whether it will be configured as input or output. If a bit is cleared (0), the pin will be configured as output. In the same manner, if a bit is set to 1 the pin will be configured as input. After reset, as well as when turning the microcontroller on , all bits on these ports are set to one (1). This means that the appropriate pins will be configured as inputs.
2 Counters and Timers 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 lasts 12 quartz quartz oscill oscillato atorr period periodss (one (one machin machinee cycle) cycle),, by embeddi embedding ng quartz quartz with 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. .1 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 16 -digit binary number n umber..
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 co ntains 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 = T Matching 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 ca se of exceeding this value, the timer will be automatically reset and afterwords that counting starts from 0. It is called overflow. overflow. Two Two registers TMOD and TCON are closely connected to this timer and control how it operates. .1.1 TMOD Register (Timer Mode)
This register selects mode of o f 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 b its (bit4 - bit7) refer
to the timer 1. There are in total of o f 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):
•
•
•
•
•
o
1 - Timer 1 operates only if the bit INT1 is set
o
0 - Timer 1 operates regardless of the state of the bit INT 1
C/T1 selects which pulses are to be counted cou nted up by the timer/counter 1: o
1 - Timer counts pulses provided to the pin T1 (P3.5)
o
0 - Timer counts pulses from internal oscillator
T1M1,T1M0 These two bits selects the Timer 1 operating mode.
T1M1
T1M0
Mode
Description
0
0
0
13-bit timer
0
1
1
16-bit timer
1
0
2
8-bit auto-reload
1
1
3
Split mode
GATE0 starts and stops Timer 1, using a signal provided to the pin INT0 (P3.2): o
1 - Timer 0 operates only if the bit INT0 is set
o
0 - Timer 0 operates regardless of the state of the bit INT0
cou nted up by the timer/counter 0: C/T0 selects which pulses are to be counted o
1 - Timer counts pulses provided to the pin T0(P3.4)
o
0 - Timer counts pulses from internal oscillator
T0M1,T0M0 These two bits select the Timer 0 operating mode.
T0M1
T0M0
Mode
Description
0
0
0
13-bit timer
0
1
1
16-bit timer
1
0
2
8-bit auto-reload
1
1
3
Split mode
.1.1.1 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.
.1.1.2 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.
.1.1.3 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 (0255) 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. 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 whole procedu procedure. re. The microc microcont ontrol roller ler perfor performs ms the same same procedu procedure re in mode mode 2 automatically. 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.
.1.1.4 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 co ntrol 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. generator. .1.2 TCON - Timer Control Register
This This is also also one one of the the regi regist ster erss whos whosee bits bits dire direct ctly ly cont contro roll time timerr oper operat atin ing. g. Only 4 of all 8 bits this register has are used for timer control, while others are used for interrupt control which will be b e discussed later.
•
TF1 This bit is automatically set with the Timer 1 overflow
•
TR1 This bit turns the Timer 1 on
•
o
1 - Timer 1 is turned on
o
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 o
1 - Timer 0 is turned on
o
0 - Timer 0 is turned off
How to start Timer 0 ?
Normally, 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, 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 bit’s value is logic one (1).
.2Timer 1 Referring to its characteristics, this timer is “ a twin brother “ to the Timer 0. This means that they have the same purpose, their operating is controlled by the same registers TMOD and TCON and both of them can operate in one of 4 different modes.
2.7 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 receiv receivee data data simult simultane aneous ously ly.. Without ithout it, serial serial data data sendin sending g and receiv receiving ing would 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. 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 ser serial ial “wor word” cont contai ains ns,, what what the baud baud rate ate is and and what what the the puls pulsee sour source ce for for synchronization is. All bits controlling this are stored in the SFR Register SCON (Serial Control). .1 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. 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, 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 USART operates as a transmitt transmitter er.. In that way processor “knows” “knows” that the line is available for sending a new byte. Bit must be clear from within the program! RI - bit bit is autom automat atic ical ally ly set set once once one one byte byte has has been been recei receive ved. d. Ever Everyt ythi hing ng
•
functions in the similar way as in the previous case but on the receive side. This is line line a “door “doorbel bell” l” which which annou announce ncess that that a byte byte has has been been rece receiv ived ed via via seri serial al 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 b y combining the bits SM0 and SM2 :
SM0
SM1
Mode
0
0
0
0
1
1
Description
8-bit Shift Register
8-bit UART
Baud Rate
1/12 the quartz frequency Determined by the timer 1 1/32 the quartz
1
0
2
9-bit UART
frequency (1/64 the quartz frequency)
1
MODE 0
1
3
9-bit UART
Determined 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 d ata 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 b it RI (register SCON) is automatically set, which indicates that one byte is received.
Since, there are no START 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 START bit (always 0), 8 data bits (LSB first) and a STOP bit (always 1) last. The START 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. register. End of 1 byte b yte transmission is indicated by setting the TI bit in the SCON register. register.
RECEIVE - Receiving starts as soon as the STAR START T bit (logic zero (0)) appears on the pin
RXD. The condition is that bit REN=1and bit b it RI=0. Both of them are stored in the SCON register. 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 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. frequency. TRANSMIT - A sequence for data transmission via serial communication is
automatically started upon the data has been written to the SBUF register. register. End of 1 byte b yte transmission is indicated by setting the TI bit in the SCON register. register.
RECEIVE - Receiving starts as soon as the STAR START T bit (logic zero (0)) appears on the pin
RXD. The condition is that bit REN=1and bit b it RI=0. Both of them are stored in the SCON register. 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. 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, 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 th e receive side is the opposite : received byte is stored in the accumulator a ccumulator 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. register. All necessary formulas are specified in the table :
Baud Rate Mode 0
BitSMOD
Fosc. / 12 1 Fosc.
Mode 1
16 12 (256-
BitSMOD
TH1) Mode 2
Fosc. / 32
1
Fosc. / 64
0
1 Fosc. Mode 3
16 12 (256TH1)
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. register. The consequence is the following: when the STOP bit is ready, ready, indicating end of o f message, the serial port interrupt will be requested only in case the bit RB8 = 1 (the 9th bit). b it). 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. register. All following data will be normally received by that microcontroller and ignored by other microcontrollers.
2.8 8051 Microcontroller 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 s ystem can be disabled by clearing bit EA from the same register. 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) o
0 - (when cleared) any interrupt request is ignored (even if it is enabled)
o
1 - (when set to 1) enables all interrupts requests which are individually enabled
•
•
•
•
•
o r disables serial communication interrupt (UART) (UART) ES - bit enables or o
0 - UART System can not generate interrupt
o
1 - UART UART System enables interrupt
ET1 - bit enables or disables Timer 1 interrupt o
0 - Timer 1 can not generate interrupt
o
1 - Timer 1 enables interrupt
EX1 - bit enables or disables INT 0 pin external interrupt o
0 - change of the pin INT0 logic state can not generate interrupt
o
1 - enables external interrupt at the moment of changing the pin INT0 state
ET0 - bit enables or disables timer 0 interrupt o
0 - Timer 0 can not generate interrupt
o
1 - enables timer 0 interrupt
EX0 - bit enables or disables INT1 pin external interrupt o
0 - change of the INT1 pin logic state can not cause interrupt
o
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. Rese Reset! t! The The apso apsolu lute te mast master er of the the situ situat atio ion. n. If an requ reques estt for for Rese Resett omit omits, s, everything is stopped and the microcontroller starts operating from the beginning. 2. Interrupt Interrupt priori priority ty 1 can be stopped stopped by Reset Reset only only..
3. Interrupt Interrupt priority priority 0 can be stopped stopped by both Reset Reset and interru interrupt pt priority 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 interrupt requests, requests, at the same priority level, occur one after another , the one who came ca me 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. Exter Externa nall inte interr rrup uptt INT0 INT0 2. Timer imer 0 int inter erru rupt pt 3. Exter Externa nall Inte Interr rrup uptt INT INT1 1 4. Timer imer 1 int inter erru rupt pt 5. Serial Serial Commun Communica icatio tion n Inte Interru rrupt pt
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 o
Priority 0
o
Priority 1
•
•
•
•
PT1 - Timer 1 interrupt priority o
Priority 0
o
Priority 1
PX1 - External Interrupt INT1 priority o
Priority 0
o
Priority 1
PT0 - Timer 0 Interrupt Priority o
Priority 0
o
Priority 1
PX0 - External Interrupt INT0 Priority o
Priority 0
o
Priority 1
Handling Interrupt Once some of interrupt requests arrives, everything occurs according to the following order: 1. Instru Instructi ction on in in progr progres esss is is ended ended 2. The address address of the next instructio instruction n to execute execute is pushed on on the stack stack 3. Depending Depending on which interr interrupt upt is requeste requested, d, one of 5 vectors vectors (address (addresses) es) is writte written n to the program counter in accordance to the following table: Interrupt Source
Vector (address)
IE0
3h
TF0
Bh
TF1
1B h
RI, TI
23 h
All addresses are in hexadecimal format 4. The appropriate appropriate subrout subroutines ines processi processing ng interrupts interrupts should should be located located at these these addresses. Instead of them, there are usually jump instructions indicating the location where the subroutines reside.
5. When interrupt interrupt routine routine is executed, executed, the address address of of the next instr instruction uction to 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, e lectronics 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 cr ystal 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. 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 on e 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 External interrupt interrupt sensiti sensitive ve to the signal signal level level should should be enabled (for (for example example INT0). 2. Three followin following g instruction instructionss should should be entered entered into the program program (start (start from 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 p rocessor 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. 2.9 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 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 secon d to send a command to TV receiver. receiver. While normally operating, the AT89S53 AT89S53 uses current of approximately 25mA, which shows that it is not too sparing microcontroller. Anyway, Anyway, it doesn’t have to be always like this, it can easily switch the operation mode in order to reduce its total consumption to approximately 40uA. Actually, Actually, there are two power-saving modes of operation: ope ration: 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 no rmally,, it is necessary to enable and execute exec ute any interrupt or reset.Then, the IDL bit is automatically cleared and the program continues co ntinues 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 o f 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.
ADDR ESSIN G M ODES
The various addressing mode are immediate, register, direct and indirect. Data is stored at a source address and moved (copied) to a destination address. The ways by which these addresses are specified are called the addressing mnemonics
are
written
with
the
modes.
The
(data) destination address
named first, followed by the source address.
1. Imm ediate Ad dr es sin g Mod e
Instruction using # Next bytes are source of data
8051
data only
2. Regis ter A dd r es sing Mod e
Instruction using R0 to R7
Register R0 Register R0 to R7 in current Source or bank
destination of data of data
3. Dir ec t Ad dr es sin g Mo de
Instruction using a RAM address
Address in
Source or destination or destination
RAM
data
4. Ind ir ect Add r es s in g Mod e
Instruction using @ R0 or @R1 or @R1
Register R0 Register R0 or R1 or R1 in
Address of
current bank
data
Address in RAM
Source or destination or destination data