0% found this document useful (0 votes)
48 views72 pages

EE3017 Embedded C Programming Lab Manual

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

EE3017 Embedded C Programming Lab Manual

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

4931_Grace college of Engineering,Thoothukudi

GRACE COLLEGE OF ENGINEERING


Mullakkadu, Tuticorin

E
CO
CE

LAB MANUAL
Department of Electrical and Electronics Engineering
RA

B.E. – Electrical and Electronics


Engineering
G

EE3017 Embedded C programming

Period: July 2023 – December 2023

Prepared by HOD/EEE PRINCIPAL


[Link], AP/EEE

EC3017 _ECP
4931_Grace college of Engineering,Thoothukudi

GRACE COLLEGE OF ENGINEERING


Mullakkadu, Tuticorin

E
CO
CE

Department of Electrical and Electronics Engineering


RA

B.E. – Electrical and Electronics Engineering


EE3017 Embedded C programming
Period: July 2023 – December 2023
G

Name: ……………………………………………………………

Semester: …………………… Year: ………………………

Register No: …………………………………………………

EC3017 _ECP
4931_Grace college of Engineering,Thoothukudi

GRACE COLLEGE OF ENGINEERING,MULLAKKADU, TUTICORIN

E
CO
CE

BONAFIDE CERTIFICATE
RA

Certified that this practical work entitled EE3017


Embedded C programming is the bonafide record of work done by
G

Mr/Ms............................................................................. of V Semester in
Department of
Electrical and Electronics Engineering of the College during the year July 2023
– Dec 2023

Staff In – Charge Head of the Department

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

EE3017 EMBEDDED C- PROGRAMMING (LAB) LTPC


0 03 3
SYLLABUS
OBJECTIVES:
 To expose the students to the fundamentals of embedded Programming
 To Introduce the GNU C Programming Tool Chain.
 To study the basic concepts of embedded C.
 To teach the basics of 8051 Programming
 To involve Discussions/ Practice/Exercise in revising & familiarizing the concepts acquired over the
 Units of the subject for improved employability skills.
1. Introduction to IDE (like code blocks, vscode , etc)and Programming Environment (like Keil uvision,
Proteus)
2. Configuring an I/O port using bitwise programming.

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

8. Interfacing 16x2 LCD with 8051


9. Configuring timer for generating PWM signal
RA

10. Interfacing a stepper motor with 8051


11. Assignment: Introduction to Arduino IDE, Raspberry Pi
TOTAL: 45 PERIODS
G

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

6. Interfacing an ADC with 8051


7. Interfacing an analog sensor with 8051
CO
8. Interfacing 16x2 LCD with 8051
CE
9. Configuring timer for generating PWM signal
10. Interfacing a stepper motor with 8051
RA

11. Assignment: Introduction to Arduino IDE, Raspberry Pi

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

EX. NO.: 1. INTRODUCTION TO IDE KEIL UVISION AND PROTEUS


PROGRAMMING
DATE:
INTRODUCTION
Earlier to Microcontrollers, Microprocessors were greatly used for each and every purpose.
Microprocessors were containing ALU, general purpose register, stack pointer, program counter,
clock counter and so many other features which the today’s Microcontroller also possesses. But
the difference between them exists with respect to the number of instructions, access times, size,
reliability, PCB size and so on. Microprocessor contains large instruction set called as CISC
processor whereas Microcontroller contains less number of instructions and is called as RISC
processor. The access time is less in case of microcontrollers compared to microprocessors and
the PCB size reduces in case of microcontrollers. There are many versions of microcontrollers

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

The main features of the 8051 core are:


Operates with single Power Supply +5V.
CO
single chip microcontroller with many built-in functions and is the core for all MCS-51 devices.

 8-bit CPU optimized for control applications.


CE
 16-bit program counter (PC) and 16-bit data pointer (DPTR).
 8-bit program status word (PSW).
 8-bit stack pointer (SP).
