MCLab Manual Final

You might also like

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

CHAITANYA BHARATHI INSTITUTE OF TECHNOLOGY

Autonomous Institution under UGC


Hyderabad-500 075 -A.P.

DEPARTMENT
OF
ELECTRONICS & COMMUNICATION ENGINEERING

Microcontrollers Lab Manual

B.E. (ECE)
AICTE Model Curriculum
(with effect from AY 2018-19)
MICROCONTROLLERS LAB

This lab is associated with the theory subject ‘Microcontrollers’. The objective of
the lab is to aid the student to understand the programming aspects of two different
microcontrollers INTEL 8051 and LPC2148and practically implement simple programs
and interfacing techniques.

The contents of the lab manual are presented in step by step format. Initially
programs are written using instructions of 8051 microcontroller that can perform simple
tasks like data transfer, manipulation of data, arithmetic operations, branching operations.
Experiments illustrating interfacing 8051 are also performed with different peripherals.
Typical peripheral devices like LED, Switch, Relay, Buzzer, Digital to Analog Converter,
Liquid crystal display and Stepper motor are included in the experiments.

In the second cycle small programs are written using instructions of ARM7 based
microcontroller. Experiments illustrating working of on-chip and off-chip peripherals of
LPC2148 are also performed. On-chip peripheral devices like Analog to Digital converter
and Digital to Analog Converter and off-chip peripherals like LED, Switch, Relay,
Buzzer and DC motor are included in the experiments.

At the end of the lab, the student will be:


- Able to understand mnemonics of 8051 microcontroller and ARM7
- Realize the critical importance of flags.
- Helps in interfacing and designing 8051 and LPC2148 microcontroller based
products.

Each batch of students(maximum 3) is assigned a miniproject under open ended


enquiry, which is not included in the regular lab class, but is discussed in theory. The
students are expected to carry out the design, implement and demonstrate the working of
the system.
CBIT (A) AICTE Model Curriculum with effect from AY 2018-19

18ECC24
MICROCONTROLLERS LAB
Instruction 2 P Hours per Week
Duration of SEE 2 Hours
SEE 35 Marks
CIE 15 Marks
Credits 1

Prerequisite: Basic knowledge of programming in C language.

Course Objectives:
This course aims to:
1. Develop and understand the 8051 and ARM7 C programming
2. Understand the usage of Integrated Development Environment (Keil)
3. Control the operation of various peripherals using 8051 and ARM7 microcontroller

Course Outcomes:
Upon completing this course, students will be able to:
1. Develop the programs of 8051 and ARM using their respective instruction set.
2. Understand the usage of various debugging tools available to program different microcontrollers
3. Build code for 8051 and ARM7 to interface various input/output modules
4. Analyze the hardware and software interaction and integration.
5. Design and develop the 8051 and ARM 7 based embedded systems for various applications

List of Experiments

I. 8051 Programming

1. Familiarity and use of 8051 microcontroller trainer kit, Keil IDE and simple programs under different
addressing modes.
2. Assembly programming using instruction set
3. Timer and counter operations and programming using 8051.
4. Interfacing applications using LED, switch, relay and buzzer.
5. Generation of waveforms using DAC by interfacing it with 8051.
6. Stepper motor interfacing.
7. LCD interfacing.
8. Development of Embedded 'C' Code based on the module specifications. (under Structured enquiry)

II. ARM7 Programming

1. Study and use of LPC214x Microcontroller trainer kit and simple programs using its instruction set
2. Interfacing applications using LED, switch, relay and buzzer.
3. DC Motor interfacing.
4. Programming on-chip ADC.
5. Waveform generation using internal DAC.
6. Development of Embedded 'C' Code based on the module specifications. (under Structured enquiry)

III. Design an experiment related to the Embedded Application of your choice using 8051/ARM based
architectures. (under Open ended enquiry)

63
CBIT (A) AICTE Model Curriculum with effect from AY 2018-19

Suggested Reading:
1. Mazidi M.A, Mazidi JG &Rolin D. Mckinlay, “The 8051 Microcontroller & Embedded Systems using
Assembly and C”, 2/e, Pearson Education, 2007.
2. Philips semiconductors, “ARM 7 (LPC 214x) user manual”, 2005.

64
INDEX

S.no. Name of the Experiment Page no.


I. Programming with 8051 Microcontroller

1. Addressing modes 1

2. Bit Operations 5

3. Timer/counter applications and programming using 8051 7

4. Interfacing applications using LED, Switch, relay and buzzer 10

5. Generation of waveforms using DAC by interfacing it with 15


8051.
6. Stepper motor interfacing 19

7. LCD interfacing 24

8. Development of Embedded 'C' Code based on the module 29


specifications. (under Structured enquiry)

II. Programming with LPC 2148 Microcontroller

1. Programs using ARM7 instruction set 30

2. Interfacing applications using LED, Switch, relay and buzzer 31

3. DC Motor interfacing. 36
4. Programming on-chip ADC 38
5. Waveform generation using internal DAC 40
6. Development of Embedded 'C' Code based on the module
specifications. (under Structured enquiry) 42

III. Mini project under Open ended enquiry

1. Sample Mini projects 43

Appendix I Introduction to KEIL uvision IDE and Flash Magic 44


Appendix II Introduction to 8051 Development Board and Its Features 47
Appendix III Introduction to LPC 2148 Development Board and Its Features 49
-x-
I. Programming with 8051 Microcontroller

1. ADDRESSING MODES

Aim: Program to copy the value 55H into RAM memory locations 30H to 35H using
a. Direct Addressing mode.
b. Register Indirect Addressing mode without a Loop.
c. Register Indirect Addressing mode with a Loop.

Hardware:

1. PC with
a. Keil Compiler

Program:

a.
ORG 0 ; Start at location 0
MOV A,#55H ; Load A with value 55H

MOV 30H,A ; Copy A to RAM location 30H


MOV 31H,A ; Copy A to RAM location 31H
MOV 32H,A ; Copy A to RAM location 32H
MOV 33H,A ; Copy A to RAM location 33H
MOV 34H,A ; Copy A to RAM location 34H
HERE: SJMP HERE ;

Flowchart:

START

A 55H

[30H] A
[31H] A
[32H] A
[33H] A
[34H] A

1
STOP
Program:

b.
ORG 0 ; Start at location 0
MOV A,#55H ; Load A with value 55H
MOV R0,#30H ; Load the pointer. R0 = 40H

MOV @R0,A ; Copy A to RAM location R0 points to


INC R0 ; Increment pointer. Now R0 = 41H
MOV @R0,A ; Copy A to RAM location R0 points to
INC R0 ; Increment pointer. Now R0 = 42H
MOV @R0,A ; Copy A to RAM location R0 points to
INC R0 ; Increment pointer. Now R0 = 43H
MOV @R0,A ; Copy A to RAM location R0 points to
INC R0 ; Increment pointer. Now R0 = 44H
MOV @R0,A ; Copy A to RAM location R0 points to

HERE: SJMP HERE

Flowchart:

START

A 55H
R0 30H

[R0] A
R0 R0+1
[R0] A
R0 R0+1
[R0] A
R0 R0+1
[R0] A
R0 R0+1

STOP

2
Program:

c.
ORG 0 ; Start at location 0
MOV A,#55H ; Load A with value 55H
MOV R0,#30H ; Load the pointer. R0 = 40H
MOV R2, #05 ; Load counter, R2 = 5

AGAIN: MOV @R0,A ; Copy A to RAM location R0 points to


INC R0 ; Increment R0 pointer
DJNZ R2,AGAIN ; Loop until counter = zero

HERE: SJMP HERE


Flowchart:

START

A 55H
R0 30H
R2 05

[R0] A
R0 R0+1

R2 R2-1

NO (R2=0)?

YES

STOP

Result:

View internal memory locations

3
Exercise:
1. Write a program to copy the value AAH into RAM memory locations 30H to 35H using
Stack operations.
2. Write a program to copy the complement of a block of 5 bytes of data from RAM
locations starting at 30H to RAM locations starting at 35H.
3. Write a program to copy the data after masking the upper nibble of a block of 5 bytes of
data from External RAM locations starting at 20F0H to RAM locations starting at 30H.
4. Write a program to convert BCD to 7-segment code.
5. Write a program to find the sum of five bytes from 30H to 34H. At the end of the
program, register A should contain the lower byte and register R7 the higher byte.
Assume that RAM locations 30H to 34H have the following values.
[30H] = 7D
[31H] = EB
[32H] = C5
[33H] = 5B
[34H] = 30
6. Write a program to add all digits of your mobile number and save the result in R2. The
result must be in BCD.
7. Write a program to subtract 197795 from 344548 and save the result in RAM memory
location starting at 30H.
8. Write a program that finds the number of zeros in an 8-bit data item.
9. Write a program that finds the position of the first HIGH in an 8-bit data item.
10. Write a program that sets the CY if the number in A is even else set CY = 0.
11. Write a program to convert packed BCD present in accumulator A, to two ASCII
numbers and places them in R2 and R6 registers.

4
2. BIT OPERATIONS

Aim: Program to generate a square wave of 50% duty cycle on bit 2 of port 1.

Hardware:
1. PC with
a. Keil Compiler
2. 8051 Microcontroller kit
3. Serial cable
4. Oscilloscope
Program:
ORG 0H ;Start at location 0
HERE: SETB P1.2 ;Set to high bit 2 of port 1
LCALL DELAY ;Call the delay sub routine
CLR P1.2 ;Clear bit 2 of port 1
LCALL DELAY ;Call the delay sub routine
SJMP HERE ;Jump unconditionally
;Dealy routine
DELAY: MOV R1, #0FFh
NEXT: DJNZ R1, NEXT
RET
Flowchart:

START

P1.2 0

CALL DELAY

P1.2 1

CALL DELAY

5
Result:
Connect P1.2 of 8051 pin to Oscilloscope and view square waveform.

Exercise:
1. Write a program to move bit 4 of RAM location 30H to bit 2 of A.
2. Write a program to see if the accumulator contains even number. If so divide it by 2 else
make it even and divide it by 2.
3. Write a program to save the status of bits P1.1 and P1.2 on RAM bit locations 8 and 9
respectively.
4. Write a program to see whether the accumulator is divisible by 8 or not.

6
3. TIMER AND COUNTER APPLICATIONS

Aim: Program to generate a square wave of 50Hz on P1.2 by using Timer 0 of 8051. Assume
that XTAL = 11.0592 MHz.

Hardware:
1. PC with
a. Keil Compiler
2. 8051 Microcontroller kit
3. Serial cable
4. Oscilloscope
Calculations:

STEP 1. FOR CRYSTAL FREQUENCY = 11.0592MHz


STEP 2. ONE MACHINE CYCLE FREQUENCY = CRYSTAL FREQUENCY/12 =
11.0592/12 = 921.6 KHZ
STEP 3. ONE MACHINE CYCLE TIME PERIOD = 1/FREQUENCY = 1/921.6KHZ =
1.085us
STEP 4. TIMER CALCULATION:
IF U WANT INTERRUPT DELAY OF 10ms THEN
1. 10ms/MACHINE CYCLE TIME = 10ms/1.085us = 9216.59 ~= 9217 = 2401H
2. FFFFH – 2401H = DBFEH
SO THIS IS THE VALUE TO BE LOAD INTO TH AND TL REG TO PRODUCE
INTERRUPT OF 10ms.
LIKE THIS YOU CAN CALCULATE FOR ANY DESIRED VALUE.

7
Program:
; Polling method:

; SPECIAL FUNCTION REGISTER EQUATES

ORG 20C0H ; Start at location 0


P1: EQU 90H
TMOD: EQU 89H
TL0: EQU 8AH
TH0: EQU 8CH
TR0: EQU 8CH
TF0: EQU 8DH
CLR P1.2 ; Clear P2.3
MOV TMOD, #01H ; Timer 0, mode 1(16-bit mode)

HERE: MOV TL0, #0FEH ; TL0=0, the low byte


MOV TH0, #0DBH ; TH0=EEH, the high byte
SETB TR0 ; Start the timer0

AGAIN: JNB TF0, AGAIN ; monitor flag 0, stay until timer rolls over
CPL P1.2 ; complement P2.3 to get hi, lo
CLR TR0 ; stop the timer 0
CLR TF0 ; clear timer 0 flag for next round
SJMP HERE

8
Flowchart:

START

DECIDE TIMER AND MODE OF


OPERATION BY INITILIZING TMOD
REGISTER
TMOD 01H

CALUCULATE & INITIALIZE


TL0, TH0 FOR DELAY OF
5msec
TL0 0
TH0 EEH

START TIMER RUN


TR0 1

NO
TF0 =1

YES
COMPLEMENT P2.3
P1.2 !P1.2

STOP TIMER RUN


TR0 0

CLEAR TF0
TF 0

Result:
Connect P1.2 of 8051 pin to Oscilloscope and view square waveform.
Exercise:
1. Write a program to generate a rectangular wave of 1 kHz with 40% duty cycle on P1.6
pin using Timer 1 of 8051.
2. Write a program to generate a square wave of 100Hz on P1.6 pin using interrupt method.

9
4.a. INTERFACING AN LIGHT EMITTING DIODE (LED)

Aim: Interfacing an LED with a 8051 micro controller


Hardware:
1. PC with
a. Keil Compiler
2. 8051 Microcontroller kit
Circuit Diagram:

Flow Chart:

START

CONNECT PORT 1.0 OF 8051


TO LED LEG

P1.0 = HIGH
TRANSISTOR ON
(LED ON)

DELAY = 1000 ms

P1.0 = LOW
TRANSISTOR OFF
(LED OFF)

DELAY = 1000 ms

STOP

10
Program:
/****Program to interface a LED with a 8051 micro controller***/

#include<reg51.h>
void delay_ms(unsigned int);
sbit LED = P1^0; /*equating P1^0 pin to a constant(LED)*/

void main(void)
{
P1= 0x00;
while(1)
{
LED = 1;
delay_ms(1000);
LED = 0;
delay_ms(1000);
}
}
//generates delay in milli seconds
void delay_ms(unsigned int i)
{
unsigned int j;
while(i-->0)
{
for(j=0;j<500;j++);
}
}
Result:
Observe the LED’s status.

11
Exercise:
1. Write a C Program to generate the given pattern by interfacing 8 LEDs to 8051.
a. Blink all LEDs at a frequency of 1Hz.
b. Blink alternate LEDs at a frequency of 2Hz.
(Hint: use #define LED P1 to equate P1 port to a constant (LEDS)*/

12
4.b. INTERFACING A SWITCH AND AN LED

Aim: Interfacing a Switch & LED with a 8051 micro controller


Hardware:
1. PC with
a. Keil Compiler
2. 8051 Microcontroller kit

Circuit Diagram:

Program:
/*********Program to interface a led and a switch*********/
#include<reg51.h>
#include<delay.h>

sbit LED = P2^0; /*equating P2^0 pin to a constant(LED)*/


sbit SWITCH = P1^0; /*equating P1^0 pin to a constant(SWITCH)*/

void main(void)
{
LED = 0;
while (1) /*toggling a switch results in a led coming on and off*/
{
if ( SWITCH == 0)
{

13
LED = 1 – LED;
delay(200);
}
}
}

Result:
Observe the LED status when the switch toggles.

Exercise:
1. Write a C Program to Interface Switch and LED using an Interrupt.
2. Interface a LED in such a way that it should be ON only when the switch is closed. When
the switch is open LED should be OFF.
3. Interface a relay and a buzzer to 8051; and write a program to on the buzzer when the
relay is on.

14
5. INTERFACING DAC
Aim: Interfacing of 8 bit DAC

Hardware:
1. PC with
a. Keil Compiler
2. 8051 Microcontroller kit
3. Oscilloscope

Circuit diagram:

Program:

/*---------------------------------------------------------------------------------------------
Module Name : DAC
Detail : Interface dac to generate diffrent wave forms(sine,square,trangular,
ramp etc.) with variable frequuency and amplitude using variable pot.

Describition : This Module is used to select the wave form,default is sign wave

Procedure : To select the waveform first press the reset buteen (keep continuesly pressing
reset butten) after that press the key1 push butten switch and then relese the reset butten ,by
continuesly pressing key1 observe the wave form selection led on dac board select the wave
form an relese the key1.and then see the wave form on CRO.
---------------------------------------------------------------------------------------------*/
#include<reg51.h>
#define wave_sel P2
#define dac_databus P1

15
void main(void)
{
unsigned char temp=0;
unsigned char i,j;
unsigned char sample_point[]={0x80,0xc0,0xee,0xff,0xee,0xc0,0x80,0x40,0x11,
0x00,0x11,0x40,0x80} ;
P3=0;
while(1)
{

temp = wave_sel;
P3 = temp ;

temp &= 0xF0;


P3 = temp ;
temp= temp >>4;
P3 = temp ;
temp= ~temp ;
P3 = temp ;
temp &= 0x0f;

P3 = temp ;

switch(temp)
{
case(0):
{
while(1)
{
for(i=0;i<=11;i++)
{
dac_databus = sample_point[i];

for(j=0;j<=50;j++);
}
}break;
}

case(1):
{
while(1)
{
for(dac_data=0;dac_data<255;dac_data++)
{
dac_databus=dac_data;
}

16
} break;
}

case(2):
{
while(1)
{
for(dac_data=0;dac_data<255;dac_data++)
{
dac_databus=dac_data;
}

for(dac_data=255;dac_data>0;dac_data--)
{
dac_databus=dac_data;

}
}
break;
}

case(4):
{
while(1)
{
dac_databus = 0x00;
delay_us(75);
dac_databus = 0xff;
delay_us(75);
}
break;
}
case(8):
{
while(1)
{
for(i=0;i<=11;i++)
{
dac_databus = sample_point[i];

for(j=0;j<=50;j++);
}
}break;
}
break;
}}}

17
}
Result:
Observe the waveform on Oscilloscope.

