Download as pdf or txt
Download as pdf or txt
You are on page 1of 37

Microcontroller

Laboratory
Branch: ELECTRONICS AND
TELECOMMUNICATION
ENGINEERING
Third Year SEM IV
Subject Code: ECL401
Department
of
Electronics And Telecommunication Engineering

Mahatma Education Society’s


Pillai HOC College of engineering and Technology Engineering, Rasayani
Department of Telecommunication Engineering
1
ESAY 2020-21

List of Experiments
Subject: Microcontrollers Laboratory Class: SE EXTC Sem: IV

Sr.
No. Name of the experiment Page No

01 Arithmetic Operations Using 8051 3

02 Packing And Unpacking of BCD Numbers On 8051 7

03 Finding Largest Number Using 8051 10

04 Arranging Numbers In Ascending Order Using 8051 12

05 Buzzer Interfacing With 8051 14

06 DC Motor Interfacing With 8051 16

07 Stepper Motor Interfacing With 8051 21

08 LCD Interfacing With 8051 24

09 Buzzer Interfacing With ARM7 33

10 Switch and LED Interfacing With ARM7 35

Hardware Requirements 8051, ARM Trainer Kits

Software Requirements C51 compiler, Flash Magic

Subject Incharge Incharge HOD

2
Experiment No 01

Arithmetic Operations Using 8051


Aim: To perform Arithmetic operations: Addition, Subtraction, Multiplication and division using
8051 Microcontroller.

Apparatus: 8051 Trainer Kit.

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:

C000: MOV A, #04H ; Move first number in to accumulator.

MOV R0, #08H ; Move second number to Register.

ADD A, R0 ; Add first and second number and store result in accumulator

MOV DPTR, #C050H ; Initialize data pointer at C050H.

MOVX @DPTR, A ; Store result at data pointer.

LJMP 6000H ; return to command mode on DYNA-51

2. Subtraction:

C000: MOV A, #05H ; -Move first number in to accumulator.

MOV R0, #03H ; - Move second number to Register.

SUBB A, R0 ; - Subtract first and second number and store result in A

MOV DPTR, #C051H ; - Initialize data pointer at C051H.

MOVX @DPTR, A ; - Store result at data pointer .

LJMP 6000H

3. Multiplication:

C000: MOV A,#25H ;-Move first number in to accumulator.

MOV B, #02H ;- Move second number to Register B.

MUL AB ;- Multiply first and second number and store result in accumulator

MOV DPTR, #C052H ; - Initialize data pointer at C052H.

MOVX @DPTR, A ;- Store result at data pointer .

MOV A, B ; - Move remainder to Accumulator.

MOV DPTR, #C053H

MOVX @DPTR, A

LJMP 6000H
4
4. Division:

C000: MOV A, #27H ;Move first number in to accumulator.

MOV B, #03H ; Move second number to Register.

DIV AB ; Divide first and second number and store result in accumulator

MOV DPTR, #C054H ; Initialize data pointer at C053H.

MOVX @DPTR, A ; Store result at data pointer.

MOV A, B ; Move contents of B register to Accumulator.

MOV DPTR, #C055H ;

MOVX @DPTR, A

LJMP 6000H

5
Results:

Before Execution:

After Execution:

Conclusion:

....................................................................................................................................................................
....................................................................................................................................................................
...................................................................................................................................................................

6
Experiment No 02

Packing and Unpacking of BCD Numbers


Aim: Write & Execute assembly language program to pack the unpack BCD Number and
program to unpack the pack BCD Number using 8051 microcontroller.

Apparatus: 8051 Trainer Kit.

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.

C000: MOV B, #02H ; Move first number in to B Register

MOV A, #06H ; Move second number in to accumulator

SWAP A ; Swap Lower nibble with higher nibble

ADD A, B

MOV DPTR, #C055H

MOVX @DPTR, A

LJMP 6000H

2. Program to unpack the pack BCD.

C000: MOV A, #62H

MOV B, A

ANL A, #0FH

MOV DPTR, #C050H

MOVX @DPTR, A

MOV A, B

SWAP A

ANL A, #0FH

MOV DPTR, #C051H

MOVX @DPTR, A

LJMP 6000H

8
Result:

Before Execution:

After Execution:

Conclusion:

......................................................................................................................................................
......................................................................................................................................................
......................................................................................................................................................

9
Experiment No 03

Finding Largest Number Using 8051


AIM: To developed 8051 assembly language program for finding largest number in given
memory block.

APPARATUS: 8051 Trainer Kit

PROGRAM:

A C000

LOC OBJ LINE SOURCE

C000 7B06 1 MOV R3, #06H ; COUNTER


C002 90D000 2 MOV DPTR, #0D000H ; BLOCK POINTER
C005 E0 3 MOVX A, @DPTR ; READ FIRST NO IN ACC
C006 F9 4 MOV R1, A ; SAVE AS LARGEST NO IN R1

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

Arranging Numbers in Ascending Order Using 8051


Aim: To develop 8051 assembly language program to arrange numbers in memory block in
ascending order.

Apparatus: 8051 Trainer Kit.

Program:

LOC OPCODE LINE SOURCE COMMENTS

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.

Apparatus: 8051 Trainer Kit

Software: Keil uVision5,


Flash Magic.

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.

Figure: Interfacing of Buzzer

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,

// function for generating a delay


void delay_ms(unsigned int ms)
{
unsigned int i, j;
for (i = 0; i < ms; i++)
for (j = 0; j < 53; j++);
}

/************ main function ********************/


void main (void)
{
while(1)
{
buzzer = 0; //BUZZER ON
delay_ms(100); //DELAY
buzzer = 1; // BUZZER OFF
delay_ms(100); //DELAY
}
}

Conclusion:
......................................................................................................................................................
......................................................................................................................................................
......................................................................................................................................................

15
Experiment No 06

DC Motor Direction Control

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.

Figure: Interfacing Motors Using L293D Driver IC

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

Table: Logic table for motor direction control.

17
Program:

/*******************************************************************/
/* File: motorControl.h */
/*******************************************************************/

sbit LB = P1^0; //LEFT BACK Pin


sbit LF = P1^1; //LEFT FRONT Pin
sbit RF = P1^2; //RIGHT FRONT Pin
sbit RB = P3^4; //RIGHT BACK Pin
sbit left_velocity = P1^3; //Left velocity control pin.
sbit right_velocity = P1^4; //Right velocity control pin.

18
/******************************************************************/
/* File: motorControl.c */
/******************************************************************/

// Function: delay_ms(unsigned int ms)


// Generates delay
void delay_ms(unsigned int ms)
{
unsigned int i,j;
for(i=0; i<ms; i++)
for(j=0; j<53; j++);
}

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"

sbit buzzer=P2^7; //buzzer = 1; buzzer off, buzzer = 0; buzzer on,

// Function: void main(void)


void main (void)
{
buzzer = 1; //switch off the buzzer
left_velocity = 1; //enable left morot drivers.
right_velocity= 1; //enable right morot drivers.

while(1)
{
forward(); // forward motion
delay_ms(2500);
stop(); // stops
delay_ms(2500);

backward(); // backward motion


delay_ms(2500);
stop(); // stops
delay_ms(2500);

left(); // left turn


delay_ms(2500);
stop(); // stops
delay_ms(2500);

right(); // write turn


delay_ms(2500);
stop(); // stops
delay_ms(2500);
}
}

Conclusion:
..................................................................................................................................................................
..................................................................................................................................................................
..................................................................................................................................................................

20
Experiment No 07

Stepper Motor Interfacing With 8051


Aim: To write 8051 assembly language program to drive stepper motor.

Apparatus: 8051 Trainer Kit,


Stepper Motor with driver circuit

Software: Keil uVision5,


Flash Magic.

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.

Full-Step or 4-Step Sequence:


Full step sequence can be achieved in two ways as shown in figure 1a and 1b. In first
way two windings will be on for single step as shown in figure 1a and in next type single
winding will be on for each step..

Step 1 Step 2 Step 3 Step 4

Figure 1a: Full Step Sequence (type 1).

Step 1 Step 2 Step 3 Step 4


Figure 1b: Full Step Sequence (type 2).

21
Half-Step or 8-Step Sequence:

Figure 2 shows half stepping sequence.

Step 1 Step 2 Step 3 Step 4

Step 5 Step 6 Step 4 Step 8