RA

 4K Bytes of On-Chip Program Memory (Internal ROM or EPROM).


 128 bytes of On-Chip Data Memory (Internal RAM):
 Four Register Banks, each containing 8 registers (R0 to R7) [Total 32 reg]
G

 16-bytes of bit addressable memory.


 80 bytes of general-purpose data memory (Scratch Pad Area).
 Special Function Registers (SFR) to configure/operate microcontroller.
 32 bit bi-directional I/O Lines (4 ports P0 to P3). Two 16-bit timers/counters (T0 and T1).
 Full duplex UART (Universal Asynchronous Receiver/Transmitter).
 On-Chip oscillator and clock circuitry.

7
EC3017 _ECP
4931_Grace college of Engineering,Thoothukudi

E
CO
CE

STEPS TO CREATE AND COMPILE Keil µVision-


1. Double Click on the icon on the desktop.vision keil
RA

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

errors. After an error free, to build go to Debug mode.


8. Now user can enter into Debug mode with Debug- Start / Stop Debug session dialog Or by
clicking in the icon.
9. The program is run using the Debug-Run command & halted using Debug-Stop Running. Also
the (reset, run, halt) icons can be used. Additional icons are (step, step over, and step into, run till
cursor).
10. If it is an interface program the outputs can be seen on the LCD, CRO, motor, led status, etc.
If it is a part-A program, the appropriate memory window is opened using View -> memory
window (for data RAM & XRAM locations), Watch window (for timer program), serial window,
etc.
Note: To access data RAM area type address as D: 0020h. Similarly to access the DPTR region
(XRAM-present on chip in AT89C51ED2) say 9000h location type in X: 09000H

C51 Development Tools

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.

This introduction includes a brief explanation of the:

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:

Commands used from the menu Debug or the Debug Toolbar.


CE
Commands entered manually in the Command Window.
 Commands available from the Context Menu of the Editor or Disassembly
window.
RA

Debug Functions executed from an initialization file.

Start the Debugger


G

 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.

Create new u vision project Procedure:-

12
EC3017 _ECP
4931_Grace college of Engineering,Thoothukudi

E
Create new u vision project CO
CE
RA
G

Select the folder (newly created) to save the project

13
EC3017 _ECP
4931_Grace college of Engineering,Thoothukudi

E
Save the project CO
CE
RA
G

Select the vendor “Atmel” and device “AT89C51”

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

The program is saved as prg3.c

16
EC3017 _ECP
4931_Grace college of Engineering,Thoothukudi

E
Prg3.c is to be added to Source Group1 CO
CE
RA
G

Select the program prg3

17
EC3017 _ECP
4931_Grace college of Engineering,Thoothukudi

E
Now prg3.c is added to Source Group 1 CO
CE
RA
G

Build the target

18
EC3017 _ECP
4931_Grace college of Engineering,Thoothukudi

E
Debugging the target CO
CE
RA
G

New window evaluation mode appeared. Press ok

19
EC3017 _ECP
4931_Grace college of Engineering,Thoothukudi

E
Run the program CO
CE
RA
G

Selecting for UART#1 from serial windows

20
EC3017 _ECP
4931_Grace college of Engineering,Thoothukudi

E
Check the output at UART#1 window CO
CE
RA
G

Select Port1 from i/o ports in peripherals

21
EC3017 _ECP
4931_Grace college of Engineering,Thoothukudi

E
Port1 window is displayed with D0 as 0 CO
CE
RA
G

Port1 window is displayed with D0 as 0

22
EC3017 _ECP
4931_Grace college of Engineering,Thoothukudi

E
Stop debugging process CO
CE
RA
G

After debugging, window appears in this format

23
EC3017 _ECP
4931_Grace college of Engineering,Thoothukudi

Experiment-2a CONFIGURING AN I/O PORT USING BITWISE PROGRAMMING –


TOGGLING OF LED

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

P1.7 P1.6 P1.5 P1.4 P1.3 P1.2 P1.1 P1.0


G

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

Experiment-2b CONFIGURING AN I/O PORT USING BITWISE PROGRAMMING-


