Top Banner
INDUSTRIAL TRAINING REPORT EMBEDDED SYSTEM Submitted By - ANUGYA MISHRA ELECTRONICS AND COMMUNICATION UNIV ROLL NO 0701031027 10/22/2010
57

training report on embedded system

Oct 14, 2014

Download

Documents

Anugya Mishra
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: training report on embedded system

INDUSTRIAL TRAINING REPORT

Submitted By - ANUGYA MISHRA ELECTRONICS AND COMMUNICATION UNIV ROLL NO 0701031027 10/22/2010

Page 2: training report on embedded system

Table Of Contents

1. Front Page

2. Preface

3. Acknowledgemt

4. Profile of Company

5. Introduction

6. Definition of a Microcontroller

7. Pin Configuration

8. Reset Circuit

9. Registers

10. Instruction Set

11. Addressing Modes

12. Microcontrollers vs Microprocessors

13. LED (Light Emitting Diodes)

14. 7 Segment Display

15. Timer/Counter

16. Stepper Motor

17. Interrupts

18. LCD (Liquid crystal display)

19. Serial Commmunication

20. References and Bibliography

Page 3: training report on embedded system

Preface This training report is all about the embedded systems and its applicationsin various fields of real world. We are living in the Embedded World. We aresurrounded with many embedded products and our daily life largely dependson proper functioning of these gadgets. Television, Radio, CD player, WashingMachine or Microwave Oven in our kitchen, Card readers, Access Controllers,Palm devices of our space enable us to do many of our tasks very effectively Apart from all these , many controllers embedded in our car take care of car operations between the bumpers. All kinds of magazines and journalsregularly dish out details about latest technologies, new devices; applicationswhich make us believe that our basic survival is controlled by these embeddedproducts. Now we can agree to the fact that these embedded products havesuccessfully invaded into our vast world. What is this Embedded System?Theoretically an embedded controller is combination of piece of microprocessorbased hardware and the suitable software to undertake a specific task.

Acknowledgement I would like to express my sincere gratitude to CETPA infotech for giving me an

opportunity to undergo my Industrial Training for four week as well as for providing me the knowledge of Embedded System.I would also like tothank to all the technical experts, engineers and executives for explaining practicalaspects of the theoretical knowledge. My greatest debt in creating this project goes to the technical experts Mr. Vineet Gupta Thank you to Sir for encouraging me every step of the way. I am deeply indebted to my HOD Mr.A.K.Mehrotra whose help, stimulating suggestions and encouragements was always with me

Profile of the company

The mission of CETPA is to work for the promotion of computer education and technology in India and abroad. CETPA is a group of professionals who are working for the promotion of

Page 4: training report on embedded system

technology. CETPA provides open platform for the development of the various computer software. We are the part of Linux Promotion Organization.

CETPA Objectives Promoting Computer Education & Technology Open platform for the development jobs Provide World Class Computer Education Organize Paper Presentation & Quizzes Organize Conferences & Seminars Collaboration with other Institute Launch Research Paper & Projects of the Members Research for Advance Technology Honor Outstanding Personalities

CETPA Education CETPA is an association dedicated for spreading advance computer education to all over the world. CETPA provides computer education in advance technology courses like LINUX, J2EE, VHDL, EMBEDDED SYSTEM, ADVANCE EMBEDDED SYSTEM, CAD , Pro-E or Mechanical & Electronics students, .NET, MATLAB, ADVANCE JAVA, ORACLE, SOFTWARE TESTING etc.

CETPA Research & Development CETPA is working continuously in Research and Development field from the very beginning .CETPA has developed a number of advanced software and currently working in following main projects

Congestion Control in Wireless Traffic Real Time Scheduling for Automatic Guided Vehicles Advancement in Microprocessors Technology CETPA Linux

CETPA Open Platform CETPA is an association, which is providing open platform for software development. Our thinking is that development tools should be provided free of cost so that the technological advancement and refinement can take place unhindered. In this mission everyone is invited .CETPA Linux is a special flavor of Linux that can be easily optimized and customized for just about any application or need .Extreme performance, configurability and a top-notch user and developer community are all hallmarks of the CETPA experience.

CETPA E-Magazine

Page 5: training report on embedded system

CETPA e-magazine is the best way to give new comers a chance to show their ability .Through e-magazines our student members can give their articles, projects, & thoughts to CETPA. These magazines not only have ideas of new comers but also thesis of well-known scientists, professionals and professors.

CETPA Club

CETPA has two types of clubs for professional and student. Members of the professional club are professionals from various domains who are working seriously for the fulfillment of CETPA objectives. The members of the students Club are the students who are provided platform by CETPA to present their research papers, and to share their views with other members of the club. Projects submitted by the students are sponsored in the national and international seminars for paper presentation and publishing.

IntroductionCircumstances that we find ourselves in today in the field of microcontrollers had their beginnings in the development of technology of integrated circuits. This development has made it possible to store hundreds of thousands of transistors into one chip. That was a pre requisite for production of microprocessors, and the first computers were made by adding external peripherals such as memory, input-output lines, timers and other. Further increasing of the volume of the package resulted in creation of integrated circuits. These integrated circuits contained both processor and peripherals. That is how the first chip containing a microcomputer, or what would later be known as a microcontroller came about

Definition of a Microcontroller

Microcontroller, as the name suggests, are small controllers. They are like single chip computers that are often embedded into other systems to function as processing/controlling unit. For example, the remote control you are using probably has microcontrollers inside that do decoding and other controlling functions. They are also used in automobiles, washing machines, microwave ovens, toys ... etc, where automation is needed.The key features of microcontrollers include: High Integration of Functionality Microcontrollers sometimes are called single-chip computers because they have on-chipmemory and I/O circuitry and other circuitries that enable them to function as small

Page 6: training report on embedded system

standalone computers without other supporting circuitry. Field Programmability, Flexibility Microcontrollers often use EEPROM or EPROM as their storage device to allow fieldprogram ability so they are flexible to use. Once the program is tested to be correct then large quantities of microcontrollers can be programmed to be used in embedded systems. Easy to UseAssembly language is often used in microcontrollers and since they usually follow RISC architecture, the instruction set is small. The development package of microcontrollers often includes an assembler, a simulator, a programmer to "burn" the chip and a demonstration board. Some packages include a high level language compiler such as a Compiler and more sophisticated libraries

Most microcontrollers will also combine other devices such as: A Timer module to allow the microcontroller to perform tasks for certain time periods. A serial I/O port to allow data to flow between the microcontroller and other devicessuch as a PC or another microcontroller.

Page 7: training report on embedded system

An ADC to allow the microcontroller to accept analogue input data for processing.

Pin Configuration :

The AT89C51 provides the following standard features: 4K bytes of Flash, 128 bytes of RAM, 32 I/O lines, two 16-bit timer/counters, a five vector two-level interrupt architecture, a full duplex serial port, on-chip oscillator and clock circuitry. In addition, the AT89C51 is designed with static logic for operation down to zero frequency and supports two software selectable power saving modes. The Idle Mode stops the CPU while allowing the RAM, timer/counters, serial port and

