0% found this document useful (0 votes)
5 views29 pages

Assignment 3 Key

The document provides an overview of the architecture and functionality of the 8051 and PIC microcontrollers, detailing their components such as ALU, registers, memory organization, and I/O ports. It also discusses the interfacing of stepper motors with the 8051 microcontroller, explaining the types of stepper motors and their operational modes. Additionally, it covers the principles of serial communication, interrupts, and the use of timers in PIC microcontrollers.

Uploaded by

helen jenefa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views29 pages

Assignment 3 Key

The document provides an overview of the architecture and functionality of the 8051 and PIC microcontrollers, detailing their components such as ALU, registers, memory organization, and I/O ports. It also discusses the interfacing of stepper motors with the 8051 microcontroller, explaining the types of stepper motors and their operational modes. Additionally, it covers the principles of serial communication, interrupts, and the use of timers in PIC microcontrollers.

Uploaded by

helen jenefa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

1) ARCHITECTURE & BLOCK DIAGRAM OF 8051 MICROCONTROLLER:

 It has hardware architecture with RISC (Reduced Instruction Set Computer) concept.
 The block diagram of 8051 microcontroller is shown in Fig 3.
 8051 has 8-bit ALU.
 ALU can perform all the 8-bit arithmetic and logical operations in one machine cycle.
 The ALU is associated with two registers A & B
A and B Registers:
 The A and B registers are special function registers.
 A & B registers hold the results of many arithmetic and logical operations of 8051.
 The A register is also called the Accumulator.
 A register is used as a general register to accumulate the results of a large number of instructions.
 By default, it is used for all mathematical operations and data transfer operations between CPU
and external memory.
 The B register is mainly used for multiplication and division operations along with A register.
 Ex: MUL AB : DIV AB.
 It has no other function other than as a store data.
R registers:
 "R" registers are a set of eight registers that are named R0, R1, etc. up to R7.
 These registers are used as auxiliary registers in many operations.
 The "R" registers are also used to temporarily store values.

Fig.3. Block Diagram of 8051 Microcontroller


Program Counter (PC) :
 8051 has a 16-bit program counter.
 The program counter holds address of the next instruction to be executed.
 After execution of one instruction, the program counter is incremented.
Data Pointer Register (DPTR):
 It is a 16-bit register which is the only user-accessible.
 DPTR is used to point the data. 8051 will access external memory at the address indicated by DPTR.
 DPTR can also be used as two 8-registers DPH and DPL.
Stack Pointer Register (SP) :
 It is an 8-bit register which stores the address of the stack top.
 When a value is pushed onto the stack, the 8051 first increments the value of SP and then stores
the value.
 Similarly when a value is popped off the stack, the 8051 returns the value from the memory
location indicated by SP and then decrements the value of SP.
 Since the SP is only 8-bit wide.
 It is incremented or decremented by two.
 SP is modified directly by the 8051 by six instructions: PUSH, POP, ACALL, LCALL, RET, and RETI.
 It is also used intrinsically whenever an interrupt is triggered.

Fig 3a: Internal architecture diagram of 8051 Microcontroller

Fig: Structure of register


Program Status Register (PSW):
Give PSW of 8051 and describe the use of each bit in PSW. (NOV 2015)
 The 8051 has an 8-bit PSW register which is also known as Flag register.
 In the 8-bit register only 6-bits are used by 8051.The two unused bits are user definable bits.
 In the 6-bits, four of them are conditional flags. They are Carry –CY, Auxiliary Carry-AC, Parity-P,
and Overflow-OV.
 These flag bits indicate some conditions of result after an instruction was executed.

 The bits PSW3 and PSW4 are denoted as RS0 and RS1.
 These bits are used to select the bank registers of the RAM location.
 The meaning of various bits of PSW register is shown below.
CY PSW.7 Carry Flag
AC PSW.6 Auxiliary Carry Flag
FO PSW.5 Flag 0 available for general purpose
RS1 PSW.4 Register Bank select bit 1
RS0 PSW.3 Register bank select bit 0
OV PSW.2 Overflow flag
--- PSW.1 User definable flag
P PSW.0 Parity flag .set/cleared by hardware.

 The selection of the register Banks and their addresses are given below.

RS1 RS0 Register Bank Address

0 0 0 00H-07H

0 1 1 08H-0FH

1 0 2 10H-17H

1 1 3 18H-1FH

RAM & ROM:


 The 8051 microcontroller has 128 bytes of Internal RAM and 4KB of on chip ROM.
 The RAM is also known as Data memory and the ROM is known as program (Code) memory.
 Code memory holds program that is to be executed.
 Program Address Register holds address of the ROM/ Flash memory.
 Data Address Register holds address of the RAM.
