Professional Documents
Culture Documents
Micro Controllers
Micro Controllers
Laboratory
Branch: ELECTRONICS AND
TELECOMMUNICATION
ENGINEERING
Third Year SEM IV
Subject Code: ECL401
Department
of
Electronics And Telecommunication Engineering
List of Experiments
Subject: Microcontrollers Laboratory Class: SE EXTC Sem: IV
Sr.
No. Name of the experiment Page No
2
Experiment No 01
Theory :
The instruction ADD is used to add two 8 bit operands. One of the operand is always
in register ACC and other operand can be a any register, or immediate data, or in memory.
Memory-to-memory arithmetic operations are never allowed in 8051 assembly language. In
many microprocessors there are two different instructions for subtraction: SUB and SUBB
(subtract with borrow). In the 8051 we have only SUBB. 8051 uses adder circuitry to perform
the subtraction using 2’s complement of the subtrahend (source operand). The 8051 supports
byte by byte multiplication using MUL AB instruction. For multiplication one of the operand
should be in ACC register and second in B register. After multiplication higher byte of result
is stored in B register and lower byte in A. DIV AB is used to divide content of ACC register
with B. After division quotient is stored in ACC and remainder in B.
3
Program:
1. Addition:
ADD A, R0 ; Add first and second number and store result in accumulator
2. Subtraction:
LJMP 6000H
3. Multiplication:
MUL AB ;- Multiply first and second number and store result in accumulator
MOVX @DPTR, A
LJMP 6000H
4
4. Division:
DIV AB ; Divide first and second number and store result in accumulator
MOVX @DPTR, A
LJMP 6000H
5
Results:
Before Execution:
After Execution:
Conclusion:
....................................................................................................................................................................
....................................................................................................................................................................
...................................................................................................................................................................
6
Experiment No 02
Program:
BCD is binary coded decimal number system used in computers to feed decimal
numbers in computers (i.e. numbers 0 to 9). Single byte can store two BCD digits e.g.
89(1000 1001B), where two decimal digits 8 and 9 are packed in one byte. Such a BCD
numbers are called packed BCD numbers. On other hand each BCD digit can be stored
separately to occupy two bytes e.g. 08(0000 1000B) and 09(00001001B). Such BCD
numbers are called unpacked BCD numbers.
It is easy to perform arithmetic operations on packed BCD numbers. It is some time
required to separate packed BCD digits e.g. to display BCD number on LCD display it is
required to unpack two BCD digits and adding 30H to each digit to convert them in to ASCII
character.
7
1. Program to pack the unpack BCD.
ADD A, B
MOVX @DPTR, A
LJMP 6000H
MOV B, A
ANL A, #0FH
MOVX @DPTR, A
MOV A, B
SWAP A
ANL A, #0FH
MOVX @DPTR, A
LJMP 6000H
8
Result:
Before Execution:
After Execution:
Conclusion:
......................................................................................................................................................
......................................................................................................................................................
......................................................................................................................................................
9
Experiment No 03
PROGRAM:
A C000
C007 LOOP:
C007 A3 5 INC DPTR ; POINT TO NEXT NO
C008 E0 6 MOVX A,@DPTR ; READ NEXT NUMBER IN ACC
C009 FA 7 MOV R2, A ; SAVE NEXT NUMBER IN R2
; COMPARE NO1 & NO2
C00A C3 8 CLR C
C00B 99 9 SUBB A, R1 ; ACC = NO2 - NO1
C00C 4002 10 JC GO_AHEAD ; JUMP IF NO2 < NO1
C00E EA 11 MOV A, R2 ; NO2 AS LARGEST NUMBER
C00F F9 12 MOV R1, A ; NO2 AS LARGEST NUMBER
C010 GO_AHEAD:
C010 DBF5 13 DJNZ R3, LOOP ; LOOP
C012 E9 14 MOV A, R1
C013 90D007 15 MOV DPTR, #0D007H
C016 F0 16 MOVX @DPTR, A
C017 026000 17 LJMP 6000H
10
Result:
Before Execution:
After Execution:
Conclusion:
......................................................................................................................................................
......................................................................................................................................................
......................................................................................................................................................
11
Experiment No 04
Program:
C000 START:
C000 7C03 1 MOV R4,#03H ;ITERATION COUNT
C002 NXT_ITER:
C002 7B03 2 MOV R3,#03H ;COMPARISION COUNT
C004 90D000 3 MOV DPTR,#0D000H ;POINTER TO BLOCK
C007 NXT_COMP:
C007 E0 4 MOVX A,@DPTR ;READ NO1 IN R1
C008 F9 5 MOV R1, A ;READ NO1 IN R1
C009 A3 6 INC DPTR ; READ NO2 IN R2
C00A E0 7 MOVX A, @DPTR ; READ NO2 IN R2
C00B FA 8 MOV R2, A ;READ NO2 IN R2
C00C 99 9 SUBB A, R1 ; ACC = NO2 - NO1
C00D 500E 10 JNC SKIP_SWAP ; IF NO1>NO2 SWAP
C00F E9 11 MOV A, R1 ; STORE NO1 AT 2nd POSITION
C010 F0 12 MOVX @DPTR, A ; STORE NO1 AT 2nd POSITION
C011 1582 13 DEC DPL;
C013 AD82 14 MOV R5, DPL;
C015 BDFF02 15 CJNE R5, #0FFH, MOVE_AHEAD;
C018 1583 16 DEC DPH;
C01A MOVE_AHEAD:
C01A EA 17 MOV A,R2 ;STORE NO2 AT FIRST POSITION
C01B F0 18 MOVX @DPTR, A ;STORE NO2 AT FIRST POSITION
C01C A3 19 INC DPTR ; POINT TO SECOND POSITION
C01D SKIP_SWAP:
C01D DBE8 20 DJNZ R3, NXT_COMP
C01F DCE1 21 DJNZ R4, NXT_ITER
C021 026000 22 LJMP 6000H
12
Result:
Before Execution:
After Execution:
Conclusion:
......................................................................................................................................................
......................................................................................................................................................
......................................................................................................................................................
13
Experiment No 05
Buzzer Interfacing With 8051
Aim: To write 8051 embedded C language program to turn on and off buzzer.
Theory:
Piezo buzzer can be used for debugging purpose or as attention seeker for a particular event. The
buzzer is can be interfaced with GPIO pins of microcontroller as shown in figure.
14
Program:
/******* This program turns the buzzer ON and OFF with a finite delay in between ******/
#include <intrins.h>
#include "p89v51rx2.h"
sbit buzzer=P2^7; //buzzer = 1; buzzer off,
//buzzer = 0; buzzer on,
Conclusion:
......................................................................................................................................................
......................................................................................................................................................
......................................................................................................................................................
15
Experiment No 06
Aim: To interface and write 8051 program in embedded C language to control directions of
DC motors.
Apparatus:
8051 Trainer Kit, DC Motors with driver circuit.
Software:
Keil uVision5,
Flash Magic
Theory:
DC motors are used for the locomotion. Microcontroller pins are not capable of
supplying current required to drive DC motor. Hence motors can be controlled using driver
circuits such as IC L293D. To change the direction of the motor, appropriate logic levels
(High/Low) are applied to L293D’s direction control pins. Velocity control is done using pulse
width modulation (PWM). Single L293D can drive 2 DC motors. Each channel of the motor
driver IC can provide current up to 600mA to drive motor.
16
Direction LEFT LEFT RIGHT RIGHT
Backward Forward Forward Backward
(LB) (LF) (RF) (RB)
P1.0 P1.1 P1.2 P3.4
FORWARD 0 1 1 0
REVERSE 1 0 0 1
RIGHT (Left wheel forward, Right 0 1 0 1
wheel backward)
LEFT(Left wheel backward, Right 1 0 1 0
wheel forward,)
SOFT RIGHT(Left wheel forward, 0 1 0 0
Right wheel stop)
SOFT LEFT(Left wheel stop, 0 0 1 0
Right wheel forward,)
SOFT RIGHT 2 (Left wheel stop, 0 0 0 1
Right wheel backward)
SOFT LEFT 2 (Left wheel 1 0 0 0
backward, Right wheel stop)
STOP 0 0 0 0
17
Program:
/*******************************************************************/
/* File: motorControl.h */
/*******************************************************************/
18
/******************************************************************/
/* File: motorControl.c */
/******************************************************************/
void forward(void)
{
RF=1;RB=0; // right motor forward
LF=1;LB=0; // left motor forward
}
void backward(void)
{
RF=0;RB=1; // right motor backward
LF=0;LB=1; // left motor backward
}
void left(void)
{
RF=1;RB=0; // right motor forward
LF=0;LB=1; // left motor backward
}
void right(void)
{
RF=0;RB=1; // right motor backward
LF=1;LB=0; // left motor forward
}
void stop(void)
{
RF=0;RB=0; // right motor OFF
LF=0;LB=0; // left motor OFF
}
19
/* File: main.c *************************************************/
#include "p89v51rx2.H"
#include "motorcontrol.h"
#include "motorcontrol.c"
while(1)
{
forward(); // forward motion
delay_ms(2500);
stop(); // stops
delay_ms(2500);
Conclusion:
..................................................................................................................................................................
..................................................................................................................................................................
..................................................................................................................................................................
20
Experiment No 07
Theory:
A stepper motor is a widely used device that translate electrical pulses into
mechanical movement. In applications such as disk drives, dot matrix printers, and robotics,
the stepper motor is used for position control. Stepper motor have a permanent magnet as
rotor surrounded by stator. The stepper motor have four stator windings that are paired with a
centre-tapped common. These type of motor is commonly referred as a four-phase or unipolar
stepper motor.
21
Half-Step or 8-Step Sequence:
22
Program:
// B2 P0.0
// A2 P0.1
// B1 P0.2
// A1 P0.3
// A1B1A2B2
//Coil Sequence B1B2A1A2 (clockwise)
//Coil Sequence B2B1A2A1 (anticlockwise)
MAIN: mov a, #05h
mov P0,a //Step-1 B1B2
call delay
mov a, #09h
mov P0,a //Step-2 A1B2
call delay
mov a, #0ah
mov P0,a //Step-3 A1A2
call delay
mov a, #06h
mov P0,a //Step-4 B1A2
call delay
sjmp MAIN
DELAY:
L2: mov R1, #04H
L1: mov R3, #0FFH
L3: djnz R3,L3
djnz R1,L1
ret
Conclusion:
…………………………………………………………………………………………………
…..………………………………………………………………………………….………….
………………………….……………………………………………………………………….
23
Experiment No 08
Aim: To write 8051 program in embedded C language to initialize and display data string on
LCD display.
Theory:
Normally to interface LCD with the microcontroller requires 3 control signals and 8 data
lines. This is known as 8 bit interfacing mode which requires total 11 I/O lines. To save number
of I/Os required for LCD interfacing we can use 3 control signals with 4 data lines. This is known
as 4 bit interfacing mode and it requires 7 I/O lines. We are using 4 bit interfacing mode to reduce
number of I/O lines. In this mode higher nibble and lower nibble of commands/data set needs to
be sent separately. Figure 1 shows LCD interfacing in 4 bit mode. The three control lines are
referred to as EN, RS, and RW.
The EN line is called "Enable" and it is connected to PORT 2’s 5th pin (P2.4). This
control line is used to tell the LCD that microcontroller has sent data to it or microcontroller is
ready to receive data from LCD. This is indicated by a high-to-low transition on this line. To send
data to the LCD, program should make sure that this line is low (0) and then set the other two
control lines as required and put data on the data bus. When this is done, make EN high (1) and
wait for the minimum amount of time as specified by the LCD datasheet, and end by bringing it
to low (0) again.
The RS line is the "Register Select" line and it is connected to PORT 2’s 7 th pin (P2.6).
When RS is low (0), the data is treated as a command or special instruction by the LCD (such as
clear screen, position cursor, etc.). When RS is high (1), the data being sent is treated as text data
which should be displayed on the screen. The RW line is the "Read/Write" control line and it is
connected to PORT 2’s 6th pin (P2.5). When RW is low (0), the information on the data bus is
being written to the LCD. When RW is high (1), the program is effectively querying (or reading
from) the LCD.
The data bus is bidirectional, 4 bit wide and is connected to PORT2 ( P2^0 to P2^3) of
the microcontroller. The MSB bit (DB7) of data bus is also used as a Busy flag. When the Busy
flag is 1, the LCD is in internal operation mode, and the next instruction will not be accepted.
When RS = 0 and R/W = 1, the Busy flag is output on DB7. The next instruction must be written
after ensuring that the busy flag is 0.
24
Figure 1: LCD Interfacing.
25
Figure 3: LCD Interfacing.
26
COMMAND CODE
RS RW D7 D6 D5 D4 D3 D2 D1 D0
Clear Display 0 0 0 0 0 0 0 0 0 1
Cursor At Home 0 0 0 0 0 0 0 0 1 *
Entry Mode 0 0 0 0 0 0 0 1 I/D S
Display ON/OFF 0 0 0 0 0 0 1 D C B
Cursor/ Display Shift 0 0 0 0 0 1 S/C R/L * *
Function Set 0 0 0 0 1 DL N F * *
Write Data 1 0 DATA
Note:
I/D:- 0 - Decrement Cursor After Write
1 - Increment Cursor after Write
27
Program:
/*************************************************************/
/* File: LCD.H */
/* PIN definitions used for 4-Bit LCD interfacing */
/* LCD D4 = P2.0 */
/* LCD D5 = P2.1 */
/* LCD D6 = P2.2 */
/* LCD D7 = P2.3 */
/*************************************************************/
28
/*************************************************************/
/* File: LCD.C */
/* Definitions of functions used for 4-Bit LCD interfacing */
/*************************************************************/
// Function: lcdReady(void)
// This function waits till LCD busy pin is high
void lcdReady(void)
{
bit busyPin;
E = 0; // Enable initially low
BUSY = 1; // make BUSY pin as INPUT pin
RS = 0; // select command register
RW = 1; // select read mode
lcdReady();
RW = 0; // select write mode
RS = 0; // select command register
E = 0; // Enable initially low
// Function: lcdInit(void)
// This function initialises LCD display in 4-bit mode.
void lcdInit(void)
{
RS = 0; // to select comand register
RW = 0; // to select write mode
lcdDelay(40); // Wait more than 15 msecs after power is applied
E = 0;
P2 = (P2 & 0xF0) | 0x03; // Write 0x03 to LCD and wait 160
usecs for
P2 = (P2 & 0xF0) | 0x03; //write 0x03 to LCD and wait 5 msecs for the
E = 1;E = 0; lcdDelay(6); // delay
P2 = (P2 & 0xF0) | 0x03; // write 0x03 to LCD and wait 160 usecs for
E = 1;E = 0; lcdDelay(2); // delay
P2 = (P2 & 0xF0) | 0x03; // Write 0x03 to LCD again and wait 160 usecs for
E = 1;E = 0; lcdDelay(2); // delay
// Function: lcdDisplay(void)
// Display two strings on two lines in LCD
void lcdDisplay(void)
{
unsigned int i = 0;
unsigned char lcd_data1[16]={" PHCET RASAYANI "};//16 chars
unsigned char lcd_data2[16]={" # MPMC LAB # "}; //16 chars
31
/******************************************************************/
/* File: main.c */
/* Initialises LCD & prints strings on two lines */
/******************************************************************/
#include <intrins.h>
#include "p89v51rx2.h"
#include "LCD.H"
#include "LCD.C"
// main function
void main (void)
{
lcdInit(); //Initialiase LCD in 4-Bit Mode
buzzer = 1; //buzzer off
lcdDisplay(); // Display two strings on two lines
while(1)
{
commandSend(0x18); //Shift Disp Left
lcdDelay(2000); //delay
commandSend(0x1C); //Shift Disp Right
lcdDelay(2000); //delay
commandSend(0x1C); //Shift Disp Right
lcdDelay(2000); //delay
commandSend(0x18); //Shift Disp Left
lcdDelay(2000); //delay
}
}
Conclusion:
......................................................................................................................................................
......................................................................................................................................................
......................................................................................................................................................
32
Experiment No 09
Apparatus:
ARM7 Trainer Kit.
Theory:
Each port of ARM7 has configuration registers associated with it. These are used to configure
port pins as either input or output pins. Also using these registers pin status can be read as
well as set to desired value.
PINSEL0:
The PINSEL0 register controls the functions of the P0.0 to P0.15 pins. Port pin act as
GPIO pin when corresponding bits in PINSEL0 register are set to 00.
PINSEL1:
The PINSEL1 register controls the functions of the P0.16 to P0.31 pins. Port pin act
as GPIO pin when coresponding bits in PINSEL1 register are 00.
IOPIN0:
This is GPIO Port Pin value register. The current state of the GPIO configured port
pins can always be read from this register, regardless of pin direction. By writing to this
register port’s pins will be set to the desired level instantaneously.
IOSET0:
This is GPIO Port Output Set register. This register controls the state of output pins in
conjunction with the IOCLR register. Writing ones produces highs at the corresponding port
pins. Writing zeroes has no effect.
33
IODIR0:
GPIO Port Direction control register. This register individually controls the direction
of each port pin.
IOCLR0:
This is GPIO Port Output Clear register. This register controls the state of output pins.
Writing ones produces lows at the corresponding port pins and clears the corresponding bits
in the IOSET register. Writing zeroes has no effect.
Program:
/**************************************************************************/
/**Buzzer Is Connected to P0.20 **********************************************/
/**************************************************************************/
#include <lpc214x.h>
/**************************************************************************
Function : Buzzer_Delay
Description : Provides small amount of delay between buzzer toggles.
**************************************************************************/
void Buzzer_Delay(void)
{
unsigned int i, j;
for(j=0; j<5; j++)
{
for(i=0; i<60000; i++);
}
}
int main(void)
{
PINSEL1 &= 0xFFFFFCFF; //Select P0.20 as GPIO
IO0DIR|= 0x00100000; //Set P0.20 as Output Pin
while(1)
{
IO0SET |= 0x00100000; //Turn ON buzzer
Buzzer_Delay(); //Wait
IO0CLR |= 0x00100000; //Turn OFF Buzzer
Buzzer_Delay(); //Wait
}
}
Conclusion:
......................................................................................................................................................
......................................................................................................................................................
......................................................................................................................................................
34
Experiment No 10
Aim: To write ARM7 program in embedded C language for reading switches and to turn
ON corresponding LEDs.
Theory:
Switches are used to input information to microcontrollers system. Figure 1 shows
interfacing of switches with microcontroller port pins. Pull-Up resistors are used to pull up
port pin to Vcc, when switch is open. When switch is pressed corresponding port pin goes
low.
35
Program:
int main(void)
{
long LEDs;
while(1)
{
LEDs = 0x00000000; //Reset LEDs variable
Conclusion:
......................................................................................................................................................
......................................................................................................................................................
......................................................................................................................................................
37