Figure 2: Half Step or 8-Step Sequence.

22
Program:

Full Step Sequence Clockwise(Two Coils):

// 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

LCD Interfacing in 4-Bit Mode

Aim: To write 8051 program in embedded C language to initialize and display data string on
LCD display.

Apparatus: 8051 Trainer Kit.

Software: Keil uVision5,


Flash Magic.

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.

Figure 2: LCD Timing Diagram.

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

Table 1: LCD Commands Format

Note:
I/D:- 0 - Decrement Cursor After Write
1 - Increment Cursor after Write

S:- 0 - Do not shift display after write


1 - Shift display after write

D:- 0 - Display OFF


1 - Display ON

C:- 0 - Cursor OFF


1 - Cursor ON

B:- 0 - Cursor Blink OFF


1 - Cursor Blink ON

S/C:- 0 - Select Cursor For Shift


1 - Select Display for shift

R/L:- 0 - Shift Right


1 - Shift Left

DL:- 0 - 4-bit data bus (D7-D4)


1 - 8-bit data bus (D7-D0)

N:- 0 - One Line Display


1 - Two line Display

F:- 0 - 5x7 Font


1 - 5x10 Font

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 */
/*************************************************************/

sbit buzzer = P2^7; //buzzer = 1; buzzer OFF, buzzer = 0; buzzer ON,


sbit RS = P2^6; //P2^6 is connected to the RS line of LCD
sbit RW = P2^5; //P2^5 is connected to the RW line of LCD
sbit E = P2^4; //P2^4 is connected to the EN line of LCD
sbit BUSY = P2^3; //P2^3 is connected to the DB7 pin of LCD

28
/*************************************************************/
/* File: LCD.C */
/* Definitions of functions used for 4-Bit LCD interfacing */
/*************************************************************/

// Function: lcdDelay(unsigned int ms)


// Generates delay using interlaced for loops
void lcdDelay(unsigned int ms)
{
unsigned int i, j;
for(i=0;i<ms;i++)
for(j=0;j<53;j++);
}

// 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

while (busyPin) // wait till BUSY pin is high


{
E = 1; // generate clock pulse High
busyPin = BUSY; // read D7 of LCD i.e P2.3 of 8051
E = 0; // generate clock pulse Low
}
}

// Function: commandSend(unsigned char command)


// function sends data to LCD as command by selecting the instruction register.
// MSB nibble is send first followed by LSB nibble.
void commandSend(unsigned char command)
{
unsigned char tempCommand, P2_MSB_status;

lcdReady();
RW = 0; // select write mode
RS = 0; // select command register
E = 0; // Enable initially low

P2_MSB_status = P2 & 0xF0; //save status of MSB nibble


of P2 29
tempCommand = _cror_(command,4);//swap MSB & LSB nibbles of
command
tempCommand = tempCommand & 0x0F;//mask MSBs
P2 = P2_MSB_status | tempCommand;//write command
P2_MSB_status = P2 & 0xF0; //save status of MSB nibble of P2
tempCommand = _cror_(command,4); //swap MSB & LSB nibbles of
command
tempCommand = tempCommand & 0x0F; //mask MSBs
P2 = P2_MSB_status | tempCommand; //write command
E = 1;E = 0; // to generate Enable pulse

P2_MSB_status = P2 & 0xF0; //save status of MSB nibble of P2


tempCommand = command & 0x0F; //Mask MSBs of command
P2 = P2_MSB_status | tempCommand; //write command
E = 1; E = 0; // to generate Enable pulse
}

// Function: dataSend(unsigned char command)


// This function sends data to LCD as data by selecting the data register
void dataSend (unsigned char lcddata)
{
unsigned char tempData, P2_MSB_status;

lcdReady(); // wait till LCD is busy


RW = 0; // select write mode
RS = 1; // select data register
E = 0; // enable is low initially

P2_MSB_status = P2 & 0xF0; //save status of MSB nibble of P2


tempData = _cror_(lcddata,4); //swap MSB & LSB nibbles of command
tempData = tempData & 0x0F; //mask MSBs
P2 = P2_MSB_status | tempData; //write command
E = 1; E = 0; // to generate Enable pulse

P2_MSB_status = P2 & 0xF0; //save status of MSB nibble of P2


tempData = lcddata & 0x0F; //mask MSBs of command
P2 = P2_MSB_status | tempData; //write command
E = 1; E = 0; // to generate Enable pulse
}

