0% found this document useful (0 votes)
47 views

Microcontroller Lab Manual

The document outlines experiments to be performed on an 8051 microcontroller and ARM Cortex-M3 microcontroller using assembly language and embedded C. It includes 10 sets of programs involving tasks like data transfer, arithmetic operations, timer programming, serial communication, and interfacing with devices like LCD, keypad, ADC, DAC. The programs test skills related to microcontroller programming and interfacing.

Uploaded by

DRathika
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views

Microcontroller Lab Manual

The document outlines experiments to be performed on an 8051 microcontroller and ARM Cortex-M3 microcontroller using assembly language and embedded C. It includes 10 sets of programs involving tasks like data transfer, arithmetic operations, timer programming, serial communication, and interfacing with devices like LCD, keypad, ADC, DAC. The programs test skills related to microcontroller programming and interfacing.

Uploaded by

DRathika
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 78

Department of Electronics and communication

Microcontroller LAB (EC420)


Jan-Apr 2020

Staff in Charge

Dr. Shankariah

Prof. Renuka B S
LAB EXPERIMENTS:

Software programs: To be implemented on 8051 microcontroller


1. Problems related with data transfer and exchange.
2. Problems related with arithmetic and logical operations.
3. Problems related with programming timers in all modes with and without interrupts.
4. Problems related with programming serial communication with and without interrupts.
5. Program related with handling external interrupts.
Hardware programs: To be implemented on 8051 and ARM CORTEX-M3 (using Embedded C)
1. Interface LCD.
2. Interfacing of matrix keypad.
3. Interfacing of ADC and DAC.
4. Interfacing of multi digit 7 segment displays.
5. Interfacing of stepper motor and D C motor.

MICROCONTROLLER LAB
Set of experiments
SET I
1. Write an 8051 assembly level program to add 10 bytes of data.
2. Write an 8051 assembly level program to transfer 10 bytes of data from external RAM location starting with
2000h to internal RAM starting from 30h.
3. Write an 8051 assembly level program to transfer 10 bytes of data from location starting at 30h to location 35h.
4. Write an 8051 assembly level program to transfer 10 bytes of data from location starting at 35h to location 30h.
5. Write an 8051 assembly level program to exchange 10 bytes of data from location starting at 30h with data
from location starting from 1000h.
6. Write an 8051 assembly level program to transfer 10 bytes of data starting from location 8000h to location
9000h within the external memory.

SET II
1. Write an 8051 assembly level program to add ‘N’ bytes of data taking into account the possible carry output.
2. Write an 8051 assembly level program to add ‘N’ bytes of BCD numbers taking into account the possible carry
output.
3. Write an 8051 assembly level program to find the average of ‘N’ bytes of data.
4. Write an 8051 assembly level program to subtract two BCD numbers.
5. Write an 8051 assembly level program to add two multi-byte numbers.

SET III
1. Write an 8051 assembly level program to count the number of even numbers and number of odd numbers in
an array of ‘N’ bytes of data.
2. Write an 8051 assembly level program to count the number of +ve numbers and number of -ve numbers in an
array of ‘N’ bytes of data.
3. Check whether the given byte of data is present in an array of ‘N’ bytes of data. If present send 00 in Port 0 else
send FF in Port 0.
4. Read the data from Port 1. If P1.1 is at logic 0, find the largest number in an array of ‘N’ bytes of data and store
in location 40h. If P1.0 is at logic 1, find the smallest number in the array and store in the location 40h.

SET IV
1. Write an 8051 assembly level program to arrange an array of ‘N’ bytes of data in ascending order.
2. Write an 8051 assembly level program to arrange an array of ‘N’ bytes of data in descending order.
3. Write an 8051 assembly level program to find whether the given number is prime or not. If prime send FF to
Port 0 else send 00 to Port 0.
4. Write an 8051 assembly level program to find the factorial of a given number (using recursive procedure).
5. Write an 8051 assembly level program for BCD up counter. Show each count in Port 0 with appropriate delay.
6. Write an 8051 assembly level program for BCD down counter. Show each count in Port 0 with appropriate
delay.

SET V
1. Write an 8051 assembly level program to check whether the given byte of data is palindrome. If ‘yes’ send 00
to Port 0 else send FF to Port 0.
2. Write an 8051 assembly level program to check whether the lower nibble is greater than higher nibble of A. If
‘yes’ send 00 to Port 0 else send FF to Port 0.
3. Write an 8051 assembly level program to convert 2 digit BCD to ASCII numbers and store them in location
30h(LSB) and 31h(MSB).
4. Write an 8051 assembly level program to find the square of a number using look up table technique.
5. Write an 8051 assembly level program to find the square root of a number.

SET VI
1. Write an 8051 assembly level program to find LCM and HCF of two numbers.
2. Write an 8051 assembly level program to check whether the given number is 2 out of 5 code. If ‘yes’ send 00 to
Port 0 else send FF to Port 0.
3. Write an 8051 assembly level program to generate Fibonacci series.

SET VII
1. Write an 8051 assembly level program to generate square wave on P1.5 with 50% duty cycle. Program timers in
mode 0, mode1 and mode2 to generate the delay.
2. Write an 8051 assembly level program to generate square wave with ON period of 20ms and OFF period of
40ms. Use timers in mode 0, mode 1 and mode 2 to generate the delay.
3. Repeat the problems with interrupt for timers.
4. Repeat the above problems by writing programs in embedded C.

SET VIII
1. A switch is connected to P2.5. write an 8051 assembly level program to read the status of switch and if switch is
closed send serially ‘HELLO’, else send ‘WELCOME’ at baud rate 9600.
2. Write an 8051 assembly level program to transfer a message SJCE serially by programming serial
communication in interrupt mode with baud rate 9600.
3. Repeat the problem 1 & 2 using embedded C program.

SET IX
1. Interface 4 digit multiplexed 7 segments LED and writes a program to display the message SJCE.
2. Interface 4x4 hex keyboards and write a program to read the key closure and display hex code for key pressed
on 7 segment display.

SET X
1. Interface LCD module and write program to display a message.
2. Interface ADC a write a program to sample the signal and convert it into digital
3. Interface DAC and write program to generate various waveforms.
SET 1 PROGRAMS

1. WRITE AN 8051 ALP TO ADD 10 BYTES OF DATA

Before Execution:

After Execution:
2 Write an 8051 assembly level program to transfer 10 bytes of
data from external RAM location starting with 2000h to internal
RAM starting from 30h

Before Execution:

After Execution:
3.Write an 8051 assembly level program to transfer 10 bytes of
data from location starting at 30h to location 35h.

Before Execution :

After Execution:
4.Write an 8051 assembly level program to transfer 10 bytes of
data from location starting at 35h to location 30h.

Before Execution:

After execution:
3. Writean 8051 assembly level program to exchange 10 bytes of
data from location starting at 30h with data from location starting
from 1000h.

Before Execution
1000h:0BH,17H,2DH,41,4AH,53,52H,0FH,1CH,0AH
30H: 01H,02H,3H,4H,5H,6H,7H,8H,9H,0AH

After Execution:
30H: 0BH, 17H, 2DH, 41, 4AH, 53, 52H, 0FH, 1CH, 0AH
1000H:01H,02H,3H,4H,5H,6H,7H,8H,9H,0AH
4. Write an 8051 assembly level program to transfer 10 bytes of data
starting from location 8000h to location 9000h within the external
memory.

Before Execution:

After Execution:
SET 2 PROGRAMS

1. Write an alp to add N bytes of data taking into account the


possible carry output.

Before Execution:

After Execution:
2. Write an alp to add N bytes of BCD numbers talking into
account the possible carry output.

Before Execution:

After Execution:
3. Write an alp to find the average of N bytes of data.

Before Execution:

After Execution
4. Write an alp to subtract two BCD numbers.

Before Execution:

After Execution:
5. Write an alp to add 2 multibyte numbers. Numbers starts from
location with address 30h and 40h. Store the results starting
from location 30h.

Before Execution:

After Execution:
SET 3 PROGRAMS

1.Write an 8051 assembly level program to count the number of even


Numbers and number of odd numbers in an array of ‘N’ bytes of data

Before execution

After Execution
2.Write an 8051 assembly level program to count the number of +ve
numbers and number of –ve numbers in an array of ‘N’ bytes of data.

MSB bit of data represents sign of the


number. If MSB is 1, then the number is
negative.
If MSB is 0, then the number is positive

Before execution:

After execution
3. Check whether the given byte of data is present in an array of ‘N’ bytes of data. If
present send 00 in Port 0 else send FF in Port 0

Before execution:

After Execution:
4.Read the data from Port1. IfP1.1 is at logic0, find the largest
number in an array of ‘N’ bytes of data and store in location 40h.
If P1.0 is at logic1, find the smallest number in the array and store
in the location 40h

Before execution:
After execution:
SET 4 PROGRAMS

1. Write an 8051 assembly level program to arrange an array of ‘N’ bytes of


data in ascending order.

Before Execution:

After Execution:
2. Write an 8051 assembly level program to arrange an array of ‘N’ bytes of
data in descending order.

Before Execution:

After Execution:
3. Write an 8051 assembly level program to find whether the given number
is prime or not. If prime send FF to Port 0 else send 00 to Port 0.

After Execution:
4. Write an 8051 assembly level program to find the factorial of a given
number (using recursive procedure).

After Execution:
5. Write an 8051 assembly level program for BCD up counter. Show each
count in Port 0 with appropriate delay.

After Execution:
6. Write an 8051 assembly level program for BCD down counter. Show
each count in Port 0 with appropriate delay.

After Execution:
SET 5 PROGRAMS

1. 1.Write an 8051 assembly level program to check whether the given


byte of data is palindrome. If ‘yes’ send 00 to Port 0 else send FF to Port 0

Before and after execution


2. Write an 8051 assembly level program to check whether the lower
nibble is greater than upper nibble of A. If ‘yes’ send 00 to Port 0 else send
FF to Port 0.

Before execution

After execution
3. Write an 8051 assembly level program to convert 2 digit BCD to ASCII
numbers and store them in location 30h(LSB) and 31h(MSB)

Before execution

After execution
4. Write an 8051 assembly level program to find the square of a number
using look up table technique

Before execution
After execution
4.Write an 8051 assembly level program to find the square root of a
number

Before execution

After execution
SET 6 PROGRAMS

1.Write a assembly level program (8051) to check if the number is a 2 out


of 5 code or not.

After Execution:
2.Write a assembly level program to find the Fibonacci series.

Before Execution:

After Execution:
3. Write an assembly level program to find LCM and HCF of 2 numbers.

Before Execution:

After Execution:
SET 7 PROGRAMS
1. WAP to generate a square wave on P1.5 with 50% Duty
Cycle. Program timers in mode0, mode1 and mode2 to generate
delay.
Mode 0

After execution
Mode 1

After execution
Mode 2

After execution
2. WAP to generate a rectangular wave with T(on)=20ms and
T(off)=40ms. Use Timer0 in mode0, mode1 and mode2 to
generate delay.
Mode 0
After Execution:

Mode 1
After execution

Mode 2
1. Repeat above problems to generate square wave with
interrupts for timers
Mode 0
After execution

Mode 1
After execution

Mode 2
After execution

2. Repeat above problems of status check and interrupt methods


to generate square wave using embedded c programs.
Mode 0 using interrupt
After execution

Mode 1 using interrupt


After execution

Mode 2 using interrupt


After execution

Mode 0 using status check


After execution

Mode 1 using status check


After execution

Mode 2 using status check


After execution
SET 8 PROGRAMS

1. A switch is connected to P2.5. write an 8051 assembly level


program to read the status of switch and if switch is closed
send serially "HELL0', else send WELCOME' at baud rate
9600.
2.Write an 8051 assembly level program to transfer a message
SJCE serially by programming serial1 communication in
interrupt mode with baud rate 9600.
Repeat the problem 1 & 2 using embedded C program.
Problem 1
Problem 2
HARDWARE EXPERIMENT

Question Number - 1
Interface a Multichannel ADC to 8051 and develop a program to read
the analog data, Convert into digital value and display the digitized
value on port 0 connected to leds
Circuit Diagram :
Program
Result :

For the analog voltage of 2.5 volts output is 10000000


LCD interface

LCD routine:
/* LCD related functions. */
#include "at89c51ed2.h"
#include <intrins.h> //For _nop_();

