Unit - 1
Introduction to concept of microcontroller
A microcontroller is a computer present in a single integrated circuit that is dedicated to performing one task and execute one specific application.
It contains memory, programmable input/output peripherals as well a processor. Microcontrollers are mostly designed for embedded applications and are heavily used in automatically controlled electronic devices such as cell phones, cameras, microwave ovens, washing machines, etc.
Figure 1. 8051 Functional diagram
8051 has 4 K Bytes of internal ROM. The address space is from 0000 to 0FFFh. If the program size is more than 4 K Bytes 8051 will fetch the code automatically from external memory.
The accumulator is an 8-bit register widely used for all arithmetic and logical operations. The accumulator is also used to transfer data between external memory. B register is used along with Accumulator for multiplication and division. A and B registers together are also called MATH registers.
PSW (Program Status Word). This is an 8-bit register that contains the arithmetic status of ALU and the bank select bits of register banks.
CY - carry flag
AC - auxiliary carry flag
F0 - available to the user for general purpose
RS1, RS0 - register bank select bits
OV - overflow
P - parity
Stack Pointer (SP) – Contains the address of the data item on the top of the stack. The stack may reside anywhere on the internal RAM. On reset, SP is initialized to 07 so that the default stack will start from address 08 onwards.
Data Pointer (DPTR)
– DPH (Data pointer higher byte), DPL (Data pointer lower byte). This is a 16-bit register that is used to furnish address information for internal and external program memory and external data memory.
Program Counter (PC) – 16 -bit PC contains the address of the next instruction to be executed. On reset, the PC will set to 0000. After fetching every instruction PC will increment by one.
Key Takeaways:
Pin Diagram
Figure 2. Pin Diagram of 8051
Pinout Description:
Pin 1-8 | PORT1- Each of these pins can be configured as an input or output. |
Pin 9 | RESET- A logic set on this pin disables the microcontroller and clears the contents of most registers. In other words, a positive voltage on this pin resets the microcontroller. |
Pin 10-17 | PORT -3: Similar to Port 1. Each pin serves as general output or input. Besides all of them have alternate 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 | External Interrupt 0 input |
Pin 14 INT1 | External Interrupt 1 input |
Pin 15 T0 | T0 Counter 0 clock input |
Pin16 T1 | T1 Counter 1 clock input |
Pin 17 RD | Read from external RAM |
Pin 18, 19 XTAL2, XTAL1 | Internal oscillator input and output. A quartz crystal that specifies operating frequency is usually connected to these pins. |
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 general inputs/outputs. In case external memory is used, the higher address byte, i.e. addresses A8-A15 will appear on this port. Even though memory with a capacity of 64Kb is not used, which means that not all eight port bits are used for its addressing, the rest of them are not available as inputs/outputs. |
Pin 29 PSEN | If external ROM is used for storing program then a logic zero (0) appears on it every time the microcontroller reads a byte from memory. |
Pin 30 ALE | Before reading from external memory, the microcontroller puts the lower address byte (A0-A7) on P0 and then activates the ALE output. After receiving a signal from the ALE pin, the external latch latches the state of P0 and uses it as a memory chip address. Immediately the ALE pin is returned to its previous logic state and P0 is now used as a Data Bus. |
Pin 31 EA | By applying logic zero to this pin, P2 and P3 are used for data and address transmission whether there is internal memory or not. It means that even if there is a program written to the microcontroller, it will not be executed. Instead, the program was written to external ROM and executed. By applying logic one to the EA pin, the microcontroller will use both memories, first internal then external if present. |
Pin 32-39 PORT 0 | Similar to P2, if external memory is not used, these pins can be used as general inputs/outputs. Otherwise, P0 is configured as address output (A0-A7) when the ALE pin is driven high (1) or as data output (Data Bus) when the ALE pin is driven low (0). |
Pin 40 VCC | +5V power supply |
8051 microcontrollers have 4 I/O ports each of 8-bit, which can be configured as input or output. Hence, a total of 32 input/output pins allows the microcontroller to be connected with the peripheral devices.
Program Status Word (PSW)
The PSW or Program Status Word Register is called Flag Register is one of the important SFRs. The PSW Register consists of Flag Bits, which help the programmer in checking the condition of the result and make decisions.
Flags are 1-bit storage elements that store and indicate the nature of the result that is generated by the execution of certain instructions. The following image shows the contents of the PSW Register.
0 0 0 0 0 0 0 0
PSW.7 | PSW.6 | PSW.5 | PSW.4 | PSW.3 | PSW.2 | PSW.1 | PSW.0 |
CY | AC | F0 | RS1 | RS0 | OV | ----- | P |
Figure 3. PSW Register
The following table describes the function of each flag.
BIT | SYMBOL | FLAG NAME | DESCRIPTION | |||
7 | C or CY | Carry | Used in Arithmetic, Logic, and Boolean Operations | |||
6 | AC | Auxiliary Carry | Used in BCD operations | |||
5 | F0 | Flag0 | General Purpose User Flag | |||
4 | RS1 | Register Bank Selection Bit 1 |
| |||
3 | RS0 | Register Bank Selection Bit 0 |
| |||
|
| RS1 RS0 Bank |
| |||
|
| 0 0 | Bank 0 | |||
|
| 0 1 | Bank 1 | |||
|
| 1 0 | Bank 2 | |||
|
| 1 1 | Bank 3 | |||
2 | OV | Overflow | Used in Arithmetic Operations | |||
1 | _ | Reserved | May be used as General Purpose Flag | |||
0 | P | Parity | Set to 1 If A has an odd number of 1’s; Otherwise, Reset. | |||
Key takeaways:
8051 microcontrollers is a 40 pin Dual Inline Package (DIP). These 40 pins serve different functions like read, write, I/O operations interrupts, etc.
Stack:
The stack is a section of RAM used by the CPU to store information temporarily. This information could be data or an address. The CPU needs this storage area since there are only a limited number of registers.
The register used to access the stack is called the SP (stack pointer) register. The stack pointer is 8 bits wide, which takes values of 00 to FFH.
When the 8051 is powered up, the SP register contains the value 07. This means that RAM location 08 is the first location used for the stack by the 8051. The storing of a CPU register in the stack is called a PUSH, and pulling the contents off the stack back into a CPU register is called a POP.
Pushing onto the stack
In the 8051 the stack pointer (SP) points to the last used location of the stack. When we push data onto the stack, the stack pointer (SP) is incremented by one.
The SP is decremented when data is pushed onto the stack.
For example, when PUSH is executed, the contents of the register are saved on the stack and SP is incremented by 1.
For example, the instruction “PUSH 1″ pushes register Rl onto the stack.
Show that the stack and stack pointer for the following. Assume the default stack area and register 0 is selected.
MOV R6, #25H
MOV R1,#12H
MOV R4, #0F3H
PUSH 6
PUSH 1
PUSH 4
Solution:
| After PUSH 6 | After PUSH 1 | After PUSH 4 |
0B | 0B | 0B | 0B |
0A | 0A | 0A | 0A F3 |
09 | 09 | 09 12 | 09 12 |
08 | 08 25 | 08 25 | 08 25 |
Start SP=07 SP=08 SP=09 SP=0A
Popping from the stack
Popping the contents of the stack back into a given register is the opposite process of pushing. With every pop, the top byte of the stack is copied to the register specified by the instruction, and the stack pointer is decremented once.
Example:
Examine the stack shows the contents of the registers and SP after the execution of the following instructions.
POP 3; POP stack into R3
POP 5; POP stack into R5
POP 2; POP stack into R2
Solution:
| After PUSH 6 | After PUSH 1 | After PUSH 4 |
0B 54 | 0B | 0B | 0B |
0A F9 | 0A F9 | 0A | 0A |
09 76 | 09 76 | 09 76 | 09 |
08 6C | 08 6C | 08 6C | 08 6C |
Start SP=0B SP=0A SP=09 SP=08
Stack Pointer (SP)
SP or Stack Pointer points to the top of the Stack and it indicates the next data to be accessed. Stack Pointer can be accessed using PUSH, POP, CALL, and RET instructions. The Stack Pointer is an 8-bit register and upon reset, the Stack Pointer is initialized with 07H.
When writing a new data byte into the stack, the SP (Stack Pointer) is automatically incremented by 1 and the new data is written at an address SP+1. When reading data from the stack, the data is retrieved from the Address in SP and after that, the SP is decremented by 1 (SP-1).
There are 21 Special function registers (SFR) in the 8051 microcontrollers, and this includes Register A, Register B, Processor Status Word (PSW), PCON, etc.
There are 21 unique locations for these 21 special function registers and each of these registers is of 1-byte size.
Some of these special function registers are bit addressable which means you can access 8 individual bits inside a single byte, while some others are only byte-addressable.
Register A/Accumulator
The Accumulator referred to as Register A holds the result of most arithmetic and logic operations. ACC is usually accessed by direct addressing and its physical address is E0H. The accumulator is both bytes and bit addressable.
Figure 4. Register A Accumulator
Register B
The major purpose of this register is in executing multiplication and division. The 8051 microcontroller has a single instruction for multiplication (MUL) and division (DIV).
MUL A, B – When this instruction is executed, data inside A and data inside B is multiplied and the answer is stored in A.
Figure 5. Accumulator
SFR | Function |
DPH | Data pointer register (High) |
DPL | Data pointer register (Low) |
IP | Interrupt Priority |
IE | Interrupt Enable |
SBUF | Serial Input /Output Buffer |
SCON | Serial communication control |
TCON | Timer Control |
TH0 | Timer 0 counter High |
TL0 | Timer 0 counter Low |
TH1 | Timer 1 counter High |
TL1 | Timer 1 counter Low |
TMOD | Timer mode select |
In this group, the instructions perform data transfer operations of the following types.
a. Move the contents of a register Rn to A
i. MOV A, R2
ii. MOV A, R7
b. Move the contents of register A to Rn
i. MOV R4, A
ii. MOV R1, A
c. Move an immediate 8-bit data to register A or to Rn or a memory location(direct or indirect)
i. MOV A, #45H
ii. MOV R6, #51H i
iii.MOV 30H, #44H
iv. MOV @R0, #0E8H
v. MOV DPTR, #0F5A2H
vi. MOV DPTR, #5467H
d. Move the contents of a memory location to A or A to a memory location using direct and indirect addressing
i. MOV A, 65H
ii. MOV A, @R0
iii.MOV 45H, A
iv. MOV @R1, A
e. Move the contents of a memory location to Rn or Rn to a memory location using direct addressing
i. MOV R3, 65H
ii. MOV 45H, R2
f. Move the contents of the memory location to another memory location using direct and indirect addressing
i. MOV 47H, 65H
ii. MOV 45H, @R0
g. Move the contents of external memory to A or A to an external memory
i. MOVX A,@R1
ii. MOVX @R0, A
iii. MOVX A,@DPTR iv. MOVX@DPTR, A
h. Move the contents of program memory to A
i. MOVC A, @A+PC
ii. MOVC A, @A+DPTR
Program
Write 8085 Assembly language program to swap two 8-bit numbers stored at location 8000H and 8001H using direct addressing mode.
MOV B, M; Load the first number to B
INX H; Point to the next number
MOV A, M; Load the second number into A
MOV M, B; Store first number to the second position
DCX H; Point to the previous location
MOV M, A; Store second number to the first position.
HLT.
Write a program to transfer the block of data from 20h to 30h to an external location 1020h to 1030h.
Mov r7, #0Ah ; initialize counter by 10d
Mov r0, #20h ; get initial source location
Mov dptr, #1020h ; get initial destination location
Nxt: Mov a, @r0 ; get first content in acc
Movx @dptr, a ; move it to external location
Inc r0 ; increment source location
Inc dptr ; increase destination location
Djnz r7, nxt ; decrease r7. if zero then over otherwise move next
Key Takeaways:
Data transfer instructions are responsible for transferring data between various memory storing elements like registers, RAM, and ROM.
References:
1. The 8051 Microcontroller and Embedded Systems using Assembly and C by Muhammad Ali Mazidi.
2. The 8051 Microcontroller by I. Scott Mackenzie, Raphael C.W Phan
8051 Microcontrollers: Internals, Instructions, Programming, and Interfacing Book by Subrata Ghoshal
5. Advanced PIC Microcontroller Projects in C Book by Dogan Ibrahim