I/O ports:
 The 8051 microcontroller has 4 parallel I/O ports, each of 8-bits.
 So, it provides 32 I/O lines for connecting the microcontroller to the peripherals.
 The four ports are P0 (Port 0), P1 (Port1), P2 (Port 2) and P3 (Port3).

1)b) PIC MICROCONTROLLER ARCHITECTURE:

PIC stands for Peripheral Interface Controller. PIC microcontroller was developed by microchip
technology in 1993. It was developed for supporting PDP computers to control its peripheral
devices and that’s why it was named Peripheral Interface Controller. PIC microcontrollers are of
low cost, very fast and easy for the programming and execution of program. Their interfacing with
other peripherals is also very easy. PIC Microcontrollers from Microchip Company are divided
into 4 large families.

 First family: PIC10 (10FXXX) called Low End


 Second family: PIC12 (PIC12FXXX) called Mid-Range
 Third family: PIC16 (16FXXX)
 Fourth family: PIC 17/18 (18FXXX)
Each family has a variety of components along with built in special features. It offers a lot of
memory sizes and pin packages and different clock ratings.

ARCHITECTURE:
PIC Microcontroller architecture is based on Harvard architecture and supports RISC architecture
(Reduced Instruction Set Computer). PIC microcontroller architecture consists of memory
organization (ram, rom, stack), CPU, timers, counter, ADC, DAC, serial communication, CCP
module and I/O ports. PIC microcontroller also supports the protocols like CAN, SPI, UART for
interfacing with other peripherals.

PIC MICROCONTROLLER ARCHITECTURE block diagram

1. CPU (Central Processing Unit):


PIC microcontroller’s CPU consists of
 Arithmetic logic unit (ALU)
 Memory unit (MU)
 Control unit (CU)
 Accumulator
ALU is used for arithmetic operations and for logical decisions. Memory is used for storing the
instructions after processing. Control unit is used to control the internal and external peripherals
which are connected to the CPU and accumulator is used for storing the results.

2. MEMORY ORGANIZATION:
PIC microcontroller memory module consists of mainly 3 types of memories:
 PROGRAM MEMORY:
It contains the written program after we burned it in microcontroller. Program Counter executes
commands stored in the program memory, one after the other. Pic microcontroller can have 8K
words x 14 bits of Flash program memory that can be electrically erased and
reprogrammed. Whenever we burn program into the micro, we erase an old program and write a
new one.
 DATA MEMORY:
It is a RAM type which is used to store the data temporarily in its registers. The RAM memory is
classified into banks. Each bank extends up to 7Fh (128 bytes). Number of banks may vary
depending on the microcontroller. PIC16F84 has only two banks. Banks contain Special Function
Registers (SFR) and General Purpose Registers (GPR). The lower locations of each bank are
reserved for the Special Function Registers and upper locations are for General Purpose Registers.
General Purpose Registers (GPR):
These registers don’t have any special function. These are used for general purpose for
multiplying, addition or subtraction and then storing the results in other registers. CPU can easily
access the data in these registers.
Special Function Registers (SFR):
These registers are used for special purposes and they cannot be used as normal registers. Their
function is set at the time of manufacturing. They perform the function assigned to them and user
cannot change the function of SFR. Three important SFRs for programming are:
STATUS register: It changes the bank
PORT registers: It assigns logic values 0 or 1 to the ports
TRIS registers: It is a data direction register for input and output
 DATA EEPROM:
This memory allows storing the variables as a result of burning the written program. It is readable
and writable during normal operation (over the full VDD range). This memory is not directly
mapped in the register file. It is indirectly addressed through the SFRs. There are six SFRs which
are used to read and write to this memory (EECON1, EECON2, EEDATA, EEDATH, EEADR,
EEADRH).
3. SERIAL COMMUNICATION:
The transfer of one bit of data at time consecutively over a communication channel is called Serial
Communication. There are three protocols of serial communication:

 USART: It stands for Universal synchronous and Asynchronous Receiver and Transmitter
which provides a serial communication in two devices. In this protocol data is transmitted and
received bit by bit through a single wire according to the clock pulses. To send and receive data
serially the PIC microcontroller has two pins TXD and RXD.
 SPI Protocol: SPI stands for Serial Peripheral Interface. It is used to send data between PIC
microcontrollers and other peripherals like sensors, shift registers and SD cards. Three wire SPI
communications is supported in PIC microcontroller between two devices on a common clock
source. SPI protocol has greater data handling capability than that of the USART.
 I2C Protocol: I2C stands for Inter Integrated Circuit, and this protocol is used to connect low
speed devices like microcontrollers, EEPROMS and A/D converters. PIC microcontroller support
two wire Interface or I2C communication between two devices which can work as both Master
and Slave device.

