EE3017 Embedded C Programming Lab Manual
EE3017 Embedded C Programming Lab Manual
E
CO
CE
LAB MANUAL
Department of Electrical and Electronics Engineering
RA
EC3017 _ECP
4931_Grace college of Engineering,Thoothukudi
E
CO
CE
Name: ……………………………………………………………
EC3017 _ECP
4931_Grace college of Engineering,Thoothukudi
E
CO
CE
BONAFIDE CERTIFICATE
RA
Mr/Ms............................................................................. of V Semester in
Department of
Electrical and Electronics Engineering of the College during the year July 2023
– Dec 2023
Submitted for the Anna University B.E. (EEE) Degree Practical Examination
held at Grace College of Engineering on................................
Internal
EC3017 _ECP Examiner External Examiner
4931_Grace college of Engineering,Thoothukudi
E
3. Configuring timer for generating hardware delay.
4. Flashing an LED using an interrupt
5. Serial communication using UART port of 8051
6. Interfacing an ADC with 8051
CO
7. Interfacing an analog sensor with 8051
CE
OUTCOMES:
At the end of the course, the student should have the :
CO1: Deliver insight into embedded C programming and its salient features for embedded systems.
CO2: Illustrate the software and hardware architecture for distributed computing in embedded systems
CO3: Develop a solution for problems by using the conceptlearnednt in programming using the embedded
controllers
CO4: Develop simple applications with 8051 by using its various features and interfacing with various external
hardware.
CO5: Improved Employability and entrepreneurship capacity due to knowledge upgradation on recent trends in
embedded programming skills.
EC3017 _ECP
4931_Grace college of Engineering,Thoothukudi
LIST OF EXPERIMENTS
CYCLE – I
11. Introduction to IDE (like code blocks, vscode , etc)and Programming Environment (like Keil uvision,
Proteus)
2. Configuring an I/O port using bitwise programming.
3. Configuring timer for generating hardware delay.
4. Flashing an LED using an interrupt
5. Serial communication using UART port of 8051
E
CYCLE – II
TOTAL: 45 PERIODS
G
EC3017 _ECP
4931_Grace college of Engineering,Thoothukudi
INDEX
SL. PAGE SIGNAT
TITLE OF EXPERIMENT DATE MARKS
NO. NO. URE
E
CO
CE
RA
G
6
EC3017 _ECP
4931_Grace college of Engineering,Thoothukudi
E
8051, 80528751, AT8951 from Atmel Corporation and many more. In this manual we will study
about the 8051 architecture, its features, programming and interfacing. MCS 8051 is an 8-bit
7
EC3017 _ECP
4931_Grace college of Engineering,Thoothukudi
E
CO
CE
2. Close any previous projects that were opened using – Project -> Close.
3. Start Project – New Project, and select the CPU from the device database (Database-Atmel-
).On clicking ‘OK’, the following option is displayed. Choose ‘No’.
G
4. Create a source file (using File->New), type in the assembly or C program and save this
([Link]/filename.c) and add this source file to the project using either one of the following
two methods.
(i) Project->Manage->Components, Environment Books->add files-> browse to the required file
-> OK “OR”
ii) right click on the Source Group in the Project Window and the Add Files to Group option.
5. Set the Target options using -> Project – Options for Target opens the µ Options for Target –
Target configuration dialog. Set the Xtal (Crystal frequency) frequency as 11.0592 MHz, and
also the Options for Target – Debug – use either Simulator / Keil Monitor- 51 driver.
6. If Keil Monitor- 51 driver is used click on Settings -> COM Port settings select the COM Port
to which the board is connected and select the baud rate as 19200 or 9600 (recommended).
Enable Serial Interrupt option if the user application is not using on-chip UART, to stop program
execution.
7. Build the project; using Project -> Build Project. application and links. Any errors in the code
are indicated by – “Target not created” in the Build window, along with the error line. Debug the
8
EC3017 _ECP
4931_Grace college of Engineering,Thoothukudi
E
Keil development tools for the 8051 microcontroller family support every level of developer from
CO
the professional applications engineer to the student just learning about embedded software
development. The industry-standard Keil C Compilers, Macro Assemblers, Debuggers, Real-time
Kernels, and Single-board Computers support ALL 8051-compatible derivatives and help you get
your projects completed on schedule.
CE
The following table shows the Keil C51 Product Line (across the top) and the Components that are
included (along the left side). You may use this information to find the development tool kit that
best fits your needs.
RA
Introduction
The C51 development tool chains are designed for the professional software developer, but any
G
level of programmer can use them to get the most out of the 8051 microcontroller architecture.
With the C51 tools, embedded applications can be generated for virtually every 8051 variant. Refer
to the µ Vision Device Database for a list of currently supported microcontrollers.
Software Development Cycle that describes the steps and tools involved to create a
project.
Development Tools that describes the major features of the Keil C51 development tools
including the µ Vision IDE and Debugger.
Folder Structure that describes the default location of µ Vision and the C51 tool chain
installation
9
EC3017 _ECP
4931_Grace college of Engineering,Thoothukudi
Development Tools
The Keil C51 development tools offer numerous features and advantages that help you to develop
embedded applications quickly and successfully. Find out more about the supported devices and
the possible tool combinations available for the different 8051 variants.
The following block diagram shows the components involved in the build process.
The µ Vision IDE is a window-based software development tool that combines project management
and a rich-featured editor with interactive error correction, option setup, make facility, and on-line
help. Use µ Vision to create source files and organize them into a project that defines your target
application.
E
CO
CE
Fig: C Compiler IDE
RA
The Keil Cx51 Compiler is a full ANSI implementation of the C programming language and
supports all standard features of the C language. In addition, numerous extensions have been
included to directly support the 8051 and extended8051 architecture.
G
Macro Assembler
The Keil Ax51 Macro Assembler supports the complete instruction set of the 8051 and all 8051
derivatives.
Library Manager
The LIBx51 Library Manager allows you to create the object library from object files created by
the compiler and assembler. Libraries are specially formatted, ordered program collections of
object modules that may be used by the linker at a later time. When the linker processes a library,
only those object modules necessary to create the program are used.
Linker/Locater
The Lx51 Linker/Locater creates the final executable 8051 program and combines the object files
created by the compiler or assembler, resolves external and public references, and assigns absolute
addresses. In addition, it selects and includes the appropriate run-time library modules.
10
EC3017 _ECP
4931_Grace college of Engineering,Thoothukudi
µVision Debugger
The µ Vision Debugger is ideally suited for fast and reliable program debugging. The debugger
includes a high-speed simulator capable of simulating an entire 8051 system including on-chip
peripherals and external hardware.
The µ Vision Debugger provides several ways to test programs on target hardware:
Use the Keil ULINK USB-JTAG adapter for downloading and testing your
program.
Install a target monitor on your target system and download your program
using the built-in monitor interface of the µ Vision Debugger.
Use the Advanced GDI interface to attach and use the µ VisionDebugger
front end with your target system.
RTOS Kernel
The RTOS Kernel, describes the advantages of using a real-time kernel like the Keil RTX51 Tiny
E
in embedded systems.
Start Debugging
CO
µVision provides several ways to invoke debugging commands:
Use the Start/Stop Debug Session button from the Debug Toolbar to start or stop a
debugging session.
The current instruction or high-level statement (the one about to execute) is marked with
a yellow arrow. For each step-command, the arrow moves to reflect the new current line
or instruction.
Depending on the Options for Target — Debug configuration, µ Vision loads the
application program and runs the startup code (Run to main ()).
µVision saves the editor screen layout and restores the screen layout of the last debug
session. When program execution stops, µ Vision opens an Editor window with the source
text or shows MCU instructions in the Disassembly Window.
Execute Commands
Run the program to the next break point, or type GO in the Command Line.
Halt the program, or press Esc while in the Command Line
11
EC3017 _ECP
4931_Grace college of Engineering,Thoothukudi
Click Reset from the Debug Toolbar or from the Debug — Reset CPU Menu or
type RESET in the Command Line to reset the CPU.
Single-Stepping Commands
To step through the program and into function calls. Alternatively, you can enter TSTEP in
the Command Line, or press F11.
To step over the program and over function calls. Alternatively, you canenter PSTEP in
the Command Line, or press F10.
To step out of the current function. Alternatively, you can enter OSTEP in the
Command Line, or press Ctrl+F11.
On-Chip Peripherals
There are a number of techniques you must know to create programs that can use the various on-
chip peripherals and features of the 8051 family. Use the code examples provided here to get
E
started working with the 8051.
CO
There is no single standard set of on-chip peripherals for the 8051 family. Instead, 8051 chip
vendors use a wide variety of on-chip peripherals to distinguish their parts from each other. The
code examples demonstrate how to use the peripherals of a particular chip or family. Be aware that
there are more configuration options available than are presented in this text.
Follow the links to the on-chip peripherals:
CE
Header Files - use the include files to define peripheral registers of thedevice in use.
Startup Code - initializes the microcontroller and transfers control to the
RA
main function.
Special Function Registers - explains how to use Special Function Registers
(SFRs).
Register Banks - explains how to use Register Banks.
G
Interrupt Service Routines - lists the different interrupt variants on 8051 devices.
Interrupt Enable Registers - shows how to enable the interrupts.
Parallel Port I/O - explains how to use standard I/O ports.
Timers/Counters - explains standard timers and counters.
Serial Interface - explains the implementation of serial UART
communication.
12
EC3017 _ECP
4931_Grace college of Engineering,Thoothukudi
E
Create new u vision project CO
CE
RA
G
13
EC3017 _ECP
4931_Grace college of Engineering,Thoothukudi
E
Save the project CO
CE
RA
G
14
EC3017 _ECP
4931_Grace college of Engineering,Thoothukudi
E
Addition of STARTUP.A51 to project folder CO
CE
RA
G
STARTUP.A51 is added
15
EC3017 _ECP
4931_Grace college of Engineering,Thoothukudi
E
CO
The program to print “welcome to ECE department” is written
CE
RA
G
16
EC3017 _ECP
4931_Grace college of Engineering,Thoothukudi
E
Prg3.c is to be added to Source Group1 CO
CE
RA
G
17
EC3017 _ECP
4931_Grace college of Engineering,Thoothukudi
E
Now prg3.c is added to Source Group 1 CO
CE
RA
G
18
EC3017 _ECP
4931_Grace college of Engineering,Thoothukudi
E
Debugging the target CO
CE
RA
G
19
EC3017 _ECP
4931_Grace college of Engineering,Thoothukudi
E
Run the program CO
CE
RA
G
20
EC3017 _ECP
4931_Grace college of Engineering,Thoothukudi
E
Check the output at UART#1 window CO
CE
RA
G
21
EC3017 _ECP
4931_Grace college of Engineering,Thoothukudi
E
Port1 window is displayed with D0 as 0 CO
CE
RA
G
22
EC3017 _ECP
4931_Grace college of Engineering,Thoothukudi
E
Stop debugging process CO
CE
RA
G
23
EC3017 _ECP
4931_Grace college of Engineering,Thoothukudi
Date:
Aim:
Write a C program to toggle all the bits of P1 continuously.
Software required:-
Keil u vision
Theory:-
This experiment aims to toggle all the bits of Port1 continuously using the software keil u vision 5.
Firstly the header files reg51.H is declared for the intended 8051. Then the main function starts. In
the main function an infinite for loop starts where each time 0x55 followed by 0xAA is sent to Port1
E
and the loop goes on.
Program:-
#include void main()
{
for (;;)
CO
CE
{ p1=0x55;
p1=0xAA;
}
}
RA
Procedure:-
The procedure for the program of storing a data in accumulator is as follows. File ->new -
>Program(code) -> Save -> Add existing files to source group 1 -> Build -> Start Debug -> Run -
>Peripherals -> Port1 -> Debug (step) -> Debug the process
Output :
G
24
EC3017 _ECP
4931_Grace college of Engineering,Thoothukudi
E
CO
CE
RA
Observation:- From the above output it is observed that all the bits of P1 are toggling continuously.
Result:-
The experiment to toggle all the bits of P1 continuously using keil u vision successfully
25
EC3017 _ECP
4931_Grace college of Engineering,Thoothukudi
Date:
Aim of experiment:-
Software required:-
Keil u vision
Theory:-
This experiment aims to toggle bit D0 of Port1 50,000 times using the software keil u vision 5. Firstly
E
the header files reg51.H is declared for the intended 8051. The sbit keyword is a widely used 8051 C
CO
data type designed specifically to access single bits of SFR registers. Here we use sbit toaccess the
individual bits of the Port1. Then the main function starts. In the main function a for loop runs 50000
times where each time D0 of Port1 is made 0 and then 1.
CE
Program:-
RA
unsigned int z;
for (z=0;z<=50000;z++)
{
MYBIT=0;
MYBIT=1;
}
}
26
EC3017 _ECP
4931_Grace college of Engineering,Thoothukudi
Procedure:-
The procedure for the program of storing a data in accumulator is as follows. File ->new -
>Program(code) -> Save -> Add existing files to source group 1 -> Build -> Start Debug -> Run -
>Peripherals -> Port1 -> Debug (step) -> Debug the process
Output:-
E
CO
CE
RA
G
Observation:- From the above output it is observed that the bit D0 of the Port1 (P1.0) is toggled
50,000 times.
Result:- The experiment to toggle bit D0 of port 1 50,000 times using keil u vision successfully
27
EC3017 _ECP
4931_Grace college of Engineering,Thoothukudi
Aim of experiment:-
Write a C program to generate a square wave for 1ms delay.
Software required:-
Keil u vision
Theory:-
Timer interrupt in 8051 Microcontroller. so, now before going into the details, let me first throw
some light on how we are gonna implement this. Timers count from 0 to 255 in 8 bit mode as in 8 bit
255 is the maximum value and when timer hits the 255 number then we say that our timer is
overflowed. Now when timer overflows, then it sends us a indication using which we generate our
intterupt. In timers, there are few registers in which they store their value. If we are talking about
E
Timer0 then timer0 stores its value in TL0 register. Now suppose I want my timer to start counting
CO
from 10 instead 0 then I will store 10 in my TL0 register and it will count from 10 instead 0 and when
it reaches 255 it will overflow. Now when Timer0 will overflow then it will make TF0 bit HIGH.
TF0 is another register value, if its 1 then it means that our timer is full and if its 0 then it means our
timer is still counting. So, that's how we count from our timer and check the pin TF0.
This experiment aims to generate a square wave for 1ms delay using the software keil u vision 5.
CE
There are two ways to create a time delay in 8051 C: 1. Using a simple for loop 2. Using the 8051
timers.
RA
G
28
EC3017 _ECP
4931_Grace college of Engineering,Thoothukudi
Timer1 Timer 0
7 6 5 4 3 2 1 0
Gate C/T M M Gat C/ M M
1 0 e T 1 0
E
TCON (Timer Control register):
CO
TCON is an 8-bit register. Its bits are used for generating interrupts internal or external.
The most important bits of the timer TR and TF are also in it.
TR (timer run) and TF (timer overflow) bits which we use in almost all over timer
applications are in it.
CE
RA
G
Program:-
#include void delay( void );
void main()
{
for(;;)
{ P1=0x00;
29
EC3017 _ECP
4931_Grace college of Engineering,Thoothukudi
delay();
P1=0xFF;
delay();
}
}
void delay(void)
{
TMOD=0x01;
TL0=0X18;
TH0=0XFC;
TR0=1;
while(TF0==0);
TR0=0;
TF0=0;
}
E
Procedure:-
The procedure for the program of storing a data in accumulator is as follows. File new
CO
Program(code) Save Add existing files to source group 1 Write P1 New Set up Analysis
Window Run Start DebugBuild Close Run Debug the process
Output:-
CE
RA
G
Observation:-
From the above output it is observed that the square wave is generated in the analysis window.
Sl No Time Voltage
Result :- The experiment to generate a square wave for 1ms delay using keil u vision is done
successfully
30
EC3017 _ECP
4931_Grace college of Engineering,Thoothukudi
DATE:
Aim: - Write a C Program to print hello world.
Software required: -
Keil u vision 5
Theory: -
Timer interrupt in 8051 [Link] count from 0 to 255 in 8 bit mode as in 8 bit 255 is
the maximum value and when timer hits the 255 number then we say that our timer is overflowed.
Now when timer overflows, then it sends us a indication using which we generate our intterupt. In
timers, there are few registers in which they store their value. If we are talking about Timer0 then
timer0 stores its value in TL0 register. Now suppose I want my timer to start counting from 10
E
instead 0 then I will store 10 in my TL0 register and it will count from 10 instead 0 and when it
CO
reaches 255 it will overflow. Now when Timer0 will overflow then it will make TF0 bit HIGH. TF0
is another register value, if its 1 then it means that our timer is full and if its 0 then it means our timer
is still counting
Program :
CE
// Use of Timer mode 1 for blinking LED with interrupt method
// XTAL frequency 11.0592MHz
#include <reg51.h>
sbit LED = P1^0; // LED connected to 1st pin o f port P1
RA
Output:
Result :- The experiment to generate a square wave for 1ms delay using keil u vision is done
successfully
31
EC3017 _ECP
4931_Grace college of Engineering,Thoothukudi
Software required: -
Keil u vision 5
Theory: -
This experiment aims to print “hello world” using the software keil u vision 5. The program is by
default present after installation of this software. Firstly the header files REG52.H, stdio.h are
declared for the intended 8051. Programming for debugging with Monitor-51is made. Now the
main function starts. The serial port for 1200 baud at 16MHz is set up. An embedded program
E
never exits (because there is no operating system to return to). It must loop and execute forever.
So an infinite loopis made that Toggle P1.0 each time we print "Hello World".
Program:-
#include<reg52.h>
CO
CE
#include <stdio.h>
#ifdef MONITOR51
char code reserve [3] _at_ 0x23;
RA
#endif
void main (void)
{
#ifndef MONITOR51
G
SCON = 0x50;
TMOD |= 0x20; TH1
= 221;
TR1 = 1;
TI = 1;
#endif While
(1) {
P1 ^= 0x01;
printf ("Hello World\n");
}
}
32
EC3017 _ECP
4931_Grace college of Engineering,Thoothukudi
Procedure:-
As said the program for hello world is by default present after the installation of the keil u
vision software, the program is loaded by following certain steps whichis as follows:-
E
CO
CE
RA
G
33
EC3017 _ECP
4931_Grace college of Engineering,Thoothukudi
E
Select Keil_v5 present in Local Disk (C:) CO
CE
RA
G
34
EC3017 _ECP
4931_Grace college of Engineering,Thoothukudi
E
Next select the folder examples
CO
CE
RA
G
35
EC3017 _ECP
4931_Grace college of Engineering,Thoothukudi
E
Now select and open uvision4 project file
CO
CE
RA
G
After selecting the window appears in this format (HELLO.C is added to Source Group 1)
36
EC3017 _ECP
4931_Grace college of Engineering,Thoothukudi
E
Program of HELLO.C
CO
CE
RA
G
37
EC3017 _ECP
4931_Grace college of Engineering,Thoothukudi
E
Displaying 0 errors and 0 warnings
CO
CE
RA
G
38
EC3017 _ECP
4931_Grace college of Engineering,Thoothukudi
E
CO
New window evaluation mode appeared. Press ok
CE
RA
G
39
EC3017 _ECP
4931_Grace college of Engineering,Thoothukudi
E
Run the program CO
CE
RA
G
40
EC3017 _ECP
4931_Grace college of Engineering,Thoothukudi
E
Check the output at UART#1 window
CO
CE
RA
G
41
EC3017 _ECP
4931_Grace college of Engineering,Thoothukudi
E
Port1 window is displayed with D0 as 1
CO
CE
RA
G
42
EC3017 _ECP
4931_Grace college of Engineering,Thoothukudi
E
Then stop debugging the process
CO
CE
RA
G
43
EC3017 _ECP
4931_Grace college of Engineering,Thoothukudi
E
Now remove HELLO.C from source group 1 CO
CE
RA
G
44
EC3017 _ECP
4931_Grace college of Engineering,Thoothukudi
Output:-
E
HELLO WORLD with D0=1
CO
CE
RA
G
Observation:-
From the above outputs it is analyzed that the message “hello world” is printed at UART#1 at each time P1.0
is toggled.
Result:-
The experiment to print hello world using keil u vision successfully.
45
EC3017 _ECP
4931_Grace college of Engineering,Thoothukudi
EXPERIMENT 6 : ADC
DATE:
Aim: - Write a C Program to perform ADC operation using 8051
Software required: -
Keil u vision 5 , proteus
Theory: -
E
ADC/DAC INTERFACING:
Interfacing 8051 with 0808
CO
Most modern microcontrollers with 8051 IP cores have an inbuilt ADC. Older versions of 8051
like the MCS-51 and A789C51 do not have an on-chip ADC. Therefore to connect these
microcontrollers to analog sensors like temperature sensors, the microcontroller needs to be
hooked to an ADC. It converts the analog values to digital values, which the microcontroller
CE
can process and understand. Here is how we can interface the 8051 with 0808.
Procedure:
RA
G
To interface the ADC to 8051, follow these steps. In our case, we are using Proteus as the
simulation software and the AT89C51 microcontroller.
46
Connect the oscillator circuit to pins 19 and 20. This includes a crystal oscillator and two
EC3017 _ECP
4931_Grace college of Engineering,Thoothukudi
capacitors of 22uF each. Connect them to the pins, asshown in the diagram.
Connect one end of the capacitor to the EA’ pin and the other to the resister. Connect
this resistor to the RST pin, as shown in the diagram.
We are using port 1 as the input port, so we have connected the output ports of the ADC
to port 1.
As mentioned earlier, the 0808 does not have an internal clock; therefore, we have to
connect an external clock. Connect the externalclock to pin 10.
Connect Vref (+) to a voltage source according to the step size youneed.
Ground Vref (-) and connect the analog sensor to any one of the analog input pins on the
ADC. We have connected a variable resistor to INT2 for getting a variable voltage at the
pin.
Connect ADD A, ADD B, ADD C, and ALE pins to the microcontroller for selecting the
input analog port. We have connected ADD A- P2.0; ADD B- P2.1; ADD C- P2.2 and
the ALE pin to port 2.4.
Connect the control pins Start, OE, and Start to the microcontroller. These pins are
E
connected as follows in our case Start-Port-2.6; OE-Port-2.5 and EOC-Port-2.7.
Several control signals need to be sent to the ADC to extract the required datafrom it.
CE
Step 1: Set the port you connected to the output lines of the ADC as an input port. You
can learn more about the Ports in 8051 here.
Step 2: Make the Port connected to EOC pin high. The reason for doing this is that the
ADC sends a high to low signal when the conversion of data is complete. So this line
RA
select the data lines and send a High to Low pulse atthe ALE pin to select the address.
Step 5: Now that we have selected the analog input pin, we can tell the ADC to start the
conversion by sending a pulse to the START pin.
Step 6: Wait for the High to low signal by polling the EOC pin.
Step 7: Wait for the signal to get high again.
Step 8: Extract the converted data by sending a High to low signal tothe OE pin.
47
EC3017 _ECP
4931_Grace college of Engineering,Thoothukudi
OE = P2^5; sbit SC =
E
P2^0;sbit ADDR_B =
P2^1;sbit ADDR_C =
P2^2;
CO
CE
RA
G
48
EC3017 _ECP
4931_Grace college of Engineering,Thoothukudi
for(i=0;i<delay;i++)
for(j=0;j<1275;j++);
void main()
E
{
MYDATA = 0xFF;
CO
CE
EOC = 1;
ALE = 0;
RA
OE = 0;
SC = 0;
G
while(1)
ADDR_C = 0;
ADDR_B = 0;
ADDR_A = 0;
MSDelay(1);
ALE = 1;
MSDelay(1);
SC = 1;
49
EC3017 _ECP
4931_Grace college of Engineering,Thoothukudi
MSDelay(1);
ALE = 0;
SC = 0;
while(EOC==1);
while(EOC==0);
OE=1;
MSDelay(1);
value = MYDATA;
SENDDATA = value;
E
OE = 0 ;
}
CO
CE
Output:
RA
G
50
EC3017 _ECP
4931_Grace college of Engineering,Thoothukudi
Date:
Aim: - Write a C Program to display sensor output in s LCD
Software required: -
Keil u vision 5 , Proteus
Theory: -
Circuit diagram for LCD interfacing with 8051 microcontroller is shown in the above figure. If
you have basic understanding of 8051 then you must know about EA(PIN 31), XTAL1 &
XTAL2, RST pin(PI 9), Vcc and Ground Pin of 8051 microcontroller. I have used
these Pins in above circuit.
E
So besides these above pins we have connected the data pins (D0-D7) of LCD to the Port 2 (P2_0
– P2_7) microcontroller. And control pins RS, RW and E to the pin 12,13,14 (pin 2,3,4 of port 3)
CO
CE
RA
G
of microcontroller respectively.
PIN 2(VDD) and PIN 15(Backlight supply) of LCD are connected to voltage (5v), and PIN 1
(VSS) and PIN 16(Backlight ground) are connected to ground.
Pin 3(V0) is connected to voltage (Vcc) through a variable resistor of 10k to adjust the contrast of
LCD. Middle leg of the variable resistor is connected to PIN 3 and other two legs are connected
to voltage supply and Ground.
51
EC3017 _ECP
4931_Grace college of Engineering,Thoothukudi
The DHT11 is a commonly used Temperature and humidity sensor. The sensor comes with a dedicated NTC
to measure temperature and an 8-bit microcontroller to output the values of temperature and humidity as serial
data. The sensor is also factory calibrated and hence easy to interface with other microcontrollers.
The sensor can measure temperature from 0°C to 50°C and humidity from 20% to 90% with an accuracy of ±1°C
and ±1%. So if you are looking to measure in this range then this sensor might be the right choice for you.
The DHT11 Sensor is factory calibrated and outputs serial data and hence it is highly easy to set it up. The
E
connection diagram for this sensor is shown below.
CO
CE
RA
Circuit diagram
G
52
DHT11 Sensor Interfacing with 8051.
EC3017 _ECP
4931_Grace college of Engineering,Thoothukudi
The above circuit diagram shows the interfacing of 8051 with the DHT11 sensor.
In that, a DHT11 sensor is connected to P2.1(Pin No 22) of the microcontroller.
Programming Steps
E
Program :
/** DHT11 Interfacing with 8051
*
*/
CO
CE
# include<reg51.h>
RA
# include<stdio.h>
# include<string.h>
G
# include <stdlib.h>
# include "LCD16x2_4bit.h"
53
EC3017 _ECP
4931_Grace college of Engineering,Thoothukudi
E
TR0 = 1; /* Start timer0 */
TF0 = 0;
/* Stop timer0 */
CO
while(TF0 == 0);/* Wait until timer0 flag set */
TR0 = 0;
/* Clear timer0 flag */
}
CE
E
while(DHT11==1);
}
}
return c; CO
CE
void main()
{
unsigned char dat[20];
RA
while(1)
{
55
EC3017 _ECP
4931_Grace college of Engineering,Thoothukudi
E
}
else
{
CO
CE
sprintf(dat,"Hum = %d.%d",I_RH,D_RH);
LCD_String_xy(0,0,dat);
sprintf(dat,"Tem = %d.%d",I_Temp,D_Temp);
RA
LCD_String_xy(1,0,dat);
LCD_Char(0xDF);
LCD_String("C");
G
memset(dat,0,20);
sprintf(dat,"%d ",CheckSum);
LCD_String_xy(1,13,dat);
}
delay(100);
}
}
56
EC3017 _ECP
4931_Grace college of Engineering,Thoothukudi
Date:
Aim: - Write a C Program to display string in LCD
Software required: -
Keil u vision 5 , Proteus
Theory: -
Circuit diagram for LCD interfacing with 8051 microcontroller is shown in the above figure.
If you have basic understanding of 8051 then you must know about EA(PIN 31), XTAL1 &
XTAL2, RST pin(PIN 9), Vcc and Ground Pin of 8051 microcontroller. I have used
E
CO
CE
RA
G
57
EC3017 _ECP
4931_Grace college of Engineering,Thoothukudi
So besides these above pins we have connected the data pins (D0-D7) of LCD to the Port 2 (P2_0
– P2_7) microcontroller. And control pins RS, RW and E to the pin 12,13,14 (pin 2,3,4 of port 3)
of microcontroller respectively.
PIN 2(VDD) and PIN 15(Backlight supply) of LCD are connected to voltage (5v), and PIN 1
(VSS) and PIN 16(Backlight ground) are connected to ground.
Pin 3(V0) is connected to voltage (Vcc) through a variable resistor of 10k to adjust the contrast of
LCD. Middle leg of the variable resistor is connected to PIN 3 and other two legs are connected
to voltage supply and Ground.
Program:
E
#include<reg51.h>
#define display_port P2 //Data pins connected to port 2 on microcontroller
CO
sbit rs = P3^2; //RS pin connected to pin 2 of port 3
sbit rw = P3^3; // RW pin connected to pin 3 of port 3
sbit e = P3^4; //E pin connected to pin 4 of port 3
msdelay(1); e=0;
}
msdelay(10);
}
void main()
{
unsigned char a[15]="CIRCUIT DIGEST"; //string of 14 characters with a null terminator.
int l=0;
lcd_init();
while(a[l] != '\0') // searching the null terminator in the sentence
{
lcd_data(a[l]); l++;
msdelay(50);
}
}
E
CO
CE
Result:-
The experiment to generate square pulse using PWM keil and proteus was completed successfully.
RA
G
59
EC3017 _ECP
4931_Grace college of Engineering,Thoothukudi
Date:
Aim: - Write a C Program to create square wave using PWM
Software required: -
Keil u vision 5
Theory: -
PWM, as the name suggests, is simply a pulse width modulation. We take a pulse and then we modulate its width
and make it small or big. Another term important while studying PWM is named duty cycle. The duty
cycle shows the duration for which the PWM pulse remains HIGH. Now if the pulse remains high for 50% and
LOW for 50% then we say that PWM pulse has a duty cycle of 50%. Similarly, if the pulse is HIGH for 70% and
E
Low for 30% then it has a duty cycle of 70%.
CO
Most of the microcontrollers have special pins assigned for PWM as in Arduino UNO .It has 6 PWM pins on it.
Similarly, PIC Microcontrollers also have PWM pins but unfortunately, the 8051 Microcontroller doesn't have
this luxury means there are no special PWM pins available in 8051 Microcontroller. But PWM is necessary so we
are going to manually generate the PWM pulse using Timer0 interrupt.
CE
Program:s
RA
#include<reg51.h> // PWM_Pin
sbit PWM_Pin = P2^0; // Pin P2.0 is named asPWM_Pin
// Function declarations
G
void cct_init(void);
void InitTimer0(void);
void InitPWM(void);
// Global variables
unsigned char PWM = 0; // It can have a value from 0 (0% duty cycle) to 255
(100% duty cycle)
unsigned int temp = 0; // Used inside Timer0 ISR
E
// Init CCT function
void cct_init(void)
{
P0 = 0x00; P1 =
0x00; P2 =
CO
0x00; P3 =
CE
0x00;
}
RA
// Timer0 initialize
void InitTimer0(void)
{
TMOD &= 0xF0; // Clear 4bit field for timer0
G
// Timer0 ISR
void Timer0_ISR (void) interrupt 1
{
TR0 = 0; // Stop Timer 0
E
else // if PWM_Pin is low
{
PWM_Pin = 1;
temp = PWM*PWM_Freq_Num;
TH0 = 0xFF - (temp>>8)&0xFF;
TL0 = 0xFF - temp&0xFF;
CO
}
CE
}
G
62
// PWM initialize void
EC3017 _ECP
4931_Grace college of Engineering,Thoothukudi
InitPWM(void)
{
PWM = 0; // Initialize with 0% duty cycle InitTimer0(); //
Initialize timer0 to start
generating interrupts
// PWM generation code is
written inside the Timer0 ISR
}
// Timer0 ISR
void Timer0_ISR (void) interrupt 1
{
TR0 = 0; // Stop Timer 0
E
PWM_Pin = 0;
temp = (255-PWM)*PWM_Freq_Num; TH0 =
}
else
0xFF - (temp>>8)&0xFF;
TL0 = 0xFF - temp&0xFF;
// if PWM_Pin is low
CO
{
CE
PWM_Pin = 1;
temp = PWM*PWM_Freq_Num;
TH0 = 0xFF - (temp>>8)&0xFF;
RA
Start Timer 0
}
Output:
63
EC3017 _ECP
4931_Grace college of Engineering,Thoothukudi
E
CO
CE
RA
Result:-
The experiment to generate square pulse using PWM using proteus was completed successfully.
G
64
EC3017 _ECP
4931_Grace college of Engineering,Thoothukudi
DATE:
Aim: - Write a C Program to rotate steeper motor clockwise and anticlockwise
Software required: -
Keil u vision 5 , Proteus
Theory: -
Stepper Motor
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.
E
CO
CE
RA
Normal motor shafts can move freely but the stepper motor shafts move in fixed repeatable
increments.
Some parameters of stepper motors −
G
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
Interfacing Stepper Motor with 8051 Microcontroller
We are using Port P0 of 8051 for connecting the stepper motor. HereULN2003 is used. This 65
is
EC3017 _ECP
4931_Grace college of Engineering,Thoothukudi
basically a high voltage, high current Darlington transistor array. Each ULN2003 has seven NPN
Darlington pairs. It can provide high voltage output with common cathode clamp diodes for
switching inductive loads.
The Unipolar stepper motor works in three modes.
Wave Drive Mode − In this mode, one coil is energized at a time. So all four coils are
energized one after another. This mode produces less torque than full step drive mode.
The following table is showing the sequence of input states in different windings.
1 1 0 0 0
2 0 1 0 0
3 0 0 1 0
E
4 0 0 0 1
CO
Full Drive Mode − In this mode, two coils are energized at the same time. This mode
produces more torque. Here the power consumption is also high
The following table is showing the sequence of input states in different windings.
CE
1 1 1 0 0
2 0 1 1 0
3 0 0 1 1
G
4 1 0 0 1
Half Drive Mode − In this mode, one and two coils are energized alternately. At first,
one coil is energized then two coils are energized. This is basically a
66
EC3017 _ECP
4931_Grace college of Engineering,Thoothukudi
combination of wave and full drive mode. It increases the angular rotation of
themotor
The following table is showing the sequence of input states in different windings.
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
E
6 0 0 1 1
7 0 0
CO
0 1
8 1 0 0 1
CE
The circuit diagram is like below: We are using the full drive mode.
Program :
#include<reg51.h>
RA
sbit LED_pin = P2^0; //set the LED pin as P2.0 void delay(int ms){
unsigned int i, j;
for(i = 0; i<ms; i++){ // Outer for loop for given millisecondsvalue
G
EC3017 _ECP
4931_Grace college of Engineering,Thoothukudi
E
Result : Stepper motor rotate both in clockwise and anti clock wise direction successfully
CO
CE
RA
G
68
EC3017 _ECP
4931_Grace college of Engineering,Thoothukudi
VIVA QUESTION:
E
CO
4. What are the various bank register in 8051?
CE
RA
69
EC3017 _ECP
4931_Grace college of Engineering,Thoothukudi
E
PSW register Bits D4 and D3 of the PSW are used to select the desired register bank ¾ Use the bit-
addressable instructions SETB and CLR to access PSW.4 and PSW.3
CO
CE
8051 is only 8 bit wide, which means that it can take value of 00 to FFH
the stack. As we push data onto the stack, the SP is incremented by one
ii) Loading the contents of the stack back into a CPU register is called a POP. With every pop, the top
byte of the stack is copied to the register specified by the instruction and the stack pointer is
decremented once
10. Examining the stack, show the contents of the register and SP after execution of the following
instructions. All value are in hex.
POP 3 ; POP stack into R3
POP 5 ; POP stack into R5
POP 2 ; POP stack into R2
E
16.. Define Framing? CO
Asynchronous serial data communication is widely used for character-oriented transmissions. Each
character is placed in between start and stop bits, this is called framing. Block-oriented data transfers use
the synchronous method.
-3 to +3 undefined.
21. With XTAL = 11.0592 MHz, find the TH1 value needed to have the following baud rates. (a) 9600
(b) 2400 (c) 1200? Solution: The machine cycle frequency of 8051 = 11.0592 / 12 = 921.6 kHz, and
921.6 kHz / 32 = 28,800 Hz is frequency by UART to timer 1 to set baud rate. (a) 28,800 / 3 = 9600
where -3 = FD (hex) is loaded into TH1 (b) 28,800 / 12 = 2400 where -12 = F4 (hex) is loaded into TH1
(c) 28,800 / 24 = 1200 where -24 = E8 (hex) is loaded into TH1 71
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, the 8051 deframes it by
eliminating the stop and start bits, making a byte out of the data received, and then placing it in SBUF
23. What is the Use of SCON?
SCON is an 8-bit register used to program the start bit, stop bit, and data bits of data framing, among
other things [Link] is the value loaded to the register for various baud rate?
24. Find the period of the machine cycle.
for 11.0592 MHz crystal frequency Solution: 11.0592/12 = 921.6 kHz; machine cycle is 1/921.6 kHz =
1.085μs
25. What are the two factors can affect the accuracy of the delay ¾. Crystal frequency - The duration of
the clock period of the machine cycle is a function of this crystal frequency .
E
CO
27. Define Polling. Polling can monitor the status of several devices and serve each of them as certain
conditions are met ¾ The polling method is not efficient, since it wastes much of the microcontroller’s
time by polling devices that do not need service .
28. What is the basic principle of stepper motor?
The basic working principle of the stepper motor is the following: By energizing one or more of the
CE
stator phases, a magnetic field is generated by the current flowing in the coil and the rotor aligns with
this field.
29. What is the main use of stepper motor?
RA
Commercially, stepper motors are used in floppy disk drives, flatbed scanners, computer printers,
plotters, slot machines, image scanners, compact disc drives, intelligent lighting, camera lenses, CNC
machines, and 3D printers
G
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.
72
EC3017 _ECP
The procedure for sending control signals to an ADC interfaced with an 8051 microcontroller involves setting the connected port as an input port. Ensure EOC pin is high, clear ALE, START, and OE lines by setting them low. Use the ALE pin for address selection with a High to Low pulse, proceed to send a pulse to the START pin for conversion, poll the EOC pin for conversion completion, and finally extract data with a High to Low pulse to the OE pin .
Proteus assists in simulating the interfacing of an ADC with an 8051 microcontroller by allowing visualization of circuit connections and data flows between components. It provides a virtual environment to verify logic and functionality before real-world implementation, demonstrated by connecting oscillators, control signals, and verifying ADC outputs visually within the Proteus software interface .
In a microcontroller-based system to toggle an LED with a fixed delay, the characteristic of timers being able to overflow and trigger interrupts is leveraged. By counting clock cycles until overflow, a precise delay can be achieved repeatedly, allowing the system to toggle the LED at regular intervals without direct MCU supervision after initial setup .
To initialize and configure a microcontroller project in Keil uVision for the AT89C51 processor, you should create a new project and select the vendor 'Atmel' and device 'AT89C51'. Add STARTUP.A51 to the project folder and write the program, saving it as a .c file (for example, prg3.c) and add it to Source Group 1. Build the target and debug the program, checking output via UART windows .
LCD interfacing with an 8051 microcontroller involves connecting data pins D0-D7 of the LCD to a suitable port of the microcontroller. Control pins RS, RW, and E are connected to other suitable pins for command operation. Sensor data is read, processed, and commands/data are sent by toggling these control pins in sequence. Delay functions are used for timing adjustments to ensure reliable data write onto the LCD, facilitating display of sensor readings .
Serial communication in an 8051 microcontroller using UART is managed by configuring the SCON register for the desired mode, setting baud rate using Timer1 in Mode 2, and activating the necessary pins for transmission. The TI flag in SCON denotes the readiness to send another byte while RI indicates data reception. Continuous data exchange often involves checking and resetting these flags in an infinite loop .
The TCON register in an 8051 microcontroller manages timers by holding control bits like TR (Timer Run) and TF (Timer Overflow) for starting and indicating the overflow of timers. TF indicates that the timer has counted to its maximum, triggering an associated interrupt if enabled. Managing these bits is crucial for handling time-based operations and ensuring proper sequence in interrupt-driven tasks .
The #include directive in 8051 programming is significant for peripheral configuration as it incorporates necessary header files defining peripheral registers specific to the microcontroller. This facilitates direct manipulation of hardware registers and convenient access to pre-defined symbolic names, ensuring correctness and ease in configuring various peripherals .
A LED can be toggled using interrupts in an 8051 microcontroller by setting up Timer0 in Mode1, loading initial values to the timer, enabling interrupts with IE register, and starting the timer with TR0 set to 1. On timer overflow, the interrupt service routine toggles the LED variable, which is mapped to a physical pin, thereby toggling the LED state .
To generate a time delay using a timer in an 8051 microcontroller, load the desired initial value in the TL and TH registers. Start the timer by setting the TR0 bit in the TCON register to 1. The timer overflows when it reaches 255, setting the TF0 flag. After overflow, clear TF0 to restart the process or stop with TR0=0. Repeat as needed for the delay length .