You are on page 1of 80

Lab Report on Microprocessors

and Microcontrollers

Final Lab Copy

Submitted By: Submitted to:


Name: Soumalya Chowdhury Dr. Kamalika Datta
Roll: 181112015026
_________________
Year: 3rd Sem: 6th
Subject: Microprocessors and
Microcontrollers Lab
Subject Code: CS692
Index

1. LAB 1:
a. Write an assembly language code to add two numbers 50
and60.
b. Write an assembly language code to add 255 and 1.
c. Write an assembly language code to XOR three numbers
X=10100111(A7), Y=11001010(CA), Z=00010001(11). Store the
result in accumulator.
d. Write an assembly language code to subtract 30 from 130.
e. Write an assembly language code to add three numbers 40,
50 and 60. Store the result in accumulator.

2. LAB 2:
a. Write an assembly language code to add ten numbers which
are stored at memory location 2010 to 2019.
3. LAB 3:
a. Move n-bytes of data from memory location 6800 to 6810.
b. Multiply two 8-bit numbers which are stored in register B
& C and store the 8-bit product in memory location
2800.
c. Multiply two 8 bit numbers which are stored in register B
& C and store the 16-bit product in memory location
2800 & 2801.
4. LAB 4:

a. Write an assembly language code to find factorial of a


number stored at memory location 2080 and store the
result at memory location 2090.
b. Write an assembly language to find GCD of two numbers
stored at location 6050H and 6051H.
5. LAB 5:
a. Write an assembly language code to find the maximum in an
array of elements.
b. Write an assembly language code to find the smallest
element in an array of elements.
6. LAB 6:
a. Write an assembly language code to find the bit-sum of a
Number and store the result is some memory location.
b. Write assembly language code to sort n numbers which are
Stored in memory location from 2051. Location 2050 stores
the value of n.

7. LAB 7:
a. Write a subroutine to find the factorial of a number.
b. Write a subroutine to find GCD of n numbers.
8. LAB 8:
a. Submit the following codes along with explanation and
output analysis with STM NucleoFRE40.
i. On-board LED blinking
ii. User LED blinking
iii. On board user button usage
iv. User Switch operation

9. LAB 9:
a. Write embed an C code to display 0 to 9 continuously
using a common anode 7-segment display with a delay of 2
seconds.
b. Write an embed C code to display 0 to F continuously
using a common anode 7-segment display with a delay of 3
seconds.
10. LAB 10:
11. Generate a sound using a speaker with three tones. Use
pwm output ports of the STM board. Show the schematic for the
same and write the embed C code.
a. Generate sa re ga ma using speaker. Interface with STM
microcontroller board and show the schematic and write
the embed C code for the same.
12. Connect a capacitive touch switch button module with STM
microcontroller board, whenever we touch the season a siren
sound will be generated from the speaker. Show the entire
schematic of the same and write the embed C code to do the
following.
Lab-I
Lab-II
Lab-II
Lab-IV
Lab-V
Lab-VI
Lab-VII
Lab VII
On-board LED blinking:

#include "mbed.h" // importing mbed header file


DigitalOut myled(LED3); //declaring object of the class DigitalOut as LED3

int main()
{
while (1) //Initializing an infinite loop
{
myled = 1; //declaring myled as 1
wait(2); //waiting for 2 seconds
myled = 0; //declaring myled as 0
wait(1); //waiting for 1 sec
}
}

Output- After the code is executed, the on-board led3 of the NUCLEO-F401RE glows for 2 seconds
and then dims it for 1sec which happens on an infinite loop.

User LED blinking:

#include "mbed.h" // importing mbed header file


DigitalOut myled(D2); //declaring object of the class DigitalOut as D2

int main()
{
while(1)// Initializing an infinite loop
{
myled = 1; //declaring myled as 1
wait(2); //waiting for 2 seconds
myled = 0; //declaring myled as 0
wait(1); //waiting for 1 sec
}
}

Output- We are connecting a led in D2 port of the NUCLEO-F401RE and then installing our
program. This make the led glow for 2 seconds and then dims it for 1sec which happens on an
infinite loop.
On board user button usage:

#include "mbed.h" //including header file mbed


DigitalIn myButton(USER_BUTTON); //creating a object of the class DigitalIn as myButton and
setting it as USER_BUTTON
DigitalOut out(LED3); //creating a object of the class DigitalOut and setting it as LED3

int main() {
while(1) // initializing an infinite loop
{
if(!myButton) //if the value from the input port is 0
{
out = 1; //out is set to 1
}
else
{
out = 0; //out is set to 0
}
}
return(0); //returning 0 to the operating system
}
Output- When we do not press the USER_BUTTON it gives input 1 and the bulb does not glow but
when we press it, it gives a input 0 and the led glows

User Switch operation:

#include "mbed.h" //including header file mbed


DigitalIn myInput(D2); //creating object of the class DigitalIn as myInput and setting it as D2 port
DigitalOut myled(LED3); //creating object of the class DigitalOut as myIed and setting it as LED3