Page 8: training report on embedded system

interrupt system to continue functioning. The Power-down Mode saves the RAM contents but freezes the oscillator disabling all other chip functions until the next hardware reset.

Pin Description:

VCC:

Supply voltage.

GND:

Ground.

Port 0:

Port 0 is an 8-bit open-drain bi-directional I/O port. As an output port, each pin can sink eight TTL inputs. When 1s are written to port 0 pins, the pins can be used as high impedance inputs. Port 0 may also be configured to be the multiplexed low order

Address /data bus during accesses to external program and data memory. In this mode P0 has internal pull ups. Port 0 also receives the code bytes during Flash programming,

and outputs the code bytes during program verification. External pull ups are required during program verification.

Page 9: training report on embedded system

Port 1:

Port 1 is an 8-bit bi-directional I/O port with internal pull-ups. The Port 1 output buffers can sink/source four TTL inputs. When 1s are written to Port 1 pins they are pulled high by the internal pull-ups and can be used as inputs. As inputs, Port 1 pins that are externally being pulled low will source current (IIL) because of the internal pull-ups. Port 1 also receives the low-order address bytes during Flash programming and verification.

Port 2:

Port 2 is an 8-bit bi-directional I/O port with internal pull-ups. The Port 2 output buffers can sink/source four TTL inputs. When 1s are written to Port 2 pins they are pulled high by the internal pull-ups and can be used as inputs. As inputs, Port 2 pins that are externally being pulled low will source current (IIL) because of the internal pull-ups. Port 2 emits the high-order address byte during fetches from external program memory and during accesses to external data memory that use 16-bit addresses (MOVX @ DPTR). In this application, it uses strong internal pull-ups when emitting 1s. During accesses to external data memory that use 8-bit addresses (MOVX @ RI), Port 2 emits the contents of the P2 Special Function Register. Port 2 also receives the high-order address bits and some control signals during Flash programming and verification.

Port 3:

Port 3 is an 8-bit bi-directional I/O port with internal pull-ups. The Port 3 output buffers can sink/source four TTL inputs. When 1s are written to Port 3 pins they are pulled high by the internal pull-ups and can be used as inputs. As inputs, Port 3 pins that are externally being pulled low will source current (IIL) because of the pull-ups. Port 3 also serves the functions of various special features of the AT89C51 as listed below: Port 3 also receives some control signals for Flash programming and verification.

Page 10: training report on embedded system

ALE/PROG:

Address Latch Enable output pulse for latching the low byte of the address during accesses to external memory. This pin is also the program pulse input (PROG) during Flash programming. In normal operation ALE is emitted at a constant rate of 1/6 the oscillator frequency, and may be used for external timing or clocking purposes. Note, however, that one ALE

Pulse is skipped during each access to external Data Memory. If desired, ALE operation can be disabled by setting bit 0 of SFR location 8EH. With the bit set, ALE is active only during a MOVX or MOVC instruction. Otherwise, the pin is weakly pulled high. Setting the ALE-disable bit has no effect if the microcontroller is in external execution mode.

RESET:

Reset input. A high on this pin for two machine cycles while the oscillator is running resets the device.

Page 11: training report on embedded system

PSEN:

Program Store Enable is the read strobe to external program memory. When the AT89C51 is executing code from external program memory, PSEN is activated twice each machine cycle, except that two PSEN activations are skipped during each access to external data memory.

EA/VPP:

External Access Enable. EA must be strapped to GND in order to enable the device to fetch code from external program memory locations starting at 0000H up to FFFFH. Note, however, that if lock bit 1 is programmed, EA will be internally latched on reset. EA should be strapped to VCC for internal program executions. This pin also receives the 12-volt programming enable voltage (VPP) during Flash programming, for parts that require 12-volt VPP.

XTAL1:

Input to the inverting oscillator amplifier and input to the internal clock operating circuit.

XTAL2: Output from the inverting oscillator amplifier.

Page 12: training report on embedded system

Oscillator Characters:

XTAL1 and XTAL2 are the input and output, respectively, of an inverting amplifier which can be configured for use as an on-chip oscillator, as shown in Figure 1. Either a quartz crystal or ceramic resonator may be used. To drive the device from an external clock source, XTAL2 should be left unconnected while XTAL1 is driven as shown in Figure 2. There are no requirements on the duty cycle of the external clock signal, since the input to the internal clocking circuitry is through a divide-by-two flip-flop, but minimum and maximum voltage high and low time specifications must be observed.

Idle Mode:

In idle mode, the CPU puts itself to sleep while all the on chip peripherals remain active. The mode is invoked by software. The content of the on-chip RAM and all the special functions registers remain unchanged during this mode. The idle mode can be terminated by any enabled interrupt or by a hardware reset. It should be noted that when idle is terminated by a hard ware reset, the device normally resumes program execution, from where it left off, up to two machine cycles before the internal reset algorithm takes control. On-chip hardware inhibits access to internal RAM in this event, but access to the port pins is not inhibited. To eliminate the possibility of an unexpected write to a port pin when Idle is terminated by reset, the instruction following the one that invokes Idle should not be one that writes to a port pin or to external memory

Programming Status Word:

The Program Status Word (PSW) contains status bits that reflect the current state of the CPU. The PSW, shown in Figure 11, resides in SFR space. The PSW contains the Carry bit, the Auxiliary Carry (for BCD operations), the two register bank select bits, the Overflow flag, a Parity bit, and two user-definable status flags. The Carry bit, in addition to serving as a Carry bit in arithmetic operations, also serves as the “Accumulator” for a number of Boolean operations.

Page 13: training report on embedded system

The bits RS0 and RS1 select one of the four register banks shown in Figure 8. A number of instructions refer to these RAM locations as R0 through R7. The status of the RS0 and RS1 bits at execution time determines which of the four banks is selected. The Parity bit reflects the number of 1s in the Accumulator: P=1 if the Accumulator contains an odd number of 1s, and P=0 if the Accumulator contains an even number of 1s. Thus, the number of 1s in the Accumulator plus P is always even. Two bits in the PSW are uncommitted and can be used as general purpose status flags.

Page 14: training report on embedded system

Registers

The AccumulatorThe 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 "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 auxiliary registers in many operations.

The "B" RegisterThe "B" register is very similar to the Accumulator in the sense that it may hold an 8-bit value(1-byte) value. The "B" register is only used by two 8051 instructions: MUL AB and DIV AB.

The Data Pointer (DPTR)The Data Pointer (DPTR) is the 8051’s only user-accessible 16-bit (2-byte) register.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.

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.

.The Stack Pointer (SP) The 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

Page 15: training report on embedded system

Instruction set:

MOVINSTRUCTIONS:Simply stated, the MOV instruction copies data from one location to another. It has the following format