Serial Communication
4. INTERRUPTS:
There are 20 internal interrupts and three external interrupt sources in PIC microcontrollers which
are related with different peripherals like ADC, USART, Timers, and CCP etc.
5. I/O PORTS:
Let us take PIC16 series, it consists of five ports, such as Port A, Port B, Port C, Port D and Port
E.
 Port A: This port is 7-bit wide and can be used for both input and output. The status of
TRISA register decided whether it is used as input or output port.
 Port B: It is an 8-bit port. This port also can be used as input and output. Moreover in
input mode four of its bits are variable according to the interrupt signals.
 Port C: It is also an 8-bit port and can be used as both input and output port which is
determined by the status of the TRISC register.
 Port D: This 8-bit port, unlike Port A, B and C is not an input/output port, but is used
as acts as a slave port for the connection to the microprocessor When in I/O mode Port D all pins
should have Schmitt Trigger buffers.
 Port E: It is a 3-bit port which is used as the additional feature of the control signals to
the A/D converter.
6. CCP MODULE:
A CCP module works in the following three modes:
 Capture Mode: In this mode time is captured when a signal is arrived, or we can say that, when
the CCP pin goes high it captures the value of the Timer1.
 Compare Mode: It works same as an analog comparator, which means that when timer 1’s value
reaches some reference value it will give an output signal.
 PWM Mode: This mode provides a 10 bit resolution pulse and duty cycle that is programmable.
7. Timers:
8. Timers and counters are important as timers can tell the time and count. PIC microcontroller can have up
to four timers (depending upon the family) Timer0, Timer1, Timer2 and Timer3. Timer0 and Timer2 are of
8-bits while the Timer1 and Timer3 are of 16-bits, which can also be used as a counter. These timers work
according to the selected modes.
9. D/A CONVERTER:
There are no analog outputs in PIC Microcontroller. To get analog output we have to use
external Digital-to-Analog Converter (DAC). It can convert 8 bits of digital number from the eight
digital outputs of PIC microcontroller.
10.A/D CONVERTER:
It converts the analog voltage levels to digital voltage values. In PIC Microcontroller, ADC has 8-
channels and has resolution of 10-bit, which means that if we have to convert an analog voltage
between 0V to 5V the converter will divide it to 2^10 levels (1024 levels). The special function
registers ADCON0 and ADCON1 control the operation of ADC. The converter stores the lower 8
bits in ADRESL register and the upper bits in the ADRESH register. Reference voltage of 5V is
required for the operation of the converter.

2) Stepper Motor Interfacing with 8051 Microcontroller

Stepper motor is brushless DC motor, which can be rotated in small angles, these angles are called
steps. Generally stepper motor use 200 steps to complete 360 degree rotation, means its rotate 1.8
degree per step. Stepper motor used in many devices which needs precise rotational movement
like robots, antennas, hard drives etc. We can rotate stepper motor to any particular angle by giving
it proper instructions.

Stepper motors are used to translate electrical pulses into mechanical movements. In some disk
drives, dot matrix printers, and some other different places the stepper motors are used. The main
advantage of using the stepper motor is the position control. Stepper motors generally have a
permanent magnet shaft (rotor), and it is surrounded by a stator.
Normal motor shafts can move freely but the stepper motor shafts move in fixed repeatable
increments.
Some parameters of stepper motors −
 Step Angle − The step angle is the angle in which the rotor moves when one pulse is applied as
an input of the stator. This parameter is used to determine the positioning of a stepper motor.
 Steps per Revolution − This is the number of step angles required for a complete revolution. So
the formula is 360° /Step Angle.
 Steps per Second − This parameter is used to measure a number of steps covered in each second.
 RPM − The RPM is the Revolution Per Minute. It measures the frequency of rotation. By this
parameter, we can measure the number of rotations in one minute.
The relation between RPM, steps per revolution, and steps per second is like below:

Steps per Second = rpm x steps per revolution / 60


Stepper motors are basically two types: Unipolar and Bipolar. Unipolar stepper motor generally
has five or six wire, in which four wires are one end of four stator coils, and other end of the all
four coils is tied together which represents fifth wire, this is called common wire (common point).
Generally there are two common wire, formed by connecting one end of the two-two coils as
shown in below figure. Unipolar stepper motor is very common and popular because of its ease of
use.

In Bipolar stepper motor there is just four wires coming out from two sets of coils, means there
are no common wire.
Stepper motor is made up of a stator and a rotator. Stator represents the four electromagnet coils
which remain stationary around the rotator, and rotator represents permanent magnet which
rotates. Whenever the coils energised by applying the current, the electromagnetic field is created,
resulting the rotation of rotator (permanent magnet). Coils should be energised in a particular
sequence to make the rotator rotate. On the basis of this “sequence” we can divide the working
method of Unipolar stepper motor in three modes: Wave drive mode, full step drive mode and
half step drive mode.

