Docsity

micro controller tutorial, Study Guides, Projects, Research of Microcontrollers

Partial preview of the text

Download micro controller tutorial and more Study Guides, Projects, Research Microcontrollers in PDF only on Docsity!

TUTORIAL

Donal Heffernan

University of Limerick

May-

Blank

CONTENTS

  • Chapter 1 8051 Microcomputer Overview
  • Chapter 2 A Simple Design Example
  • Chapter 3 Software Delay Routines
  • Chapter 4 Interrupts
  • Chapter 5 Timer/Counters
  • Chapter 6 The 8051 Serial Port
  • Appendix A Example Term Assignments A
  • Appendix B Sample Exam Questions & Answers B
  • Appendix C A Brief Introduction to Using Keil Tools C

Figure 1.2 shows the external code memory and data memory connected to the 8051 chip.

Note – part of the external code memory can be located within the chip but we will ignore this feature for now. Also, variants of the chip will allow a lot more memory devices and I/O devices to be accommodate within the chip but such enhanced features will not be considered right now.

External DATA Memory (RAM)

External CODE Memory (ROM)

I-RAM

ADDRESS BUS (16-bit)

DATA BUS (8-bit)

control lines

I/O ports e.g. P1, P3 etc.

12MHz

Figure 1.2 8051 chip with external memory

A quick comparison with the well known Pentium processor

A modern PC is powered by a Pentium processor (or equivalent), which is really a very powerful microprocessor. Where the 8051 microcontroller represents the low end of the market in terms of processing power, the Pentium processor is one of the most complex processors in the world. Figure 1.3 shows a simplified block diagram of the Pentium processor and a simple comparison between the 8051 and the Pentium is given in the table below.

PENTIUM

Chip

The Pentium's Memory Space

DATA BUS (64-bit)

control lines

ADDRESS BUS (32-bit)

multiple 32-bit ALUs (Super- scalar)

1,000MHz(1 GHz.)

Figure 1.3 Simplified diagram of a Pentium processor

Simple comparison: Pentium vs. 8051

FEATURE 8051 PENTIUM COMMENT Clock Speed 12Mhz. typical but 60MHz. ICs available

1,000 MHz. (1GHz.) 8051 internally divides clock by 12 so for 12MHz. clock effective clock rate is just 1MHz. Address bus 16 bits 32 bits 8051 can address 2 16 , or 64Kbytes of memory. Pentium can address 2 32 , or 4 GigaBytes of memory. Data bus 8 bits 64 bits Pentium’s wide bus allows very fast data transfers. ALU width 8 bits 32 bits But - Pentium has multiple 32 bit ALUs – along with floating-point units. Applications Domestic appliances, Peripherals, automotive etc.

Personal Computers And other high performance areas. Power consumption

Small fraction of a watt Tens of watts Pentium runs hot as power consumption increases with frequency. Cost of chip About 2 Euros. In volume

About 200 Euros – Depending on spec.

1.2 MEMORY AND REGISTER ORGANISATION

The 8051 has a separate memory space for code (programs) and data. We will refer here to on-chip memory and external memory as shown in figure 1.5. In an actual implementation the external memory may, in fact, be contained within the microcomputer chip. However, we will use the definitions of internal and external memory to be consistent with 8051 instructions which operate on memory. Note, the separation of the code and data memory in the 8051 architecture is a little unusual. The separated memory architecture is referred to as Harvard architecture whereas Von Neumann architecture defines a system where code and data can share common memory.

Figure 1.5 8051 Memory representation

External Code Memory The executable program code is stored in this code memory. The code memory size is limited to 64KBytes (in a standard 8051). The code memory is read-only in normal operation and is programmed under special conditions e.g. it is a PROM or a Flash RAM type of memory.

External RAM Data Memory This is read-write memory and is available for storage of data. Up to 64KBytes of external RAM data memory is supported (in a standard 8051).