Exercise:

1. Write a program to generate up stair case waveform by interfacing DAC to 8051.


2. Write a program to generate down stair case waveform by interfacing DAC to 8086.
3. Generate a sine waveform of P-P voltage 2V by interfacing DAC.

18
6. STEPPER MOTOR INTERFACING
Aim : Interfacing of Stepper motors to 8051.

Hardware :
1. PC with
a. Keil Compiler
2. 8051 Microcontroller kit

Circuit Diagram :

Program:

/*---------------------------------------------------------------------------------------------
Module Name : stepper motor speed and direction control

Describition : Interface stepper motor to control direction and speed by accepting key pressed.

Procedure : 1. Connect port J3 of SDM card to 26 pin connector of MEGA-EM-P02.


2. Connect the power supply to both microcontroller and SDM card.
Note - SDM card require separate +12V power supply.without power supply motor is not
rotating. Also remove the jumper J1 on microcontroller board.

19
3. Write the program and create hex file.Connect the serial cable to serial port.
Then apply poer supply to boards.Download the program trough flah magic.
4. Use the key1 and key2 on the SDM card to rotate the motor.If you press key1
to rotate the motor clockwise and if press key2 it rotate in anticlockwise. key3 & key4 ars used
for speed control.By pressing key3 speed goes on incremating and pressing key4 motor speed
decresing.
5. see it by practicaly.
---------------------------------------------------------------------------------------------*/
#include<reg51.h>
#define stm_databus P0
sbit REV = P0^4;
sbit FRW = P0^5;
sbit DCR = P0^6;
sbit INR = P0^7;