TOGGLING OF LED WITH DELAY

Date:
Aim of experiment:-

Write a C program to toggle bit D0 of port 1 50,000 times.

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

#include sbit MYBIT=P1^0;


void main(void)
{
G

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

P1.7 P1.6 P1.5 P1.4 P1.3 P1.2 P1.1 P1.0

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

Experiment-3 CONFIGURING TIMER FOR GENERATING HARDWARE DELAY


Date:

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

TMOD register(Timer mode register):

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 DebugBuild 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

EXPERIMENT 4: FLASHING AN LED USING AN INTERRUPT

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

void Timer(void) interrupt 1 // Interrupt No.1 for Timer 0


{ led=~led; // Toggle LED on interrupt
}
main()
{
G

TMOD = 0x01; // Mode1 of Timer0


TH0=0x00; // Initial values loaded to Timer
TL0=0x00;
IE = 0x82; // Enable interrupt
TR0=1; // Start Timer
while(1); // Do nothing
}

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

EXPERIMENT .5 SERIAL COMMUNICATION USING UART PORT OF 8051


Date:
Aim: - Write a C Program to print hello world.

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

Select open project from project

33
EC3017 _ECP
4931_Grace college of Engineering,Thoothukudi

E
Select Keil_v5 present in Local Disk (C:) CO
CE
RA
G

Next select the folder C51

34
EC3017 _ECP
4931_Grace college of Engineering,Thoothukudi

E
Next select the folder examples
CO
CE
RA
G

Next select the folder HELLO

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

Build the target

37
EC3017 _ECP
4931_Grace college of Engineering,Thoothukudi

E
Displaying 0 errors and 0 warnings
CO
CE
RA
G

Debugging the target

38
EC3017 _ECP
4931_Grace college of Engineering,Thoothukudi

E
CO
New window evaluation mode appeared. Press ok
CE
RA
G

Now the window appears in this format

39
EC3017 _ECP
4931_Grace college of Engineering,Thoothukudi

E
Run the program CO
CE
RA
G

Selecting for UART#1 from serial windows

40
EC3017 _ECP
4931_Grace college of Engineering,Thoothukudi

E
Check the output at UART#1 window
CO
CE
RA
G

Select Port1 from i/o ports in peripherals

41
EC3017 _ECP
4931_Grace college of Engineering,Thoothukudi

E
Port1 window is displayed with D0 as 1
CO
CE
RA
G

Port1 window is displayed with D0 as 0

42
EC3017 _ECP
4931_Grace college of Engineering,Thoothukudi

E
Then stop debugging the process
CO
CE
RA
G

After closing the program, the window appears in this format

43
EC3017 _ECP
4931_Grace college of Engineering,Thoothukudi

E
Now remove HELLO.C from source group 1 CO
CE
RA
G

Now select yes

44
EC3017 _ECP
4931_Grace college of Engineering,Thoothukudi

Output:-

E
HELLO WORLD with D0=1

CO
CE
RA
G

HELLO WORLD with D0=0

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.

use the ADC with the microcontroller.


Logic to communicate between 8051 and ADC 0808
CO
With this, you have successfully interfaced the 8051 to the ADC. Now let us look at the logic to

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

needs to be high so that the microcontroller can detect the change.


Step 3: Clear the data lines which are connected to pins ALE, START, and OE as all
these pins require a Low to High pulse to get activated.
Step 4: Select the data lines according to the input port you want to select. To do this,
G

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

program to interface ADC 0808 with 8051 :

The C code for the same is given below:


#include <reg51.h>

sbit ALE = P2^4; sbit

OE = P2^5; sbit SC =

P2^6; sbit EOC =

P2^7; sbit ADDR_A =

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

sfr MYDATA =P1;

sfr SENDDATA =P3;

void MSDelay(unsighned int) // Function to generate timedelay

unsighned int i,j;

for(i=0;i<delay;i++)

for(j=0;j<1275;j++);

void main()

E
{

unsigned char value;

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

Result : The output of ADC is verified using Proteus software.

50
EC3017 _ECP
4931_Grace college of Engineering,Thoothukudi

EXPERIMENT : 7 INTERFACE SENSOR AND LCD USING 8051 MICROCONTROLLER

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.

How to use DHT11 Sensor

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

 First, initialize the LCD16x2_4bit.h library.


 Define pin no. to interface DHT11 sensor, in our program we define P2.1 (Pin no.22)
 Send the start pulse to the DHT11 sensor by making low to high on the data pin.
 Receive the response pulse from the DHT11 sensor.
 After receiving the response, receive 40-bit data serially from the DHT11 sensor.
 Display this received data on LCD16x2 along with error indication.

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"

sbit DHT11=P2^1; /* Connect DHT11 output Pin to P2.1 Pin */


int I_RH,D_RH,I_Temp,D_Temp,CheckSum;

void timer_delay20ms() /* Timer0 delay function */


{
TMOD = 0x01;
TH0 = 0xB8; /* Load higher 8-bit in TH0 */
TL0 = 0x0C; /* Load lower 8-bit in TL0 */

53
EC3017 _ECP
4931_Grace college of Engineering,Thoothukudi

TR0 = 1; /* Start timer0 */


while(TF0 == 0);/* Wait until timer0 flag set */
TR0 = 0; /* Stop timer0 */
TF0 = 0; /* Clear timer0 flag */
}

void timer_delay30us() /* Timer0 delay function */


{
TMOD = 0x01; /* Timer0 mode1 (16-bit timer mode) */
TH0 = 0xFF; /* Load higher 8-bit in TH0 */
TL0 = 0xF1; /* Load lower 8-bit in TL0 */

E
TR0 = 1; /* Start timer0 */

TF0 = 0;
/* Stop timer0 */
CO
while(TF0 == 0);/* Wait until timer0 flag set */
TR0 = 0;
/* Clear timer0 flag */
}
CE

void Request() /* Microcontroller send request */


{
RA

DHT11 = 0; /* set to low pin */


timer_delay20ms(); /* wait for 20ms */
G

DHT11 = 1; /* set to high pin */


}

void Response() /* Receive response from DHT11 */


{
while(DHT11==1
);
while(DHT11==0
);
while(DHT11==1
);
} 54
EC3017 _ECP
4931_Grace college of Engineering,Thoothukudi

int Receive_data() /* Receive data */


{
int q,c=0;
for (q=0; q<8; q++)
{
while(DHT11==0);/* check received bit 0 or 1 */
timer_delay30us();
if(DHT11 == 1) /* If high pulse is greater than 30ms */
c = (c<<1)|(0x01);/* Then its logic HIGH */ else /*
otherwise its logic LOW */c = (c<<1);

E
while(DHT11==1);

}
}
return c; CO
CE
void main()
{
unsigned char dat[20];
RA

LCD_Init(); /* initialize LCD */


G

while(1)
{

Request(); /* send start pulse */


Response(); /* receive response */

55
EC3017 _ECP
4931_Grace college of Engineering,Thoothukudi

I_RH=Receive_data(); /* store first eight bit in I_RH */


D_RH=Receive_data(); /* store next eight bit in D_RH */
I_Temp=Receive_data(); /* store next eight bit in I_Temp */
D_Temp=Receive_data(); /* store next eight bit in D_Temp */
CheckSum=Receive_data();/* store next eight bit in CheckSum */

if ((I_RH + D_RH + I_Temp + D_Temp) != CheckSum)


{
LCD_String_xy(0,0,"Error");

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);
}
}

RESULT: Thus interfacing sensor was studied and exceuted successfully

56
EC3017 _ECP
4931_Grace college of Engineering,Thoothukudi

EXPERIMENT : 8 DISPLAYING IN LCD USING 8051 MICROCONTROLLER

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

these Pins in above circuit.

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:

// Program for LCD Interfacing with 8051 Microcontroller (AT89S52)

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

void msdelay(unsigned int time) // Function for creating delay in milliseconds.


CE
{
unsigned i,j ; for(i=0;i<time;i++)
for(j=0;j<1275;j++);
}
RA

void lcd_cmd(unsigned char command) //Function to send command instruction to LCD


{
display_port = command; rs= 0;
rw=0; e=1;
G

msdelay(1); e=0;
}

void lcd_data(unsigned char disp_data) //Function to send display data to LCD


{
display_port = disp_data; rs= 1;
rw=0; e=1;
msdelay(1); e=0;
}

void lcd_init() //Function to prepare the LCD and get it ready


{
lcd_cmd(0x38); // for using 2 lines and 5X7 matrix of LCD
msdelay(10);
lcd_cmd(0x0F); // turn display ON, cursor blinking
msdelay(10);
lcd_cmd(0x01); //clear screen msdelay(10); 58
lcd_cmd(0x81); // bring cursor to position 1 of line 1
EC3017 _ECP
4931_Grace college of Engineering,Thoothukudi

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

EXPERIMENT : 9 CONFIGURING TIMER FOR GENERATING PWM SIGNAL

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

// PWM frequency selector


/* PWM_Freq_Num can have values in between 1 to 257 only
* When PWM_Freq_Num is equal to 1, then it means highest PWMfrequency
* which is approximately 1000000/(1*255) = 3.9kHz
* When PWM_Freq_Num is equal to 257, then it means lowestPWM frequency
* which is approximately 1000000/(257*255) = 15Hz
* 60
* So, in general you can calculate PWM frequency by usingthe formula
EC3017 _ECP
4931_Grace college of Engineering,Thoothukudi

* PWM Frequency = 1000000/(PWM_Freq_Num*255)


*/

#define PWM_Freq_Num 1 //Highest possible PWMFrequency

// Main Function int


main(void)
{
cct_init(); // Make all ports zero InitPWM();
// Start PWM

PWM = 127; // Make 50% duty cycle of PWM


while(1) // Rest is done in Timer0 interrupt
{}
}

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

TMOD |= 0x01; // Set timer0 in mode 1 = 16bit mode

TH0 = 0x00; TL0 // First time value


= 0x00; // Set arbitrarily zero

ET0 = 1; // Enable Timer0 interrupts Global


EA = 1; // interrupt enable

TR0 = 1; // Start Timer 0


}

// PWM initialize void


InitPWM(void)
{
PWM = 0; // Initialize with 0% duty cycle InitTimer0(); //
Initialize timer0 to start 61
generating interrupts
EC3017 _ECP
4931_Grace college of Engineering,Thoothukudi

// PWM generation code is


written inside the Timer0 ISR
}

// Timer0 ISR
void Timer0_ISR (void) interrupt 1
{
TR0 = 0; // Stop Timer 0

if(PWM_Pin) // if PWM_Pin is high


{
PWM_Pin = 0;
temp = (255-PWM)*PWM_Freq_Num; TH0 =
0xFF - (temp>>8)&0xFF;
TL0 = 0xFF - temp&0xFF;
}

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

TF0 = 0; // Clear the interrupt flagTR0 = 1; //


Start Timer 0
RA

}
G

// Timer0 initialize void


InitTimer0(void)
{
TMOD &= 0xF0; // Clear 4bit field for timer0
TMOD |= 0x01; // Set timer0 in mode 1 = 16bit mode

TH0 = 0x00; TL0 // First time value


= 0x00; // Set arbitrarily zero

ET0 = 1; // Enable Timer0 interrupts Global


EA = 1; // interrupt enable

TR0 = 1; // Start Timer 0


}

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

if(PWM_Pin) // if PWM_Pin is high


{

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

TL0 = 0xFF - temp&0xFF;


}

TF0 = 0; // Clear the interrupt flagTR0 = 1; //


G

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

EXPERIMENT 10 : STEPPER MOTOR

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.

Steps Winding A Winding B Winding C Winding D

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

Steps Winding A Winding B Winding C Winding D


RA

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.

Steps Winding Winding Winding Winding


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

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

for(j = 0; j< 1275; j++){


//execute in each milliseconds;
}
}
}
void main(){
int rot_angle[] = {0x0C,0x06,0x03,0x09};int i;
while(1){
//infinite loop for LED blinkingfor(i = 0; i<4; i++){
P0 = rot_angle[i];
delay(100);
} 67
}
}
Output:

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:

