Microcontrollers Lab Manual for EEE Students
Microcontrollers Lab Manual for EEE Students
Manual
EEE DEPARTMENT
THIAGARAJAR COLLEGE OF ENGINEERING,
MADURAI -625 015.
2
LIST OF EXPERIMENTS
MODULE - 0 (1 WEEK)
1. INTRODUCTION TO UNIVERSAL EMBEDDED TRAINER (VUET-REV0)
2. INTRODUCTION TO KEIL SOFTWARE
3. INTRODUCTION TO PROTEUS SOFTWARE
MODULE - I (3 WEEKS)
1. LED INTERFACE WITH 8051 USING EMBEDDED C PROGRAM
2. LCD INTERFACE WITH 8051 USING EMBEDDED C PROGRAM
3. MATRIX KEYPAD INTERFACE WITH 8051 USING EMBEDDED C PROGRAM
4. SEVEN SEGMENT LED INTERFACE WITH 8051 USING EMBEDDED C PROGRAM
MODULE - II (1 WEEK)
PROJECT - 1: INTERFACING MULTIPLE PERIPHERAL DEVICES WITH 8051 FOR AN
APPLICATION
MODULE - IV (2 WEEKS)
1. ASSEMBLY LANGUAGE PROGRAMMING USING KEIL
2. CREATING DELAYS USING TIMERS
3. SPEED CONTROL OF STEPPER MOTOR/DC MOTOR
MODULE - V (1 WEEK)
PROJECT - 2: REALIZATION OF A MICROCONTROLLER BASED SYSTEM USING PROTEUS
SOFTWARE / LAB HARDWARE
MODULE - VI (1 WEEK)
INTERFACING PERIPHERALS WITH TM4C123 MICROCONTROLLER KIT.
3
CONTENTS
MODULE - 0
1. INTRODUCTION TO UNIVERSAL EMBEDDED TRAINER (VUET-REV0)
2. INTRODUCTION TO PROTEUS SOFTWARE
INTRODUCTION
VUET is a basic embedded trainer for educational purposes. From the
name UET implies Universal Embedded Trainer. As the name represents
universal it has the provision to plug and develop coding for several controllers.
VUET normally have the AVR controller on the base board additionally we
can port 8051 controller to VUET through EURO connector as a PIGGY BACK.
5
BOARD FEATURES:
MCU:89V51RD2 8 bit with 64K Bytes Program Flash, 1024 bytes Data RAM, 1x
UARTs, SPI, 4 channel (PWM/CAPTURE/COMPARE), Four 8bits 5V tolerant I/O,
up to 40MHz operation.
USB connector for USB Programming and UART Communication
16 LEDs for user interface
4x4 Matrix Keypad buttons
Four seven segment display (LT543) using PCF8574.
One 12V relay with relevant driver Circuit.
Two 8 – way DIP switch for user interface.
Stepper motor Interface with Internal or External voltage selection.
One 16x2 Alphanumeric LCD display with Connector.
One 10k trim pot for LCD brightness Control.
One RTC with battery backup using DS1307 / PCF8583.
One external EEPROM interface using 24LC512.
Temperature sensor(LM35) connected to ADC
On board supply ±12v, 5v
power supply LED
RESET button
SPDT switch for power on off control
11.0592 MHz crystal on socket
I/O lines are terminated through a 26 pin FRC connector for external
interface.
1. Since it is a universal trainer we can add upon any AVR or 8051 core based 8
bit controllers.
2. While we are working with AVR controllers remove the PIGGY BACK and
place the AVR controller in a 40 pin IC base, and use RESET key to reset the
system.
6
The cable you will need depends on the programmer/debugger you use.
USB A-B cable, if you use USB Programmer.
Software:
Keil µVersion3.
Flash Magic software for program Downloading.
Controller FEATURES:
Idle mode
7
Block Diagram
PINOUT Diagram:
RESET CIRCUIT
VSK – AVR reset circuit includes one 4.7k Pull up resistor with VCC was
connected with the 9th pin of the ATmega32.
CLOCK CIRCUIT
When the 1st and 2nd pins of the Jumper SP1 will short means the
external ADC input gets connected to Digital Thermometer DS18B20.
When the 2nd and 3rd pins of the Jumper SP1 will short means the
internal ADC (PA7) input gets connected to Digital Thermometer
DS18B20.
When the 1st and 2nd pins of the Jumper SP2 will short means the
external ADC input gets connected to Temperature Sensor LM35.
When the 2nd and 3rd pins of the Jumper SP2 will short means the
internal ADC (PA7) input gets connected to Temperature Sensor LM35.
NOTE: while we are working with 8051 don’t be bother about these SP1 and SP2 jumpers.
When the 1st and 2nd pins of the Jumper SP3 will short means the
stepper motor will gets power externally to drive the motor via P1
connector.
If the 2nd and 3rd pin of the Jumper SP3 gets shorted means you must
give power internally from the board.
When the 1st and 2nd pins of the Jumper SP4 will short means the
relay driver will gets +5V to drive the relay.
If the 2nd and 3rd pin of the Jumper SP4 will short means the relay
driver will gets +12V to drive the relay.
If you connected Common anode display means you must connect 1st
and 2nd pin of the SP5 Jumper.
If you connected Common cathode display means you must connect
2nd and 1st pin of the SP5 Jumper.
When the 1st and 2nd pins of the Jumper SP1 will short means the
external ADC input gets connected to 10KΩ Trimmer.
When the 2nd and 3rd pins of the Jumper SP1 will short means the
internal ADC (PA6) input gets connected to 10KΩ Trimmer.
CONNECTOR DESCRIPTIONS
PWR_JACK (P5):
1 Power Input
2 GND
USB (P10):
INTRODUCTION TO KEIL
Keil is a German based Software development company. It provides several
development tools like
• IDE (Integrated Development environment)
• Project Manager
• Simulator
• Debugger
• C Cross Compiler , Cross Assembler, Locator/Linker
Keil Software provides you with software development tools for the 8051 family of
microcontrollers. With these tools, you can generate embedded applications for the
multitude of 8051 derivatives. Keil provides following tools for 8051 development
1. C51 Optimizing C Cross Compiler,
2. A51 Macro Assembler,
3. 8051 Utilities (linker, object file converter, library manager),
4. Source-Level Debugger/Simulator,
5. µVision for Windows Integrated Development Environment.
The keil 8051 tool kit includes three main tools, assembler, compiler and linker.
An assembler is used to assemble your 8051 assembly program
A compiler is used to compile your C source code into an object file
A linker is used to create an absolute object module suitable for your in-circuit emulator.
8051 project development cycle: - these are the steps to develop 8051 project using keil
2. After installation open the compiler, from the displayed window select
project menu and click New Project for creating new project.
3. By this act a small window is opened to select the appropriate path for
new project, at last gave the project name and save the project.
4. Then select the target for the project, which means to select the working
controller. Our controller is Phillips made with 8051 core, so we want to
select Phillips vendor, from that vendor list select P89V51RD2 controller.
5. Now our project is created, after that we want to create editor window for the
project, for this select file menu and click New from the drop down list.
7. After editing the coding select project from the menu and click “Option
for Target ‘Target 1’ “.
8. Now a new window is opened, form that we want to configure the
oscillator frequency as “ 11.0592” as shown below.
9. Then from the same window select output drop list and check the “Create
HEX file” check box.
10. Finally build the project by selecting project menu and click build all
target files.
11. If there is no error the hex file is generated at the same location where the
project is created.
12. Now load the hex file to the controller by using Flash magic Downloader.
1. Install the Flash Magic setup and open the installed software. Connect the cable.
17
INTRODUCTION TO PROTEUS
About Proteus
ISIS is the software used to draw schematics and simulate the circuits in real time.The
simulation allows human access during run time,thus providing real time simulation.
ARES is used for PCB designing.It has the feature of viewing output in 3D view of the
designed PCB along with components.
The designer can also develop 2D drawings for the product.
Features
ISIS has wide range of components in its library. It has sources, signal generators,
measurement and analysis tools like oscilloscope, voltmeter, ammeter etc., probes for
real time monitoring of the parameters of the circuit, switches, displays, loads like
motors and lamps, discrete components like resistors, capacitors, inductors,
transformers, digital and analog Integrated circuits, semi-conductor switches, relays,
microcontrollers, processors, sensors etc.
ARES offers PCB designing up to 14 inner layers, with surface mount and through hole
packages. It is embedded with the foot prints of different category of components like
ICs, transistors, headers, connectors and other discrete components. It offers Auto
routing and manual routing options to the PCB Designer. The schematic drawn in the
ISIS can be directly transferred ARES.
18
USING PROTEUS
The basic steps are as follows:
Create a new project in ISIS software
Place the components from the library in a new project
Connect them accordingly
Load HEX file (Can be created using Keil software)
Simulate the circuit
MODULE I:
Led Interface:
LEDs are surely one of the most commonly used elements in electronics.
LED is an abbreviation for 'Light Emitting Diode'. When choosing a LED, several
parameters should be looked at: diameter, which is usually 3 or 5 mm
(millimetres), working current which is usually about 10mA (It can be as low as
2mA for LEDs with high efficiency - high light output), and colour of course,
which can be red or green though there are also orange, blue, yellow....
LEDs must be connected around the correct way, in order to emit light and the
current-limiting resistor must be the correct value so that the LED is not damaged or
burn out (overheated). The positive of the supply is taken to the anode, and the
cathode goes to the negative or ground of the project (circuit). In order to identify each
lead, the cathode is the shorter lead and the LED "bulb" usually has a cut or "flat" on
the cathode side. Diodes will emit light only if current is flowing from anode to
cathode. Otherwise, its PN junction is reverse biased and current won't flow. In order
to connect a LED correctly, a resistor must be added in series that to limit the amount
of current through the diode, so that it does not burn out. The value of the resistor is
determined by the amount of current you want to flow through the LED. Maximum
current flow trough LED was defined by manufacturer. High-efficiency LEDs can
produce a very good output with a current as low as 2mA.
19
20
Here PORTB is used to drive the LEDs. In ATMEGA32 have only 32 IO lines only.
It’s not possible to use all the peripherals with that IO lines alone. So we use
74LS273 buffer IC. In that 74LS273 IC we connect all the port lines of PORTB as
inputs and one control line PA0.
21
#include<reg51.h>
#define HIGH 1
#define LOW 0
sbit LEDSET1 = P0^0;
sbit LEDSET2 = P0^1;
unsigned char arr[9]={0x00, 0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
void main()
{
int i;
while(HIGH)
{
for(i=0;i<10;i++)
{
P1 = arr[i];
LEDSET1 = HIGH;
LEDSET2 = HIGH;
Delay(100);
LEDSET1 = LOW;
LEDSET2 = LOW;
}
for(i=9;i>0;i--)
{
P1 = arr[i];
LEDSET1 = HIGH;
LEDSET2 = HIGH;
Delay(100);
LEDSET1 = LOW;
LEDSET2 = LOW;
}
P1 = 0xFF;
LEDSET1 = HIGH;
LEDSET2 = HIGH;
Delay(300);
LEDSET1 = LOW;
LEDSET2 = LOW;
}
}
void Delay(int x)
{
int k,l;
for(k = x;k > 0;k--)
for(l = 0;l < x;l++);
}
CODE OUTPUT:
Both rows LEDs will glow one by one as running fashion and the same pattern reversed. After one cylcle of
glowing, all LEDs will glow.
Reference: https://www.youtube.com/watch?v=e0ZCfxlfzlQ&t=17s
22
The microcontroller devices are using 'smart LCD' displays to output visual
information. The LCD displays have a standard ASCII set of characters plus
Japanese, Greek and mathematical symbols.
Each of the 640 pixels of the display must be accessed individually and this is done
with a number of surface-mount driver/controller chips mounted on the back of
the display. This saves an enormous amount of wiring and controlling so that only
a few lines are required to access the display to the outside world. We can
communicate to the display via an 8-bit data bus or 4-bit data bus. For a 8-bit data
bus, the display requires a +5V supply plus 11 I/O lines. For a 4-bit data bus it only
requires the supply lines plus seven extra lines. When the LCD display is not
enabled, data lines are tri-state which means they are in a state of high impedance
(as though they are disconnected) and this means they do not interfere with the
operation of the microcontroller when the display is not being addressed. The LCD
also requires 3 "control" lines from the microcontroller.
The Enable (E) line allows access to the display through R/W and RS lines.
When this line is low, the LCD is disabled and ignores signals from R/W and RS.
When (E) line is high, the LCD checks the state of the two control lines and responds
accordingly. The Read, Write (R/W) line determines the direction of data between the
LCD and microcontroller. When it is low, data is written to the LCD. When it is
high, data is read from the LCD. With the help of the Register select (RS) line, the
LCD interprets the type of data on data lines. When it is low, an instruction is being
written to the LCD. When it is high, a character is being written to the LCD.
RS: 0 Instruction.
1 Character.
Writing data to the LCD is done in several steps:
Reading data from the LCD is done in the same way, but control line R/W
has to be high. When we send a high to the LCD, it will reset and wait for
instructions. Typical instructions sent to LCD display after a reset are: turning on a
display, turning on a cursor and writing characters from left to right. When the
LCD is initialized, it is ready to continue receiving data or instructions. If it
receives a character, it will write it on the display and move the cursor one space to
the right. The Cursor marks the next location where a character will be written.
When we want to write a string of characters, first we need to set up the starting
address, and then send one character at a time. Characters that can be shown on
the display are stored in data display (DD) RAM. The size of DDRAM is 80 bytes.
The LCD display also possesses 64 bytes of Character-Generator (CG) RAM. This
memory is used for characters defined by the user. Data in CG RAM is represented
as an 8-bit character bit-map. Each character takes up 8 bytes of CG RAM, so the
total number of characters, which the user can define is eight. In order to read in
the character bit-map to the LCD display, we must first set the CG RAM address to
24
starting point (usually 0), and then write data to the display. The definition of a
'special' character is given in the picture
Data
Lines Used:
Example Code:
#include<reg51.h>
sbit rs = P3^2;
sbit en = P3^3;
sbit d7 = P3^7;
sbit d6 = P3^6;
sbit d5 = P3^5;
sbit d4 = P3^4;
unsigned char a[16] = {" TCE "};
unsigned char b[16]= {" EEE "};
unsigned char i;
void delay(int x)
{
int k,l;
for(k = x;k > 0;k--)
for(l = 0;l < x;l++);
}
void data_disp(unsigned char data_out) // LCD take 21ms of execution time.
{
d4 = data_out & 0x01;
d5 = (data_out & 0x02) >> 1;
d6 = (data_out & 0x04) >> 2;
d7 = (data_out & 0x08) >> 3;
}
28
OUTPUT: ‘TCE’ AND ‘EEE’ will be displayed in the centre of first and second row of LCD.
29
In Figure below shows a 4x4 matrix key connected to PORTB. The rows are
connected to an output port and the columns are connected to an input port. If no
key has been pressed, reading the input port yields 1s for all columns since they all
are connected to high (VCC). If all the rows are grounded and a key is pressed, one
of the columns will have 0 since the key pressed provides the path to ground, and
identify the key pressed.
Example Code:
#include<REG51.h>
#define HIGH 1
#define LOW 0
sbit SCANLINE_EN = P0^4;
sbit READLINE_EN = P0^5;
sbit rs = P3^2;
sbit en = P3^3;
sbit d7 = P3^7;
sbit d6 = P3^6;
sbit d5 = P3^5;
sbit d4 = P3^4;
unsigned char LcdData[14]={"PRESSED KEY = "};
unsigned char LcdCommands[7]={0x33,0x32,0x28,0x01,0x06,0x0c,0x80};
while(HIGH)
{
P1 = 0x0E;
SCANLINE_EN=1;
SCANLINE_EN=0;
READLINE_EN=0;
ReadVal = (P1 & 0xF0);
READLINE_EN = 1;
if(ReadVal == 0x70)
DispVal=0x33; //printf("\n\r You Pressed Key:
3"); if(ReadVal == 0xb0)
DispVal=0x32; //printf("\n\r You Pressed Key:
2"); if(ReadVal == 0xd0)
DispVal=0x31; //printf("\n\r You Pressed Key:
1"); if(ReadVal == 0xe0)
DispVal=0x30;
//printf("
\n\r You Pressed Key: 0");
P1 = 0x07;
SCANLINE_EN = 1;
SCANLINE_EN = 0;
READLINE_EN = 0;
ReadVal = (P1 & 0xF0);
READLINE_EN = 1;
if(ReadVal == 0x70)
DispVal=0x46; //printf("\n\r You Pressed Key:
F"); if(ReadVal == 0xb0)
DispVal=0x45; //printf("\n\r You Pressed Key:
E"); if(ReadVal == 0xd0)
DispVal=0x44; //printf("\n\r You Pressed Key:
D"); if(ReadVal == 0xe0)
DispVal=0x43; //printf("\n\r You Pressed Key: C");
P1 = 0x0d;
SCANLINE_EN = 1;
SCANLINE_EN = 0;
READLINE_EN = 0;
P1 = 0x0b;
SCANLINE_EN = 1;
SCANLINE_EN = 0;
READLINE_EN = 0;
ReadVal = (P1 & 0xF0);
READLINE_EN = 1;
if(ReadVal == 0x70)
DispVal=0x42; //printf("\n\r You Pressed Key:
B"); if(ReadVal == 0xb0)
DispVal=0x41; //printf("\n\r You Pressed Key:
A"); if(ReadVal == 0xd0)
DispVal=0x39; //printf("\n\r You Pressed Key:
9"); if(ReadVal == 0xe0)
DispVal=0x38; //printf("\n\r You Pressed Key: 8");
Command_Write_Function(0x8E);
Data_Write_Function(DispVal);
}
}
void Delay_Function(int x)
{
int k,l;
for(k = x;k > 0;k--)
for(l = 0;l < x;l++);
}
OUTPUT:
7 - Segment Interface:
Each display is turned on at a rate above 100 times per second, and it will appear
that all the displays are turned on at the same time.
Here we using PCF8574 IC Remote 8-bit I/O expander for I2C-busfor 7-segment interface.
Because ATMEGA32 has only 32IO lines. For that purpose we use I2c based IO expander
for this interface.
The device consists of an 8-bit quasi-bidirectional port and an I2C-bus interface. The
PCF8574 has a low current consumption and includes latched outputs with high
current drive capability for directly driving LEDs. It also possesses an interrupt line
(INT) which can be connected to the interrupt logic of the microcontroller. By sending
an interrupt signal on this line, the remote I/O can inform the microcontroller if there
is incoming data on its ports without having to communicate via the I2C-bus. This
means that the PCF8574 can remain a simple slave device.
36
Example:
#include<REG51.h>
#define HIGH 1
#define LOW 0
sbit SCL = P2^0;
sbit SDA = P2^1;
bit ack;
unsigned char LookUp[10] =
{0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x67,0x3F};
unsigned char Address[4] = {0x40, 0x42, 0x44, 0x48};
void I2c_Start_Function();
void I2c_Stop_Function();
void I2c_Write_Function(int WriteData);
char I2c_Read_Function();
void Delay_Function(int DelayLength);
void main()
{
int i,j;
for(i=0;i<4;i++)
{
for(j=0;j<10;j++)
{
I2c_Start_Function();
I2c_Write_Function(Address[i]);
37
I2c_Write_Function(LookUp[j]);
I2c_Stop_Function();
Delay_Function(250);
}
Delay_Function(250);
}
while(HIGH);
}
void I2c_Start_Function()
{
SDA = HIGH;
SCL = HIGH;
SDA = LOW;
SCL = LOW;
}
void I2c_Stop_Function()
{
int retry;
retry = 9;
SDA = HIGH;
while (retry--)
if (!SDA)
{
SCL = HIGH;
SCL = LOW;
}
}
void I2c_Write_Function(int WriteData)
{
unsigned char index;
for(index = 0x80; index != 0; index >>= 1)
{
if (WriteData & index)
SDA = HIGH;
else
SDA = LOW;
SCL = HIGH;
SCL = LOW;
}
SDA = HIGH;
SCL = HIGH;
ack = SDA;
SCL = LOW;
//return(ack == 0);
char I2c_Read_Function()
38
{
unsigned char X;
unsigned char index;
SDA = 1;
X = LOW;
for(index = 0x80; index != 0; index >>= 1)
{
SCL = 1;
if (SCL)
SCL = 0; X |= index;
}
SDA = ack; // ACK (0) or NACK (1) bit
SCL = HIGH;
SCL = LOW;
return(X);
}
OUTPUT:
Each 7 segment LED will be glowing from numbers 0- 9.
SAMPLE TASKS:
If first key is pressed all LEDs in the first row should glow from left to right in running
fashion and if the second key is pressed, all LEDs in the second row should glow from
right to left in running fashion.
If a key is pressed, The odd LEDs in the first row should glow, After a delay, the even
LEDs in the second row should glow. Then all LEDs should glow and the process
repeats. If another key is pressed, process is stopped.
Display “tce” if one key is pressed and “EEE’ in seven segment LEDs if second key is
pressed.
39
MODULE - II
PROJECT - 1: INTERFACING MULTIPLE PERIPHERAL DEVICES WITH 8051 FOR AN
APPLICATION
SAMPLE PROJECTS:
Four lights in a room are controlled by a single switch. (Pressing the switch once will
turn on the lights, again pressing will turn off the lights) (SIMPLE PROJECT)
Control a device by two switches, one for switching on and other for switching off.
In a chemical industry, three different products are manufactured. When a key
corresponding to the product is pressed, product number should be displayed in 7-
segment LED. (SIMPLE PROJECT)
Get the marks of a student and display the average mark. (WORTHY PROJECT)
Four devices have to be started sequentially after particular time by pressing the same
switch. (SIMPLE PROJECT)
Design a decorative light sequence for a family function with selection key. (SIMPLE
PROJECT)
In a Cool drinks factory, bottle filling process takes place as follows. The bottles are
placed with a gap in a conveyer belt. Some time is taken to fill the bottle. Simulate
suitable control to move the conveyer belt which is driven by motor. (SIMPLE
40
PROJECT)
In a university, four different grades of marks are given as follows. S= 10; A=8; B=6;
C=0. The grades for six subjects are given as input. For a particular student, get the
information of grades and put the total out of 60 and display the name and total
marks. (WORTHY PROJECT)
Simulate the process of washing machine. If first key is pressed, machine is on.
Second key set the timer value. Third key starts the countdown timer. After time
elapsed,
machine turned off. (WORTHY PROJECT)
Design a system to display the number of cars inside parking lot. If first key is
pressed, it represents one car entering into the lot. If second key is pressed, it
represents one car exiting. (WORTHY PROJECT)
Simulate the process of oven/ induction stove/ Washing machine etc.( WORTHY
PROJECT)
MODULE - III
If we take 8 lines and arrange than so they accept binary values, the total
count will be 256 (this is obtained by a count to 255 plus the value 0). If we connect
these 8 lines into a "black box," they will be called output lines and so we must
provide a single input line. With this arrangement we can detect up to 255
increments between "0" and "1." This black box is called a CONVERTER and since
we are converting from Analogue to Digital, the converter is called an A-to-D
converter or ADC. AD converters can be classified according to different
parameters. The most important parameters are precision and mode of data
transfer.
As to precision, the range is: 8-bit, 10-bit, 12-bit, 14-bit, 16-bit. Since 12-bit
conversion is an industrial standard, the example we have provided below was
done with a 12-bit ADC. The other important parameter is the way data is
transferred to a microcontroller. It can be parallel or serial. Parallel transmission is
faster. However, these converters are usually more expensive. Serial transmission
is slower, but in terms of cost and fewer input lines to a microcontroller, it is the
favourite for many applications. Analogue signals can sometimes go above the
allowed input limit of an ADC. This may damage the converter. To protect the
input, two diodes are connected as shown in the diagram. This will protect from
voltages above 5V and below 0V.
The ADC contains a Sample and Hold circuit which ensures that the input
voltage to the ADC is held at a constant level during conversion. A block diagram
of the ADC is shown in Figure. The ADC has a separate analog supply voltage pin,
AVCC. AVCC must not differ more than ±0.3V from VCC. Internal reference
42
voltages of nominally 2.56V or AVCC are provided On-chip. The voltage reference
may be externally decoupled at the AREF pin by a capacitor for better noise
performance.
Example Code:
#include<REG51.h>
#include<stdio.h>
#define HIGH 1
#define LOW 0
sbit SCL = P2^0;
sbit SDA = P2^1;
bit ack;
void Uart_Transmit_Function(char ch);
void Uart_Initialize_Function();
void I2c_Start_Function();
void I2c_Stop_Function();
void I2c_Write_Function(int WriteData);
char I2c_Read_Function();
void Delay_Function(int DelayLength);
void main()
{
unsigned int I2cReadData;
Uart_Initialize_Function();
I2c_Start_Function();
I2c_Write_Function(0x9C);
I2c_Write_Function(0x00);
I2c_Stop_Function();
Delay_Function(100);
43
while(HIGH)
{
I2c_Start_Function();
I2c_Write_Function(0x9D);
I2cReadData = I2c_Read_Function();
I2c_Stop_Function();
printf("\n\r The ADC Value is %d",
I2cReadData); Delay_Function(50);
}
void Uart_Initialize_Function()
{
TMOD = 0x20;
TH1 = 0xFD;
TL1 = 0x00;
TCON = 0x40;
SCON = 0x52;
SBUF = ' ';
}
void Uart_Transmit_Function(char ch)
{
while((SCON&0X02)!=0X02);
SCON = (SCON&0XFD);
SBUF = ch;
}
void I2c_Start_Function()
{
SDA = HIGH;
SCL = HIGH;
SDA = LOW;
SCL = LOW;
}
void I2c_Stop_Function()
{
int retry;
retry = 9;
SDA = HIGH;
while (retry--)
if (!SDA)
{
SCL = HIGH;
SCL = LOW;
}
}
void I2c_Write_Function(int WriteData)
{
unsigned char index;
44
SCL = HIGH;
ack = SDA;
SCL = LOW;
//return(ack == 0);
char I2c_Read_Function()
{
unsigned char X;
unsigned char index;
SDA = 1;
X = LOW;
for(index = 0x80; index != 0; index >>= 1)
{
SCL = HIGH;
if (SDA)
SCL = LOW; X |= index;
}
SDA = ack; // ACK (0) or NACK (1) bit
SCL = HIGH;
SCL = LOW;
return(X);
}
void Delay_Function(int DelayLength)
{
int i,j;
for(i=0;i<DelayLength;i++)
for(j=0;j<i;j++);
}
#include<REG51.h>
#include<stdio.h>
#define HIGH 1
#define LOW 0
sbit SCL = P2^0;
sbit SDA = P2^1;
bit ack;
45
void I2c_Start_Function();
void I2c_Stop_Function();
void I2c_Write_Function(int WriteData);
void Delay_Function(int DelayLength);
void main()
{
int i;
while(HIGH)
{
I2c_Start_Function();
I2c_Write_Function(0x9C);
I2c_Write_Function(0x40);
I2c_Write_Function(0x00);
Delay_Function(100);
I2c_Write_Function(0x40);
Delay_Function(100);
I2c_Write_Function(0x80);
Delay_Function(100);
for(i=0x80;i<=0xff;i++)
{
I2c_Write_Function(i);
Delay_Function(5);
}
for(i=0xff;i>0x80;i--)
{
I2c_Write_Function(i);
Delay_Function(5);
I2c_Write_Function(0x80);
Delay_Function(100);
I2c_Write_Function(0x40);
Delay_Function(100);
I2c_Write_Function(0x00);
Delay_Function(100);
I2c_Stop_Function();
}
}
46
void I2c_Start_Function()
{
SDA = HIGH;
SCL = HIGH;
SDA = LOW;
SCL = LOW;
}
void I2c_Stop_Function()
{
int retry;
retry = 9;
SDA = HIGH;
while (retry--)
if (!SDA)
{
SCL = HIGH;
SCL = LOW;
}
}
SCL = HIGH;
ack = SDA;
SCL = LOW;
//return(ack == 0);
SAMPLE TASKS:
Display the given waveform in CRO using DAC interface.
create a code for alert of voltage level exceeding a particular value .
Reference: https://www.youtube.com/watch?v=N8IlGfClDEo
47
MODULE - IV
Assembly Language Programming Using Keil:
1.ADDITION OF TWO 8 BIT NUMBERS:
ORG 0000H
MOV A,#05H
MOV R1,#04H
ADD A,R1
MOV DPTR,#3000H
MOVX @DPTR,A
AGAIN : SJMP AGAIN
END
To multiply two data stored in memory location and store the result in external memory.
ORG 0000H
MOV DPTR,#4201H
MOVX A,@DPTR
MOV B,A
INC DPTR
MOVX A,@DPTR
MUL AB
INC DPTR
MOVX @DPTR,A
INC DPTR
MOV A,B
MOVX @DPTR,A
HALT:SJMP HALT
END
3. 8 BIT DIVISION:
To divide two data stored in memory location and store the result in external memory.
ORG 0000H
MOV DPTR,#4201H
MOVX A,@DPTR
MOV B,A
INC DPTR
MOVX A,@DPTR
DIV AB
INC DPTR
MOVX @DPTR,A
INC DPTR
MOV A,B
MOVX @DPTR,A
HALT:SJMP HALT
END
48
To exchange two numbers present in accumulator and register and store the result in external memory
location 9000H.
ORG 0000H
MOV A,#05H
MOV R0,#06H
XCH A,R0
MOV DPTR,#9000H
MOVX @DPTR,A
INC DPTR
MOV A,R0
MOVX @DPTR,A
RET
END
5. BIGGEST NUMBER:
To find the biggest number by using decrement,jumping operators and store the result in external
memory location
ORG 0000H
MOV DPTR,#9000H
MOVX A,@DPTR
MOV R0,A
INC DPTR
MOVX A,@DPTR
MOV R1,A
DEC R0
LOOP:
INC DPTR
MOVX A,@DPTR
MOV R2,A
SUBB A,R1
JC LEVEL
MOV A,R2
MOV R1,A
LEVEL:
DJNZ R0,LOOP
MOV A,R1
MOV DPTR,#9100H
MOVX @DPTR,A
HERE:SJMP HERE
END
49
6. SMALLEST NUMBER:
To determine the smallest number using conditional instructions and store the result in external
memory location 9001H.
ORG 0000H
MOV DPTR,#9000H
MOVX A,@DPTR
MOV R0,A
INC DPTR
MOVX A,@DPTR
MOV R1,A
DEC R0
LOOP:
INC DPTR
MOVX A,@DPTR
MOV R2,A
SUBB A,R1
JNC LEVEL
MOV A,R2
MOV R1,A
LEVEL:
DJNZ R0,LOOP
MOV A,R1
MOV DPTR,#9100H
MOVX @DPTR,A
HERE:SJMP HERE
END
7. ASCENDING ORDER:
To sort the given datas in ascending order using registers,accumulator and external memory location
ORG 0000H
MOV R0,#05
DEC R0
LOOP1:MOV DPTR,#9000
MOV A,R0
MOV R1,A
LOOP2:MOVX A,@DPTR
MOV R2,A
INC DPTR
MOVX A,@DPTR
SUBB A,R2
JNC LOOP3
MOVX A,@DPTR
XCH A,R2
MOVX @DPTR,A
DEC 82
MOV A,R2
MOVX @DPTR,A
INC DPTR
LOOP3:R1,LOOP2
DJNZ R0,LOOP1
50
HALT:SJMP HALT
END
8. DESCENDING ORDER:
Sorting of given datas in descending order and store the result in external memory locations.
ORG 0000H
MOV R0,#0A
DEC R0
LOOP1:MOV DPTR #9000H
MOV A,R0
MOV R1,A
LOOP2:MOVX A,@DPTR
MOV R2,A
INC DPTR
MOVX A,@DPTR
SUBB A,R2
JC LOOP3
MOVX A,@DPTR
XCH A,R2
MOVX @DPTR,A
DEC 82
MOV A,R2
MOVX @DPTR,A
INC DPTR
LOOP3:DJNZ R1,LOOP2
DJNZ R0,LOOP1
HERE:SJMP HERE
END
To convert decimal value stored in register RO to hexa value and store the result in register R1
ORG 0000H
MOV R0,#99d
MOV A,R0
ANL A,#0F0H
SWAP A
MOV B,#0AH
MUL AB
MOV R1,A
MOV A,R0
ANL A,#0FH
ADD A,R1
MOV R1,A
HERE:SJMP HERE
END
51
ORG 0000H
MOV DPTR,#9000H
MOVX A,@DPTR
MOV B,#64H
DIV AB
MOV R0,A
MOV A,B
MOV B,#0AH
DIV AB
SWAP A
ADD A,B
INC DPTR
MOVX @DPTR,A
MOV A,R0
INC DPTR
MOVX @DPTR,A
HERE:SJMP HERE
END
52
PROGRAM:
ORG 0000H
CLR TF0
MOV TMOD,#01H
L1: MOV TLO,#0C H
MOV THO.#0FE H
SET B TRO
L2:JNB TFO ,L1;
CPL P1.0
CLR TRO
CLR TFO
JMP L2
53
Stepper motors operate differently from normal DC motors, which simply spin
when voltage is applied to their terminals. Stepper motors, on the other hand,
effectively have multiple "toothed" electromagnets arranged around a central metal
gear, as shown at right. To make the motor shaft turn, first one electromagnet is given
power, which makes the gear's teeth magnetically attracted to the electromagnet's
teeth. When the gear's teeth are thus aligned to the first electromagnet, they are slightly
offset from the next electromagnet. So when the next electromagnet is turned on and
the first is turned off, the gear rotates slightly to align with the next one, and from
there the process is repeated. Each of those slight rotations is called a "step." In that
way, the motor can be turned a precise angle. There are two basic arrangements for the
electromagnetic coils: bipolar and unipolar.
Theory:
A step motor can be viewed as a DC motor with the number of poles (on both
rotor and stator) increased, taking care that they have no common denominator.
Additionally, soft magnetic material with many teeth on the rotor and stator cheaply
multiplies the number of poles (reluctance motor). Like an AC synchronous motor,
sinusoidal current, allowing a stepless operation, ideally drives it but this puts some
burden on the controller. When using an 8- bit digital controller, 256 micro steps per
step are possible. As a digital-toanalog converter produces unwanted Ohmic heat in
the controller, pulsewidth modulation is used instead to regulate the mean current.
Simpler models switch voltage only for doing a step, thus needing an extra current
limiter: for every step, they switch a single cable to the motor. Bipolar controllers can
switch between supply. voltage, ground, and unconnected. Unipolar controllers can
only connect or disconnect a cable, because the voltage is already hard wired.
54
The torque they produce rates stepper motors. Synchronous electric motors
using soft magnetic materials (having a core) have the ability to provide position-
holding torque (called detent torque, and sometimes included in the specifications)
while not driven electrically. To achieve full rated torque, the coils in a stepper
motor must reach their full rated current during each step. The voltage rating (if
there is one) is almost meaningless. The motors also suffer from EMF, which means
that once the coil is turned off it starts to generate current because the motor is still
rotating. Their needs to be an explicit way to handle this extra current in a circuit
otherwise it can cause damage and affect performance of the motor.
Port Used:
Example Code:
#include<REG51.h>
void main(void)
{
while(1)
{
P2 = 0x24;
Delay_Function(20);
P2 = 0x14;
Delay_Function(20);
P2 = 0x18;
Delay_Function(20);
P2 = 0x28;
Delay_Function(20);
}
}
void Delay_Function(int x)
{
int k,l;
for(k = x;k > 0;k--)
for(l = 0;l < x;l++);
}
}
SAMPLE TASK:
MODEL QUESTIONS
Interface 8051 with suitable devices and develop an embedded C program for the given
task:
1. If one key is pressed LED first array should blink. If another key is pressed, LED second
array should blink.
2. If one key is pressed LCD displays “CALL Incoming”. If another key is pressed LCD
displays “CALL Outgoing”.
5V
2.5v
56
7. Find the digital equivalent for the analog voltage levels 1v, 2.5v, and 4v
9. Rotate the stepper motor 180 clockwise and immediately 180 anticlockwise and stop the
motor. Assume step angle = 6°
10. Display “tce” if one key is pressed and “EEE’ in seven segment LEDs if second key is
pressed.
11. If a key is pressed, The odd LEDs in the first row should glow, After a delay, the even
LEDs in the second row should glow. Then all LEDs should glow and the process
repeats. If another key is pressed, process is stopped.
13. If first key is pressed all LEDs in the first row should glow from left to right in running
fashion and if the second key is pressed, all LEDs in the second row should glow from
right to left in running fashion.
15. Obtain the 4- bit BINARY sequence of illumination in LED arrays and correspondingly
display the hexa number in seven segment LED.
16. Blinking of first four LEDS in the two Led arrays; Use timer to produce delay.
17. Assembly language programs for expression evaluation, Data transfer, Sorting, Finding
biggest/smallest data, Binary to BCD and vice versa, finding number of odd and even
numbers in an array.
VIVA QUESTIONS:
Students should able to explain each and every line of the code they have written.
HARDWARE:
1. Which ports are used?
2. List the various hardware connected.
3. Explain the interfacing of hardware with 8051.
4. How a particular hardware is addressed / programmed / controlled by 8051?
5. Explain the architecture of chips interfaced. ( 74LS273, PCF8574, LTS543, PCF8591, 74LS244 )
6. Explain the construction and working principle of Stepper motor
7. Explain the construction and working principle of DC motor
57
SOFTWARE:
1. What is the change in output if some particular part of the code is changed?
2. To get a particular output, how the sample code may be modified?
3. Explain the setting of bit patterns in SFRs.
4. How the array values are generated and what is the impact on changing the string or hexa values
initialized in an array?