void main(void)
{
P0=0xff;
P1=0xff;
P2=0xff;
P3=0xff;
FRW = 0;
while(1)
{
if(FRW==0)
{
while(1)
{
stm_databus = 0x06;
delay_ms(count);
stm_databus = 0x0A;
delay_ms(count);
stm_databus = 0x09;
delay_ms(count);
stm_databus = 0x05;
delay_ms(count-5);

stm_databus =0XF5;
if(REV == 0)
{
stm_reverse(count);
}

if(DCR == 0)
{

20
count += 1;
if(count > 450)
{
count += 10;
}
if(count > 1000)
{
count += 100;
}
if(count > 5000)
{
count += 500;
}

}
if(INR==0)
{
count -= 1;
if(count > 450)
{
count -= 10;
}
if(count > 1000)
{
count -= 100;
}
if(count > 5000)
{
count -= 500;
}
if(count < 160)
{
count = 160;
}

if(REV==0)
{
while(1)
{

21
stm_databus= 0x05;
delay_ms(count);
stm_databus = 0x09;
delay_ms(count);
stm_databus = 0x0a;
delay_ms(count);
stm_databus = 0x06;
delay_ms(count-5);

stm_databus =0XF5;

if(FRW ==0)
{
stm_forward(count);
}

if(DCR ==0)
{
count += 1;
if(count>450)
{
count += 10;
}
if(count > 1000)
{
count += 100;
}
if(count > 5000)
{
count += 500;
}
}
if(INR ==0)
{
count -= 1;
if(count >450)
{
count -= 10;
}
if(count > 1000)
{
count -= 100;
}
if(count > 5000)
{
count -= 500;

22
}
if(count < 160)
{
count = 160;
}

}
}
}
}
}

Result:

Observe the rotation of stepper motor.

Exercise:

1. Write a program to move the motor clockwise continuously by applying


a. Full step sequence
b. Half step sequence
c. Wave drive sequence

2. Write a program to move the motor by an angle 180 degrees in anticlockwise direction by
applying
a. Full step sequence
b. Half step sequence
c. Wave drive sequence

3. Write a program to rotate the stepper motor by an angle 270 degrees in clockwise
direction by applying above different sequences.

23
7. INTERFACING LIQUID CRYSTAL DISPLAY (LCD) IN 8-BIT MODE

Aim: To interface a LCD to 8051


Hardware :
1. PC with
a. Keil Compiler
2. 8051 Microcontroller kit

Circuit Diagram :

24
Command Encoding
Instruction RS RW D7 D6 D5 D4 D3 D2 D1 D0 Description
Clear all display and returns the cursor
Clear Display 0 0 0 0 0 0 0 0 0 1
to the home position, address 080h
Returns the cursor to the home position
(address 080h). Also returns the display
Return home 0 0 0 0 0 0 0 0 1 *
being shifted to the original position.DD
Ram contents remain unchanged.
Set the cursor move direction and
Entry Mode specifies or not the shift display. These
0 0 0 0 0 0 0 1 I/D S
Set operations are performed during data
write and read.
I/D=1 Increment(+1)
I/D=0 Decrement(-1)
S=1 Followsdisplayshift
Sets On/Off of all display (D), Cursor
Display On/
0 0 0 0 0 0 1 D C B On/Off (C), and blink of cursor position
Off Control
character (B)
Moves the Cursor and shifts the
Cursor and
0 0 0 0 0 1 SC RL * * display without changing DD Ram
Display Shift
contents
SC=1 DisplayShift
SC=0 Cursormove
RL=1 ShifttotheRight
RL=0 ShifttotheLeft
Sets Interface data length (DL), number
Function Set 0 0 0 0 1 DL N F * * of display lines (N) and
character font (F)
DL=1 bits:8
DL=0 bits:4
N=1 lines:2

25
N=0 lines:1
F=1 dots:5x10
F=0 dots:5x7
Write Data 1 0 Write Data Writes data into DD Ram
Read Data 1 1 Read Data Reads data from DD Ram
Set CG Ram Sets the CG Ram address. CG Ram
0 0 0 1 Address CG Ram
Address Data is sent and received after this.
Set DD Ram Sets the DD Ram address. DD Ram
0 0 1 Address DD Ram
Address Data is sent and received after this.
BF=1 ControllerBusy
BF=0 Contr.Available
DDRAM DisplayDataRam
CGRAM CharacterGeneratoruserRAM

Program:

//To display message on lcd display


#include <reg51.h>
#define ldata P1
sbit rs = P0^4;
sbit rw = P0^5;
sbit en = P0^6;
sbit back_lite =P0^7;

void MSDelay(unsigned int);


void lcdcmd(unsigned char );
void lcddata(unsigned char );
void lcdready();
void main()
{

unsigned char lcd_command[]={0x38,0x0e,0x01,0x06,0x83};


unsigned char lcd_message[]=" C B I T ";

unsigned char lcd_command1[]={0x38,0x0e,0x06,0xc0,0xc2};


unsigned char lcd_message1[]=" Microcontrollers ";
unsigned char c,d;

26
back_lite =0;

for(c=0;c<5;c++)
{
lcdcmd(lcd_command[c]);
}

for(d=0;d<15;d++)
{
lcddata(lcd_message[d]);
MSDelay(30);
}

back_lite =1;

for(c=0;c<5;c++)
{
lcdcmd(lcd_command1[c]);
}

for(d=0;d<15;d++)
{
lcddata(lcd_message1[d]);
MSDelay(30);
}
while(1)
{
}
}

void lcdcmd(unsigned char value)


{
ldata=value; //put the value on the pins
rs=0;
rw=0;
en=1;
MSDelay(1);
en=0;
return;
}

void lcddata(unsigned char value)


{
ldata = value; // put the value on the pin

27
rs=1;
rw=0;
en=1;
MSDelay(1);
en=0;
return;
}

void MSDelay(unsigned int itime)


{
unsigned int i,j;
for(i=0;i<itime;i++)
for(j=0;j<1275;j++);
}

Result:
Observed the display on LCD.

Exercise:
1. Program to interface a LCD, LED’s & Switches to create a user selectable menu and
perform the specified operation on given number.
1st Set of Display: WELCOME TO
MICROCONTROLLERS
nd
2 Set of Display: 1 INC 2 DEC
3 SET 4 RESET

28
8. PROGRAMS UNDER STRUCTURED ENQUIRY

1. Interface ADC0800 to 8051 and display the digital converted data in hex format after
applying analog input to it.
2. Write a C Program by interfacing two 7-segment displays and display the count from 99
to 00. And it restarts the counting after reaches the final Count.
3. Write a C program by interfacing four 7-segment displays and display the string
“CBIT”.

29
II. Programming with LPC 2148 Microcontroller
1. PROGRAMS USING ARM7 INSTRUCTION SET

Aim:
To find the maximum/ minimum from given set of numbers.
Hardware:

1. PC with
a. Keil Compiler
Program:

AREA mycode, CODE, READONLY


ENTRY

ADR R0, LIST


MOV R3,#9
LDR R1,[R0],#4
AGAIN LDR R2,[R0],#4
CMP R1, R2
MOVCC R1, R2 ;MAXIMUM IN R1
;MOVCS R1, R2 ;MINIMUM IN R1
SUBS R3,R3,#1
BNE AGAIN
EXIT B EXIT

LIST DCD 1,2,3,4,998129867,0X7FEABEA,10,6,7,9


END

Result:

Observe the output.

Exercise:

1. Write a program to sort the given numbers in Ascending/ Descending order.


2. Write a Perform the division of any two numbers.
3. Write a program to evaluate the given expression.

30
2. a. INTERFACING LED

Aim: To Blink an LED with software delay

Hardware:

1. PC with
a. Keil Compiler
2. LPC2148 kit
Theory:

LED (Light Emitting Diodes)

Light Emitting Diodes (LED) is the most commonly used components, usually for displaying
pins digital states. Typical uses of LEDs include alarm devices, timers and confirmation of user
input such as a mouse click or keystroke.

Interfacing LED

Below figure shows how to interface the LED to microcontroller. As you can see the Anode is
connected through a resistor to GND & the Cathode is connected to the Microcontroller pin. So
when the Port Pin is HIGH the LED is OFF & when the Port Pin is LOW the LED is turned
ON.

Circuit Diagram to Interface LED with LPC2148

31
Program:

#include <LPC21xx.h>

unsigned int delay;

int main()

PINSEL1 = 0x00000000 ; // Configure P0.16 to P0.31 as GPIO

IO0DIR = 0x00FF0000 ; // Configure P0.16 to P0.23 as Output

while(1)

IO0CLR = 0x00FF0000; // CLEAR (0) P0.16 to P0.23, LEDs ON

for(delay=0; delay<500000; delay++); // delay

IO0SET = 0x00FF0000; // SET (1) P0.16 to P0.23, LEDs OFF

for(delay=0; delay<500000; delay++); // delay

Result:

Observe the LED status

Exercise:

1. Generate any pattern using 8 LEDs interfacing to LPC2148.


2. Blink LEDs for every sec.

32
2.b. INTERFACING SWITCH

Aim: To control an LED using switch by polling method

Hardware:

1. PC with
a. Keil Compiler
2. LPC2148 kit

Theory:

A switch is an electrical component that can break an electrical circuit, interrupting the current or
diverting it from one conductor to another. A switch may be directly manipulated by a human as
a control signal to a system, or to control power flow in a circuit.

Circuit Diagram to Interface Switch with LPC2148

33
Program:

//Program to Blink LEDs controlling by switches


#include <LPC21xx.h> // Define LPC2148 Header File
#define LED 16
#define Switch 24 void Delay(int);
int main(void)
{
unsigned char Status=1;
PINSEL2 &= 0xFFFFFFF3;

//Configure P1.16 - P1.31 as GPIO IO1DIR = 0x00 << Switch;


//Configure P1.24 - P1.31 as Input IO1DIR|= 0xFF << LED;
//Configure P1.16 - P1.23 as Output
while(1)
{
Status = 1;
IOSET1 = 0xFF << LED;
Delay (10);
IOCLR1 = 0xFF << LED;
Delay (10);
Delay (10);
Delay (10);
while (~Status)
{
Status = ((IO1PIN & (0xFF << Switch))>> Switch);
IO1PIN = ((~Status) << LED);
}

}
}
void Delay(int n)
{
int p,q;
for(p=0;p<n;p++)
{
for(q=0;q<0x9990;q++);
}
}

34
Result:

Observe the output by giving input via switches.

Exercise:

1. Interface a relay to LPC2148.


2. Interface a buzzer to LPC2148.

35
3. DC MOTOR INTERFACING

Aim: To control an LED using switch by polling method

Hardware:

1. PC with
a. Keil Compiler
2. LPC2148 kit
3. DC Motor

Circuit Diagram:

The DC Motor can also be interfaced to the board by connecting it to the Reliamate RM4. The
direction of the rotation can be changed through software using Relay RLY1. Port lines used for
DC motor are P0.8 and P0.11. The BUZZER is connected through port line P0.9. The Relay
RLY2 is switched ON and OFF. The LED L12 will toggle while relay turns on and off. The NO
contacts of the relay can be checked at the MKDSN connector CN5 pins 1 & 2 using a DMM, a
click sound will be heard when the relay contact closes (check connectivity). The port line P0.10
is used for relay interface.

36
Program:

#include<lpc214x.h>

void clock_wise(void);
void anti_clock_wise(void);
unsigned int j=0;
int main()
{
IO0DIR= 0X00000900;
IO0SET= 0X00000100; //P0.8 should always high.
while(1)
{
clock_wise();
for(j=0;j<400000;j++); //delay
anti_clock_wise();
for(j=0;j<400000;j++); //delay
} //End of while(1)
} //End of Main
void clock_wise(void)
{
IO0CLR = 0x00000900; //stop motor and also turn off relay
for(j=0;j<10000;j++); //small delay to allow motor to turn off
IO0SET = 0X00000900; //Selecting the P0.11 line for clockwise and turn on motor
}

void anti_clock_wise(void)
{
IO0CLR = 0X00000900; //stop motor and also turn off relay
for(j=0;j<10000;j++); //small delay to allow motor to turn off
IO0SET = 0X00000100; //not selecting the P0.11 line for Anti clockwise
}
Result:

Observe the rotation of DC Motor.

Exercise:

1. Write a program to generate a PWM output by using on-chip PWM of LPC 2148 and from
that drive the DC motor. (Motor Speed Control)

37
4. PROGRAMMING ON-CHIP ADC

Aim: Program on-chip ADC of LPC 2148.

Hardware:

1. PC with
a. Keil Compiler
2. LPC2148 kit

Circuit Diagram:

On board there is one interface for internal ADC. AD0.4 (pin P0.25) of controller is used to
convert the analog input voltage varied using POT1 to digital value. A 0.00 to 3.3V is the input
voltage range. 000 to 3FF is the converted digital voltage range here. Jumper JP9 (1, 2) has to be
shorted to use this interface.

Program:
//10-bit internal ADC
//AIN0 pin is selected. You can change the channel by changing PINSEL1 and ADCR value
#include<lpc214x.h>
void display(unsigned int);
void delay(unsigned int );
unsigned char Disp[16]={0x3F, 0x06, 0x5B, 0x4F, 0x66,0x6D,0x7D, 0x07, 0x7F,
0x6F,0x77,0x7c,0x39,0x5e,0x79,0x71};
int main()
{
unsigned int temp_adc,adc_value;
PINSEL1 = 0x00040000; ////AD0.4 pin is selected(P0.25)
IO0DIR = 0xE0FF0000;
while(1)

38
{
AD0CR = 0x01200010; //command register for ADC-AD0.4
while(!((temp_adc = AD0GDR) &0x80000000)); //to check the interrupt bit
adc_value = AD0GDR;
adc_value >>=6;
adc_value &= 0x000003ff;
display(adc_value);

}
}
void display(unsigned int val)
{
unsigned int temp, digit0, digit1,digit2, count;
temp = val;
digit0 = temp%16;
temp = temp/16;
digit1 = temp%16;
temp = temp/16;
digit2 = temp%16;
for(count=0;count<10000;count++)
{
IO0PIN = (Disp[digit0]<<16)|0x80000000; // get the 7-seg display value from the array
delay(10);

IO0PIN = (Disp[digit1]<<16)|0x40000000; // get the 7-seg display value from the array
delay(10);

IO0PIN = (Disp[digit2]<<16)|0x20000000; // get the 7-seg display value from the array
delay(10);
}
}
void delay(unsigned int i)
{
unsigned int j;
for (j=0;j<i;j++);
}

Result:
Observe the ADC output on 7-segment display unit.

39
5. WAVEFORM GENERATION USING INTERNAL DAC

Aim: Program on-chip DAC of LPC 2148.

Hardware:

1. PC with
a. Keil Compiler
2. LPC2148 kit

Circuit Diagram:

DAC0800 is used to convert the digital data into analog signals. Digital data from specified port
lines is given to DAC input. Amplitude of output waveform can be varied by varying POT3 (5K
Pot) that is by varying the reference voltage of DAC0800 when JP3 is closed. For Bipolar mode
open jumper JP11. To test DAC in Unipolar mode short jumper JP11. Port lines used for DAC
are P0.16, P0.23.

40
Program:

//10-bit internal DAC


//AOUT-P0.25 pin is selected PINSEL1 and Load Digital value DACR
#include <lpc214x.h>
#include <Stdio.h>

unsigned int count;

int main()
{
PINSEL1 = 0X00080000; //AOUT pin is selected(P0.25)

while(1)
{
DACR = 0x00000000;
for(count=0;count<1000;count++);
DACR = 0x0000ffc0;
for(count=0;count<1000;count++);
}
} //end of main()

Result:

Observe the output in Oscilloscope

Exercise:

1. Generate a traingular waveform by using on-chip DAC of LPC2148.

2. Generate a sine waveform by using on-chip DAC of LPC2148.

41
6. PROGRAMS UNDER STRUCTURED ENQUIRY

1. Program the PLL of LPC2148 for CPU operating frequency of 60MHz and PCLK of 30
MHz.
2. Develop a Program to generate a single edge controlled PWM with 30% duty cycle on
PWM3 (P0.1).
3. Develop a Program to vary the intensity of the LED using PWM of LPC2148.
4. Write a program To find out frequency of the input signal using capture mode of
LPC2148
5. Write a program to generate a Square wave of 5Hz with 50% Duty cycle using timers of
LPC2148

42
III. Mini projects under Open ended enquiry

SAMPLE MINI PROJECTS

1. LEDs chasing using ADC and Switches.


2. Digital frequency meter using ARM7.
3. Password based automatic locking system using LPC2148.
4. Working with Watchdog timer of LPC2148.
5. STM32 based Bluetooth controlled Bot.
6. IR Flame detection sensor interfacing with ARM
7. Interfacing UART with LPC2148.'
8. Interfacing RTC to LPC 2148 via I2C.
9. Controlling LED via Bluetooth by STM32.
10. ARM7 based web server for controlling devices.
11. Home automation using LPC2148
12. Interfacing GPS, GSM, ZIGBEE with 8051 Microcontroller.
13. Interfacing LDR to LPC2148.

43
APPENDIX I
INTRODUCTION TO KEIL UVISION IDE AND FLASH MAGIC

KEIL UVISION4 IDE INSTALLATION:


1. Installation of keiluVision4 as follows.
2. Go to Software folder in the CD and run Keil4 Arm.exe file.
3. Next
4. Click on the option “I agree to all the terms of...” and then give Next
5. Next
6. Give some name and the mail id and then Next
7. Click Finish to complete the installation.

PROJECT CREATION IN KEILUV4 IDE:


1. Create a project folder before creating NEW project.
2. Open Keil uVision4 IDE software by double clicking on “Keil Uvision4” icon.
3. Go to “Project” then to “New Project” and save it with a name in the Respective
project folder, already you created.
4. Select the device as “NXP (founded by Philips)” In that “LPC2148” then press OK
and then press “YES” button to add “startup.s” file.
5. In startup file go to Configuration Wizard. In Configuration Wizard window disable
PLL Setup and enable VPBDIV Setup.
6. Go to “File” In that “New” to open an editor window. Create your source file And use
the header file “lpc21xx.h” in the source file and save the file. Colour syntax
highlighting will be enabled once the file is saved with a Recognized extension such
as “.C “.
7. Right click on “Source Group 1” and select the option “Add Files to Group 'Source
Group 1' “add the .C source file(s) to the group.
8. After adding the source file you can see the file in Project Window.
9. Then go to “Project” in that “Translate” to compile the File (s).
10. 10.Go to “Project” in that “Build Target” for building all source files such as “.C”,
”.ASM”, “.h”, files, etc…This will create the .HEX file if no warnings & no Errors.

Some Settings to be done in KEILUV4 for Executing C programs:


1. In Project Window Right click “TARGET1” and select “options for target ‘TARGET1’
a. Then go to option “Target” in that
2. Xtal 12.0MHz
3. Select “Use MicroLIB”.
4. Select IROM1 (starting 0x0 size 0x80000).
5. Select IRAM1 (starting 0x40000000 size 0x8000).

44
a. Then go to option “Output”
6. Select “Create Hex file”.
a. Then go to option “Linker”
7. Select “Use Memory Layout for Target Dialog”.

FLASH MAGIC VERSION 6.01.2547:


Installation of Flash Magic as follows.
1. Go to Software folder in the CD and run FlashMagic.exe file.
2. Next
3. Click on the option “I Accept the Agreement” and then give Next
4. Then it asks the Destination location, Click Next.
5. Further Select start menu folder, Click Next.
6. Select “Create a desktop icon” then Next
7. It asks “Ready to Install” Click INSTALL.
8. Click Finish to complete the installation.

ISP PROGRAMMING:
FLASH MAGIC software can be used to download the HEX files to the Flash memory of
controller. To download, Connect the serial cross cable from 9-pin DSUB Female connector
(DB2) to the PC COM port. Switch on both Switches of SW11. SW11(1-RTS), SW11(2-DTR).
Connect DC +5V Power, through the 9-pin DSUB FEMALE connector (DB1) applied from an
external source. Switch ON the power. Remove JP13 while downloading the software.

Some Settings in FLASH MAGIC:

Options -> Advanced options -> Hardware Config


Enable these options only
Use DTR and RTS to control RST and ISP pin
Keep RTS asserted while COM port open
Press OK then do the below settings

Step1. Communications:
 Device : LPC2148
 Com Port : COM1
 Baud Rate : 9600
 Interface : None(ISP)
 Oscillator : 12MHz

Step2. ERASE:

 Select “Erase Blocks Used By Hex File”.

45
Step3. Hex file:

 Browse and select the Hex file which you want to Download.

Step4. Start:

 Click Start to download the hex file to the controller.

After downloading the code the program starts executing in the hardware, then remove the ISP
jumper JP7.

46
APPENDIX II
Introduction to 8051 Development Board and Its Features

Introduction:-

I2C-200 RD2 UNIVERSAL Microcontroller based general-purpose development board for


8051. I2C-200 can be used extensively to test and validate programs. At the heart of the
development board is Phillips P89V51RD2 this provides advance features like ISP and IAP. The
uC has 64KB flash memory and 1KB on-chip RAM. The development board comes with RS232
interface to allow user to program the uC directly from PC. I2C- 200 board and related software
routines help the system designers to rapidly design and prototype their designs based on 8051. It
provides a complete development platform with Different modules’ interface that accelerates the
task of designers to run application software on target 8051 hardware; thus providing a platform
to benchmark their system, save time & expense of building their own application test board and
enabling them to get their designs to market quickly. I2C-200 is a unique hardware and software
combination providing designers, the tools to develop most advanced 8051 series
Microcontroller applications. The I2C-200 hardware reference and software application
programs also simplify 8051 based hardware and software development.

Technical Specification:-

The I2C-200 board consists:


_ 8 bit microcontroller P89V51RD2 which operates at 11.0592 MHz frequency.
_ USB/RS232 ISP/IAP flash programming.
_ 2 Lines X 16 Character LCD Display.
_ 4 Seven segment display (common anode).
_ 4 X 4 Matrix keyboard.
_ ADC0808/09, 8 Bit, 8 channels Analog to Digital Converter.
_ DAC0808, 8 Bit Digital to Analog Converter.
_ Stepper motor driver circuit.
_ DC motor driver circuit.
_ 24C16, Serial EEPROM chip (i2c).
_ DS1307, Real Time Clock (RTC) with battery backup (i2c).
_ Elevator with 8 floors.
_ Buzzer interface.
_ LED and switch interface.
_ Onboard hardware reset.

47
Features of P89V51rd2:-

• 80C51 Central Processing Unit


• 5 V Operating voltage from 0 to 40 MHz
• 64 KB of on-chip Flash program memory with ISP (In-System Programming) and IAP (In-
Application Programming)
• Supports 12-clock (default) or 6-clock mode selection via software or ISP
• SPI (Serial Peripheral Interface) and enhanced UART
• PCA (Programmable Counter Array) with PWM and Capture/Compare functions
• Four 8-bit I/O ports with three high-current Port 1 pins (16 mA each)
• Three 16-bit timers/counters
• Programmable Watchdog timer (WDT)
• Eight interrupt sources with four priority levels
• Second DPTR register
• Low EMI mode (ALE inhibit)
• TTL- and CMOS-compatible logic levels

48
APPENDIX III
. Introduction to LPC 2148 Development Board and Its Features
Introduction:-

The ALS/EVBRD/ARM7T7 Evaluation board is a study Board which includes LPC2148


ARM7TDMI-S micro-controller with USB 2.0 Full speed device, multiple UARTs, SPI, I2C &
on chip. 512K Flash and SRAM up to 40kB, produced by NXP Semiconductors.

The ARM7TDMI-S is a general-purpose 32-bit microprocessor, which offers high performance


and very low power consumption. The ARM processor is based on Reduced Instruction Set
(RISC) architecture, And the instruction set and related decode mechanism are much simpler
than those of micro programmed Complex Instruction Set Computers. This simplicity results in a
high instruction throughput and impressive real-time interrupt response from a small and cost-
effective processor Core. Pipeline techniques are employed so that all parts of the processing and
memory systems can operate continuously. Typically, while one instruction is being executed, its
successor is being decoded, and a third instruction is being fetched from memory.

The ARM7TDMI-S processor also employs a unique architectural strategy known as


THUMB, which makes it ideally suited to high-volume applications with memory restrictions, or
applications where code density is an issue. The key idea behind THUMB is that of a super
reduced instruction set. Essentially, the ARM7TDMI-S processor has two Instruction sets:

1. The standard 32-bit ARM instruction set.


2. A 16-bit THUMB instruction set.

The THUMB set’s 16-bit instruction length allows it to approach twice the density of standard
ARM code while retaining most of the Arm’s performance advantage over a traditional 16-bit
processor using 16-bit registers. This is possible because THUMB code operates on the same 32-
bit register set as ARM code. THUMB code is able to provide up to 65% of the code size of
ARM, and 160% of the performance of an equivalent ARM Processor connected to a 16-bit
memory system.

Features of LPC2148 :-

Following are the features of 16 bit /32 bit LPC2148 Arm Micro-controller:
 PHILIPS LPC2148 is a 16-bit or 32-bit Micro-controller in a LQFP64-pin Package.
 40 kB of on-chip static RAM and 512 kB of on-chip flash memory. 128-bit wide
interface/accelerator enables high-speed 60 MHz operation.
 The LPC2148 provides 100000 erase/write cycles and 20 years of Data-retention.

49
 In-System Programming/In-Application Programming (ISP/IAP) via on-chip boot loader
software. Single flash sector or full chip erase takes 400ms and Flash programming takes
1ms per 256-byte line.
 USB 2.0 Full speed compliant device controller with 2 kB of endpoint RAM. In addition,
the LPC2148 provides 8 kB of on-chip RAM accessible to USB by DMA.
 Embedded ICE-RT and Embedded Trace Macro cell (ETM) interfaces offer real time
debugging with on-chip Real Monitor software and high-speed real-time tracing of
instruction execution.
 Two 10-bit ADCs provide a total of 14 analog inputs, with conversion times as low as
2.44μs per channel.
 Single 10-bit DAC provides variable analog output.
 Two 32-bit Timers/External event Counters (with four Capture and four Compare
channels each), PWM unit (six outputs) and watchdog.
 Low power Real-Time Clock (RTC) with independent power and 32 kHz clock input.
 Multiple serial interfaces including two UARTs (16C550 equivalent), two Fast I2C-bus
(400 kbit/s), SPI and SSP with buffering and variable data length capabilities.
 Vectored interrupt controller (VIC) with configurable priorities and vector addresses.
 Up to 45 numbers of 5 V tolerant fast general purpose I/O pins in a tiny LQFP64
package.
 Up to nine edge or level sensitive external interrupt pins available.
 60 MHz maximum CPU clock available from programmable on-chip PLL with settling
time of 100 μs.
 On-chip integrated oscillator operates with an external crystal in range from 1 MHz to 30
MHz and with an external oscillator up to 50 MHz.
 Power saving modes include Idle and Power-down.
 Individual power enable/disable of peripheral functions as well as peripheral clock
scaling for additional power optimization.
 Processor wake-up from Power-down mode via external interrupt, USB, Brown-Out
Detect (BOD) or Real-Time Clock (RTC).
 Single power supply chip with Power-On Reset (POR) and BOD circuits:CPU operating
voltage range of 3.0 V to 3.6 V (3.3 V ± 10 %) with 5 V tolerant I/O pads.

50

You might also like