Wave drive mode: In this mode one coil is energised at a time, all four coil are energised one after
another. It produces less torque in compare with Full step drive mode but power consumption is
less. Following is the table for producing this mode using microcontroller, means we need to give
Logic 1 to the coils in the sequential manner.

Steps A B C D

1 1 0 0 0

2 0 1 0 0

3 0 0 1 0

4 0 0 0 1

Full Drive mode: In this, two coil are energised at the same time producing high torque. Power
consumption is higher. We need to give Logic 1 to two coils at the same time, then to the next two
coils and so on.

Steps A B C D

1 1 1 0 0

2 0 1 1 0

3 0 0 1 1

4 1 0 0 1

Half Drive mode: In this mode one and two coils are energised alternatively, means firstly one
coil is energised then two coils are energised then again one coil is energised then again two, and
so on. This is combination of full and wave drive mode, and used to increase the angular rotation
of the motor.

Steps A B C D

1 1 0 0 0

2 1 1 0 0

3 0 1 0 0

4 0 1 1 0

5 0 0 1 0

6 0 0 1 1

7 0 0 0 1

8 1 0 0 1

Interfacing Stepper Motor with 8051 Microcontroller


Interfacing with 8051 is very easy we just need to give the 0 and 1 to the four wires of stepper
motor according to the above tables depending on which mode we want to run the stepper motor.
And rest two wires should be connected to a proper 12v supply (depending on the stepper motor).
Here we have used the unipolar stepper motor. We have connected four ends of the coils to the
first four pins of port 2 of 8051 through the ULN2003A.
8051 doesn’t provide enough current to drive the coils so we need to use a current driver IC that
is ULN2003A. ULN2003A is the array of seven NPN Darlington transistor pairs. Darlington pair
is constructed by connecting two bipolar transistors to achieve high current amplification. In
ULN2003A, 7 pins are input pins and 7 pins are output pins, two pins are for Vcc (power supply)
and Ground. Here we are using four input and four output pins. We can also use L293D IC in place
of ULN2003A for current amplification.

You need to find out four coil wires and two common wires very carefully otherwise motor will
not rotate. You can find it out by measuring resistance using multimeter, multimeter won’t show
any readings between the wires of two phases. Common wire and the other two wire in the same
phase should show the same resistance, and the two end points of the two coils in the same phase
will show the twice resistance in compared with resistance between common point and one end
point.

Interfacing stepper motor with 8051

STEPPER MOTOR
A stepper motor is a brushless, synchronous electric motor that converts digital pulses into
mechanical shaft rotation. Every revolution of the stepper motor is divided into a discrete number of steps,
and the motor must be sent a separate pulse for each step.
Stepper motors can be used in various areas of your microcontroller projects such as making robots,
robotic arm, and automatic door lock system.
Fig. shows how to interface the Stepper Motor to microcontroller. As you can see the stepper motor
is connected with Microcontroller output port pins through a ULN2003A array. So when the
microcontroller is giving pulses with particular frequency to ULN2003A, the motor is rotated in clockwise
or anticlockwise.

Step Angle

 Step angle of the stepper motor is defined as the angle traversed by the motor in one step.

 To calculate step angle, simply divide 360 by number of steps a motor takes to complete one
revolution.

 Motor rotating in full mode takes 4 steps to complete a revolution ,so step angle can be calculated
as step angle θ = 360° / 4 =90.
 By knowing the stepper motor step angle helps to move the motor in correct angular position.
 As you can see the stepper motor is connected with Microcontroller output port pins through a
ULN2003A array.
 So when the microcontroller is giving pulses with particular frequency to ULN2003A, the motor
is rotated in clockwise or anticlockwise
Program to interface Stepper motor with 8051
 To control a stepper motor in 8051 trainer by turning ON & OFF a four I/O port lines generating at
a particular frequency.
 The 8051 trainer kit has three numbers of I/O port connectors, connected with I/O Port lines (P1.0
– P1.7), (P3.0 – P3.7) to rotate the stepper motor.
 LS293D is used as a driver for port I/O lines, drivers output connected to stepper motor, connector
provided for external power supply if needed.
By giving the excitation as indicated above through port 1 we can rotate stepper motor in clockwise or
anticlockwise direction.

NOTE: To turn the motor in the reverse direction enter as (RL A instead of RR A). The schematic
sections given is, stepper motor connected to port 1 and the sample program is given based on 8255.

Example 4: Describe the 8051 connection to the stepper motor of figure shows and code a
program to rotate it continuously.

Figure : Interfacing Stepper Motor with8051

MOV A, #66H Load step sequence


