Microcontroller 1 J2 and 3 Module Notes
Microcontroller 1 J2 and 3 Module Notes
32 bi directional I/O lines (can be used as four 8 bit ports or 32 individually addressable I/O
lines)
8051 CPU consists of 8 bit ALU with associated registers like accumulator ‘A’ , B register,
PSW, SP, 16 bit program counter, stack pointer.
The 8051 Microcontroller is an 8 – bit Microcontroller i.e. it can read, write and process 8 –
bit Data. There are a bunch of manufacturers like Atmel, NXP, TI, who manufacture their own
versions of 8051 Microcontroller.
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.
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
OV - overflow
P - parity
Stack Pointer (SP) – it 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.
Pins 1-8 --- PORT 1. Each of these pins can be configured as an input or an output
Pin 9 --RESET. A logic one on this pin disables the microcontroller and clears the contents of
most registers. In other words, the positive voltage on this pin resets the microcontroller. By
applying logic zero to this pin, the program starts execution from the beginning.
Pins10-17--PORT 3. Similar to port 1, each of these pins can serve as general input or output.
Pin 18, 19-XTAL2, XTAL1. Internal oscillator input and output. A quartz crystal which specifies
operating frequency is usually connected to these pins.
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 activates the ALE output. After receiving signal from the ALE
pin, the external latch latches the state of P0 and uses it as a memory chip address.
Immediately after that, the ALE pin is returned 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 with no regard to whether there is internal memory or not. It means that even
there is a program written to the microcontroller, it will not be executed. Instead, the program
written to external ROM will be executed. By applying logic one to the EA pin, the
microcontroller will use both memories, first internal then external (if exists).
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).
TIMER MODES
Timers can operate in four different modes. They are as follows
Timer Mode-0: In this mode, the timer is used as a 13-bit UP counter as follows.
The lower 5 bits of TLX and 8 bits of THX are used for the 13 bit count.Upper 3 bits of TLX are
ignored. When the counter rolls over from all 0's to all 1's, TFX flag is set and an interrupt is
generated. The input pulse is obtained from the previous stage. If TR1/0 bit is 1 and Gate bit
is 0, the counter continues counting up. If TR1/0 bit is 1 and Gate bit is 1, then the operation
of the counter is controlled by input. This mode is useful to measure the width of a given pulse
fed to input.
Timer Mode-1: This mode is similar to mode-0 except for the fact that the Timer operates in
16-bit mode
Timer Mode-3: Timer 1 in mode-3 simply holds its count. The effect is same as setting TR1=0.
Timer0 in mode-3 establishes TL0 and TH0 as two separate counters.
Control bits TR1 and TF1 are used by Timer-0 (higher 8 bits) (TH0) in Mode-3 while TR0 and
TF0 are available to Timer-0 lower 8 bits(TL0).
1. SBUF Register: Serial Buffer (SBUF) register is an 8-bit register. It has separate SBUF
registers for data transmission and for data reception. For a byte of data to be transferred via
the TXD line, it must be placed in SBUF register. Similarly, SBUF holds the 8-bit data received
by the RXD pin and read to accept the received data.
2. SCON register: The contents of the Serial Control (SCON) register are shown below. This
register contains mode selection bits, serial port interrupt bit (TI and RI) and also the ninth
data bit for transmission and reception (TB8 and RB8).
1.2.4 To Explain interrupts in 8051 and associated registers-IP,IE.
8051 makes use of two registers to deal with interrupts.
1. IE Register
This is an 8 bit register used for enabling or disabling the interrupts. The structure of IE register
is shown below.
2. IP Register.
This is an 8 bit register used for setting the priority of the interrupts.
MODULE II Programming and Application of Microcontroller
1. Immediate addressing.
2. Register addressing.
3. Direct addressing.
4. Indirect addressing.
5. Relative addressing.
6. Absolute addressing.
7. Long addressing.
8. Indexed addressing.
1. Immediate addressing.
In this addressing mode the data is provided as a part of instruction itself. In other words data
immediately follows the instruction.
Eg. MOV A,#30H
2. Register addressing.
In this addressing mode the register will hold the data. One of the eight general registers (R0
to R7) can be used and specified as the operand.
ADD A,R6
R0 – R7 will be selected from the current selection of register bank. The default register bank
will be bank 0.
3. Direct addressing
There are two ways to access the internal memory. Using direct address and indirect address.
Using direct addressing mode we can not only address the internal memory but SFRs also. In
direct addressing, an 8 bit internal data memory address is specified as part of the instruction
and hence, it can specify the address only in the range of 00H to FFH. In this addressing mode,
data is obtained directly from the memory.
ADD A,30h
4. Indirect addressing
The indirect addressing mode uses a register to hold the actual address that will be used in
data movement. Registers R0 and R1 and DPTR are the only registers that can be used as data
pointers. Indirect addressing cannot be used to refer to SFR registers. Both R0 and R1 can hold
8 bit address and DPTR can hold 16 bit address.
ADD A,@R1
MOVX A,@DPTR
5. Indexed addressing.
In indexed addressing, either the program counter (PC), or the data pointer (DTPR)—is used
to hold the base address, and the A is used to hold the offset address. Adding the value of the
base address to the value of the offset address forms the effective address. Indexed
addressing is used with JMP or MOVC instructions. Look up tables are easily implemented
with the help of index addressing.
Eg. MOVC A, @A+DPTR // copies the contents of memory location pointed by the sum of the
accumulator A and the DPTR into accumulator A.
MOVC A, @A+PC // copies the contents of memory location pointed by the sum of the
accumulator A and the program counter into accumulator A.
6. Relative Addressing. Relative addressing is used only with conditional jump instructions.
The relative address (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. The jump destination is usually specified using a label and
the assembler calculates the jump offset accordingly. The advantage of relative addressing is
that the program code is easy to relocate and the address is relative to position in the
memory.
JC BACK
7. Absolute addressing
Absolute addressing is used only by the AJMP (Absolute Jump) and ACALL (Absolute Call)
instructions. These are 2 bytes instructions. The absolute addressing mode specifies the
lowest 11 bit of the memory address as part of the instruction. The upper 5 bit of the
destination address are the upper 5 bit of the current program counter. Hence, absolute
addressing allows branching only within the current 2 Kbyte page of the program memory.
ACALL LOOP2
1. Instruction Timings
The 8051 internal operations and external read/write operations are controlled by the
oscillator clock.
T-state, Machine cycle and Instruction cycle are terms used in instruction timings.
T-state is defined as one subdivision of the operation performed in one clock period. The
terms 'T-state' and 'clock period' are often used synonymously.
Machine cycle is defined as 12 oscillator periods. A machine cycle consists of six states and
each state lasts for two oscillator periods. An instruction takes one to four machine cycles to
execute an instruction. Instruction cycle is defined as the time required for completing the
execution of an instruction. The 8051 instruction cycle consists of one to four machine cycles.
2. 8051 Instructions
The instructions of 8051 can be broadly classified under the following headings.
2. Arithmetic instructions
3. Logical instructions
4. Branch instructions
5. Subroutine instructions
Logical AND
ANL destination, source: ANL does a bitwise "AND" operation between source and
destination, leaving the resulting value in destination. The value in source is not affected.
"AND" instruction logically AND the bits of source and destination.
Logical OR
ORL destination, source: ORL does a bitwise "OR" operation between source and destination,
leaving the resulting value in destination. The value in source is not affected. " OR " instruction
logically OR the bits of source and destination.
XRL destination, source: XRL does a bitwise "EX-OR" operation between source and
destination, leaving the resulting value in destination. The value in source is not affected. "
XRL " instruction logically EX-OR the bits of source and destination.
Logical NOT
CPL complements operand, leaving the result in operand. If operand is a single bit then the
state of the bit will be reversed. If operand is the Accumulator then all the bits in the
Accumulator will be reversed.
Rotate Instructions
RR A
This instruction is rotate right the accumulator. Its operation is illustrated below. Each bit is
shifted one location to the right, with bit 0 going to bit 7.
RL A
Rotate left the accumulator. Each bit is shifted one location to the left, with bit 7 going to bit
0
RRC A
Rotate right through the carry. Each bit is shifted one location to the right, with bit 0 going
into the carry bit in the PSW, while the carry was at goes into bit 7
RLC A
Rotate left through the carry. Each bit is shifted one location to the left, with bit 7 going into
the carry bit in the PSW, while the carry goes into bit 0.
The 8051 can perform addition, subtraction. Multiplication and division operations on 8 bit
numbers.
Addition
i. ADD A, #45H
i. ADD A, R5
ii. ADDC A, R2
iii. Add the contents of A with contents of memory with or without carry using direct and
indirect addressing
i. ADD A, 51H
ii. ADDC A, 75H
Subtraction
i. SUBB A, #45H
i. SUBB A, R5
ii. SUBB A, R2
iii. Subtract the contents of A with contents of memory with or without carry using direct and
indirect addressing
i. SUBB A, 51H
Multiplication
MUL AB. This instruction multiplies two 8 bit unsigned numbers which are stored in A and B
register. After multiplication the lower byte of the result will be stored in accumulator and
higher byte of result will be stored in B register.
;[A]=09H, [B]=42H
Division
DIV AB. This instruction divides the 8 bit unsigned number which is stored in A by the 8 bit
unsigned number which is stored in B register. After division the result will be stored in
accumulator and remainder will be stored in B register.
When two BCD numbers are added, the answer is a non-BCD number. To get the result in
BCD, we use DA A instruction after the addition. DA A works as follows.
Eg 1: MOV A,#23H
MOV R1,#55H
Eg 2: MOV A,#53H
MOV R1,#58H
INC increments the value of source by 1. If the initial value of register is FFh, incrementing the
value will cause it to reset to 0. The Carry Flag is not set when the value "rolls over" from 255
to 0.
In the case of "INC DPTR", the value two-byte unsigned integer value of DPTR is incremented.
If the initial value of DPTR is FFFFh, incrementing the value will cause it to reset to 0.
DEC decrements the value of source by 1. If the initial value of is 0, decrementing the value
will cause it to reset to FFh. The Carry Flag is not set when the value "rolls over" from 0 to FFh.
1. Relative Jump
Relative Jump
Jump that replaces the PC (program counter) content with a new address that is greater than
(the address following the jump instruction by 127 or less) or less than (the address following
the jump by 128 or less) is called a relative jump.
JC <relative address>
JZ <relative address>
a. During execution of LCALL, [PC] = [PC]+3; (if address where LCALL resides is say, 0x3254;
during execution of this instruction [PC] = 3254h + 3h = 3257h
b. [SP]=[SP]+1; (if SP contains default value 07, then SP increments and [SP]=08
c. [[SP]] = [PC7-0]; (lower byte of PC content ie., 57 will be stored in memory location 08.
e. [[SP]] = [PC15-8]; (higher byte of PC content ie., 32 will be stored in memory location 09.
f. [PC]= address (16 bit); the new address of subroutine is loaded to PC. No flags are affected.
This is absolute call instruction which unconditionally calls the subroutine located at the
indicated 11 bit address. This is a 2 byte instruction. The SCALL instruction works as follows.
a. During execution of SCALL, [PC] = [PC]+2; (if address where LCALL resides is say, 0x8549;
during execution of this instruction [PC] = 8549h + 2h = 854Bh
b. [SP]=[SP]+1; (if SP contains default value 07, then SP increments and [SP]=08
c. [[SP]] = [PC7-0]; (lower byte of PC content ie., 4B will be stored in memory location 08.
e. [[SP]] = [PC15-8]; (higher byte of PC content ie., 85 will be stored in memory location 09.
SERIAL COMMUNICATION
DATA COMMUNICATION
The 8051 microcontroller is parallel device that transfers eight bits of data simultaneously
over eight data lines to parallel I/O devices. Parallel data transfer over a long is very expensive.
Hence, a serial communication is widely used in long distance communication. In serial data
communication, 8-bit data is converted to serial bits using a parallel in serial out shift register
and then it is transmitted over a single data line. The data byte is always transmitted with
least significant bit first.
Baud rate: The rate at which the data is transmitted is called baud or transfer rate. The baud
rate is the reciprocal of the time to send one bit. In asynchronous transmission, baud rate is
not equal to number of bits per second. This is because; each byte is preceded by a start bit
and followed by parity and stop bit. For example, in synchronous transmission, if data is
transmitted with 9600 baud, it means that 9600 bits are transmitted in one second. For bit
transmission time = 1 second/ 9600 = 0.104 ms.
1. SBUF Register: Serial Buffer (SBUF) register is an 8-bit register. It has separate SBUF
registers for data transmission and for data reception. For a byte of data to be transferred via
the TXD line, it must be placed in SBUF register. Similarly, SBUF holds the 8-bit data received
by the RXD pin and read to accept the received data.
2. SCON register: The contents of the Serial Control (SCON) register are shown below. This
register contains mode selection bits, serial port interrupt bit (TI and RI) and also the ninth
data bit for transmission and reception (TB8 and RB8).
SERIAL COMMUNICATION MODES
1.Mode 0
In this mode serial port runs in synchronous mode. The data is transmitted and received
through RXD pin and TXD is used for clock output. In this mode the baud rate is 1/12 of clock
frequency.
2. Mode 1
In this mode SBUF becomes a 10 bit full duplex transceiver. The ten bits are 1 start bit, 8 data
bit and 1 stop bit. The interrupt flag TI/RI will be set once transmission or reception is over.
In this mode the baud rate is variable and is determined by the timer 1 overflow rate.
3. Mode 2
This is similar to mode 1 except 11 bits are transmitted or received. The 11 bits are, 1 start
bit, 8 data bit, a programmable 9th data bit, 1 stop bit.
Baud rate = [2smod/64] x Oscillator Clock Freq
4. Mode 3
Features
• Three programming modes for Peripheral Ports: Mode 0 (Basic Input/Output), Mode 1
(Strobed Input/Output), and Mode 2 (Bidirectional)
• 8-bit bidirectional system data bus with standard microprocessor interface controls
ARCHITECTURE OF 8255A
Read/Write Control Logic has six connections.
Read, Write: This control signal enables the Read/Write operation. When the signal is
low, the controller reads/writes data from/to a selected I/O Port of the 8255.
RESET: This is an active high signal; it clears the control register and sets all ports in
the input mode.
CS, A0 and A1: Theses are device select signals. Chip Select is connected to a decoded
address, and A0 and A1 are generally connected to MPU address lines A0 and A1
respectively
Control register is an 8 bit register. The contents of this register called control word.
This register can be accessed to write a control word when A0 and A1 are at logic 1.
This control register is not accessible for a read operation.
Bit D7 of the control register specifies either I/O function or the Bit Set/Reset function.
If bit D7=1, bits D6-D0 determines I/O functions in various modes. If bit D7=0, Port C
operates in the Bit Set/Reset (BSR) mode. The BSR control word does not affect the
functions of Port A and Port B.
Electromechanical relays are frequently used in industrial and control applications. The
relay coil operating at 5V has a coil resistance of 50 ohm and operates at a current of
normally 100Ma.
There are two types of relays normally open(NO) and normally closed type (NC)
When energized the NO type relay is closed while the NC type of relay becomes open to
connect or disconnect the device. For example to start or stop a 230-250V AC motor using
a micro controllers , a relay is used. This simple connection of aa relay with 8051 can be
used. In this a relay is connected at pin 1.0 of 8051
1. It is 8-bit Microcontroller
2. It has 16-bit Instruction sets
5. 32 KB Flash Memory
MegaAVR: Popular, memory up to 256 kb, several quantities of inbuilt peripherals, complex
application.
Key Features
• High Performance
• Low Power
• Efficient
• Low Expensive
The device is manufactured using Atmel’s high density non-volatile memory technology. The
On-chip ISP Flash allows the Program memory to be re-programmed In-System through an
SPI serial interface, by a conventional non-volatile memory programmer or by an On-chip
boot code running on the AVR core.
The ATtiny25/45/85 AVR is supported with a full suite of program and system development
tools including: C Compilers, Macro Assemblers, Program Debugger/Simulators and
Evaluation kits.
About I/O pins: ATmega32 is capable of handling analogue inputs. Port A can be used as either
DIGITAL I/O Lines or each individual pin can be used as a single input channel to the internal
ADC of ATmega32, plus a pair of pins AREF, AVCC & GND together can make an ADC channel.
No pins can perform and serve for two purposes (for an example: Port A pins cannot work as
a Digital I/O pin while the Internal ADC is activated) at the same time.
Digital I/O pins: ATmega32 has 32 pins (4portsx8pins) configurable as Digital I/O pins.
Timers: 3 Inbuilt timer/counters, two 8 bit (timer0, timer2) and one 16 bit (timer1).
ADC: It has one successive approximation type ADC in which total 8 single channels are
selectable. They can also be used as 7 (for TQFP packages) or 2 (for DIP packages) differential
channels. Reference is selectable, either an external reference can be used or the internal
2.56V reference can be brought into action. There external reference can be connected to
the AREF pin.
Communication Options: ATmega32 has three data transfer modules embedded in it. They
are
2. USART
Clock: It can run at a frequency from 1 to 16 MHz. Frequency can be obtained from external
Quartz Crystal, Ceramic crystal or an R-C network. Internal calibrated RC oscillator can also be
used.
a) PIC18.
b) PIC 16.
PIC18FXX2 series consists of four devices. PIC18F2X2 microcontrollers are 28-pin devices,
while PIC18F4X2 microcontrollers are 40-pin devices.
The architectures of the two groups are almost identical except that the larger devices have
more input-output ports and more A/D converter channels. In this section we shall be looking
standard PIC18F-series microcontrollers are similar, and the knowledge gained in this section
The program counter and program memory are shown in the upper left portion of the
diagram. Program memory addresses consist of 21 bits, capable of accessing 2Mbytes of
program memory locations. The PIC18F452 has only 32Kbytes of program memory, which
requires only 15 bits. The remaining 6 address bits are redundant and not used. A table
pointer provides access to tables and to the data stored in program memory. The program
memory contains a 31-level stack which is normally used to store the interrupt and subroutine
return addresses.
The data memory can be seen at the top center of the diagram. The data memory bus is 12
bits wide, capable of accessing 4Kbytes of data memory locations., the data memory consists
of special function registers (SFR) and general purpose registers, all organized in banks.
registers, USART, A/D converter, and EEPROM data memory. The PIC18F452
consists of:
4 timers/counters
2 capture/compare/PWM modules
The oscillator circuit, located at the left side of the diagram, consists of:
Power-up timer
Power-on reset
Watchdog timer
Brown-out reset
Low-voltage programming
In-circuit debugger
PLL circuit
The PLL circuit is new to the PIC18F series and provides the option of multiplying up the
oscillator frequency to speed up the overall operation. The watchdog timer can be used to
force a restart of the microcontroller in the event of a program crash. The in-circuit debugger
is useful during program development and can be used to return diagnostic data, including
the register values, as the microcontroller is executing a program.
The input-output ports are located at the right side of the diagram. The PIC18F452 has five
parallel ports named PORTA, PORTB, PORTC, PORTD, and PORTE. Most port pins have multiple
functions. For example, PORTA pins can be used as parallel inputs-outputs or analog inputs.
PORTB pins can be used as parallel inputs-outputs or as interrupt inputs.
PIC16
It is not different from other microcontrollers CPU and the PIC microcontroller CPU
consists of the ALU, CU, MU and accumulator, etc. Arithmetic logic unit is mainly used
for arithmetic operations and to take logical decisions. Memory is used for storing the
instructions after processing. To control the internal and external peripherals, control
unit is used which are connected to the CPU and the accumulator is used for storing
the results and further process.
Memory Organization
The memory module in the PIC microcontroller architecture consists of RAM (Random
Access Memory), ROM (Read Only Memory) and STACK.
RAM is an unstable memory which is used to store the data temporarily in its registers.
The RAM memory is classified into two banks, and each bank consists of so many
registers. The RAM registers are classified into two types: Special Function Registers
(SFR) and General Purpose Registers (GPR).
Flash Memory
Flash memory is also programmable read only memory (PROM) in which we can read,
write and erase the program thousands of times. Generally, the PIC microcontroller
uses this type of ROM.
Stack
When an interrupt occurs, first the PIC microcontroller has to execute the interrupt
and the existing process address. Then that is being executed is stored in the stack.
After completing the execution of the interrupt, the microcontroller calls the process
with the help of address, which is stored in the stack and get executes the process.
I/O Ports
The series of PIC16 consists of five ports such as Port A, Port B, Port C, Port D & Port
E.
Port A is an 16-bit port that can be used as input or output port based on the status of
the TRISA (Tradoc Intelligence Support Activity) register.
Port B is an 8- bit port that can be used as both input and output port.
Port C is an 8-bit and the input of output operation is decided by the status of the
TRISC register.
Port D is an 8-bit port acts as a slave port for connection to the microprocessor BUS.
Port E is a 3-bit port which serves the additional function of the control signals to the
analog to digital converter.
BUS
BUS is used to transfer and receive the data from one peripheral to another. It is
classified into two types such as data bus and address.