MOV destination, sourceThis instruction tells the CPU to move (copy) the source operand to the destination operand, without changing the content of the source operand.

MOV A,#55hLoad 55h into register A

;ADD INSTRUCTIONS:

TheADD instruction has the following format: ADD A, source This tells the CPU to add the source byte to reg A and put the result in reg A

LOOPANDJUMPINSTRUCTIONS:

Repeating a sequence of instructions a certainnumber of times is called a loop.The loop action is performed by the instruction

DJNZreg,label

In this instruction, the register is decremented;if it is not zero, it jumps to the target addressreferred to by the label.Prior to the start of the loop the register is loaded with the counter for the number of repetitions.

UNCONDITIONALJUMPINSTRUCTIONS:

All conditional jumps are short jumps,meaning that the address of the target must be within -128 and +127 bytes of the contents of the program counter(PC).

Unconditional jump instructions are: LJMP (Long jump) ² 3 byte instruction SJMP (Short jump) ² 2 byte instructionCALL INSTRUCTIONS:

CALL instruction is used to call a subroutine

Page 16: training report on embedded system

LCALL(long call) ² 3 byte instruction ACALL(absolutecall)²2byteinstruction

When a subroutine is called, control is transferred to that subroutine .After finishing execution of the subroutine,the instruction RET(return) transfers control back to the caller.

ADDRESSING MODES:

An "addressing mode" refers to how you are addressing a given memory location .Theaddressing modes are as follows,With an example of each:

Immediate Addressing MOV A, #20h

Direct Addressing MOV A, #30h

Indirect Addressing MOV A, @R0

External Direct MOVX A, @DPTR

Code Indirect MOVC A, @A+DPTR

Each of these addressing modes provides important flexibility

INDIRECT ADDRESSING:

Indirect addressing is a very powerful addressing mode which in many cases provides an exceptional level of flexibility.MOV A,@R0This instruction causes the 8051 to analyze the value of theR0 register. The 8051 will thenload the accumulator with the value from Internal RAM which is found at the address indicated by R0.Indirect addressing always refers to InternalRAM; it never refers to an SFR.

EXTERNAL DIRECT ADDRESSING

Page 17: training report on embedded system

External Memory is accessed using "External Direct" addressing .There are only two commands that use External Direct addressing mode:MOVX A,@DPTRMOVX @DPTR,AAs 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 allowyou to write the value of the Accumulator to theexternal memory address pointed to by DPTR.

EXTERNAL INDIRECT ADDRESSING:

External memory can also be accessed using a form of indirect addressing .This form of addressing is usually only used in relatively small projects that have very small amount of external RAM. An example of this addressing mode is:MOVX @R0,AOnce again, the value ofR0 is first read and the valueof the Accumulator is written to that addressing 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.

Microcontrollers versus Microprocessors

Microcontroller differs from a microprocessor in many ways. First and the most important is its functionality. In order for a microprocessor to be used, other components such as memory, o components for receiving and sending data must be added to it. In short that means that microprocessor is the very heart of the computer. On the other hand, microcontroller is designed to be all of that in one. No other external components are needed for its application because all necessary peripherals are already built into it. Thus, we save the time and space needed to construct device

LED AND ITS PROGRAMMING:

WAP to toggle LED

Page 18: training report on embedded system

$ mod 51 org 0here: clr p1.3acall delaysetb p1.3acall delaysjmp heredelay:mov r2,#25l3: mov r0,#255l2: mov r1,#255l1: djnz r1,l1djnz r0,l2djnz r0,l3ret end

8 LED are connected port 1 .WAP such that first odd one glow and then the even one

$ mod 51 org 0 mov p1,#10101010 bacall delaymov p1,#01010101 bacall delayhere: sjmp heredelay:mov r2,#25l3: mov r0,#255l2: mov r1,#255l1: djnz r1,l1djnz r0,l2djnz r0,l3ret end

WAP to select the 3 bank

Page 19: training report on embedded system

$ mod 51 Org 0Setb rs0Setb rs1 mov r0,#22 hend

7 Segment Display INTRODUCTION For the seven segment display you can use the LT-541 or LSD5061-11 chip. Each of the segments of the display is connected to a pin on the 8051 (the schematic shows how to do this). In order to light up a segment on the the pin must be set to 0V. To turn a segment off the corresponding pin must be set to 5V. This is simply done by setting the pins on the 8051 to '1' or '0'. LED displays are

Power-hungry (10ma per LED) Pin-hungry (8 pins per 7-seg display)

But they are cheaper than LCD display

CREATING DIGIT PATTERN

Page 20: training report on embedded system

For displaying Digit say 7 we need to light segments -a ,b, c. Since we are using Common anode display , to do so we have to to provide Logic -0 (0 v) at anode of these segments. so need to clear pins- P1.0 ,P1.1,P1.2. that is 1 1 1 1 1 0 0 0 -->F8h .

Connection Hex Code

Digit

Seg. h Seg. g Seg. f Seg. e Seg. d Seg. c Seg. b Seg. a HEX

0 1 1 0 0 0 0 0 0 C01 0 0 0 0 0 1 1 0 062 1 0 1 0 0 1 0 0 A43 1 0 1 1 0 0 0 0 B04 1 0 0 1 1 0 0 1 99

You can also do this for some characters like A ,E .. but not for D or B because it will be same as that of 0 & 8 . So this is one of limitation of 7-seg display.Since we can Enable only one 7-seg display at a time ,we need to scan these display at fast rate .The scanning frequency should be high enough to be flicker-free. At least 30HZ .Therefore – time one digit is ON is 1/30 seconds

INTERFACING

Segment number

8051 pin number

a P1.0b P1.1c P1.2d P1.3e P1.4f P1.5g p1.6h(dp) P1.7

Page 21: training report on embedded system

Note that I am using Common Anode display. so the common Anode pin is tied to 5v .The cathode pins are connected to port 1 through 330 Ohm resistance (current limiting).

Common Anode display CODE EXAMPLE:

WAP for count of 0 to 9

$ mod 51 Org 0 here:mov p2,#ocohacall delaymov p2,#0f9h acall delay mov p2,#0a4hacall delaymov p2,#0b0hacall delaymovp2,#099hacall delaymov p2,#092hacall delay mov p2,#082hacall delaymov p2,#0f8hacall delaymov p2,#080hacal delaymov p2,#090h

Page 22: training report on embedded system

acall delay

delay:mov r2,#25l3: mov r0,#255l2: mov r1,#255l1: djnz r1,l1djnz r0,l2djnz r0,l3retend

TIMER/COUNTER:

The 8051 comes equipped with two timers, both of which may be controlled, set, read, and configured individually. The 8051 timers have three general functions: 1) Keeping time and/or calculating the amount of time between events, 2) Counting the events themselves, or 3) Generating baud rates for the serial port.

USING TIMERS TO MEASURE TIME