int main() {
while(1) // initializing an infinite loop
{
if(myInput) //if the value from the input port is 1
{
myled = 1; //myled is set to 1
}
else
{
myled = 0; //myled is set to 0
}
}
return(0); //returning 0 to the operating system
}
Output- We are connecting the NUCLEO F401RE with a input switch through D2 port. When the
switch is not pressed it gives input 1 and the led glows. But when we press the switch it gives input
as 0 so the led dims.
Lab IX
Displaying 0 to 9 continuously using a common anode 7-segment display
with a delay of 2 seconds:

#include "mbed.h" // importing mbed header file


DigitalOut a(D2); //declaring object of the class DigitalOut
DigitalOut b(D3); //declaring object of the class DigitalOut
DigitalOut c(D4); //declaring object of the class DigitalOut
DigitalOut d(D5); //declaring object of the class DigitalOut
DigitalOut e(D6); //declaring object of the class DigitalOut
DigitalOut f(D7); //declaring object of the class DigitalOut
DigitalOut g(D8); //declaring object of the class DigitalOut
int num=0; // //declaring a global variable

void Display( int disp){


switch(disp)
{
case 0: a=0;b=0;c=0;d=0;e=0;f=0;g=1; break; //case to display 0
case 1: a=1;b=0;c=0;d=1;e=1;f=1;g=1; break; //case to display 1
case 2: a=0;b=0;c=1;d=0;e=0;f=0;g=1; break; //case to display 2
case 3: a=0;b=0;c=0;d=0;e=1;f=0;g=1; break; //case to display 3
case 4: a=1;b=0;c=0;d=1;e=1;f=0;g=0; break; //case to display 4
case 5: a=0;b=1;c=0;d=0;e=1;f=0;g=0; break; //case to display 5
case 6: a=1;b=0;c=0;d=1;e=1;f=0;g=0; break; //case to display 6
case 7: a=0;b=0;c=0;d=1;e=1;f=1;g=1; break; //case to display 7
case 8: a=0;b=0;c=0;d=0;e=0;f=0;g=0; break; //case to display 8
case 9: a=0;b=0;c=0;d=0;e=1;f=0;g=0; break; //case to display 9
}
}
int main() {

while(1) //Initializing an while loop


{
if(num>9){ //Initializing an Conditional if loop to keep the number record
num=0; //overwriting num variable value 0
}
Display(num); //calling Display function
wait(2); //adding Delay of 2 second
num++; //increasing num by 1
}
}

Output- After the code is executed, SEVEN Segment display to the NUCLEO-F401RE will display 0 to
9 continuously with a Delay of 2 sec which happens on an infinite loop.
Displaying 0 to 9 and A-F continuously using a common anode 7-segment
display with a delay of 2 seconds:

#include "mbed.h" // importing mbed header file


DigitalOut a(D2); //declaring object of the class DigitalOut
DigitalOut b(D3); //declaring object of the class DigitalOut
DigitalOut c(D4); //declaring object of the class DigitalOut
DigitalOut d(D5); //declaring object of the class DigitalOut
DigitalOut e(D6); //declaring object of the class DigitalOut
DigitalOut f(D7); //declaring object of the class DigitalOut
DigitalOut g(D8); //declaring object of the class DigitalOut
int num=0; // //declaring a global variable

void Display( int disp){


switch(disp)
{
case 0: a=0;b=0;c=0;d=0;e=0;f=0;g=1; break; //case to display 0
case 1: a=1;b=0;c=0;d=1;e=1;f=1;g=1; break; //case to display 1
case 2: a=0;b=0;c=1;d=0;e=0;f=0;g=1; break; //case to display 2
case 3: a=0;b=0;c=0;d=0;e=1;f=0;g=1; break; //case to display 3
case 4: a=1;b=0;c=0;d=1;e=1;f=0;g=0; break; //case to display 4
case 5: a=0;b=1;c=0;d=0;e=1;f=0;g=0; break; //case to display 5
case 6: a=1;b=0;c=0;d=1;e=1;f=0;g=0; break; //case to display 6
case 7: a=0;b=0;c=0;d=1;e=1;f=1;g=1; break; //case to display 7
case 8: a=0;b=0;c=0;d=0;e=0;f=0;g=0; break; //case to display 8
case 9: a=0;b=0;c=0;d=0;e=1;f=0;g=0; break; //case to display 9
case 10: a=0;b=0;c=0;d=1;e=0;f=0;g=0; break; //case to display A
case 11: a=1;b=1;c=0;d=0;e=0;f=0;g=0; break; //case to display B
case 12: a=0;b=1;c=1;d=0;e=1;f=0;g=1; break; //case to display C
case 13: a=1;b=0;c=0;d=0;e=0;f=1;g=0; break; //case to display D
case 14: a=0;b=1;c=1;d=0;e=0;f=0;g=0; break; //case to display E
case 15: a=0;b=1;c=1;d=1;e=0;f=0;g=0; break; //case to display F

}
}
int main() {

while(1) //Initializing an while loop


{
if(num>15){ //Initializing an Conditional if loop to keep the number
record
num=0; //overwriting num variable value 0
}
Display(num); //calling Display function
wait(2); //adding Delay of 2 second
num++; //increasing num by 1
}
}
Lab-X
Generate a sound signal using a speaker with three tones. Use pwm
output ports of the STM board. Show the schematic for the same and
write the embed C code.