Internal Memory The 8051’s on-chip memory consists of 256 memory bytes organised as follows:

First 128 bytes: 00h to 1Fh Register Banks 20h to 2Fh Bit Addressable RAM 30 to 7Fh General Purpose RAM

Next 128 bytes: 80h to FFh Special Function Registers

The first 128 bytes of internal memory is organised as shown in figure 1.6, and is referred to as Internal RAM, or IRAM.

External DATA Memory (up to 64KB) RAM

External CODE Memory (up to 64KB) ROM

8051 chip

Internal Memory

Internal RAM

Internal SFRs

0000h

FFFFh

FFFFh

0000h

Byte Address Bit address b7 b6 b5 b4 b3 b2 b1 b 7Fh

30h

General purpose RAM area. 80 bytes

2Fh 7F 78 2Eh 77 70 2Dh 6F 68 2Ch 67 60 2Bh 5F 58 2Ah 57 50 29h 4F 48 28h 47 40 27h 3F 38 26h 37 30 25h 2F 28 24h 27 20 23h 1F 18 22h 17 10 21h 0F 08 20h 07 00 1Fh 18h

Regs 0 ..7 (Bank 1)

17h 10h

Regs 0 ..7 (Bank 1)

0Fh 08h

Regs 0 ..7 (Bank 1)

07h 00h

Regs 0 ..7 (Bank 0)

Figure 1.6 Organisation of Internal RAM (IRAM) memory

Register Banks: 00h to 1Fh The 8051 uses 8 general-purpose registers R0 through R7 (R0, R1, R2, R3, R4, R5, R6, and R7). These registers are used in instructions such as:

ADD A, R2 ; adds the value contained in R2 to the accumulator

Note since R2 happens to be memory location 02h in the Internal RAM the following instruction has the same effect as the above instruction.

ADD A, 02h

Internal Memory

SFRs

Internal RAM

FFh

80h 7Fh

00h

Reg. 7 Reg. 6 Reg. 5 Reg. 4 Reg. 3 Reg. 2 Reg. 1 Reg. 0

07h 06h 05h 04h 03h 02h 01h 00h

Register Bank 0

address space is defined only 21 SFR registers are defined in the standard 8051. Undefined SFR addresses should not be accessed as this might lead to some unpredictable results. Note some of the SFR registers are bit addressable. SFRs are accessed just like normal Internal RAM locations.

Byte Bit address address b7 b6 b5 b4 b3 b2 b1 b

FFh

F0h B *

E0h A (accumulator) *

D0h PSW *

B8h IP *

B0h Port 3 (P3) *

A8h IE *

A0h Port 2 (P2) *

99h SBUF 98h SCON *

90h Port 1 (P1) *

8Dh TH 8Ch TH 8Bh TL 8Ah TL 89h TMOD 88h TCON * 87h PCON

83h DPH 82h DPL 81h SP 80h Port 0 (P0) *

  • indicates the SFR registers which are bit addressable

Figure 1.7 SFR register layout

We will discuss a few specific SFR registers here to help explain the SFR concept. Other specific SFR will be explained later.

Port Registers SFR The standard 8051 has four 8 bit I/O ports: P0, P1, P2 and P3.

Internal Memory

SFRs

Internal RAM

FFh

80h 7Fh

00h

For example Port 0 is a physical 8 bit I/O port on the 8051. Read (input) and write (output) access to this port is done in software by accessing the SFR P0 register which is located at address 80h. SFR P0 is also bit addressable. Each bit corresponds to a physical I/O pin on the 8051. Example access to port 0:

SETB P0.7 ; sets the MSB bit of Port 0 CLR P0.7 ; clears the MSB bit of Port 0

The operand P0.7 uses the dot operator and refers to bit 7 of SFR P0. The same bit could be addressed by accessing bit location 87h. Thus the following two instructions have the same meaning:

CLR P0. CLR 87h