Obviously, one of the primary uses of timers is to measure time. We will discuss this use of timers first and will subsequently discuss the use of timers to count events. When a timer is used to measure time it is also called an "interval timer" since it is measuring the time of the interval between two events.

How long does a timer take to count?

First, its worth mentioning that when a timer is in interval timer mode (as opposed to event counter mode) and correctly configured, it will increment by 1 every machine cycle. As you will recall from the previous chapter, a single machine cycle consists of 12 crystal pulses. Thus a running timer will be incremented:

11,059,000 / 12 = 921,583

921,583 times per second. Unlike instructions--some of which require 1 machine cycle, others 2, and others 4--the timers are consistent: They will always be incremented once per machine cycle. Thus if a timer has counted from 0 to 50,000 you may calculate:

50,000 / 921,583 = .0542

Page 23: training report on embedded system

.0542 seconds have passed. In plain English, about half of a tenth of a second, or one-twentieth of a second.Obviously its not very useful to know .0542 seconds have passed. If you want to execute an event once per second you have to wait for the timer to count from 0 to 50,000 18.45 times. How can you wait "half of a time?" You can’t. So we come to another important calculation.

Lets say we want to know how many times the timer will be incremented in .05 seconds. We can do simple multiplication:

.05 * 921,583 = 46,079.15.

This tells us that it will take .05 seconds (1/20th of a second) to count from 0 to 46,079. Actually, it will take it .049999837 seconds--so were off by .000000163 seconds--however, that’s close enough for government work. Consider that if you were building a watch based on the 8051 and made the above assumption your watch would only gain about one second every 2 months. Again, I think that’s accurate enough for most applications--I wish my watch only gained one second every two months!

Obviously, this is a little more useful. If you know it takes 1/20th of a second to count from 0 to 46,079 and you want to execute some event every second you simply wait for the timer to count from 0 to 46,079 twenty times; then you execute your event, reset the timers, and wait for the timer to count up another 20 times. In this manner you will effectively execute your event once per second, accurate to within thousandths of a second.

Thus, we now have a system with which to measure time. All we need to review is how to control the timers and initialize them to provide us with the information we need.

Timer SFRs

As mentioned before, the 8051 has two timers which each function essentially the same way. One timer is TIMER0 and the other is TIMER1. The two timers share two SFRs (TMOD and TCON) which control the timers, and each timer also has two SFRs dedicated solely to itself (TH0/TL0 and TH1/TL1).

We ve given SFRs names to make it easier to refer to them, but in reality an SFR has a numeric address. It is often useful to know the numeric address that corresponds to an SFR name. The SFRs relating to timers are:

SFR Name Description SFR AddressTH0 Timer 0 High Byte 8ChTL0 Timer 0 Low Byte 8AhTH1 Timer 1 High Byte 8DhTL1 Timer 1 Low Byte 8Bh

Page 24: training report on embedded system

TCON Timer Control 88hTMOD Timer Mode 89h

When you enter the name of an SFR into an assembler, it internally converts it to a number. For example, the command:

MOV TH0,#25h

moves the value 25h into the TH0 SFR. However, since TH0 is the same as SFR address 8Ch this command is equivalent to:

MOV 8Ch,#25h

Now, back to the timers. First, lets talk about Timer 0.

Timer 0 has two SFRs dedicated exclusively to itself: TH0 and TL0. Without making things too complicated to start off with, you may just think of this as the high and low byte of the timer. That is to say, when Timer 0 has a value of 0, both TH0 and TL0 will contain 0. When Timer 0 has the value 1000, TH0 will hold the high byte of the value (3 decimal) and TL0 will contain the low byte of the value (232 decimal). Reviewing low/high byte notation, recall that you must multiply the high byte by 256 and add the low byte to calculate the final value. That is to say:

TH0 * 256 + TL0 = 10003 * 256 + 232 = 1000

Timer 1 works the exact same way, but its SFRs are TH1 and TL1.

Since there are only two bytes devoted to the value of each timer it is apparent that the maximum value a timer may have is 65,535. If a timer contains the value 65,535 and is subsequently incremented, it will reset--or overflow--back to 0.

The TMOD SFR

Lets first talk about our first control SFR: TMOD (Timer Mode). The TMOD SFR is used to control the mode of operation of both timers. Each bit of the SFR gives the microcontroller specific information concerning how to run a timer. The high four bits (bits 4 through 7) relate to Timer 1 whereas the low four bits (bits 0 through 3) perform the exact same functions, but for timer 0.

The individual bits of TMOD have the following functions:

Page 25: training report on embedded system

TMOD (89h) SFR

Bit Name Explanation of Function Timer

7 GATE1When this bit is set the timer will only run when INT1 (P3.3) is high. When this bit is clear the timer will run regardless of the state of INT1.

1

6 C/T1When this bit is set the timer will count events on T1 (P3.5). When this bit is clear the timer will be incremented every machine cycle.

1

5 T1M1 Timer mode bit (see below) 14 T1M0 Timer mode bit (see below) 1

3 GATE0When this bit is set the timer will only run when INT0 (P3.2) is high. When this bit is clear the timer will run regardless of the state of INT0.

0

2 C/T0When this bit is set the timer will count events on T0 (P3.4). When this bit is clear the timer will be incremented every machine cycle.

0

1 T0M1 Timer mode bit (see below) 00 T0M0 Timer mode bit (see below) 0

As you can see in the above chart, four bits (two for each timer) are used to specify a mode of operation. The modes of operation are:

TxM1 TxM0 Timer Mode Description of Mode0 0 0 13-bit Timer.0 1 1 16-bit Timer1 0 2 8-bit auto-reload1 1 3 Split timer mode

13-bit Time Mode (mode 0)

Timer mode "0" is a 13-bit timer. This is a relic that was kept around in the 8051 to maintain compatability with its predecesor, the 8048. Generally the 13-bit timer mode is not used in new development.

Page 26: training report on embedded system

When the timer is in 13-bit mode, TLx will count from 0 to 31. When TLx is incremented from 31, it will "reset" to 0 and increment THx. Thus, effectively, only 13 bits of the two timer bytes are being used: bits 0-4 of TLx and bits 0-7 of THx. This also means, in essence, the timer can only contain 8192 values. If you set a 13-bit timer to 0, it will overflow back to zero 8192 machine cycles later.

Again, there is very little reason to use this mode and it is only mentioned so you wont be surprised if you ever end up analyzing archaeic code which has been passed down through the generations (a generation in a programming shop is often on the order of about 3 or 4 months).

16-bit Time Mode (mode 1)

Timer mode "1" is a 16-bit timer. This is a very commonly used mode. It functions just like 13-bit mode except that all 16 bits are used.

TLx is incremented from 0 to 255. When TLx is incremented from 255, it resets to 0 and causes THx to be incremented by 1. Since this is a full 16-bit timer, the timer may contain up to 65536 distinct values. If you set a 16-bit timer to 0, it will overflow back to 0 after 65,536 machine cycles.

8-bit Time Mode (mode 2)

Timer mode "2" is an 8-bit auto-reload mode. What is that, you may ask? Simple. When a timer is in mode 2, THx holds the "reload value" and TLx is the timer itself. Thus, TLx starts counting up. When TLx reaches 255 and is subsequently incremented, instead of resetting to 0 (as in the case of modes 0 and 1), it will be reset to the value stored in THx.

For example, lets say TH0 holds the value FDh and TL0 holds the value FEh. If we were to watch the values of TH0 and TL0 for a few machine cycles this is what wed see:

Machine Cycle TH0 Value TL0 Value1 FDh FEh2 FDh FFh3 FDh FDh4 FDh FEh5 FDh FFh6 FDh FDh7 FDh FEh

As you can see, the value of TH0 never changed. In fact, when you use mode 2 you almost always set THx to a known value and TLx is the SFR that is constantly incremented.

Page 27: training report on embedded system

Whats the benefit of auto-reload mode? Perhaps you want the timer to always have a value from 200 to 255. If you use mode 0 or 1, youd have to check in code to see if the timer had overflowed and, if so, reset the timer to 200. This takes precious instructions of execution time to check the value and/or to reload it. When you use mode 2 the microcontroller takes care of this for you. Once youve configured a timer in mode 2 you dont have to worry about checking to see if the timer has overflowed nor do you have to worry about resetting the value--the microcontroller hardware will do it all for you.

The auto-reload mode is very commonly used for establishing a baud rate which we will talk more about in the Serial Communications chapter.

Split Timer Mode (mode 3)

Timer mode "3" is a split-timer mode. When Timer 0 is placed in mode 3, it essentially becomes two separate 8-bit timers. That is to say, Timer 0 is TL0 and Timer 1 is TH0. Both timers count from 0 to 255 and overflow back to 0. All the bits that are related to Timer 1 will now be tied to TH0.

While Timer 0 is in split mode, the real Timer 1 (i.e. TH1 and TL1) can be put into modes 0, 1 or 2 normally--however, you may not start or stop the real timer 1 since the bits that do that are now linked to TH0. The real timer 1, in this case, will be incremented every machine cycle no matter what.

The only real use I can see of using split timer mode is if you need to have two separate timers and, additionally, a baud rate generator. In such case you can use the real Timer 1 as a baud rate generator and use TH0/TL0 as two separate timers.

The TCON SFR

Finally, theres one more SFR that controls the two timers and provides valuable information about them. The TCON SFR has the following structure:

TCON (88h) SFRBit Name Explanation of Function Timer

7 TF1 Timer 1 Overflow. This bit is set by the microcontroller when Timer 1 overflows.

1

6 TR1 Timer 1 Run. When this bit is set Timer 1 is turned on. When this bit is clear Timer 1 is off.

1

5 TF0 Timer 0 Overflow. This bit is set by the microcontroller when Timer 0 overflows.

0

4 TR0 Timer 0 Run. When this bit is set Timer 0 is turned on. When this bit is clear Timer 0 is off.

0

Page 28: training report on embedded system

As you may notice, weve only defined 4 of the 8 bits. Thats because the other 4 bits of the SFR dont have anything to do with timers--they have to do with Interrupts and they will be discussed in the chapter that addresses interrupts.Since the SFR is bit-addressable, you could just execute the command:

Initializing a Timer

We must first initialize the TMOD SFR. Since we are working with timer 0 we will be using the lowest 4 bits of TMOD. The first two bits, GATE0 and C/T0 are both 0 since we want the timer to be independent of the external pins. 16-bit mode is timer mode 1 so we must clear T0M1 and set T0M0. Effectively, the only bit we want to turn on is bit 0 of TMOD. Thus to initialize the timer we execute the instruction:

MOV TMOD,#01h

Timer 0 is now in 16-bit timer mode. However, the timer is not running. To start the timer running we must set the TR0 bit We can do that by executing the instruction:

SETB TR0

Upon executing these two instructions timer 0 will immediately begin counting, being incremented once every machine cycle (every 12 crystal pulses).

Reading the Timer

There are two common ways of reading the value of a 16-bit timer; which you use depends on your specific application. You may either read the actual value of the timer as a 16-bit number, or you may simply detect when the timer has overflowed.

Reading the value of a Timer

If your timer is in an 8-bit mode--that is, either 8-bit Auto Reload mode or in split timer mode--then reading the value of the timer is simple. You simply read the 1-byte value of the timer and you re done.

Detecting Timer Overflow

Often it is necessary to just know that the timer has reset to 0. That is to say, you are not particularly interest in the value of the timer but rather you are interested in knowing when the timer has overflowed back to 0.

Whenever a timer overflows from its highest value back to 0, the microcontroller automatically sets the TFx bit in the TCON register. This is useful since rather than checking the exact value of

Page 29: training report on embedded system

the timer you can just check if the TFx bit is set. If TF0 is set it means that timer 0 has overflowed; if TF1 is set it means that timer 1 has overflowed.

We can use this approach to cause the program to execute a fixed delay. As youll recall, we calculated earlier that it takes the 8051 1/20th of a second to count from 0 to 46,079. However, the TFx flag is set when the timer overflows back to 0. Thus, if we want to use the TFx flag to indicate when 1/20th of a second has passed we must set the timer initially to 65536 less 46079, or 19,457. If we set the timer to 19,457, 1/20th of a second later the timer will overflow. Thus we come up with the following code to execute a pause of 1/20th of a second:

MOV TH0,#76;High byte of 19,457 (76 * 256 = 19,456)MOV TL0,#01;Low byte of 19,457 (19,456 + 1 = 19,457)MOV TMOD,#01;Put Timer 0 in 16-bit modeSETB TR0;Make Timer 0 start countingJNB TF0,$;If TF0 is not set, jump back to this same instruction

In the above code the first two lines initialize the Timer 0 starting value to 19,457. The next two instructions configure timer 0 and turn it on. Finally, the last instruction JNB TF0,$, reads "Jump, if TF0 is not set, back to this same instruction." The "$" operand means, in most assemblers, the address of the current instruction. Thus as long as the timer has not overflowed and the TF0 bit has not been set the program will keep executing this same instruction. After 1/20th of a second timer 0 will overflow, set the TF0 bit, and program execution will then break out of the loop.

Creating specific delays

Maximum count of a 16 bit register is 2^16 ie FFFF hand when count increases by one than FFFF TF1 & TF0 bits are set;so at maximum it can count to 0- 65535.( 12 mhz crystal) therefore to give a specific delay suppose of 20 micro second we use following method 65535-20000 = 45535 ie 48AE for TH0 & TLO respectively but to overflow we need an extra count 45535 +1 = 45536 ie 4B5D for TH0 & TLO respectively.

example

delay: MOV TH0, # 4Bh

MOV TL0, #5Dh

MOV TMOD ,#00000001b

Setb TR0

l1:jnb TF l1

Page 30: training report on embedded system

ret