BACK: MOV P1, A Issue sequence to
Motor
RR A Rotate right
Clockwise
ACALL DELAY Wait
SJMP BACK Keep going
…………….
DELAY
MOV R2, #100H
H1: MOV R3, #255H H1:
H2: DJNZ R3, H2 H2:
DJNZ R2, H1
RET

3)PIC16F877A Interrupt

Interrupts are special events that require immediate attention, it stops a


microcontroller/microprocessor from the running task and serves a special task known as
Interrupt Service Routine (ISR) or Interrupt Handler.

PIC16F877A has the following 15 interrupt sources :

 External
 Timer 0
 Timer 1
 RB Port Change
 Parallel Slave Port Read/Write
 A/D Converter
 USART Receive
 USART Transmit
 Synchronous Serial Port
 CCP1 (Capture, Compare, PWM)
 CCP2 (Capture, Compare, PWM)
 TMR2 to PR2 Match
 Comparator
 EEPROM Write Operation
 Bus Collision
In this tutorial, we will see USART Interrupts, Timer Interrupts, and External Interrupts.

Registers Used for Interrupts

 INTCON
 OPTION_REG
 PIE1
 PIR1
 PIE2
 PIR2
INTCON Register

The INTCON register is a readable and writable register, which contains various enable and flag
bits for the TMR0 register overflow, RB port change and external RB0/INT pin interrupt.

GIE: Global Interrupt Enable bit

1-Enables all unmasked interrupts


0-Disables all interrupts

PIE: Peripheral Interrupt Enable bit

1-Enables all unmasked peripheral interrupts


0-Disables all peripheral interrupts

TMR0IE: TMR0 Overflow Interrupt Enable bit

1-Enables the TMR0 interrupt


0-Disables the TMR0 interrupt

INTE: RB0/INT External Interrupt Enable bit

1 = Enables the RB0/INT external interrupt


0 = Disables the RB0/INT external interrupt

RBIE: RB Port Change Interrupt Enable bit

1 = Enables the RB port change interrupt


0 = Disables the RB port change interrupt

TMR0IF: TMR0 Overflow Interrupt Flag bit

1-TMR0 register has overflowed (must be cleared in software)


0-TMR0 register did not overflow
INTF: RB0/INT External Interrupt Flag bit

1 = The RB0/INT external interrupt occurred (must be cleared in software)


0 = The RB0/INT external interrupt did not occur

RBIF: RB Port Change Interrupt Flag bit

1 = At least one of the RB7:RB4 pins changed state; a mismatch condition will continue to set
the bit. Reading PORTB will end the mismatch condition and allow the bit to be cleared (must
be cleared in software).
0 = None of the RB7:RB4 pins have changed state

OPTION_REG Register

The OPTION_REG Register is a readable and writable register, which contains various control
bits to configure the TMR0 Prescaler/WDT Postscaler (single assignable register known also as
the Prescaler), the external INT interrupt, TMR0, and the weak pull-ups on PORTB.

RBPU: PORTB Pull-up Enable bit (This bit is not used for timers)

1 = PORTB pull-ups are disabled


0 = PORTB pull-ups are enabled by individual port latch values

INTEDG Interrupt Edge Select bit

1 = Interrupt on the rising edge of RB0/INT pin


0 = Interrupt on the falling edge of RB0/INT pin

T0CS: TMR0 Clock Source Select bit

1 = Transition on T0CKI pin


0 = Internal instruction cycle clock (CLKO)

T0SE: TMR0 Source Edge Select bit

1 = Increment on high-to-low transition on T0CKI pin


0 = Increment on low-to-high transition on T0CKI pin
PSA: Prescaler Assignment bit

1 = Prescaler is assigned to the WDT


0 = Prescaler is assigned to the Timer0 module

PS2:PS0: Prescaler Rate Select bits

Note: There is only one Prescaler available which is mutually exclusively shared between the
Timer0 module and the Watchdog Timer. A Prescaler assignment for the Timer0 module means
that there is no Prescaler for the Watchdog Timer and vice versa. This Prescaler is not accessible
but can be configured using PS2:PS0 bits of OPTION_REG.

PIE1 Register

The PIE1 register contains the individual enable bits for the peripheral interrupts.

PSPIE: Parallel Slave Port Read/Write Interrupt Enable bit(1)

1 = Enables the PSP to read/write interrupt


0 = Disables the PSP read/write interrupt

Note (1): PSPIE is reserved on PIC16F873A/876A devices; always maintain this bit clear.

ADIE: A/D Converter Interrupt Enable bit

1 = Enables the A/D converter interrupt


0 = Disables the A/D converter interrupt

RCIE: USART Receive Interrupt Enable bit


1 = Enables the USART to receive interrupt
0 = Disables the USART receive interrupt

TXIE: USART Transmit Interrupt Enable bit

1 = Enables the USART to transmit interrupt


0 = Disables the USART transmit interrupt