PSW Program Status Word PSW, the Program Status Word is at address D0h and is a bit-addressable register. The status bits are listed in table 1.1.

Table 1.1. Program status word (PSW) flags Symbol Bit Address Description C (or CY) PSW.7 D7h Carry flag AC PSW.6 D6h Auxiliary carry flag F0 PSW.5 D5h Flag 0 RS1 PSW.4 D4h Register bank select 1 RS0 PSW.3 D3h Register bank select 0 0V PSW.2 D2h Overflow flag PSW.1 D1h Reserved P PSW.0 D0h Even Parity flag

Carry flag. C This is a conventional carry, or borrow, flag used in arithmetic operations. The carry flag is also used as the ‘Boolean accumulator’ for Boolean instruction operating at the bit level. This flag is sometimes referenced as the CY flag.

Auxiliary carry flag. AC This is a conventional auxiliary carry (half carry) for use in BCD arithmetic.

Flag 0. F This is a general-purpose flag for user programming.

Register bank select 0 and register bank select 1. RS0 and RS These bits define the active register bank (bank 0 is the default register bank).

Overflow flag. OV This is a conventional overflow bit for signed arithmetic to determine if the result of a signed arithmetic operation is out of range.

However, the accumulator is defined as an SFR register at address E0h. So the following two instructions have the same effect:

MOV A, #52h ; Move immediate the value 52h to the accumulator

MOV E0h, #52h ; Move immediate the value 52h to Internal RAM location E0h, which is, in fact, the accumulator SFR register.

Usually the first method, MOV A, #52h, is used as this is the most conventional (and happens to use less space, 2 bytes as oppose to 3 bytes!)

B Register The B register is an SFR register at addresses F0h which is bit-addressable. The B register is used in two instructions only: i.e. MUL (multiply) and DIV (divide). The B register can also be used as a general-purpose register.

Program Counter The PC (Program Counter) is a 2 byte (16 bit) register which always contains the memory address of the next instruction to be executed. When the 8051 is reset the PC is always initialised to 0000h. If a 2 byte instruction is executed the PC is incremented by 2 and if a 3 byte instruction is executed the PC is incremented by three so as to correctly point to the next instruction to be executed. A jump instruction (e.g. LJMP) has the effect of causing the program to branch to a newly specified location, so the jump instruction causes the PC contents to change to the new address value. Jump instructions cause the program to flow in a non-sequential fashion, as will be described later.

SFR Registers for the Internal Timer

The set up and operation of the on-chip hardware timers will be described later, but the associated registers are briefly described here:

TCON, the Timer Control register is an SFR at address 88h, which is bit-addressable. TCON is used to configure and monitor the 8051 timers. The TCON SFR also contains some interrupt control bits, described later.

TMOD, the Timer Mode register is an SFR at address 89h and is used to define the operational modes for the timers, as will be described later.

TL0 (Timer 0 Low) and TH0 (Timer 0 High) are two SFR registers addressed at 8Ah and 8Bh respectively. The two registers are associated with Timer 0.

TL1 (Timer 1 Low) and TH1 (Timer 1 High) are two SFR registers addressed at 8Ch and 8Dh respectively. These two registers are associated with Timer 1.

Power Control Register PCON (Power Control) register is an SFR at address 87h. It contains various control bits including a control bit, which allows the 8051 to go to ‘sleep’ so as to save power when not in immediate use.

Serial Port Registers Programming of the on-chip serial communications port will be described later in the text. The associated SFR registers, SBUF and SCON, are briefly introduced here, as follows:

The SCON (Serial Control) is an SFR register located at addresses 98h, and it is bit- addressable. SCON configures the behaviour of the on-chip serial port, setting up parameters such as the baud rate of the serial port, activating send and/or receive data, and setting up some specific control flags.

The SBUF (Serial Buffer) is an SFR register located at address 99h. SBUF is just a single byte deep buffer used for sending and receiving data via the on-chip serial port