// LCD FUNCTION PROTOTYPE

extern void lcd_comm(void);


extern void wr_cn(void);
extern void wr_dn(void);
extern void lcd_data_cmnd(char, char);
void delay(int);

extern unsigned char temp1;


extern unsigned char temp2;
unsigned char var;
void lcd_init(void)
{
temp1 = 0x30; // D5(P3.3)=1,D4(P3.2)=1
wr_cn();
delay(500);

temp1 = 0x30; // D5(P3.3)=1,D4(P3.2)=1


wr_cn();
delay(500);

temp1 = 0x30; // D5(P3.3)=1,D4(P3.2)=1


wr_cn();
delay(500);

temp1 = 0x20; // D5(P3.3)=1


wr_cn();
delay(500);

temp1 = 0x28; // Set


lcd_comm();
delay(500);

temp1 = 0x0f; //display on, cursor on, cursor blinking


lcd_comm();
delay(500);

temp1 = 0x06; //shift cursor right with auto increment


lcd_comm ();
delay (500);

temp1 = 0x80; //clear display with cursor on first position


lcd_comm ();
delay (500);

temp1 = 0x01;
lcd_comm ();
delay(500);
}

// Function to pass commands to LCD


void lcd_comm(void)
{
var = temp1;
temp1 = temp1 & 0xf0;
temp1 = temp1 >> 4;

wr_cn ();

temp1 = var & 0x0f;


wr_cn ();

delay (60);
}

// Function to pass data to LCD


Void lcd_data (void)
{
var = temp2;
temp2 = temp2 & 0xf0; // convert the byte into nibble
temp2 = temp2 >> 4;
wr_dn();

temp2 = var & 0x0f;


// temp2 = temp2 << 2;
wr_dn();

delay(60);
}

// Function to write to command reg of LCD