STEPPER MOTOR

Of all motors, step motor is the easiest to control. It's handling simplicity is really hard to deny - all there is to do is to bring the sequence of rectangle impulses to one input of step controller and direction information to another input. Direction information is very simple and comes down to "left" for logical one on that pin and "right" for logical zero. Motor control is also very simple - every impulse makes the motor operating for one step and if there is no impulse the motor won't start. Pause between impulses can be shorter or longer and it defines revolution rate. This rate cannot be infinite because the motor won't be able to "catch up" with all the impulses (documentation on specific motor should contain such information). The picture below represents the scheme for connecting the step motor to microcontroller and appropriate program code follows.

The key to driving a stepper is realizing how the motor is constructed. A diagram shows the representation of a 4 coil motor, so named because 4 coils are used to cause the revolution of the drive shaft. Each coil must be energized in the correct order for the motor to spin.

Step angle

It is angle through which motor shaft rotates in one step. step angle is different for different motor . selection of motor according to step angle depends on the application , simply if you require small increments in rottion choose motor having smaller step angle.No of steps require to rotate one complete rotation = 360 deg. / step angle in deg.

Steps/second

The relation between RPM and steps per sec. is given by ,steps or impulses /sec. =(RPM X Steps /revolution ) /60Pause between impulses can be shorter or longer and it defines revolution rate. This rate cannot be infinite because the motor won't be able to "catch up" with all the impulses (documentation on specific motor should contain such information). So referring to RPM value in datasheet you can calculate steps/sec and from it delay or pause between impulses

Page 31: training report on embedded system

INTERFACING TO 8051.

To cause the stepper to rotate, we have to send a pulse to each coil in turn. The 8051 does not have sufficient drive capability on its output to drive each coil, so there are a number of ways to drive a stepper,Stepper motors are usually controlled by transistor or driver IC like ULN2003. Driving current for each coil is then needed about 60mA at +5V supply. A Darlington transistor array, ULN2003 is used to increase driving capacity of the 2051 chip. Four 4.7k resistors help the 2051 to provide more sourcing current from the +5V supply.

Coil A Coil B Coil C Coil D Step

0 1 1 0 1

0 0 1 1 2

1 0 0 1 3

1 1 0 0 4

Page 32: training report on embedded system

Interrupts

As the name implies, an interrupt is some event which interrupts normal program execution.

As stated earlier, program flow is always sequential, being altered only by those instructions which expressly cause program flow to deviate in some way. However, interrupts give us a mechanism to "put on hold" the normal program flow, execute a subroutine, and

gram inefficient since wed be burning precious "instruction cycles" checking for events that usually dont happen. then resume normal program flow as if we had never left it. This subroutine, called an interrupt handler, is only executed when a certain event (interrupt) occurs. The event may be one of the timers "overflowing," receiving a character via the serial port, transmitting a character via the serial port, or one of two "external events." The 8051 may be configured so that when any of these events occur the main program is temporarily suspended and control passed to a special section of code which presumably would execute some function related to the event that occured. Once complete, control would be returned to the original program. The main program never even knows it was interrupted.

The ability to interrupt normal program execution when certain events occur makes it much easier and much more efficient to handle certain conditions. If it were not for interrupts we would have to manually check in our main program whether the timers had overflown, whether we had received another character via the serial port, or if some external event had occured. Besides making the main program ugly and hard to read, such a situation would make our pro

What Events Can Trigger Interrupts, and where do they go?

We can configure the 8051 so that any of the following events will cause an interrupt:

Timer 0 Overflow. Timer 1 Overflow. Reception/Transmission of Serial Character. External Event 0. External Event 1.

In other words, we can configure the 8051 so that when Timer 0 Overflows or when a character is sent/received, the appropriate interrupt handler routines are called.

Page 33: training report on embedded system

Obviously we need to be able to distinguish between various interrupts and executing different code depending on what interrupt was triggered. This is accomplished by jumping to a fixed address when a given interrupt occurs.

Interrupt Flag Interrupt Handler AddressExternal 0 IE0 0003hTimer 0 TF0 000BhExternal 1 IE1 0013hTimer 1 TF1 001BhSerial RI/TI 0023h

By consulting the above chart we see that whenever Timer 0 overflows (i.e., the TF0 bit is set), the main program will be temporarily suspended and control will jump to 000BH. It is assumed that we have code at address 000BH that handles the situation of Timer 0 overflowing.

Setting Up Interrupts

By default at powerup, all interrupts are disabled. This means that even if, for example, the TF0 bit is set, the 8051 will not execute the interrupt. Your program must specifically tell the 8051 that it wishes to enable interrupts and specifically which interrupts it wishes to enable.

Your program may the IE SFR (enable and disable interrupts by modifying A8h):

Bit Name Explanation of Function7 EA Global Interrupt Enable/Disable6 - Undefined5 - Undefined4 ES Enable Serial Interrupt3 ET1 Enable Timer 1 Interrupt2 EX1 Enable External 1 Interrupt1 ET0 Enable Timer 0 Interrupt0 EX0 Enable External 0 Interrupt

As you can see, each of the 8051s interrupts has its own bit in the IE SFR. You enable a given interrupt by setting the corresponding bit. For example, if you wish to enable Timer 1 Interrupt, you would execute either:

MOV IE,#08hor

Page 34: training report on embedded system

SETB ET1Both of the above instructions set bit 3 of IE, thus enabling Timer 1 Interrupt. Once Timer 1 Interrupt is enabled, whenever the TF1 bit is set, the 8051 will automatically put "on hold" the main program and execute the Timer 1 Interrupt Handler at address 001Bh.

Polling Sequence

The 8051 automatically evaluates whether an interrupt should occur after every instruction. When checking for interrupt conditions, it checks them in the following order:

External 0 Interrupt Timer 0 Interrupt External 1 Interrupt Timer 1 Interrupt Serial Interrupt

This means that if a Serial Interrupt occurs at the exact same instant that an External 0 Interrupt occurs, the External 0 Interrupt will be executed first and the Serial Interrupt will be executed once the External 0 Interrupt has completed.

Interrupt Priorities

The 8051 offers two levels of interrupt priority: high and low. By using interrupt priorities you may assign higher priority to certain interrupt conditions .Programmer should assign 1 to bit to be served first else default order will be followed .Interrupt priorities are controlled by the IP SFR (B8h). The IP SFR has the following format:

Bit Name Bit Address Explanation of Function7 - - Undefined6 - - Undefined

5 - - Undefined4 PS BCh Serial Interrupt Priority3 PT1 BBh Timer 1 Interrupt Priority2 PX1 BAh External 1 Interrupt Priority1 PT0 B9h Timer 0 Interrupt Priority0 PX0 B8h External 0 Interrupt Priority

When considering interrupt priorities, the following rules apply:

Nothing can interrupt a high-priority interrupt--not even another high priority interrupt. A high-priority interrupt may interrupt a low-priority interrupt. A low-priority interrupt may only occur if no other interrupt is already executing.