Interrupt Registers Interrupts will be discussed in more detail later. The associated SFR registers are:

IE (Interrupt Enable) is an SFR register at addresses A8h and is used to enable and disable specific interrupts. The MSB bit (bit 7) is used to disable all interrupts.

IP (Interrupt Priority) is an SFR register at addresses B8h and it is bit addressable. The IP register specifies the relative priority (high or low priority) of each interrupt. On the 8051, an interrupt may either be of low (0) priority or high (1) priority..

1.3 ADDRESSING MODES

There are a number of addressing modes available to the 8051 instruction set, as follows:

Immediate Addressing Register Addressing Direct Addressing Indirect Addressing Relative Addressing Absolute addressing Long Addressing Indexed Addressing

Immediate Addressing Immediate addressing simply means that the operand (which immediately follows the instruction op. code) is the data value to be used. For example the instruction:

MOV A, #99d

Accumulator number 99d

location, which contains the operand data. Indirect addressing refers to Internal RAM only and cannot be used to refer to SFR registers.

Note, only R0 or R1 can be used as register data pointers for indirect addressing when using MOV instructions.

The 8052 (as opposed to the 8051) has an additional 128 bytes of internal RAM. These 128 bytes of RAM can be accessed only using indirect addressing.

Relative Addressing This is a special addressing mode used with certain jump instructions. The relative address, often referred to as an offset, is an 8-bit signed number, which is automatically added to the PC to make the address of the next instruction. The 8-bit signed offset value gives an address range of + 127 to –128 locations. Consider the following example:

SJMP LABEL_X

An advantage of relative addressing is that the program code is easy to relocate in memory in that the addressing is relative to the position in memory.

Absolute addressing Absolute addressing within the 8051 is used only by the AJMP (Absolute Jump) and ACALL (Absolute Call) instructions, which will be discussed later.

Long Addressing The long addressing mode within the 8051 is used with the instructions LJMP and LCALL. The address specifies a full 16 bit destination address so that a jump or a call can be made to a location within a 64KByte code memory space (2^16 = 64K). An example instruction is:

LJMP 5000h ; full 16 bit address is specified in operand

Code Memory

2006h 2005h 2004h 2003h 2002h 2001h 2000h 1FFFh

80

(^04) SJMP LABEL_X

PC is set to next instruction address: 2002h when SJMP begins execution. The target address is then the sum of the PC

  • relative offset needed to reach LABEL_X. Offset is 4 in this case. 2002h +4h = 2006h

Indexed Addressing With indexed addressing a separate register, either the program counter, PC, or the data pointer DTPR, is used as a base address and the accumulator is used as an offset address. The effective address is formed by adding the value from the base address to the value from the offset address. Indexed addressing in the 8051 is used with the JMP or MOVC instructions. Look up tables are easy to implemented with the help of index addressing. Consider the example instruction:

MOVC A, @A+DPTR

MOVC is a move instruction, which moves data from the external code memory space. The address operand in this example is formed by adding the content of the DPTR register to the accumulator value. Here the DPTR value is referred to as the base address and the accumulator value us referred to as the index address. An example program using the indexed addressing mode will be shown later.

1.4 ASSEMBLY LANGUAGE PROGRAMMING

Number Representation for Different Bases

The following is an example showing the decimal number 46 represented in different number bases:

46d ; 46 decimal 2Eh ; 2E h is 46 decimal represented as a hex number 56o ; 56 o is 46 decimal represented as an octal number 101110b ; 101110 b is 46 decimal represented as a binary number.

Note a number digit must be used in the first character of a hexadecimal number. For example the hexadecimal number A5h is illegally represented and should be represented as 0A5h.

The Arithmetic Operators The arithmetic operators are:

  • add
  • subtract
  • multiply / divide MOD modulo (result is the remainder following division)

The Logical Operators The logical operators are: