Unit - 1
Introduction to concept of microcontroller
Q1) Explain the concept of microcontroller?
A1) A microcontroller is a computer present in a single integrated circuit which 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.
Q2) Explain the functional diagram of 8051?
A2)
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.
Accumulator is an 8 bit register widely used for all arithmetic and logical operations. 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 is also called MATH registers.
PSW (Program Status Word). This is an 8 bit register which 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. 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 which is used to furnish address information for internal and external program memory and for external data memory.
Program Counter (PC) – 16 -bit PC contains the address of next instruction to be executed. On reset PC will set to 0000. After fetching every instruction PC will increment by one.
Q3) Explain the pin diagram of 8051?
A3)
Pin out 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 which 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 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 | Prior to reading from external memory, the microcontroller puts the lower address byte (A0-A7) on P0 and then activates the ALE output. After receiving signal from 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 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 |
Q4) Explain PSW?
A4) Program Status Word (PSW)
The PSW or Program Status Word Register is called as 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 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 |
Q5) Explain Flag register?
A5) 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 odd number of 1’s ; Otherwise Reset. | |||
Q6) Explain stack?
A6) 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 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.
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.
Q7) 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
A7)
| 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
Q8) Explain Stack Pointer Register?
A8) 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 stack, the data is retrieved from the Address in SP and after that the SP is decremented by 1 (SP-1).
Q9) Explain Special Function Register?
A9) 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.
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 |
Q10) Explain Data transfer instructions?
A10) In this group, the instructions perform data transfer operations of the following types.
a. Move the contents of a register Rn to A
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