Page 35: training report on embedded system

If two interrupts occur at the same time, the interrupt with higher priority will execute first. If both interrupts are of the same priority the interrupt which is serviced first by polling sequence will be executed first.

What Happens When an Interrupt Occurs?

When an interrupt is triggered, the following actions are taken automatically by the microcontroller:

The current Program Counter is saved on the stack, low-byte first. Interrupts of the same and lower priority are blocked. In the case of Timer and External interrupts, the corresponding interrupt flag is cleared. Program execution transfers to the corresponding interrupt handler vector address. The Interrupt Handler Routine executes.

Take special note of the third step: If the interrupt being handled is a Timer or External interrupt, the microcontroller automatically clears the interrupt flag before passing control to your interrupt handler routine. This means it is not necessary that you clear the bit in your code.

What Happens When an Interrupt Ends?

An interrupt ends when your program executes the RETI (Return from Interrupt) instruction. When the RETI instruction is executed the following actions are taken by the microcontroller:

Two bytes are popped off the stack into the Program Counter to restore normal program execution.

Interrupt status is restored to its pre-interrupt status.

L C D

Liquid Crystal Display also called as LCD is very helpful in providing user interface as well as for debugging purpose. The most common type of LCD controller is HITACHI 44780 which provides a simple interface between the controller & an LCD. These LCD's are very simple to interface with the controller as well as are cost effective.

Page 36: training report on embedded system

2x16 Line Alphanumeric LCD Display

The most commonly used ALPHA NUMERIC displays are 1x16 (Single Line & 16 characters), 2x16 (Double Line & 16 character per line) & 4x20 (four lines & Twenty characters per line).

Pin diagram of lcd

The LCD requires 3 control lines (RS, R/W & EN) & 8 (or 4) data lines. The number on data lines depends on the mode of operation. If operated in 8-bit mode then 8 data lines + 3 control lines i.e. total 11 lines are required. And if operated in 4-bit mode then 4 data lines + 3 control lines i.e. 7 lines are required.

.

Pin Symbol Function

1 Vss Ground

2 Vdd Supply Voltage

3 Vo Contrast Setting

4 RS Register Select

5 R/W Read/Write Select

6 En Chip Enable Signal

7-14 DB0-DB7 Data Lines

15 A/Vee Gnd for the backlight

16 K Vcc for backlight

Page 37: training report on embedded system

When RS is low (0), the data is to be treated as a command. When RS is high (1), the data being sent is considered as text data which should be displayed on the screen.

When R/W is low (0), the information on the data bus is being written to the LCD. When RW is high (1), the program is effectively reading from the LCD. Most of the times there is no need to read from the LCD so this line can directly be connected to Gnd thus saving one controller line.

The ENABLE pin is used to latch the data present on the data pins. A HIGH - LOW signal is required to latch the data. The LCD interprets and executes our command at the instant the EN line is brought low. If you never bring EN low, your instruction will never be executed.

INTERFACING LCD TO 8051

8051 Interfacing to LCD

For Contrast setting a 10K pot should be used as shown in the figure.Display Data Ram (DDRAM) stores the display data. So when we have to display a character on LCD we basically

write it into DDRAM. For a 2x16 LCD the DDRAM address for first line is from 80h to 8fh & for second line is 0c0h to 0cfh. So if we want to display 'H' on the 7th postion of the first line then we will write it at location 87h.

Command or Special Instruction

Instruction HEX

Function set (8-bit interface, 2 lines, 5*7 Pixels) 38

Page 38: training report on embedded system

Function set (8-bit interface, 1 line, 5*7 Pixels) 30

Function set (4-bit interface, 2 lines, 5*7 Pixels) 28

Function set (4-bit interface, 1 line, 5*7 Pixels) 20

Display off cursor on 0A

Scroll display one character right (all lines) 1C

Scroll display one character left (all lines) 18

Home (move cursor to top/left character position) 2

Move cursor one character left 10

Move cursor one character right 14

Turn on visible underline cursor 0E

Turn on visible blinking-block cursor 0F

Make cursor invisible 0C

Blank the display (without clearing) 08

Restore the display (with cursor hidden) 0C

Clear Screen 01

Automatically increase cursor to right 06

Set cursor position to first line80+ addr

Automatically increase cursor to left 04

Page 39: training report on embedded system

Now let us write a program that will display 'DNA TECHNOLOGY' on LCD.

ORG 0000h

CALL lcd_initialize

CALL lcd_clr

MOV A,#80h //Location from where Data is to be displayed

CALL lcd_command

MOV A,#'D'

CALL lcd_datadisplay

MOV A,#'N'

CALL lcd_datadisplay

MOV A,#'A'

CALL lcd_datadisplay

MOV A,#20h //Hex value for blank space to be displayed

CALL lcd_datadisplay

MOV A,#'T'

CALL lcd_datadisplay

MOV A,#'E'

CALL lcd_datadisplay

MOV A,#'C'

CALL lcd_datadisplay

MOV A,#'H'

CALL lcd_datadisplay

MOV A,#'N'

Page 40: training report on embedded system

CALL lcd_datadisplay

MOV A,#'O'

CALL lcd_datadisplay

MOV A,#'L'

CALL lcd_datadisplay

MOV A,#'O'

CALL lcd_datadisplay

MOV A,#'G'

CALL lcd_datadisplay

MOV A,#'Y'

CALL lcd_datadisplay

stop:ajmp stop

lcd_clr:

MOV A,#01h

CALL lcd_command

ret

lcd_initialize:

MOV A,#38h

CALL lcd_command

MOV A,#38h

CALL lcd_command

MOV A,#38h

CALL lcd_command

Page 41: training report on embedded system

ret

lcd_datadisplay:

SETB RS //Telling the LCD that the data which is being send is to be displayed

MOV P1,A //Character to be displayed is in Acc

SETB EN

CLR EN //High to Low pulse on EN to latch the data

CALL DELAY //Delay so that LCD finishes its internal operations

ret

.

lcd_command:

CLR RS //Telling the LCD that the data which is being send is a command

MOV P1,A //Character to be displayed is in Acc

SETB EN

CLR EN //High to Low pulse on EN to latch the data

CALL DELAY //Delay so that LCD finishes its internal operations

ret

Serial communication:

Page 42: training report on embedded system