[Link] 3910 to binary


32 + 0 + 0 + 4 + 2 + 1 = 39
Therefore, 3910 = 100111
2. What are the features of 8051 microcontroller?
The 8051 is an 8-bit processor .ƒ The CPU can work on only 8 bits of data at a time .The 8051 had ƒ
128 bytes of RAM ,ƒ 4K bytes of on-chip ROM, ƒ Two timers, ƒ One serial port, ƒ Four I/O ports, each
8 bits wide 6 interrupt sources
3. What are the various 8051 family?

E
CO
4. What are the various bank register in 8051?
CE
RA

5. Draw the Flag register of8051


G

[Link] are the RAM allocation in 8051?

69

EC3017 _ECP
4931_Grace college of Engineering,Thoothukudi

[Link] is the use of PSW register?

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

[Link] is stack pointer?


The register used to access the stack is called the SP (stack pointer) register .The stack pointer in the
RA

8051 is only 8 bit wide, which means that it can take value of 00 to FFH

9. What are to Operation in Stack?


i) The storing of a CPU register in the stack is called a PUSH . SP is pointing to the last used location of
G

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

[Link] is Vref in ADC?


Vref/2 It is used for the reference voltage. If this pin is open (not connected), the analog input voltage is
in the range of 0 to 5 volts (the same as the Vcc pin). If the analog input range needs to be 0 to 4 volts,
Vref/2 is connected to 2 volts. 70
[Link] ADC?
ADCs (analog-to-digital converters) are among the most widely used devices for data acquisition. A
physical quantity, like temperature, pressure, humidity, and velocity, etc., is converted to electrical
EC3017 _ECP
4931_Grace college of Engineering,Thoothukudi