SSPIE: Synchronous Serial Port Interrupt Enable bit

1 = Enables the SSP interrupt


0 = Disables the SSP interrupt

CCP1IE: CCP1 Interrupt Enable bit

1 = Enables the CCP1 interrupt


0 = Disables the CCP1 interrupt

TMR2IE: TMR2 to PR2 Match Interrupt Enable bit

1 = Enables the TMR2 to PR2 match interrupt


0 = Disables the TMR2 to PR2 match interrupt

TMR1IE: TMR1 Overflow Interrupt Enable bit

1 = Enables the TMR1 overflow interrupt


0 = Disables the TMR1 overflow interrupt

PIR1 Register

The PIR1 register contains the individual flag bits for the peripheral interrupts.

Note: Interrupt flag bits are set when an interrupt condition occurs regardless of the state of its
corresponding enable bit or the global enable bit, GIE (INTCON<7>). User software should
ensure the appropriate interrupt bits are clear prior to enabling an interrupt.

PSPIF: Parallel Slave Port Read/Write Interrupt Flag bit(1)


1 = A read or a write operation has taken place (must be cleared in software)
0 = No read or write has occurred

Note (1): PSPIF is reserved on PIC16F873A/876A devices; always maintain this bit clear.

ADIF: A/D Converter Interrupt Flag bit

1 = An A/D conversion completed


0 = The A/D conversion is not complete

RCIF: USART Receive Interrupt Flag bit

1 = The USART receive buffer is full


0 = The USART receive buffer is empty

TXIF: USART Transmit Interrupt Flag bit

1 = The USART transmit buffer is empty


0 = The USART transmit buffer is full

SSPIF: Synchronous Serial Port (SSP) Interrupt Flag bit

CCP1IF: CCP1 Interrupt Flag bit

TMR2IF: TMR2 to PR2 Match Interrupt Flag bit

1 = TMR2 to PR2 match occurred (must be cleared in software)


0 = No TMR2 to PR2 match occurred

TMR1IF: TMR1 Overflow Interrupt Flag bit

1 = TMR1 register overflowed (must be cleared in software)


0 = TMR1 register did not overflow

PIE2 Register

The PIE2 register contains the individual enable bits for the CCP2 peripheral interrupt, the SSP
bus collision interrupts, EEPROM writes operation interrupt, and the comparator interrupt.
CMIE: Comparator Interrupt Enable bit

1 = Enables the comparator interrupt


0 = Disable the comparator interrupt

EEIE: EEPROM Write Operation Interrupt Enable bit

1 = Enable EEPROM write interrupt


0 = Disable EEPROM write interrupt

BCLIE: Bus Collision Interrupt Enable bit

1 = Enable bus collision interrupt


0 = Disable bus collision interrupt

CCP2IE: CCP2 Interrupt Enable bit

1 = Enables the CCP2 interrupt


0 = Disables the CCP2 interrupt

PIR2 Register
The PIR2 register contains the flag bits for the CCP2 interrupt, the SSP bus collision interrupt,
the EEPROM write operation interrupt, and the comparator interrupt.

CMIF: Comparator Interrupt Flag bit

1 = The comparator input has changed (must be cleared in software)


0 = The comparator input has not changed

EEIF: EEPROM Write Operation Interrupt Flag bit

1 = The write operation completed (must be cleared in software)


0 = The write operation is not complete or has not been started

BCLIF: Bus Collision Interrupt Flag bit


1 = A bus collision has occurred in the SSP when configured for I2C Master mode
0 = No bus collision has occurred

CCP2IF: CCP2 Interrupt Flag bit


4)

5)Detail the methodology of transferring/receiving a serial data with 8051 microcontrollers with
a real time example.
5) SERIAL COMMUNICATION
RS232
 It is an interfacing standard RS232.
 It was set by the Electronics Industries Association (EIA) in 1960.
 The standard was set long before the advent of the TTL logic family.
 Its input and output voltage levels are not TTL compatible.
 In RS232, a 0 is represented by -3 to -25 V, while a 1 bit is +3 to +25 V.
 IBM introduced the DB-9 version of the serial I/O standard.
Handshake signals of MODEM DTR (data terminal ready)
 When DTR =1, indicate that it is ready for communication.
DSR (data set ready)
 When DSR =1, indicate that it is ready for communication.
RTS (request to send)
It asserts RTS to signal the modem that it has a byte of data to transmit.
CTS (clear to send)
 It is to receive, it sends out signal CTS,

DCD (data carrier detect)


 The modem asserts signal DCD to inform the DTE that a valid carrier has been detected.
RI (ring indicator)
 An output from the modem and an input to a PC indicates that the telephone is ringing.