// 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 5


msecs for the
E = 1;E = 0;lcdDelay(6); // delay 30

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

P2 = (P2 & 0xF0) | 0x02; //set 4 bit mode


E = 1; E = 0; lcdDelay(2);

commandSend(0x28); //Lines , Font


commandSend(0x0F); //Disp ON, Cursor on ,blink
commandSend(0x06); //Increment Cursor, No shift
commandSend(0x01); //Clear disp & cursor to home position
lcdDelay(40); //Delay required after Clear command
}

// 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

commandSend(0x80); //cursor at 1st post in 1st line


for(i=0;i<16;i++) //write string 1
dataSend(lcd_data1[i]);

commandSend(0xC0); //cursor at 1st post in 2st line


for(i=0;i<16;i++) //write string 2
dataSend(lcd_data2[i]);
}

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

Buzzer Interfacing With ARM7


Aim: To write ARM7 program in embedded C language for turning buzzer ON and OFF.

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.

PINSEL0 Bits Port0 Pin PINSEL0 Bits Port0 Pin


0:1 P0.0 0:1 P0.16
2:3 P0.1 2:3 P0.17
4:5 P0.2 4:5 P0.18
6:7 P0.3 6:7 P0.19
8:9 P0.4 8:9 P0.20
10:11 P0.5 10:11 P0.21
12:13 P0.6 12:13 P0.22
14:15 P0.7 14:15 P0.23
. . . .
. . . .
. . . .
30:31 P0.15 30:31 P0.31
Table: PINSEL0 Bits

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

Switch and LED Interfacing With ARM7

Aim: To write ARM7 program in embedded C language for reading switches and to turn
ON corresponding LEDs.

Apparatus: ARM7 Trainer Kit

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.

Figure 1: Switch Interfacing

35
Program:

/* File: LED.h *************************************************************/


/* Description: Pin Definitions for LEDs *****************************************/
#define LED1 0x00000001
#define LED2 0x00000002
#define LED3 0x00000004
#define LED4 0x00000010
#define LED5 0x00000020
#define LED6 0x00000040
#define LED7 0x00000080
#define LED8 0x00000100

/* File: Switches.h **********************************************************/


/* Description: Pin Definitions for Switches***************************************/
#define SW1 0x04000000
#define SW2 0x02000000
#define SW3 0x01000000
#define SW4 0x00800000
#define SW5 0x00400000
#define SW6 0x00200000
#define SW7 0x00100000
#define SW8 0x40000000

/* File: main.c **************************************************************/


/* Description: main program **************************************************/
#include <lpc214x.h>
#include "switches.h"
#include "Led.h"

int main(void)
{
long LEDs;

PINSEL0 = 0x00000000; //Set P0.0 to P0.15 as GPIO


PINSEL1 = 0x00000000; //Set P016 to P0.31 as GPIO

PINSEL2 = 0x00000000; //Set P1.16 to P1.31 as GPIO


//PINs P1.0 to P1.15 are not available

IO0DIR = 0xFFFFFFFF; //P0 as O/P Pins for LEDs


IO1DIR = 0x00000000; //P1 as I/P Pins for Swiches

while(1)
{
LEDs = 0x00000000; //Reset LEDs variable

//Read Switches And Set Corresponding LED Pin In LEDs variable


if(IOPIN1 & SW1)LEDs |=LED1;
if(IOPIN1 & SW2)LEDs |=LED2;
36
if(IOPIN1 & SW3)LEDs |=LED3;
if(IOPIN1 & SW4)LEDs |=LED4;
if(IOPIN1 & SW5)LEDs |=LED5;
if(IOPIN1 & SW6)LEDs |=LED6;
if(IOPIN1 & SW7)LEDs |=LED7;
if(IOPIN1 & SW8)LEDs |=LED8;

IOSET0 |= LEDs; //Turn On LEDs Corresponding To ON Switches


IOCLR0 |=(~LEDs); //Turn OFF LEDs Corresponding To OFF Switches
}
}

Conclusion:
......................................................................................................................................................
......................................................................................................................................................
......................................................................................................................................................

37

You might also like