(voltage, current) signals using a device called a transducer, or sensor

13. What is Conversion Time in ADC?


Conversion time is another major factor in judging an ADC ƒ Conversion time is defined as the time it
takes the ADC to convert the analog input to a digital (binary) number ƒ In ADC804 conversion time
varies depending on the clocking signals applied to CLK R and CLK IN pins, but it cannot be faster
than 110 µs

14. How to calculator output voltage in ADC?


To calculate the output voltage, use the following formula ƒ Dout = digital data output (in decimal), ƒ
Vin = analog voltage, and ƒ step size (resolution) is the smallest change

15. Define Full duplex and Half duplex


If data can be transmitted and received, it is a duplex transmission. If data transmitted one way a time, it
is referred to as half duplex. If data can go both ways at a time, it is full duplex

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.

17. Draw the Data frame of Serial communication.


CE

18. What is baud rate?


The rate of data transfer in serial data communication is stated in bps (bits per second). Another widely
used terminology for bps is baud rate
RA

19. What is TTL in RS232?


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 1 is represented by -3 ~ -25 V, while a 0 bit is +3 ~ +25 V, making
G

-3 to +3 undefined.

[Link] are the Pins in 9 pin RS232?

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

22. What is the use of SBUF?


SBUF is an 8-bit register used solely for serial communication .For a byte data to be transferred via the
TxD line, it must be placed in the SBUF register . The moment a byte is written into SBUF, it is framed
EC3017 _ECP
4931_Grace college of Engineering,Thoothukudi

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 .

26. Defne interrupts and interrupts service routine.


Interrupts ƒ Whenever any device needs its service, the device notifies the microcontroller by sending it
an interrupt signal ƒ Upon receiving an interrupt signal, the microcontroller interrupts whatever it is
doing and serves the device ƒ The program which is associated with the interrupt is called the interrupt
service routine (ISR) or interrupt handler

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

30. What is a stepper motor in 8051?

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

Common questions

Powered by AI

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 .

You might also like