8051 Document
8051 Document
Learn-Practice-Grow
www.pantechsolutions.net
8/19/2021
1
Contents
Introduction to 8051 Controller ..........................................................................................................2
Getting Started with 8051 and Keil IDE ...............................................................................................7
Introduction......................................................................................................................................7
8051 UART ....................................................................................................................................... 16
8051 serial interrupt ....................................................................................................................... 22
8051 Power down and idle mode ..................................................................................................... 24
8051 Timers ..................................................................................................................................... 29
Timer interrupt in 8051 .................................................................................................................. 41
8051 Interrupts ................................................................................................................................ 44
LCD16x2 interfacing in 4-bit mode with 8051 .................................................................................... 51
4-bit Mode....................................................................................................................................... 52
HC-05 Bluetooth Module Interfacing with 8051 ................................................................................ 59
2
Microcontroller consists of all features that are found in microprocessors with additional built-in ROM,
RAM, I/O ports, Serial ports, Timers, Interrupts, and Clock circuits. It is an entire computer on a single
chip that is embedded within applications. Microcontrollers are widely used in many domestic (washing
machines, VCD players, microwave oven, robotics, etc.) as well as industrial and automobile areas.
The 8051 is the first microcontroller of the MCS-51 family developed by Intel Corporation in 1980. It was
developed using N-type Metal-Oxide-Semiconductor (NMOS) technology and later it came to be
identified by a letter C in their names e.g. 80C51 which was developed with Complementary Metal-
Oxide-Semiconductor (CMOS) technology which consumes less power than NMOS and made it better
compatible for battery-powered applications.
Microcontrollers can be classified on the basis of their bit processing capability e.g. 8-bit microcontroller
means it can read, write, and process 8-bit data. Basically, it specifies the size of the data bus. Today
microcontrollers are designed with much more compact, cheap, and powerful
specifications like AVR and PIC.
All these peripherals are integrated together to form a controlling unit ready to embed within
applications.
3
Microcontroller
Whereas microcontroller has all memories and ports available on-chip as shown in the figure. This
makes microcontrollers the most popular. Later many semiconductor companies developed their own
microcontrollers with different specifications.
Find below, the specifications for various popular 8051 family members developed by mentioned
semiconductor companies.
8051 128 4 2 32 1 6
AT89C51 128 4 2 32 1 6
Atmel
AT89C52 256 8 3 32 1 6
DS5000-8 128 8 2 32 1 6
Dallas
DS5000-32 128 32 2 32 1 6
8051 Architecture
All 8051 microcontrollers have unique architecture as shown in the figure, which consists of functional
blocks to build 8051 powerful controlling machines.
CPU
Microcontroller 8051 has a central processing unit which is also called ALU (Arithmetic Logic Unit) which
performs all arithmetic and logical operation.
Interrupts
• Interrupts are requested by internal or external peripherals which are masked while
unused.
• Interrupt handler routines are called after each interrupts event occurs.
• These routines are called an Interrupt Service Routine (ISR) and are located in special
memory loc.
• INT0 and INT1 pins used to accept external interrupts.
Oscillator
6
• It is used to provide a clock to the 8051 which decides the speed and baud rate.
• We use crystals of frequency varying from 4MHz to 30 MHz normally we use 11.0592
MHz frequency which is required for 9600 baud rate in serial communication.
I/O Ports
• P0 and P2 can’t be used as I/O pins in the external memory access operation
• 8051 has two serial communication pins TXD and RXD used for transmitting and receive
data serially via the SBUF register
• SCON SFR used to control serial operation
7
• Assembly language
• C language
8051 microcontroller popular development IDE is MCU 8051 IDE and µVision to develop code.
• C Compiler - C51.Exe
• Assembler - A51.Exe
• Linker/Locator - BL51.Exe
• Librarian - LIB51.Exe
• Hex Converter - OH51.Exe
Let’s develop a simple LED blinking program using Keil µVision IDE with the C51 compiler.
Here we are using the AT89S52 microcontroller from 8051 families.
Click on the Project menu and select the new µVision Project…
“Create New Project” window will pop up, type a project name and location for the project and
save.
9
“Select Device for Target” window will pop up, select your device (here we selecting AT89S52)
“µVision” window will ask for copy STARTUP.A51 to the project folder and add a file to the
project (here is not necessary so we have selected No)
10
Now select New file from the File menu and type your program code (here we have typed LED
blinking program)
Save program code with “ .c “ extension (In case if you are using assembly language then save
program code with “ .asm “ extension)
11
Right-click on Source Group 1 folder from Target 1 and select “Add existing files to Group
‘Source Group 1’”
12
Select the program file saved with “.c “or “.asm” (in case of assembly language) and add it. Then
close that window. You can see the added file in the “Source Group 1” folder in the left project
window
Now select the Project menu and click on “Build target”, it will build a project and give status in
the Build output window with Error and Warning count if any.
13
To create a Hex file right click on Target 1 and select Option for Target ‘Target 1’
The target window will pop up, enter Xtal (MHz) frequency (here we used 11.0592 MHz), and
tick mark in front of the “Use On-chip ROM” tag.
14
Within the same window select the “Output” option and tick mark in front of the “Create Hex
File” tag and click on OK.
Now again select build target from the Project menu or simply hit the F7 shortcut key for the
same, it will build target and also create a Hex file. You can see creating a Hex file in the Build
output window
15
The created Hex file is burned into a microcontroller flash memory using various programming
methods based on the programmer or programmer developed by the manufacturer itself.
For example, Flash magic is used for NXP Philips microcontrollers only and is developed by
NXP itself. Other manufacturers use serial programmer like an ISP programmer to flash their
controllers.
Now load the generated Hex file in one of any programmers available and flash it in your 8051
Microcontroller.
8051 UART
Introduction
Serial communication means to transfer data bit by bit serially at a time, whereas in parallel
communication, the number of bits that can be transferred at a time depends upon the number of
data lines available for communication.
8051 has built-in UART with RXD (serial data receive pin) and TXD (serial data transmit pin)
on PORT3.0 and PORT3.1 respectively.
Asynchronous communication
Asynchronous serial communication is widely used for byte-oriented transmission.
• START bit: It is a bit with which serial communication starts and it is always low.
• Data bits packet: Data bits can be 5 to 9 bits packet. Normally we use 8 data bit packet,
which is always sent after the START bit.
• STOP bit: This is one or two bits. It is sent after the data bits packet to indicate the end
of the frame. The stop bit is always logic high.
17
In an asynchronous serial communication frame, the first START bit followed by the data byte
and at last STOP bit forms a 10-bit frame. Sometimes the last bit is also used as a parity bit.
The data transmission rate is measured in bits per second (bps). In the binary system, it is also
called a baud rate (number of signal changes per second). Standard baud rates supported are
1200, 2400, 4800, 19200, 38400, 57600, and 115200. Normally most of the time 9600 bps is
used when speed is not a big issue.
Interface standard
• 8051 serial communication has TTL voltage level which are 0 v for logic 0 and 5 v for
logic 1.
• In computers and most of the old devices for serial communication, RS232 protocol with
DB9 connector is used. RS232 serial communication has different voltage levels than
8051 serial communication. i.e. +3 v to +25 v for logic zero and -3 v to -25 v for logic 1.
• So to communicate with RS232 protocol, we need to use a voltage level converter like
MAX232 IC.
• Although there are 9 pins in the DB9 connector, we don’t need to use all the pins. Only
2nd Tx(Transmit), 3rd Rx(Receive), and 5th GND pin need to be connected.
18
• To meet the standard baud rates generally crystal with 11.0592 MHz is used.
• As we know, 8051 divides crystal frequency by 12 to get a machine cycle frequency of
921.6 kHz.
• The internal UART block of 8051 divides this machine cycle frequency by 32, which
gives the frequency of 28800 Hz which is used by UART.
• To achieve a baud rate of 9600, again 28800 Hz frequency should be divided by 3.
• This is achieved by using Timer1 in mode-2 (auto-reload mode) by putting 253 in TH1
(8-bit reg.)
• So 28800 Hz will get divided by 3 as the timer will overflow after every 3 cycles.
• we can achieve different baud rates by putting the division factor in the TH1 register.
19
9600 FD
4800 FA
2400 F4
1200 E8
This is the serial communication data register used to transmit or receive data through it.
Serial control register SCON is used to set serial communication operation modes. Also it is used
to control transmit and receive operations.
Normally mode-1 (SM0 =0, SM1=1) is used with 8 data bits, 1 start bit, and 1 stop bit.
20
1 = Receive enable
0 = Receive disable
This is the 9th bit which is to be transmitted in mode 2 & 3 (9-bit mode)
This is the 9th received bit in mode 2 & 3 (9-bit mode), whereas in mode 1 if SM2 = 0 then RB8
hold the stop bit that received
This bit indicates the transmission is complete and gets set after transmitting the byte from the
buffer. Normally TI (Transmit Interrupt Flag) is set by hardware at the end of the 8th bit in mode
0 and at the beginning of stop bit in other modes.
This bit indicates reception is complete and gets set after receiving the complete byte in the
buffer. Normally RI (Receive Interrupt Flag) is set by hardware in receiving mode at the end of
the 8th bit in mode 0 and at the stop bit receive time in other modes.
Programming steps
Example
Let's Program 8051 (here AT89C51) to send character data “test” serially at 9600 baud rate in
mode 1 Program for serial data transmit
8051 UART has a serial interrupt. Whenever data is transmitted or received, serial interrupt flags
TI and RI are activated respectively.
8051 serial interrupt has a vector address (0023H) where it can jump to serve ISR (Interrupt
service routine) if the global and serial interrupt is enabled.
Let's see how the serial interrupt routine will be used in serial communication programming.
Programming steps
Note: For transmission and reception interrupt, the same interrupt vector address is assigned, so
when the controller jumps to the ISR, we have to check whether it is Tx interrupt or Rx interrupt
by TI and RI bits status.
Example
Let's Program 8051 (here AT89C51) to receive character data serially at 9600 baud rate in mode
1 and send received data on port 1 using a serial interrupt.
• In this example, after a byte is received, the RI flag is set which will generate a serial
interrupt.
• After the interrupt, 8051 will jump to the serial ISR routine.
• In the ISR routine, we will send data to port 1.
23
Power down and idle mode features are used to save power in microcontrollers. 8051 has an
inbuilt power-saving feature which is useful in embedded applications where power consumption
is the main constraint.
As shown in the above figure of 8051 power control logic, two control bits are there, IDL and
PD, which are used for Idle and Power-down mode respectively.
In Power Down mode, the oscillator clock provided to the system is OFF i.e. CPU and
peripherals clock remains inactive in this mode.
In Idle Mode, only the clock provided to the CPU gets deactivated, whereas the peripherals
clock will remain active in this mode.
25
The below table shows the power supply current required for 8051 family controllers in Normal
(Active), Idle and Power-down mode.
Operating
Oscillator Current Current required
8051 Current required
Frequency required in Idle in Power Down
Controllers in Normal mode
mode mode
Fosc.
As per the above table, it is clear that power consumption in power-down mode is less than in
Normal or idle mode.
8051 has a power control register for power control. Let's see the power control register.
PCON (Power Control) register is used to force the 8051 microcontrollers into power-saving
mode. The power control register of 8051 contains two power-saving mode bits and one serial
baud rate control bit.
Bit 7 – SMOD
1 = Enable Power-Down mode. In this mode, the Oscillator clock turned OFF and both CPU
and peripherals clock stopped. Hardware reset can cancel this mode.
1 = Enable Idle mode. CPU clock turned off whereas internal peripheral module such as a
timer, serial port, interrupts works normally. Interrupt and H/W reset can cancel this mode.
Example
Let’s program the AT89C51 microcontroller to toggle Pin 0 of port 1 and force the
microcontroller in Idle (sleep) mode by external interrupt1. We wake the microcontroller in
normal mode by external interrupt0.
Programming steps
Here, sleep mode is enabled in external1 interrupt ISR and disabled in external0 interrupt ISR.
27
To enable Power-down mode, set PD bit i.e. PCON = 0x02. Also, note that only hardware reset
can cancel this mode.
“The only exit from Power Down for the 80C51is hardware reset. Reset redefines all the SPRS,
but does not change the on-chip RAM.”
But according to AT89s51 datasheet:
“Exit from Power-down mode can be initiated either by a hardware reset or by activation of an
enabled external interrupt (INT0 or INT1). Reset redefines the SFRs but does not change the on-
chip RAM.”
So AT89s51 controller can exit from power-down mode by reset as well as by external interrupts
also.
29
8051 Timers
Introduction
Every Timer needs a clock to work, and 8051 provides it from an external crystal which is the
main clock source for Timer. The internal circuitry in the 8051 microcontrollers provides a clock
source to the timers which are 1/12th of the frequency of crystal attached to the microcontroller,
also called Machine cycle frequency.
For example, suppose we have a crystal frequency of 11.0592 MHz then the microcontroller will
provide 1/12th i.e.
8051 has two timers Timer0 (T0) and Timer1 (T1), both are 16-bit wide. Since 8051 has 8-bit
architecture, each of these is accessed by two separate 8-bit registers as shown in the figure
below. These registers are used to load timer count.
30
8051 has a Timer Mode Register and Timer Control Register for selecting a mode of operation
and controlling purpose.
TMOD register
TMOD is an 8-bit register used to set timer mode of timer0 and timer1.
Its lower 4 bits are used for Timer0 and the upper 4 bits are used for Timer1
1 = Enable Timer/Counter only when the INT0/INT1 pin is high and TR0/TR1 is set.
1 = Use as Counter
0 = Use as Timer
These are Timer/Counter mode select bit as per the below table
31
M1 M0 Mode Operation
0 0 0 (13-bit timer mode) 13-bit timer/counter, 8-bit of THx & 5-bit of TLx
Split the 16-bit timer into two 8-bit timers i.e. THx
1 1 3 (split timer mode)
and TLx like two 8-bit timer
TCON Register
TCON is an 8-bit control register and contains a timer and interrupt flags.
1 = Timer1 overflow occurred (i.e. Timer1 goes to its max and roll over back to zero).
It is cleared through software. In the Timer1 overflow interrupt service routine, this bit will get
cleared automatically while exiting from ISR.
1 = Timer1 start.
0 = Timer1 stop.
1 = Timer0 overflow occurred (i.e. Timer0 goes to its max and roll over back to zero).
It is cleared through software. In the Timer0 overflow interrupt service routine, this bit will get
cleared automatically while exiting from ISR.
32
1 = Timer0 start.
0 = Timer0 stop.
Timer Modes
Timers have their operation modes which are selected in the TMOD register using M0 & M1 bit
combinations.
Mode 0 (13-bit timer mode)
Mode 0 is a 13-bit timer mode for which 8-bit of THx and 5-bit of TLx (as Prescaler) are used. It
is mostly used for interfacing possible with old MCS-48 family microcontrollers.
33
As shown in the above figure, 8-bit of THx and lower 5-bit of TLx used to form a total 13-bit
timer. Higher 3-bits of TLx should be written as zero while using timer mode0, or it will affect
the result.
Example
Let's generate a square wave of 2mSec period using an AT89C51 microcontroller with timer0 in
mode0 on the P1.0 pin of port1. Assume xtal oscillator frequency of 11.0592 MHz.
As the Xtal oscillator frequency is 11.0592 MHz we have a machine cycle of 1.085uSec. Hence,
the required count to generate a delay of 1mSec. is,
The maximum count of Mode0 is 2^13 (0 - 8191) and the Timer0 count will increment from 0 –
8191. So we need to load value which is 921 less from its maximum count i.e. 8191. Also, here
in the below program, we need an additional 13 MC (machine cycles) from call to return of delay
function. Hence value needed to be loaded is,
1C74 = 0001 1100 0111 0100 b, now load lower 5-bit in TL0 and next 8-bit in TH0
so here we get,
1. Load Tmod register value i.e. TMOD = 0x00 for Timer0/1 mode0 (13-bit timer mode).
2. Load calculated THx value i.e. here TH0 = 0xE3.
3. Load calculated TLx value i.e. here TL0 = 0x14.
4. Start the timer by setting a TRx bit. i.e. here TR0 = 1.
5. Poll TFx flag till it does not get set.
6. Stop the timer by clearing TRx bit. i.e. here TR0 = 0.
7. Clear timer flag TFx bit i.e. here TF0 = 0.
8. Repeat from step 1 to 7 for the delay again.
35
void main()
while(1)
}
36
Mode 1 is a 16-bit timer mode used to generate a delay, it uses 8-bit of THx and 8-bit of TLx to
form a total 16-bit register.
Example
Let’s generate a square wave of 2mSec time period using an AT89C51 microcontroller with
timer0 in mode1 on the P1.0 pin of port1. Assume Xtal oscillator frequency of 11.0592 MHz.
And mode1 has a max count is 2^16 (0 - 65535) and it increments from 0 to 65535 so we need to
load value which is 921 less from its max. count i.e. 65535. Also, here in the below program, we
need an additional 13 MC (machine cycles) from call to return of delay function. Hence value
needed to be loaded is,
So we need to load FC74 Hex value higher byte in TH0 and lower byte in TL0 as,
1. Load Tmod register value i.e. TMOD = 0x01 for Timer0 mode1 (16-bit timer mode).
2. Load calculated THx value i.e. here TH0 = 0xFC.
3. Load calculated TLx value i.e. here TL0 = 0x74.
4. Start the timer by setting a TRx bit. i.e. here TR0 = 1.
5. Poll TFx flag till it does not get set.
6. Stop the timer by clearing TRx bit. i.e. here TR0 = 0.
7. Clear timer flag TFx bit i.e. here TF0 = 0.
8. Repeat from step 1 to 7 for the delay again.
38
void main()
while(1)
}
39
Mode 2 is an 8-bit auto-reload timer mode. In this mode, we have to load the THx-8 bit value
only. When the Timer gets started, the THx value gets automatically loaded into the TLx and
TLx starts counting from that value. After the value of TLx overflows from the 0xFF to 0x0, the
TFx flag gets set and again value from the THx gets automatically loaded into the TLx register.
That’s why this is called the auto-reload mode.
Example
Here we are generating a square wave on PORT1.0 with 200uSec. time period using Timer1 in
mode2. We will use 11.0592 MHz Xtal oscillator frequency.
As Xtal is 11.0592 MHz we have a machine cycle of 1.085uSec. Hence, the required count to
generate a delay of 1mSec. is,
And mode2 has a max count is 2^8 (0 - 255) and it increment from 0 – 255 so we need to load
value which is 92 less from its max. count i.e. 255. Hence value need to be load is,
TH1 = 0xA4
40
1. Load Tmod register value i.e. TMOD = 0x20 for Timer1 mode2 (8-bit timer auto reload
mode).
2. Load calculated THx value i.e. here TH1 = 0xA4.
3. Load same value for TLx i.e. here TL1 = 0xA4.
4. Start the timer by setting a TRx bit. i.e. here TR1 = 1.
5. Poll TFx flag till it does not get set.
6. Clear timer flag TFx bit i.e. here TF1 = 0.
7. Repeat from step 5 and 6 for the delay again.
void main()
while(1)
}
41
The timer overflow interrupt assigned with the vector address shown in the table. 8051
microcontroller jumps directly to the vector address on the occurrence of a corresponding
interrupt.
Example
Here we will generate a square wave of 10Hz on PORT1.0 using Timer0 interrupt. We will use
Timer0 in mode1 with 11.0592 MHz oscillator frequency.
42
As Xtal is 11.0592 MHz we have a machine cycle of 1.085uSec. Hence, the required count to
generate a delay of 50mSec. is,
And mode1 has a max count is 2^16 (0 - 65535) and it increment from 0 – 65535 so we need to
load value which is 46080 less from its max. count i.e. 65535. Hence value need to be load is,
So we need to load 4C00 Hex value in a higher byte and lower byte as,
Note that the TF0 flag no need to clear by software as a microcontroller clears it after completing
the ISR routine.
43
void Timer_init()
TL0 = 0x00;
TL0 = 0x00;
int main(void)
Timer_init();
while(1);
}
44
8051 Interrupts
Introduction
An interrupt is an event that occurs randomly in the flow of continuity. It is just like a call you
have when you are busy with some work and depending upon call priority you decide whether to
attend or neglect it.
The same thing happens in microcontrollers. 8051 architecture handles 5 interrupt sources, out of
which two are internal (Timer Interrupts), two are external and one is a serial interrupt. Each of
these interrupts has its interrupt vector address. The highest priority interrupt is the Reset, with
vector address 0x0000.
Vector Address: This is the address where the controller jumps after the interrupt to serve the
ISR (interrupt service routine).
Reset - 0000H
Reset
Reset is the highest priority interrupt, upon reset 8051 microcontroller start executing code from
0x0000 addresses.
8051 has two internal interrupts namely timer0 and timer1. Whenever timer overflows, timer
overflow flags (TF0/TF1) are set. Then the microcontroller jumps to their vector address to serve
the interrupt. For this, global and timer interrupt should be enabled.
Serial interrupt
45
8051 has a serial communication port and have related serial interrupt flags (TI/RI). When the
last bit (stop bit) of a byte is transmitted, the TI serial interrupt flag is set, and when the last bit
(stop bit) of the receiving data byte is received, the RI flag gets set.
Priority to the interrupt can be assigned by using the interrupt priority register (IP)
In the table, interrupts priorities upon reset are shown. As per 8051 interrupt priorities, the lowest
priority interrupts are not served until the microcontroller is finished with higher priority ones. In
a case when two or more interrupts arrive microcontroller queues them according to priority.
Whenever a low level is detected on the INT0/INT1 pin while global and external
interrupts are enabled, the controller jumps to interrupt service routine (ISR) to serve interrupt.
Whenever falling edge is detected on the INT0/INT1 pin while global and ext. interrupts
are enabled, the controller jumps to interrupt service routine (ISR) to serve interrupt.
There are lower four flag bits in the TCON register required to select and monitor the external
interrupt type and ISR status.
Bit 3- IE1:
External Interrupt 1 edge flag, set by hardware when interrupt on INT1 pin occurred and
cleared by hardware when interrupt get processed.
Bit 2- IT1:
This bit selects the external interrupt event type on INT1 pin,
Bit 1- IE0:
Interrupt0 edge flag, set by hardware when interrupt on INT0 pin occurred and cleared by
hardware when an interrupt is processed.
Bit 0 - IT0:
This bit selects the external interrupt event type on the INT0 pin.
Example
Let’s program the external interrupt of AT89C51 such that, when falling edge is detected on the
INT0 pin then the microcontroller will toggle the P1.0 pin.
Programming steps
Program
void Ext_int_Init()
{
EA = 1; /* Enable global interrupt */
EX0 = 1; /* Enable Ext. interrupt0 */
IT0 = 1; /* Select Ext. interrupt0 on falling edge */
}
void main()
{
Ext_int_Init(); /* Call Ext. interrupt initialize */
while(1);
}
Note: For level triggered interrupt IT0 needs to be cleared i.e. IT0 = 0.
51
LCDs (Liquid Crystal Displays) are used for displaying status or parameters in embedded
systems.
LCD 16x2 is 16 pin device which has 8 data pins (D0-D7) and 3 control pins (RS, RW, EN). The
remaining 5 pins are for supply and backlight for the LCD.
The control pins help us configure the LCD in command mode or data mode. They also help
configure read mode or write mode and also when to read or write.
LCD 16x2 can be used in 4-bit mode or 8-bit mode depending on the requirement of the
application. In order to use it, we need to send certain commands to the LCD in command mode
and once the LCD is configured according to our need, we can send the required data in data
mode.
For more information about LCD 16x2 and how to use it, refer to the topic LCD 16x2 display
module in the sensors and modules section.
52
4-bit Mode
Interfacing Diagram
53
Initialization
Now we successfully initialized LCD & it is ready to accept data in 4-bit mode to display.
To send command/data to 16x2 LCD we have to send a higher nibble followed by a lower
nibble. As 16x2 LCD’s D4 - D7 pins are connected as data pins, we have to shift the lower
nibble to the right by 4 before transmitting.
delay(2);
LCD_Port = (LCD_Port & 0x0F) | (cmnd << 4);/* Send lower nibble */
en=1; /* Enable pulse */
delay(1);
en=0;
delay(5);
}
Data write function
Program
#include<reg51.h>
int i,j;
for(i=0;i<count;i++)
for(j=0;j<112;j++);
LCD_Port =(LCD_Port & 0x0F) | (cmnd & 0xF0); /* sending upper nibble */
rs=0;
/* command reg. */
rw=0;
/* Write operation */
en=1;
delay(1);
en=0;
delay(2);
LCD_Port = (LCD_Port & 0x0F) | (cmnd << 4); /* sending lower nibble */
delay(1);
en=0;
delay(5);
LCD_Port =(LCD_Port & 0x0F) | (char_data & 0xF0); /* sending upper nibble */
57
en=1;
LCD_Port = (LCD_Port & 0x0F) | (char_data << 4); /* sending lower nibble */
int i;
void LCD_String_xy (char row, char pos, char *str) /* Send string to LCD function */
if (row == 0)
else if (row == 1)
void main()
}
59
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.
For more information about the HC-05 Bluetooth module and how to use it, refer to the
topic Bluetooth module HC-05 in the sensors and modules section.
For information on UART in 8051 and how to use it, refer to the topic on UART in 8051 in the
8051 inside section.
Interfacing Diagram
Example
Here let’s develop a small application in which we can control LED ON-OFF through
smartphones.
This is done by interfacing 8051 with the HC-05 Bluetooth module. Data from HC-05 is
received/ transmitted serially by 8051.
In this application when 1 is sent from the smartphone, LED will turn ON and if 2 is sent LED
will get Turned OFF. If received data is other than 1 or 2, it will return a message to mobile that
select the proper option.
Program
61
#include <reg51.h>
#include "UART_H_file.h" /* Include UART library */
sbit LED=P1^0;
void main()
{
char Data_in;
UART_Init(); /* Initialize UART */
P1 = 0; /* Clear port initially */
LED = 0; /* Initially LED turn OFF */
while(1)
{
Data_in = UART_RxChar(); /* Receive char serially */
if(Data_in == '1')
{
LED = 1;/* Turn ON LED */
UART_SendString("LED_ON"); /* Send status of LED*/
}
else if(Data_in == '2')
{
LED = 0;/* Turn OFF LED */
UART_SendString("LED_OFF"); /* Send status of LED*/
}
else
UART_SendString("Select proper option");
}
}