MC Module 3
MC Module 3
•Timer 0
•Timer 1
Both can work either as timers or event
counters
Four different operating modes to select
Timer/Counter
9ch….. ff
9Ch
Interfacing Peripheral Devices
BUZZER
#include<reg51.h>
#include<stdio.h>
void delay();
int i;
void main()
{
while(1)
{
P2=0xEF;
delay();
P2=0xff;
delay();
}
Program:
#include <reg51.h>
INTERFACING LED #include <stdio.h>
unsigned long int i;
void delay()
{
for (i=0; i<2500; i++);
}
void main()
{
while (1)
{
P1 = 0x00;
delay();
P1 = 0xff;
delay();
}}
REG51.Hhttps://www.keil.com/dd/docs/c51/reg51.h
When no data is being transmitted the TXD line is constantly held HIGH (ie;
a constant stream of stop bits).
Received data arrives on RXD. When the stop bit is received the RI flag is
set, alerting the processor to the fact that an byte has been received.
The 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 group (P3.0 and P3.1). Pin
11 of the 8051 (P3.1) is assigned to TxD and pin 10 (P3.0) is designated as RxD. These pins
are TTL compatible; therefore, they require a line driver to make them RS232 compatible.
One such line driver is the MAX232 chip.
MAX232
Since the RS232 is not compatible with today’s microprocessors and microcontrollers, we
need a line driver (voltage converter) to convert the RS232′s signals to TTL voltage levels
that will be acceptable to the 8051 ‘s TxD and RxD pins. One example of such a converter is
MAX232 from Maxim Corp. (www.maxim-ic.com). The MAX232 converts from RS232
voltage levels to TTL voltage levels, and vice versa. One advantage of the MAX232 chip is
that it uses a +5 V power source which, is the same as the source voltage for the 8051. In
other words, with a single +5 V power supply we can power both the 8051 and MAX232,
with no need for the dual power supplies that are common in many older systems.
INTERFACING 7 SEGMENT LED WITH 8051
7 SEGMENT LEDS ARE CONNECTED IN COMMON CATHODE CONFIGURATION
Common anode configuration
DECI h g f e d c b a Hex
MAL value
NO
0 1 1 0 0 0 0 0 0 0xc0
1 1 1 1 1 1 0 0 1 0xf9
2 1 0 1 0 0 1 0 0 0xa4
3 1 0 1 1 0 0 0 0 oxb0
4 1 0 0 1 1 0 0 1 0x99
5…9
A…f
#include<reg51.h>
#include<stdio.h> void main()
#include<string.h> {
55
8051 INTERRUPTS
• Interrupt service routine
– for each interrupt there must be ISR
– for every interrupt, there is a fixed location in
memory that holds the address of its ISR
– interrupt vector table
56
8051 INTERRUPTS
57
8051 INTERRUPTS
• Steps in executing an interrupt
1. μC finishes the instruction it is executing and saves
the address of the next instruction (PC) on the stack
2. it saves the current status of all the interrupts
internally
3. it jumps to a fixed location in memory called the
interrupt vector table
4. the microcontroller gets the address of the ISR from
the interrupt vector table and jumps to it and starts
to execute the ISR until it reaches the last instruction
RETI
5. the microcontroller returns to the place where it was
interrupted, it gets the PC address from the stack by
popping the top two bytes of the stack into the PC
and then it starts to execute from that address 58
8051 INTERRUPTS
• Six interrupts in the 8051
– 1 reset interrupt, when the reset pin is activated, the
8051 jumps to address location 0000
– 2 timer interrupts
– 2 external hardware interrupts
– pin 12 (P3.2) and 13 (P3.3) in port 3 are for the
external hardware interrupts
– 1 serial communication interrupt that belongs to
both receive and transmit
– a limited number of bytes is set aside for each
interrupt
59
8051 INTERRUPTS
• Enabling and disabling an interrupt
– upon reset all interrupts are disabled
– interrupts must be enabled by software
– IE register (interrupt enable) is responsible for
enabling and disabling the interrupts
– IE is a bit-addressable register
60
8051 INTERRUPTS
• Steps in enabling an interrupt
1. EA must be set to 1
2. set the relevant bits in IE register to high
61
IE (Interrupt Enable) Register
62
Show the instructions to
(a) enable the serial interrupt, Timer 0 interrupt,
and external hardware interrupt 1 (EX1),
Figure 1 TF Interrupt
– if the timer interrupt is enabled, whenever TF=1, the microcontroller is interrupted in
whatever it is doing, and jumps to the interrupt vector table to service the ISR
– In this way, the microcontroller can do other things until it is notified that the timer
has rolled over
64
PROGRAMMING EXTERNAL HARDWARE
INTERRUPTS
• External interrupts INT0 and INT1
66
INTERRUPT PRIORITY IN THE 8051
68
Program the IP register to assign the highest priority to INTI,
then (b) discuss what happens if INT0, INT1, and TF0are
activated at the same time. Assume that the interrupts are
both edge-triggered.
69
Inter-Integrated Circuit
• I²C is a serial computer bus, which is invented by NXP semiconductors
previously it is named as Philips semiconductors.
• The I²C bus is used to attach low-speed peripheral integrated circuits to
microcontrollers and processors.
• I²C bus uses two bidirectional open-drain lines such as SDA (serial data
line) and SCl (serial clock line) and these are pulled up with resistors.
• I²C bus permits a master device to start communication with a slave
device.
• Data is interchanged between these two devices.
• Typical voltages used are +3.3V or +5V although systems with extra
voltages are allowed.
• Nowadays new microcontrollers have inbuilt I²C Registers. But in 8051
there is no such registers.
• So we have to achieve I²C in 8051. So in this tutorial, we will see how to
achieve that.
Analog to Digital Interfacing with 8051
•ADC – analog to digital conversion
• It converts the given analog value to a digital value
•There are lot of digital ICs available to convert analog to digital
• In microcontroller interfacing the frequently used ICs are 0804 and 0808
•Different types- flash, successive approximation, integral type
•ADC0808 :
•ADC0808 is a commonly used External 8 bit ADC and it has 28 pins.
•It can measure up to eight ADC values from 0 to 5 volt since it has eight channels.
•when voltage reference is +5V, its Step size will be 19.53mV. That is, for every
increase of 19.53mV on the input side there will be an increase of 1 bit at the
output side.
Features
The Iref is the input current. This must be provided into the pin 14. Generally 2.0mA is used as
Iref
We connect the Iout pin to the resistor to convert the current to voltage. But in real life it may
cause inaccuracy since the input resistance of the load will also affect the output voltage. So
practically Iref current input is isolated by connecting it to an Op-Amp with Rf = 5KΩ as
feedback resistor. The feedback resistor value can be changed as per requirement.
Weighted Resistor DAC
A weighted resistor DAC produces an analog output, which is almost equal to the
digital (binary) input by using binary weighted resistors in the inverting adder circuit.
In short, a binary weighted resistor DAC is called as weighted resistor DAC.
We can write the generalized output voltage equation of an N-bit binary weighted resistor
DAC as shown below based on the output voltage equation of a 3-bit binary weighted resistor
DAC.
Let the 3-bit binary input is b2b1b0. Here, the bits b2 and b0 denote the Most Significant Bit (MSB) and Least
Significant Bit (LSB) respectively.
The digital switches shown in the above figure will be connected to ground, when the corresponding input bits are
equal to ‘0’. Similarly, the digital switches shown in above figure will be connected to the negative reference
voltage, −VR−VR when the corresponding input bits are equal to ‘1’.
It is difficult to get the generalized output voltage equation of a R-2R Ladder DAC. But, we can find the analog output
voltage values of R-2R Ladder DAC for individual binary input combinations easily.
•R-2R Ladder DAC contains only two values of resistor: R and 2R. So, it is easy to select and design more accurate
resistors.
•If more number of bits are present in the digital input, then we have to include required number of R-2R sections
additionally.
Due to the above advantages, R-2R Ladder DAC is preferable over binary weighted resistor DAC.
Introduction of SPI Communication Protocol
•The SPI Interface was developed by Motorola in late 1980 and it is the most popular
serial synchronous bus protocol for short-distance communication.
•Sometimes, SPI is called a four-wire serial bus and each bus has a specific role and
importance. SPI works in full-duplex mode, which means it can receive and send data at
a time.
SPI is a single master full-duplex communication protocol that means communication
always starts by the master. In SPI communication multi-slave can be attached with a
single master and slave cannot change its role to master.
Each slave has its own slave select pin which is controlled by the master. In the case of
multi-slave, the master selects the slave by pulling down its slave select line (ss). There
is four-wire is used in communication these are MOSI, MISO, SCLK, and SS.
SCLK: Serial Clock (It is produced by the master to start the communication)
MOSI: Master Out, Slave In (This line is used to carry data from the master to the
slave)
MISO: Master in, Slave out (This line is used to carry the data from the slave to the
master)
SS: Slave Select (This line is used to select the slave in case of the multi-slave
communication)
Steps for the SPI communication
Step1: Master pulling down the slave select line of a slave that it
wants to communicate.
Step2: After selecting the slave master start to generates the clock
signal which is shared by the slave. The clock configuration
(polarity and phase) of the master and slave should be the same.
ACK/NACK Bit :
After every data frame, follows an ACK/NACK bit. If the data frame is received successfully
then ACK bit is sent to the sender by the receiver.
Addressing :
The address frame is the first frame after the start bit. The address of the slave with which
the master wants to communicate is sent by the master to every slave connected with it.
The slave then compares its own address with this address and sends ACK.
I2C Packet Format :
In the I2C communication protocol, the data is transmitted in the form of packets. These
packets are 9 bits long, out of which the first 8 bits are put in SDA line and the 9th bit is
reserved for ACK/NACK i.e. Acknowledge or Not Acknowledge by the receiver.
START condition plus address packet plus one more data packet plus STOP
condition collectively form a complete Data transfer.
Interfacing EEPROM with 8051 using I2C
EEPROM
Electrically Erasable Programmable ROM (EEPROM) is a user-modifiable ROM that can be
removed and reprogrammed frequently through the application of higher than the normal
electrical voltage. An EEPROM is a kind of non-volatile memory used in electronic devices
like computers to store small quantities of data that should be saved when power is
detached.
Write Mode
Send the START command from the Master.
Send Device (EEPROM) Address with write mode.
Send Register address in Device (EEPROM), Where we have to access.
Send the Data to the Device (EEPROM).
If you want to send more than one byte, keep sending that byte.
Finally, Send the STOP command.
Read Mode
Send the START command from the Master.
Send Device (EEPROM) Address with write mode.
Send Register address in Device (EEPROM), Where we have to access.
Send again START command or Repeated START command.
Send Device address with Reading mode.
Read the data from Device (EEPROM).
Finally, Send the STOP command.
START Command
1. Initially, SDA and SCL are High.
2.SDA first goes to Zero.
3.Then SCL goes to Zero.
STOP Command
When SCL is High, We have to toggle the SDA Low to High.
Send Data
Sending Data and addresses are the same
procedure. Here we have to send bit by bit to
the SDA based on the SCL.
Read Data
Reading also the same as write
data. We have to read bit by bit
from the SDA line based on
SCL.
Features I2C Communication Protocol SPI Communication Protocol
Number of wires 2 (SDA and SCL) 4 (MOSI, MISO, SCK, and SS)
Zigbee is particularly designed locally for networks in home environment, and it does
not directly communicate with the servers on the internet.
Zigbee devices are needed to send and collect the data back to the managing server
on the internet with an additional mechanism.
For example, a gateway can be placed to connect a ZigBee network to the Internet.
In a ZigBee network, end devices collect data and send data to the gateway, which then
translates the data from the ZigBee protocol format to Internet Protocol format, and vice
versa.
This allows ZigBee devices to communicate with the servers on the Internet.
•ZigBee is a popularly adopted communication technology in smart-grid systems.
•There are three types of devices in a ZigBee network: coordinator, routers, and end devices.
A coordinator is responsible for establishing, maintaining, and controlling a ZigBee network.
•It allocates network addresses to other nodes that join the network successively. Routers,
sometimes called as Relay nodes, take care of data transmission and have the capability to
extend the scope of a ZigBee network.
•End devices collect data and transmit it to routers or coordinators.
HC-05 is a Bluetooth device used for wireless communication. It works on serial
communication (UART).
It is a 6 pin module.
The device can be used in 2 modes; data mode and command mode.
The data mode is used for data transfer between devices whereas command mode is used
for changing the settings of the Bluetooth module.
AT commands are required in command mode.
The module works on 5V or 3.3V. It has an onboard 5V to 3.3V regulator.
As the HC-05 Bluetooth module has a 3.3 V level for RX/TX and the microcontroller can
detect 3.3 V level, so, no need to shift the transmit level of the HC-05 module. But we need
to shift the transmit voltage level from the microcontroller to RX of the HC-05 module.