Cs3691 Embedded
Cs3691 Embedded
UNIT-1
The following table highlights the differences between a microprocessor and a microcontroller −
Microcontroller Microprocessor
Microcontrollers are used to execute a single task within Microprocessors are used for big
an application. applications.
It is built with CMOS technology, which requires less Its power consumption is high because
power to operate. it has to control the entire system.
Types of Microcontrollers
Microcontrollers are divided into various categories based on memory, architecture, bits and
instruction sets. Following is the list of their types −
Bit
Based on bit configuration, the microcontroller is further divided into three categories.
Based on the memory configuration, the microcontroller is further divided into two categories.
Based on the instruction set configuration, the microcontroller is further divided into two
categories.
CISC − CISC stands for complex instruction set computer. It allows the user to insert a
single instruction as an alternative to many simple instructions.
RISC − RISC stands for Reduced Instruction Set Computers. It reduces the operational
time by shortening the clock cycle per instruction.
Applications of Microcontrollers
CHAPTER-1:
Microcontrollers - 8051 Architecture
In the following diagram, the system bus connects all the support devices to the CPU. The
system bus consists of an 8-bit data bus, a 16-bit address bus and bus control signals. All other
devices like program memory, ports, data memory, serial interface, interrupt control, timers, and
the CPU are all interfaced together through the system bus.
The CPU is the brain of any processing machine. It is the part that is responsible for managing
all the tasks of the microcontroller. The CPU is an independent unit. Users can not interfere with
the CPU controlling how it should function. It identifies the tasks present in the ROM and then
processes them. In the 8051 microcontroller architecture, the CPU is responsible for managing
registers. Registers are a type of memory in a computer. They can store and manipulate data.
Interrupts
In the 8051 microcontroller architecture, interrupts stop the microcontroller’s current task.
Interrupts are caused when some other program has a higher priority request for execution. When
an interrupt occurs, the ongoing task stops, the sub-routine for the interrupt is executed, and then
the previous job resumes.
The following are the types of interrupts in the 8051 microcontroller architecture:
INT0 It is an external interrupt having code 0. External hardware cause this interrupt.
INT1 It is an external interrupt having code 1. External hardware cause this interrupt.
TF0 This is for timer 0 overflow interrupt.
Memory
The next part of the 8051 microcontroller architecture is the memory. For any data manipulation
to occur, we require a set of instructions. These programs need to be saved in memory. This
memory where the program resides in the controller's memory is called code memory or program
memory. It acts as ROM memory for the 8051 microcontroller architecture. In the 8051
microcontroller architecture, the microcontroller has 4KB ROM and 128 bytes of RAM.
Bus
A bus is a group of wires. Communication within the microcontroller happens through this bus.
There are either 8 or 16 or more wires in the bus. If the 8051 microcontroller architecture has 8
wires, it can carry 8 bits of data. If the 8051 microcontroller architecture has 16 wires, it can
carry 16 bits of data.
Types of Buses
Address Bus-
The address bus in the 8051 microcontroller architecture is 16 bits. This bus transfers data from
the CPU to the memory.
Data Bus-
In 8051 microcontroller architecture, the data bus is 8 bits. It helps in carrying the data from one
place to another.
Oscillators
Input/Output Ports
Timers/Counters
In the 8051 microcontroller architecture, we have two timers. They are each 16 bits. We have the
timers to generate gaps between two events. The two timers generate two delays(gaps), and the
suitable one is chosen.
Data Types
In 8051, there is only one data type of 8 bits, from the MSB (most significant bit) D7 to the LSB
(least significant bit) D0. With an 8-bit data type, any data type larger than 8-bit must be broken
into 8-bit chunks by the programmer before it is processed.
Stack
The stack is a section of RAM used by the CPU to store information such as data or memory
address on a temporary basis. The CPU needs this storage area considering the limited number of
registers. There are registers inside the CPU to point to the stack. A Stack Pointer register is
used to access the stack. This stack pointer is 8 bits wide. The operation of storing the data in a
stack is known as PUSH and getting it back into a CPU register is known as POP.
Registers
Registers are memory storage devices used in the CPU to temporarily store information. This
information could be data to be processed or, an address pointing to the data to be fetched. The
most widely used registers of the 8051 are
A (accumulator)
B register
R0-R7
PC (Program Counter)
Addressing modes
Addressing modes in the 8051 microcontrollers are a way of specifying the location of an
operand in memory. The addressing mode affects the number of bytes that the instruction takes
up in the memory and the speed at which it is executed. We choose different addressing modes
according to specific applications.
The 8051 microcontrollers are used in light sensing and controlling devices.
The microcontroller is used in fire-sensing devices.
It is also present in automobile and defence applications.
It is also used in a voltmeter.
8051 Pin Diagram
Pins 1 to 8 − These pins are known as Port 1. This port doesn’t serve any other
functions. It is internally pulled up, bi-directional I/O port.
Pin 9 − It is a RESET pin, which is used to reset the microcontroller to its initial values.
Pins 10 to 17 − These pins are known as Port 3. This port serves some functions like
interrupts, timer input, control signals, serial communication signals RxD and TxD, etc.
Pins 18 & 19 − These pins are used for interfacing an external crystal to get the system
clock.
Pins 21 to 28 − These pins are known as Port 2. It serves as I/O port. Higher order
address bus signals are also multiplexed using this port.
Pin 29 − This is PSEN pin which stands for Program Store Enable. It is used to read a
signal from the external program memory.
Pin 30 − This is EA pin which stands for External Access input. It is used to
enable/disable the external memory interfacing.
Pin 31 − This is ALE pin which stands for Address Latch Enable. It is used to
demultiplex the address-data signal of port.
Pins 32 to 39 − These pins are known as Port 0. It serves as I/O port. Lower order
address and data bus signals are multiplexed using this port.
CHAPTER-2:
Instruction Set and Programming
INSTRUCTION SET OF 8051
The process of writing program for the microcontroller mainly consists of giving
instructions (commands) in the specific order in which they should be executed in order to
carry out a specific task. As electronics cannot “understand” what for example an
instruction “if the push button is pressed- turn the light on” means, then a certain number
of simpler and precisely defined orders that decoder can recognise must be used. All
commands are known as INSTRUCTION SET. All microcontrollers compatibile with the
8051 have in total of 255 instructions, i.e. 255 different words available for program
writing.
At first sight, it is imposing number of odd signs that must be known by heart.
However, It is not so complicated as it looks like. Many instructions are considered to
be “different”, even though they perform the same operation, so there are only 111
truly different commands. For example: ADD A,R0, ADD A,R1, ... ADD A,R7 are
instructions that perform the same operation (additon of the accumulator and register).
Since there are 8 such registers, each instruction is counted separately. Taking into
account that all instructions perform only 53 operations (addition, subtraction, copy
etc.) and most of them are rarely used in practice, there are actually 20 -30
abbreviations to be learned, which is acceptable.
3.1 Types of instructions
Depending on operation they perform, all instructions are divided in several groups:
1.Arithmetic Instructions
2.Branch Instructions
3.Data Transfer Instructions
4.Logic Instructions
5.Bit-oriented Instructions
The first part of each instruction, called MNEMONIC refers to the operation an
instruction performs (copy, addition, logic operation etc.). Mnemonics are
abbreviations of the name of operation being executed.
For example:
INC R1 - Means: Increment register R1 (increment register R1);
LJMP LAB5 - Means: Long Jump LAB5 (long jump to the address marked as LAB5);
JNZ LOOP - Means: Jump if Not Zero LOOP (if the number in the accumulator is
not 0, jump to the address marked as LOOP);
For example:
RET - return from a subroutine;
JZ TEMP - if the number in the accumulator is not 0, jump to the address marked as
TEMP;
ADD A,R3 - add R3 and accumulator;
CJNE A,#20,LOOP - compare accumulator with 20. If they are not equal, jump to the
address marked as LOOP;
1.Arithmetic instructions
Arithmetic instructions perform several basic operations such as addition,
subtraction, division, multiplication etc. After execution, the result is stored in the
first operand. For example:
ADD A,R1 - The result of addition (A+R1) will be stored in the accumulator.
2. Branch Instructions
There are two kinds of branch instructions:
Unconditional jump instructions: upon their execution a jump to a new location from
where the program continues execution is executed.
Conditional jump instructions: a jump to a new program location is executed only if
a specified condition is met. Otherwise, the program normally proceeds wit h the next
instruction
Data transfer instructions move the content of one register to another. The register
the content of which is moved remains unchanged. If they have the suffix “X”
(MOVX), the data is exchanged with external memory.
4. Logic Instructions
Logic instructions perform logic operations upon corresponding bits of two registers.
After execution, the result is stored in the first operand.
5. Bit-oriented Instructions
Similar to logic instructions, bit-oriented instructions perform logic operations. The
difference is that these are performed upon single bits.
ARITHMETIC INSTRUCTIONS
ADD A,Rn
Adds the register to the accumulator
ADD A,direct
Adds the direct byte to the accumulator
ADD A,@Ri
Adds the indirect RAM to the accumulator
ADD A,#data
Adds the immediate data to the accumulator
ADDC A,Rn
Adds the register to the accumulator with a carry flag
ADDC A,direct
Adds the direct byte to the accumulator with a carry flag
ADDC A,@Ri
Adds the indirect RAM to the accumulator with a carry flag
ADDC A,#data
Adds the immediate data to the accumulator with a carry flag
SUBB A,Rn
Subtracts the register from the accumulator with a borrow
SUBB A,direct
Subtracts the direct byte from the accumulator with a borrow
SUBB A,@Ri
Subtracts the indirect RAM from the accumulator with a borrow
SUBB A,#data
Subtracts the immediate data from the accumulator with a borrow
INC A
Increments the accumulator by 1
INC Rn
Increments the register by 1
INC Rx
Increments the direct byte by 1
INC @Ri
Increments the indirect RAM by 1
DEC A
Decrements the accumulator by 1
DEC Rn
Decrements the register by 1
DEC Rx
Decrements the direct byte by 1
DEC @Ri
Decrements the indirect RAM by 1
INC DPTR
Increments the Data Pointer by 1
MUL AB
Multiplies A and B
DIV AB
Divides A by B
DA A
Decimal adjustment of the accumulator according to BCD code
BRANCH INSTRUCTIONS
ACALL addr11
Absolute subroutine call
LCALL addr16
Long subroutine call
RET
Returns from subroutine
RETI
Returns from interrupt subroutine
AJMP addr11
Absolute jump
LJMP addr16
Long jump
SJMP rel
Short jump (from –128 to +127 locations relative to the following instruction)
JC rel
Jump if carry flag is set. Short jump.
JNC rel
Jump if carry flag is not set. Short jump.
JB bit,rel
Jump if direct bit is set. Short jump.
JBC bit,rel
Jump if direct bit is set and clears bit. Short jump.
JMP @A+DPTR
Jump indirect relative to the DPTR
JZ rel
Jump if the accumulator is zero. Short jump.
JNZ rel
Jump if the accumulator is not zero. Short jump.
CJNE A,direct,rel
Compares direct byte to the accumulator and jumps if not equal. Short jump.
CJNE A,#data,rel
Compares immediate data to the accumulator and jumps if not equal. Short jump.
CJNE Rn,#data,rel
Compares immediate data to the register and jumps if not equal. Short jump.
CJNE @Ri,#data,rel
Compares immediate data to indirect register and jumps if not equal. Short jump.
DJNZ Rn,rel
Decrements register and jumps if not 0. Short jump.
DJNZ Rx,rel
Decrements direct byte and jump if not 0. Short jump.
NOP
No operation
MOV A,Rn
Moves the register to the accumulator
MOV A,direct
Moves the direct byte to the accumulator
MOV A,@Ri
Moves the indirect RAM to the accumulator
MOV A,#data
Moves the immediate data to the accumulator
MOV Rn,A
Moves the accumulator to the register
MOV Rn,direct
Moves the direct byte to the register
MOV Rn,#data
Moves the immediate data to the register
MOV direct,A
Moves the accumulator to the direct byte
MOV direct,Rn
Moves the register to the direct byte
MOV direct,direct
Moves the direct byte to the direct byte
MOV direct,@Ri
Moves the indirect RAM to the direct byte
MOV direct,#data
Moves the immediate data to the direct byte
MOV @Ri,A
Moves the accumulator to the indirect RAM
MOV @Ri,direct
Moves the direct byte to the indirect RAM
MOV @Ri,#data
Moves the immediate data to the indirect RAM
MOV DPTR,#data
Moves a 16-bit data to the data pointer
MOVC A,@A+DPTR
Moves the code byte relative to the DPTR to the accumulator (address=A+DPTR)
MOVC A,@A+PC
Moves the code byte relative to the PC to the accumulator (address=A+PC)
MOVX A,@Ri
Moves the external RAM (8-bit address) to the accumulator
MOVX A,@DPTR
Moves the external RAM (16-bit address) to the accumulator
MOVX @Ri,A
Moves the accumulator to the external RAM (8-bit address
MOVX @DPTR,A
Moves the accumulator to the external RAM (16-bit address)
PUSH direct
Pushes the direct byte onto the stack
POP direct
Pops the direct byte from the stack
XCH A,Rn
Exchanges the register with the accumulator
XCH A,direct
Exchanges the direct byte with the accumulator
XCH A,@Ri
Exchanges the indirect RAM with the accumulator
XCHD A,@Ri
Exchanges the low-order nibble indirect RAM with the accumulator
RETI
Returns from interrupt subroutine
LOGIC INSTRUCTIONS
ANL A,Rn
AND register to accumulator
ANL A,direct
AND direct byte to accumulator
ANL A,@Ri
AND indirect RAM to accumulator
ANL A,#data
AND immediate data to accumulator
ANL direct,A
AND accumulator to direct byte
ANL direct,#data
AND immediate data to direct register
ORL A,Rn
OR register to accumulator
ORL A,direct
OR direct byte to accumulator
ORL A,@Ri
OR indirect RAM to accumulator
ORL direct,A
OR accumulator to direct byte
ORL direct,#data
OR immediate data to direct byte
XRL A,Rn
Exclusive OR register to accumulator
XRL A,direct
Exclusive OR direct byte to accumulator
XRL A,@Ri
Exclusive OR indirect RAM to accumulator
XRL A,#data
Exclusive OR immediate data to accumulator
XRL direct,A
Exclusive OR accumulator to direct byte
XORL direct,#data
Exclusive OR immediate data to direct byte
CLR A
Clears the accumulator
CPL A
Complements the accumulator (1=0, 0=1)
SWAP A
Swaps nibbles within the accumulator
RL A
Rotates bits in the accumulator left
RLC A
Rotates bits in the accumulator left through carry
RR A
Rotates bits in the accumulator right
RRC A
Rotates bits in the accumulator right through carry
CJNE @Ri,#data,rel
Compares immediate data to indirect register and jumps if not equa l. Short jump.
BIT-ORIENTED INSTRUCTIONS
CLR C
Clears the carry flag
CLR bit
Clears the direct bit
SETB C
Sets the carry flag
SETB bit
Sets the direct bit
CPL C
Complements the carry flag
CPL bit
Complements the direct bit
ANL C,bit
AND direct bit to the carry flag
ANL C,/bit
AND complements of direct bit to the carry flag
ORL C,bit
OR direct bit to the carry flag
ORL C,/bit
OR complements of direct bit to the carry flag
MOV C,bit
Moves the direct bit to the carry flag
MOV bit,C
Moves the carry flag to the direct bit
A - accumulator;
Rn - is one of working registers (R0-R7) in the currently active RAM memory
bank;
Direct - is any 8-bit address register of RAM. It can be any general-purpose
register or a SFR (I/O port, control register etc.);
@Ri - is indirect internal or external RAM location addressed by register R0 or
R1;
#data - is an 8-bit constant included in instruction (0-255);
#data16 - is a 16-bit constant included as bytes 2 and 3 in instruction (0-65535);
addr16 - is a 16-bit address. May be anywhere within 64KB of program memory;
addr11 - is an 11-bit address. May be within the same 2KB page of program
memory as the first byte of the following instruction;
rel - is the address of a close memory location (from -128 to +127 relative to the
first byte of the following instruction). On the basis of it, assembler computes the
value to add or subtract from the number currently stored in the progr am counter;
bit - is any bit-addressable I/O pin, control or status bit; and
C - is carry flag of the status register (register PSW).
8051 Microcontroller
The 8051 microcontroller is a tiny computer developed by Intel in the 1980s. It is used in many
electronic systems like cars, medical devices, etc. It has two memory spaces, one for program
memory and the other for data memory. The program counter can access up to 64K of program
memory, while the data memory has 128 bytes of internal RAM and up to 64K of external RAM.
The microcontroller has four functional blocks: the CPU, memory, input/output ports, and serial
communication interface. It comes with many built-in tools that can be used to interface with
other devices, making it an excellent choice for many embedded system applications.
8051 Microcontroller Architecture
Interrupt control: It supports five interrupt sources, which can interrupt the normal program
execution to handle specific events.
Central Processing Unit (CPU): This is the brain of the microcontroller, which is responsible
for executing instructions and performing arithmetic and logical operations. The 8051 CPU
consists of an ALU, accumulator, registers, and a program counter.
Bus control: The 8051 microcontroller includes a bus controller that manages data transfer
between the CPU and peripheral devices, such as memory or input/output devices.
4k byte ROM: The 8051 microcontroller architecture includes a 4 kilobyte (4k) read-only
memory (ROM) for storing the program instructions that are executed by the CPU.
128-byte RAM: The 8051 microcontroller also has a 128-byte random-access memory (RAM)
for storing data that is used by the program instructions during runtime.
Input/Output Ports: They have four 8-bit input/output (I/O) ports that can be configured as
either input or output.
Timers and Counters: They have two 16-bit timers/counters that can be used for a variety of
tasks such as measuring time intervals, generating PWM signals, and counting external events.
Serial Communication Interface(SCI): It has a built-in serial port that can be used for
asynchronous serial communication.
8051 Pin Diagram
The 8051 microcontroller has a total of 40 pins arranged in four groups, namely Port 0, Port 1,
Port 2, and Port 3. Here’s a brief explanation of each group:
Port 0: Pins 32-39 are designated as Port 0 and are used for both input and output operations.
Port 0 can also be used for external memory interfacing.
Port 1: Pins 1-8 are designated as Port 1 and are used for both input and output operations. Port
1 also has built-in pull-up resistors that can be activated by writing a 1 to the corresponding bit.
Port 2: Pins 21-28 are designated as Port 2 and are used for both input and output operations.
Port 2 can also be used for external memory interfacing, and some of its pins have alternate
functions, such as being used as a serial communication interface.
Port 3: Pins 10-17 are designated as Port 3 and are used for both input and output operations.
Port 3 can also be used for external memory interfacing, and some of its pins have alternate
functions, such as being used as a timer/counter input.
Vcc: This pin is used to supply power to the microcontroller and is typically connected to a +5V
power source.
GND: This pin is used as the ground reference for the microcontroller and is typically connected
to the ground.
XTAL1 and XTAL2: These pins are used to connect an external crystal oscillator that provides
the clock signal for the microcontroller.
Reset: This pin is used to reset the microcontroller and is typically connected to a push-button
switch.
ALE: This pin is used as the Address Latch Enable signal and is used to latch the address from
the program counter onto the external memory bus.
PSEN: This pin is used as the Program Store Enable signal and is used to select the external
ROM for program execution.
EA: This pin is used to enable/disable external memory interfacing. If this pin is connected to
Vcc, the microcontroller will fetch the program from the external memory. if it is connected to
GND, the microcontroller will fetch the program from the internal ROM.
Application for 8051 Microcontroller Architecture
It is commonly used in embedded systems, such as in home appliances, security systems, and
industrial automation systems.
This is used in automotive applications, such as in engine control units, airbag control units, and
anti-lock braking systems.
It is used in medical devices, such as heart rate monitors, blood pressure monitors, and insulin
pumps.
They are used in various consumer electronics products, such as in remote controls, digital
cameras, and smart home devices.
These are used in security systems, such as access control systems and alarm systems.
Advantages for 8051 Microcontroller Architecture
It is easy to program, has a simple instruction set, and has easy-to-use development tools.
The development tools are not as advanced, and it is difficult to develop and debug code.
CHAPTER-3
Programming Parallel Ports – Timers and Serial Port
1. Serial Port: A serial port is an interface that is used for connecting the serial lines to
attain the serial communication. These ports can dock a 9-pin D-shaped connector that
connects to the transmission line, is called DB-9 connectors. Serial communication gained
by single wire and only the single stream of data is transferred from one end to other.
Therefore, like parallel transmission, in serial transmission data speed mismatch is not a
problem. The length of the wire can be increased as needed. The transmission speed of
serial port is comparatively low as compared to parallel port transmission speed. Serial
ports are typically implemented in the modems, connecting devices, controllers, mouse and
also in security cameras. DB-9 pin connector diagram is given below.
2. Parallel Port: Unlike serial port, a parallel port can move a set of 8 bits at a time on
eight different wires. That’s why it is faster in comparison to the serial communication.
Unlike serial port, It uses a 25 pin connector that is called DB-25 connector. To eliminate
crosstalk and errors, all bitstreams need to transfer data at the same speed in parallel
communication. But, that is impractical. Therefore, for this reason, transmission lines are
preferred to be short in parallel communication. Parallel ports are typically implemented in
zip-drives, printers, hard drives, CD-ROM drives, etc. DB-25 pin connector diagram is
given below.
Serial port is used to achieve serial While parallel port is used to achieve
1.
transmission. parallel transmission.
Serial port send a bit after another byte at While parallel ports send multiple bits
5.
a time. at once.
Serial ports are typically implemented in Parallel ports are typically implemented
7. modems, connecting devices, security in zip-drives, printers, hard drives, CD-
cameras and controllers. ROM drives, etc.
CHAPTER-4
Microcontrollers - 8051 Interrupts
Interrupts are the events that temporarily suspend the main program, pass the control to the
external sources and execute their task. It then passes the control to the main program where it
had left off.
8051 has 5 interrupt signals, i.e. INT0, TFO, INT1, TF1, RI/TI. Each interrupt can be enabled or
disabled by setting bits of the IE register and the whole interrupt system can be disabled by
clearing the EA bit of the same register.
This register is responsible for enabling and disabling the interrupt. EA register is set to one for
enabling interrupts and set to 0 for disabling the interrupts. Its bit sequence and their meanings
are shown in the following figure.
We can change the priority levels of the interrupts by changing the corresponding bit in the
Interrupt Priority (IP) register as shown in the following figure.
A low priority interrupt can only be interrupted by the high priority interrupt, but not
interrupted by another low priority interrupt.
If two interrupts of different priority levels are received simultaneously, the request of
higher priority level is served.
If the requests of the same priority levels are received simultaneously, then the internal
polling sequence determines which request is to be serviced.
TCON Register
Let's see the timer interrupt programming using Timer0 model for blinking
LED using interrupt method:
o Enables the equivalent bit of external interrupt in Interrupt Enable (IE) register.
o If it is level triggering, then write subroutine appropriate to this interrupt, or else
enable the bit in TCON register corresponding to the edge triggered interrupt.
1. void main()
2. {
3. IT0 = 1; // Configure interrupt 0 for falling edge on INT0
4. EXO = 1; // Enabling the EX0 interrupt
5. EA =1; // Enabling the global interrupt flag
6. }
7. void ISR_ex0(void) interrupt 0
8. {
9. <body of interrupt>
10. }
2. Serial Communication Interrupt Programming It is used when there is a need to send
or receive data. Since one interrupt bit is used for both Transfer Interrupt (TI) and
Receiver Interrupt (RI) flags, Interrupt Service Routine (ISR) must examine these flags for
knowing the actual interrupt. By the logical OR operation of RI and TI flags causes the
interrupt and it is clear by the software alone. Consider the steps involved in serial
communication interrupt programming are:-
o Configure the Interrupt Enable register for enabling serial interrupt.
o Configure the SCON register for performing transferring and receiving operation.
o Write a subroutine for given interrupt with appropriate function.
Let's see the program for sending 'E' through serial port with 9600 baud
rate using Serial Interrupt:
1. void main()
2. {
3. TMOD = 0x20:
4. TH1= 0xFD; // baud rate for 9600 bps
5. SCON = 0x50;
6. TR1=1;
7. EA=l;
8. whlle(l);
9. }
10. void ISR_Serial(void) interrupt 4
11. {
12. if(TI==l)
13. {
14. SBUF= ?E?;
15. TI=0;
16. }
17. else
18. RI =0;
19. }