Beside stated above we've added to the already existing unit the possibility of communication with an outside world. However, this way of communicating has its drawbacks. One of the basic drawbacks is the number of lines which need to be used in order to transfer data. What if it I being transferred to a distance of several kilometers? The number of lines times’ number of kilometers doesn't promise the economy of the project. It leaves us having to reduce the number of lines in such a way that we don't lessen its functionality. Suppose we are working with three lines only, and that one line is used for sending data, other for receiving, and the third one is used as a reference line for both the input and the output side. In order for this to work, we need to set the rules of exchange of data. These rules are called protocol. Protocol is therefore defined in advance so there wouldn't be any misunderstanding between the sides that are communicating with each other. For example, if one man is speaking in French, and the other in English, it is highly unlikely that they will quickly and effectively understand each other. Let's suppose we have the following protocol. The logical unit "1" is set up on the transmitting line until transfer begins. Once the transfer starts, we lower the transmission line to logical "0" for a period of time (which we will designate as T), so the receiving side will know that it is receiving data, and so it will activate its mechanism for reception. Let's go back now to the transmission side and start putting logic zeros and ones onto the transmitter line in the order from a bit of the lowest value to a bit of the highest value. Let each bit stay on line for a time period which is equal to T, and in the end, or after the 8th bit, let us bring the logical unit "1"back on the line which will mark the end of the transmission of one data. The protocol we've just described is called in professional literature NRZ (Non-Return to Zero)

As we have separate lines for receiving and sending, it is possible to receive and send data (info.) at the same time. So called full-duplex mode block which enables this way of communication is called a serial communication block. Unlike the parallel transmission, data moves here bit by bit, or in a series of bits what defines the term serial communication comes from . After the reception of data we need to read it from the receiving location and store it in memory as opposed to sending where the process is reversed. Data goes from memory through the bus to the sending location, and then to the receiving unit according to the protocol

Page 43: training report on embedded system

Setting the Serial Port Mode

The first thing we must do when using the 8051s integrated serial port is, obviously, configure it. This lets us tell the 8051 how many data bits we want, the baud rate we will be using, and how the baud rate will be determined.

First, lets present the "Serial Control" (SCON) SFR and define what each bit of the SFR represents:

Bit Name Bit Address Explanation of Function7 SM0 9Fh Serial port mode bit 06 SM1 9Eh Serial port mode bit 1.5 SM2 9Dh Mutli processor Communications Enable (explained later)

4 REN 9Ch Receiver Enable. This bit must be set in order to receive characters.

3 TB8 9Bh Transmit bit 8. The 9th bit to transmit in mode 2 and 3.2 RB8 9Ah Receive bit 8. The 9th bit received in mode 2 and 3.1 TI 99h Transmit Flag. Set when a byte has been completely transmitted.0 RI 98h Receive Flag. Set when a byte has been completely received.

Additionally, it is necessary to define the function of SM0 and SM1 by an additional table:

SM0 SM1 Serial Mode Explanation Baud Rate0 0 0 8-bit Shift Register Oscillator / 120 1 1 8-bit UART Set by Timer 1 (*)1 0 2 9-bit UART Oscillator / 64 (*)1 1 3 9-bit UART Set by Timer 1 (*)

(*) Note: The baud rate indicated in this table is doubled if PCON.7 (SMOD) is set.

The SCON SFR allows us to configure the Serial Port. Thus, well go through each bit and review its function.

The first four bits (bits 4 through 7) are configuration bits.

Bits SM0 and SM1 let us set the serial mode to a value between 0 and 3, inclusive. The four modes are defined in the chart immediately above. As you can see, selecting the Serial Mode selects the mode of operation (8-bit/9-bit, UART or Shift Register) and also determines how the baud rate will be calculated. In modes 0 and 2 the baud rate is fixed based on the oscillators

Page 44: training report on embedded system

frequency. In modes 1 and 3 the baud rate is variable based on how often Timer 1 overflows. Well talk more about the various Serial Modes in a moment.

The next bit, SM2, is a flag for "Multiprocessor communication." Generally, whenever a byte has been received the 8051 will set the "RI" (Receive Interrupt) flag. This lets the program know that a byte has been received and that it needs to be processed. However, when SM2 is set the "RI" flag will only be triggered if the 9th bit received was a "1". That is to say, if SM2 is set and a byte is received whose 9th bit is clear, the RI flag will never be set. This can be useful in certain advanced serial applications. For now it is safe to say that you will almost always want to clear this bit so that the flag is set upon reception of any character.

The next bit, REN, is "Receiver Enable." This bit is very straightforward: If you want to receive data via the serial port, set this bit. You will almost always want to set this bit.

The last four bits (bits 0 through 3) are operational bits. They are used when actually sending and receiving data--they are not used to configure the serial port.

The TB8 bit is used in modes 2 and 3. In modes 2 and 3, a total of nine data bits are transmitted. The first 8 data bits are the 8 bits of the main value, and the ninth bit is taken from TB8. If TB8 is set and a value is written to the serial port, the data s bits will be written to the serial line followed by a "set" ninth bit. If TB8 is clear the ninth bit will be "clear."

The RB8 also operates in modes 2 and 3 and functions essentially the same way as TB8, but on the reception side. When a byte is received in modes 2 or 3, a total of nine bits are received. In this case, the first eight bits received are the data of the serial byte received and the value of the ninth bit received will be placed in RB8.

TI means "Transmit Interrupt." When a program writes a value to the serial port, a certain amount of time will pass before the individual bits of the byte are "clocked out" the serial port. If the program were to write another byte to the serial port before the first byte was completely output, the data being sent would be garbled. Thus, the 8051 lets the program know that it has "clocked out" the last byte by setting the TI bit. When the TI bit is set, the program may assume that the serial port is "free" and ready to send the next byte.

Finally, the RI bit means "Receive Interrupt." It function similarly to the "TI" bit, but it indicates that a byte has been received. That is to say, whenever the 8051 has received a complete byte it will trigger the RI bit to let the program know that it needs to read the value quickly, before another byte is read.

Setting the Serial Port Baud Rate

Once the Serial Port Mode has been configured, as explained above, the program must configure the serial ports baud rate. This only applies to Serial Port modes 1 and 3. The Baud Rate is determined based on the oscillators frequency when in mode 0 and 2. In mode 0, the

Page 45: training report on embedded system

baud rate is always the oscillator frequency divided by 12. This means if your crystal is 11.059Mhz, mode 0 baud rate will always be 921,583 baud. In mode 2 the baud rate is always the oscillator frequency divided by 64, so a 11.059Mhz crystal speed will yield a baud rate of 172,797.

In modes 1 and 3, the baud rate is determined by how frequently timer 1 overflows. The more frequently timer 1 overflows, the higher the baud rate. There are many ways one can cause timer 1 to overflow at a rate that determines a baud rate, but the most common method is to put timer 1 in 8-bit auto-reload mode (timer mode 2) and set a reload value (TH1) that causes Timer 1 to overflow at a frequency appropriate to generate a baud rate.

References And Bibliography

“THE 8051 MICROCONTROLLER AND EMBEDDED SYSTEM”by Muhammad Ali Mazidi , Janice Gillispie Mazidi, Rolin D. Mckinlay.

“The 8051 MICROCONTROLLER” by K. J. Ayala.

"Advanced Microprocessors and Microcontrollers" by B.P. Singh & Renu Singh.

Net Links

www.wikipedia.com

www.cetpainfotech.com

www.electronicsforyou.com

www.encyclopedia.com