MAX232
A line driver ( MAX232) is required to convert RS232 voltage levels to TTL levels, and vice
versa.
 8051 has two pins that are used specifically for transferring and receiving data serially.
 These two pins are called TxD and RxD and are part of the port 3 (P3.0 and P3.1).
 These pins are TTL compatible.
 They require a line driver to make them RS232 compatible.
Baud rate:
 The baud rates in 8051 are programmable.
 8051 divides the crystal frequency by 12 to get machine cycle frequency.
 8051 UART circuitry divides the machine cycle frequency by 32.

 Timer 1 is used to set baud rate using TH1 register

Baud rate TH1 (decimal) TH1(Hex)


9600 -3 FD
4800 -6 FA
2400 -12 F4
1200 -24 E8

serial communication registers of the 8051.


SBUF:
 It is an 8-bit register used for serial communication.
 For a byte data to be transferred via the TxD line:
 Byte must be placed in the SBUF register.
 Bytes are framed with the start and stop bits and transferred serially via the TxD line.
 SBUF holds the byte of data when it is received by 8051 RxD line.
 When the bits are received serially via RxD.
 8051 de-frames byte by eliminating the stop and start bits.
SCON:
 It is an 8-bit register used to program the start bit, stop bit and data bits of data framing.

SM0 SM1 SM2 REN TB8 RB8 TI RI

Bit Bit Description


Number Mnemonic
SCON.7 SM0 Serial port mode specifier
SCON.6 SM1 Serial port mode specifier
SCON.5 SM2 Used for multiprocessor communication
SCON.4 REN Set/Cleared by software to enable/disable reception
SCON.3 TB8 Not widely used
SCON.2 RB8 Not widely used
SCON.1 TI Transmit interrupt flag. Set by hardware at the begin of the stop bit mode 1.
And cleared by software
SCON.0 RI Receive interrupt flag. Set by hardware at the begin of the stop bit mode 1.
And cleared by software

SM0, SM1: Serial port mode specifiers

SM0 SM1
0 0 Serial Mode 0

0 1 Serial Mode 1; 8-bit data, 1 stop bit, 1 start bit


1 0 Serial Mode 2
1 1 Serial Mode 3

In programming the 8051 to transfer character bytes serially


1. TMOD register is loaded with the value 20H, indicating the use of timer 1 in
mode 2 (8-bit auto- reload) to set baud rate.
2. The TH1 is loaded with one of the values to set baud rate for serial data transfer.
3. The SCON register is loaded with the value 50H, indicating serial mode 1, where an
8-bit data is framed with start and stop bits.
4. TR1 is set to 1 to start timer 1
5. TI is cleared by CLR TI instruction.
6. The character byte to be transferred serially is written into SBUF register.
7. The TI flag bit is monitored with the use of instruction JNB TI, xx, to see if
the character has been transferred completely.
8. To transfer the next byte, go to step 5.

Write a program for the 8051 to transfer letter “A” serially at 4800 baud, continuously.

Solution:

MOV TMOD, #20H ;timer 1, mode 2 (auto reload)


MOV TH1, #-6 ;4800 baud rate
MOV SCON, #50H ;8-bit, 1stop, REN enabled
SETB TR1:start timer 1
AGAIN:MOV SBUF, #”A” ;letter “A” to trtansfer
HERE: JNB TI, HERE ;wait for the last bit
CLR TI ;clear TI for next char
SJMP AGAIN ;keep sending A
The steps that 8051 goes through in transmitting
a character via TxD
The byte character to be transmitted is written intothe SBUF register
1. The start bit is transferred
2. The 8-bit character is transferred on bit at a time
3. The stop bit is transferred
 It is during the transfer of the stop bit that 8051 raises the TI flag,
indicating that the last character was transmitted
4. By monitoring the TI flag, we make sure that we are not overloading the SBUF
 If we write another byte into the SBUF before TI is raised, the un-
transmitted portion of the previous byte will be lost.
5. After SBUF is loaded with a new byte, the TI flag bit must be forced to 0 by CLR
TI in order for this new byte to be transferred

By checking the TI flag bit, we know whether or not the 8051 is ready to transfer another byte
 It must be noted that TI flag bit is raised by 8051 itself when it finishes data transfer
 It must be cleared by the programmer with instruction CLR TI
 If we write a byte into SBUF before the TI flag bit is raised, we risk the loss of a
portion of the byte being transferred
 The TI bit can be checked by the instruction JNB TI,xx Using an interrupt.
Write a program for the 8051 to transfer “YES” serially at 9600 baud, 8-bit data, 1 stop
bit do this continuously. (May 2006)
Solution:
MOV TMOD, #20H ;timer 1, mode 2 (auto reload) MOV
TH1, #-3 ;9600 baud rate
MOV SCON, #50H ;8-bit, 1 stop, REN enabled SETB
TR1 ;start timer1
MOV P1, A ;send to port 1
CLR RI ;get ready to receive next byte
SJMP HERE ;keep getting data
In receiving bit via its RxD pin, 8051 goes through the following steps.
1. It receives the start bit
 Indicating that the next bit is the first bit of the character byte it is about to receive
