Assignment 3 Key
Assignment 3 Key
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.
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.
0 0 0 00H-07H
0 1 1 08H-0FH
1 0 2 10H-17H
1 1 3 18H-1FH
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.
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.
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.
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:
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
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.
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.
3)PIC16F877A Interrupt
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.
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.
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)
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.
Note (1): PSPIE is reserved on PIC16F873A/876A devices; always maintain this bit clear.
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.
Note (1): PSPIF is reserved on PIC16F873A/876A devices; always maintain this bit clear.
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
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.
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,
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.
SM0 SM1
0 0 Serial Mode 0
Write a program for the 8051 to transfer letter “A” serially at 4800 baud, continuously.
Solution:
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]
JNB TI, HERE ; Wait for transmission complete flag (TI) [6, 10]
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 main() {
serial_init();
while (1) {
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