#include"mbed.h" // importing mbed header file


PwmOut mypwm(D3); /declaring object of the class PwmOut as mypwm
//AnalogIn G(A1); //declaring object of the class AnalogOut
int i=0; //declaring a global variable
int main()
{
i=0;
while(i<5) //Initializing an while loop
{
mypwm.period_us(3000); //setting the play time in Millisecond
mypwm.pulsewidth_us(1550); //generating Sound of the Given
Frequency
wait(2.0); //set a delay of 2 sec
mypwm.period_us(2200); //setting the play time in Millisecond
mypwm.pulsewidth_us(800); //generating Sound of the Given
Frequency
wait(2.0); //set a delay of 2 sec
mypwm.period_us(1500); //setting the play time in Millisecond
mypwm.pulsewidth_us(800); //generating Sound of the Given
Frequency
wait(2.0); //set a delay of 2 sec
i++;
}
return 0;
}

Result:
This Program will Produce a Siren Sound with a Delay of 2 Second

Generate sa re ga ma using a speaker. Interface with STM


microcontroller board and show the schematic and write the embed C
code for the same.

#include "mbed.h" // importing mbed header file


PwmOut mypwm(D3); //declaring object of the class PwmOut as
mypwm
int main()
{
while(1) //Initializing an while loop
{
mypwm.period_us(2500); //setting the play time in
Millisecond
mypwm.pulsewidth_us(400); //generating Sound of the
Given Frequency
wait(0.5); //set a delay of half sec
mypwm.period_us(2360); //setting the play time in
Millisecond
mypwm.pulsewidth_us(424); //generating Sound of the
Given Frequency
wait(0.5); //set a delay of half sec
mypwm.period_us(2228); //setting the play time in
Millisecond
mypwm.pulsewidth_us(449); //generating Sound of the
Given Frequency
wait(0.5); //set a delay of half sec
mypwm.period_us(2103); //setting the play time in
Millisecond
mypwm.pulsewidth_us(476); //generating Sound of the
Given Frequency
wait(0.5); //set a delay of half sec
mypwm.period_us(1985); //setting the play time in
Millisecond
mypwm.pulsewidth_us(504);
wait(0.5); //set a delay of half sec
mypwm.period_us(1873); //setting the play time in
Millisecond
mypwm.pulsewidth_us(534); //generating Sound of the
Given Frequency
wait(0.5); //set a delay of half sec
mypwm.period_us(1768); //setting the play time in
Millisecond
mypwm.pulsewidth_us(566); //generating Sound of the
Given Frequency
wait(0.5); //set a delay of half sec
mypwm.period_us(1669); //setting the play time in
Millisecond
mypwm.pulsewidth_us(600); //generating Sound of the
Given Frequency
wait(0.5); //set a delay of half sec
mypwm.period_us(1574); //setting the play time in
Millisecond
mypwm.pulsewidth_us(635); //generating Sound of the
Given Frequency
wait(0.5); //set a delay of half sec
mypwm.period_us(1487); //setting the play time in
Millisecond
mypwm.pulsewidth_us(673); //generating Sound of the
Given Frequency
wait(0.5); //set a delay of half sec
mypwm.period_us(1404); //setting the play time in
Millisecond
mypwm.pulsewidth_us(713); // generating Sound of the
Given Frequency
wait(0.5); //set a delay of half sec
mypwm.period_us(1324); //setting the play time in
Millisecond
mypwm.pulsewidth_us(756); //generating Sound of the
Given Frequency
wait(0.5); //set a delay of half sec
}
}
Result:
This Program will Produce a the Sound of SA RE GA MA PA DHA NI SA

Connect a capacitive touch switch button module with STM


microcontroller board, whenever we touch the sensor a siren sound
will be generated from the speaker. Show the entire schematic of the
same and write the embed C code to do the following.

#include "mbed.h" // importing mbed header file


PwmOut mypwm(D3); //declaring object of the class PwmOut as
mypwm
AnalogIn G(A1); //declaring object of the class AnalogOut

void siren()
{
int i=0; // initializing i as 0

while(i<5) //Initializing an while loop


mypwm.period_us(3000); //setting the play time in Millisecond
mypwm.pulsewidth_us(1550);
wait(0.5);
mypwm.period_us(2200); //setting the play time in
Millisecond
mypwm.pulsewidth_us(100);
wait(0.5);
mypwm.period_us(1000); //setting the play time in
Millisecond
mypwm.pulsewidth_us(400);
wait(0.5);
i++;
}

}
int main()
{
float count; //Initializing a real number type variable
while(1) //Initializing an while loop
{
count= G.read(); //reading data from the Capacitive Switch
if(count <0.4) //Giving a Condition
{
siren(); //if condition Satisfied playing the
Sound
}
}
return 0;
}

Result:
This program will produce a sound of Siren when the Capacitive touch
Sensor will be touched

You might also like