2. The 8-bit character is received one bit at time
3. The stop bit is received
 When receiving the stop bit 8051 makes RI = 1,indicating that an entire character
byte has been received.
5. After the SBUF contents are copied into a safe place.
 The RI flag bit must be forced to 0 by CLR RI in order to allow the next received character byte to be
placed in SBUF.
 Failure to do this causes loss of the received character.

There are two ways to increase the baud rate of data transfer
 To use a higher frequency crystal

PCON
 PCON register is an 8-bit register
 When 8051 is powered up, SMOD is zero.
 We can set it to high by software and thereby double the baud rate.
 GF1, GF0: General flag bits
 PD: Power down mode
IDL: Ideal mode
Real-time example: Reading temperature from a sensor and displaying it on a PC:
Connecting a 8051 microcontroller to a sensor like a temperature sensor, where the sensor sends data
serially to the 8051 for processing and display.
 Hardware setup:
 Connect a temperature sensor (like a LM35) to the 8051's analog input pins.
 Wire the 8051's TX pin to the RX pin of a USB-to-serial converter connected to a PC.
 Software implementation:
 Initialization:
 Configure the A/D converter to read the sensor data.
 Set up Timer 1 to generate the desired baud rate for serial communication.
 Configure the SCON register for 8-bit data transmission with appropriate parity and stop bits.
 Main loop:
 Read the analog sensor data using the A/D converter.
 Convert the raw ADC value to temperature using the sensor's conversion formula.
 Store the temperature value in a variable.
 Send the temperature data (formatted as a string) to the PC via the serial port using the SBUF register.
 Add a small delay to allow for data transmission and avoid overloading the PC.
Key code snippets (assembly language):
Setting baud rate (Timer 1 configuration).
Code
MOV TMOD, #20H ; Set Timer 1 to Mode 2 (8-bit auto-reload) [6, 11]

MOV TH1, #FDH ; Load TH1 register for desired baud rate [6]

SETB TR1 ; Start Timer 1 [6]


Sending data through serial port.
Code
MOV A, #'T' ; Load data to be sent into accumulator [6]

MOV SBUF, A ; Send data to serial port buffer [2, 3, 6]

JNB TI, HERE ; Wait for transmission complete flag (TI) [6, 10]

CLR TI ; Clear transmission flag [6, 10]


Important considerations:
 Data formatting:
Ensure the data sent to the PC is formatted in a way that can be easily interpreted by the receiving
application (e.g., ASCII characters, comma-separated values).
 Error checking:
Depending on the application, you might want to implement parity checking or other error detection
mechanisms to ensure data integrity.
 Interrupts:
For high-speed data transfer or real-time applications, consider using interrupts to handle serial data
reception.
Real-time example: A basic digital thermometer
Hardware Setup:
 Connect the LM35 sensor's output to the ADC input pin on the 8051.
 Connect the 8051's TX pin (transmit) to the RX pin of the computer's serial port (or a suitable USB-to-serial
adapter).
Software Implementation (C code example):
#include <reg51.h> // 8051 header file
// Function to read ADC value
unsigned int read_adc() {

// Set up ADC control register


// Start ADC conversion
// Wait for conversion to complete

return AD_RESULT; // Read ADC result


}

// Function to convert ADC value to temperature

float adc_to_temp(unsigned int adc_value) {

return (adc_value * 5.0) / 1023 * 100; // Assuming 10-bit ADC, adjust for your setup

}
void serial_init() {

// Configure SCON register for desired baud rate and mode [1, 4, 10]
// Set Timer 1 register for baud rate calculation [1, 3, 10]

void send_data(unsigned char data) {


SBUF = data; // Send data to serial buffer [1, 4, 10]
while (!TI); // Wait for transmission complete flag [1, 4]

TI = 0; // Clear transmission flag [1, 4]


}

void main() {

serial_init();
while (1) {

unsigned int adc_reading = read_adc();

float temperature = adc_to_temp(adc_reading);


// Convert temperature to ASCII string for serial transmission

// (implementation depends on your chosen data format)

// Send temperature data serially

for (int i = 0; i < strlen(temperature_string); i++) {

send_data(temperature_string[i]);
}
// Optional delay for refresh rate

// ...
}
}
Explanation:
 ADC reading:
The read_adc() function reads the analog value from the temperature sensor using the 8051's ADC module.
 Temperature conversion:
The adc_to_temp() function converts the ADC value to a temperature reading using the known conversion
factor for the LM35 sensor

You might also like