void wr_cn(void)
{
temp1 = temp1 & 0x7f; // RS(P3^7)=0
temp1 = temp1 & 0xDF;
temp1 = temp1 | 0x40; // EN(P3^6)=1, TXD(P3^1)=1, RXD(P3^0)=1

P2 = temp1;

_nop_();
_nop_();
_nop_();
_nop_();
_nop_();

temp1 = temp1 & 0xbf; // EN(P3^6)=0,


P2 = temp1;

// Function to write to data reg of LCD


void wr_dn(void)
{
temp2 = temp2 | 0xc0; // RS(P3^7)=1,EN=1,TXD=1,RXD=1
temp2 = temp2 & 0xDF;
P2 = temp2;

_nop_();
_nop_();
_nop_();
_nop_();
_nop_();

temp2 = temp2 & 0xbf; // EN = 0


P2 = temp2;
}

// Function to clear the LCD display


/*void clear_lcd()
{
temp1 = 0x01;
lcd_comm();
delay(500);
}
*/
void delay(int count)
{
int i;

for(i=0;i<count;i++);
}
/*
void lcd_data_cmnd(char cmnd, char l_data)
{
char check;

check =cmnd;
var = l_data;

l_data = l_data & 0xf0;


temp1 = l_data >> 2;
if(check == 1)
wr_cn();
else if(check == 2)
{
temp2 = temp1;
wr_dn();
}
l_data = var & 0x0f;
temp1 = l_data << 2;
if(check == 1)
wr_cn();
else if(check == 2)
temp2 = temp1;
wr_dn();

delay(60);
}
KEYBOARD INTERFACE:

/*

Program to demonstrate keyboard operation


Takes a key from key board and displays it on LCD screen*/

#include "at89c51xd2.h"

// This project includes the following files:


// 1. kbdisp.c the source program to keypad
// 2. LCD_routine.c to display the key read

void scan(void);
void get_key(void);
void display(void);
void delay_ms(int i);
void uart_init(void);

/******* LCD FUNCTION PROTOTYPE*******/


void lcd_init(void);
void lcd_comm(void);
void lcd_data(void);
void clear_lcd(void);
void delay(int);

unsigned char temp1=0x00;


unsigned char temp2;

idata unsigned char row,col,key;

unsigned char scan_code[16]={0xEE, 0xDE, 0xBE, 0x7E,


0xED, 0xDD, 0xBD, 0x7D,
0xEB, 0xDB, 0xBB, 0x7B,
0xE7, 0xD7, 0xB7, 0x77 };

unsigned char LED_CODE[16]= {0x3f,0x66,0x7f,0x39,


0x06,0x6d,0x6f,0x5e,
0x5b,0x7d,0x77,0x79,
0x4f,0x07,0x7c,0x71};

unsigned char LCD_CODE[16] = {'0','4','8','C',


'1','5','9','D',
'2','6','A','E',
'3','7','B','F' };

idata unsigned char temp,temp4,temp3,res1,flag,result=0x3F;


/* in this key pad program keypad lines are not kept high .we will make them high
low throgh writing data to port.*/
void main ()
{
lcd_init();

while(1)
{
get_key();
display();
P3 = 0xFF;
}
} //end of main()

void get_key(void) // get_key() function calls scan() function


{ // on sensing a key from scan() function it
int i; // will compare the received scan code with
display(); // scan code lookup table and returns ASCII code
flag = 0x00; // rows are read from Port P0 is scan() function
while(flag == 0x00) // this function is in an eternal loop
// wiil return to main() only after getting a key
{
for(row=0;row<4;row++) // This for loop makes the one of the ROW low at
{ // one time .Then scan function is called
if( row == 0)
temp3=0xFE;
else if(row == 1)
temp3=0xFD;
else if(row == 2)
temp3=0xFB;
else if(row == 3)
temp3=0xF7;
P0 = temp3; // each time temp3 value is put into this
scan();
delay_ms(10); // on sensing a key scan() function will make flag = 0xff
if(flag == 0xff)
break;
} // end of for

if(flag == 0xff)
break;
} // end of while

P3 = 0x00; // Enable U21


for(i=0;i<16;i++) // in this for for loop scan code received which is in res1
{ // variable is compared with
if(scan_code[i] == res1) // the lookup table for array scan code[] and when a match is
// found will return the correspoding led code for
the key pressed
{
temp1 = 0x87;
lcd_comm();
temp2 = LCD_CODE[i];
lcd_data();
result = LED_CODE[i];
break;
}

}
} // end of get_key();

void scan(void) // will return the scan code for the key pressed
{ // Both row lines and column lines are connected to
unsigned char t; //Port 0 only.Columns are connected to P0.0-P0.3
temp4 = P0; // P0.4-P0.7 are connected to rows
temp4 = temp4 & 0xF0; //read port0 ,mask with 0xF0h

if(temp4 != 0xF0) // Means a key is sensed


{
delay_ms(30);
delay_ms(30); // give some delay for debouncing

temp4 = P0; // read the port again


temp4 = temp4 & 0xF0;

if(temp4 != 0xF0) // debounce


{
flag = 0xff; // set the flag denoting a key is received
res1 = temp4;
t = temp3 & 0x0F;
res1 = res1 | t; // and OR it with column value
} // to get the scan code of the key pressed
else
{
flag = 0x00;
}

}
}

// end of scan()

void display(void)
{
P1 = result;
}

void delay_ms(int i)
{
int j;
for(j=0;j<i;j++);
}
DAC INTERFACE:
1. Interfacing DAC with 8051 and developing an algorithm to generate the following
outputs
I) Square wave with 50% duty cycle
II) Square wave with 75% duty cycle
III) Triangle wave.
IV) Ramp(+ve &-ve)
V) Sine wave
VI) Staircase
VII) Staircase triangle

Solution:

Interfacing Circuit:
I) Generation of square wave with 50%duty cycle.

➢ Solution:

RESULT:
2.Generation of square wave with 75%duty cycle.

➢ Solution:

RESULT:
Generation of Triangular wave.
➢ ALGORITHM

RESULT:
II) A. Generation of positive Ramp with signal.

➢ ALGORITHM

RESULT:
B. Generation of negative Ramp with signal.
➢ ALGORITHM

RESULT:
➢ GENERATION OF SINE WAVE
➢ ALGORITHM

RESULT
III) Generation of the following waveform.
➢ ALGORITHM

RESULT
Assignment question:
1. Write an 8051 ALP to send message HELLO serially once in every 2 sec.
Program timer and serial communication in interrupt mode

RESULT:

END

You might also like