0% found this document useful (0 votes)
88 views51 pages

Kanishk Iot

This document is a lab report submitted by Yachit Saksham, a student of IIIT Bhagalpur, for their course on IoT and Embedded Systems. It summarizes 11 experiments conducted on topics like shifting bits to turn LEDs on/off, blinking LEDs using buttons, software and hardware delays, displaying numbers on a 7-segment display, interfacing with an LCD screen, UART communication, and generating PWM signals. Each experiment section includes the aim, source code, output, and a conclusion. The report is certified by the assistant professor of ECE at IIIT Bhagalpur.

Uploaded by

mohit gora
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
88 views51 pages

Kanishk Iot

This document is a lab report submitted by Yachit Saksham, a student of IIIT Bhagalpur, for their course on IoT and Embedded Systems. It summarizes 11 experiments conducted on topics like shifting bits to turn LEDs on/off, blinking LEDs using buttons, software and hardware delays, displaying numbers on a 7-segment display, interfacing with an LCD screen, UART communication, and generating PWM signals. Each experiment section includes the aim, source code, output, and a conclusion. The report is certified by the assistant professor of ECE at IIIT Bhagalpur.

Uploaded by

mohit gora
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 51

INDIAN INSTITUTE OF INFORMATION TECHNOLOGY

Bhagalpur - 813210

IoT & Embedded Systems Lab Report

Submitted by:
Yachit Saksham
(170102055)
Class: B.Tech. – III
Semester: V

Department of Electronics & Communication Engineering


IIIT BHAGALPUR, BIHAR 813210, INDIA
July-Dec 2019
IIIT Bhagalpur
INDIAN INSTITUTE OF INFORMATION TECHNOLOGY
Bhagalpur-83210, Bihar, INDIA
DEPARTMENT OF ELECTRONICS & COMMUNICATION ENGINEERING

CERTIFICATE

This is to certify that Yachit Saksham Institute Roll No.


170102055 has satisfactorily completed the course in IoT & Embedded
Systems (EC312) during the academic year 2019-2020.

Date: 22/09/2019
Place: IIIT Bhagalpur

Dr. Suraj
(Assistant Professor, ECE, IIIT Bhagalpur)
Monday, 19 August 2019

Contents
1. Shifting bits from right to left keeping previous LEDs off .......... 1
1.1 Aim: -..................................................................................................................................... 1
1.2 Source Code: - ................................................................................................................. 1
1.3 Hex Code: -......................................................................................................................... 1
1.4 Simulated Output: - ..................................................................................................... 2
1.5 Conclusion: - .................................................................................................................... 2
2. Shifting bits from left to right keeping previous LEDs on ............ 2
2.1 Aim: -..................................................................................................................................... 2
2.2 Source Code: - ................................................................................................................. 2
2.3 Hex Code: -......................................................................................................................... 2
2.4 Simulated Output: - ..................................................................................................... 3
2.5 Conclusion: - .................................................................................................................... 3
3. Blinking of LEDs using keys .................................................................................... 3
3.1 Aim: -..................................................................................................................................... 4
3.2 Source Code: - ................................................................................................................. 4
3.3 Hex Code: -......................................................................................................................... 5
3.4 Simulated Output: - ..................................................................................................... 6
3.5 Conclusion: - .................................................................................................................... 7
4. Software delay using an external function .................................................. 8
4.1 Aim: -..................................................................................................................................... 8
4.2 Source Code: - ................................................................................................................. 8
4.3 Hex Code: -......................................................................................................................... 9
4.4 Simulated Output: - ..................................................................................................... 9
4.5 Conclusion: - .................................................................................................................... 9
5. Hardware delay of 1 sec ........................................................................................... 10
5.1 Aim: -................................................................................................................................... 10
5.2 Source Code: - ............................................................................................................... 10
5.3 Hex Code: -....................................................................................................................... 10
5.4 Simulated Output: - ................................................................................................... 11
5.5 Conclusion: - .................................................................................................................. 11
6. Hardware delay using LEDs blinking in a single bit of any port
12

i|Page
6.1 Aim: -................................................................................................................................... 12
6.2 Source Code: - ............................................................................................................... 12
6.3 Hex Code: -....................................................................................................................... 12
6.4 Simulated Output: - ................................................................................................... 13
6.5 Conclusion: - .................................................................................................................. 13
7. Hardware delay using interrupt ........................................................................... 14
7.1 Aim: -................................................................................................................................... 14
7.2 Source Code: - ............................................................................................................... 14
7.3 Hex Code: -....................................................................................................................... 15
7.4 Simulated Output: - ................................................................................................... 15
7.5 Hardware Output: - ...................................................................................................... 16
7.6 Conclusion: - .................................................................................................................. 16
8. Display 0 to 9 using 7 segment display ............................................................ 17
8.1 Aim: -................................................................................................................................... 17
8.2 Source Code: - ............................................................................................................... 17
8.3 Hex Code: -....................................................................................................................... 18
8.4 Conclusion: - .................................................................................................................. 19
9. LCD Interfacing ................................................................................................................... 20
9.1 Aim: -................................................................................................................................... 20
9.2 Source Code: - ............................................................................................................... 20
9.3 Hex Code: -....................................................................................................................... 21
9.4 Hardware Interfaced Output: -............................................................................ 23
9.5 Conclusion: - .................................................................................................................. 23
10. UART communication .......................................................................................................... 24
10.1 Aim: - ................................................................................................................................ 24
10.2 Source Code: - ............................................................................................................. 24
10.3 Hex Code: - .................................................................................................................... 24
10.4 Simulated Output: - ................................................................................................. 25
10.5 Conclusion: - ............................................................................................................... 25
11.Generation of PWM signals ........................................................................................... 26
11.1 Aim: - ............................................................................................................................ 26
11.2 Source Code: - ......................................................................................................... 26
11.3 Hex Code: - ................................................................................................................ 27
11.4 Simulated Output: - ............................................................................................. 27
11.5 Conclusion................................................................................................................... 29

ii | P a g e
12. Serial Communication using interrupt ............................................................... 30
12.1 Aim: - ................................................................................................................................ 30
12.2 Source Code: - ............................................................................................................. 30
12.3 Hex Code: - .................................................................................................................... 30
12.4 Simulation Output: - ............................................................................................... 31
12.5 Conclusion: - ............................................................................................................... 31
13. Generate a PWM of following duty cycle suing PCA timer .................... 32
13.1 Aim: - ................................................................................................................................ 32
13.2 Source Code: - ............................................................................................................. 32
13.3 Hex Code: - .................................................................................................................... 32
13.4 Simulated Output: - ................................................................................................. 32
13.5 Conclusion: - ............................................................................................................... 33
14. Basics of Python(loops) .............................................................................................. 34
14.1 Use of while loop ...................................................................................................... 34
14.2 Use of for loop........................................................................................................... 34
15. Basics of Python (Pandas library) ...................................................................... 37
15.1 Program: 01 .................................................................................................................... 37
15.2 Program: 02 .................................................................................................................... 37
15.3 Program: 03 .................................................................................................................... 37
16. Basics of Python (Numpy library) ........................................................................ 39
16.1 Program: 01 .................................................................................................................... 39
16.2 Program: 02 .................................................................................................................... 39
17. Basics of Python (Image Processing) ................................................................. 40
17.1 Program: 01 .................................................................................................................... 40
17.2 Program: 02 .................................................................................................................... 40
15.3 Program: 03 .................................................................................................................... 42

iii | P a g e
Figure 1 :Shifting of bits from right to left .............................................................................................. 2
Figure 2 : shifting of LED from left to right keeping previous LED on ..................................................... 3
Figure 3 : Shifting of LED from right to left and left to right respectively............................................... 6
Figure 4 : Shifting of LED from right to left and left to right respectively keeping previous LED on ...... 6
Figure 5 : Software delay of 1ms ............................................................................................................ 9
Figure 6 : Hardware delay of 1sec......................................................................................................... 11
Figure 7 : Wave showing LED blinking with hardware delay of 0.5 sec ................................................ 13
Figure 8 : Wave showing interrupt signal ............................................................................................. 15
Figure 9 : Interrupt signal in DSO .......................................................................................................... 16
Figure 10 : Hardware showing name and roll no of student ................................................................ 23
Figure 11 : Printing IIIT BHAGALPUR in UART #1 .................................................................................. 25
Figure 12 : Wave showing 80%,20% and 40% duty cycle respectively ................................................. 29
Figure 13 : Serial communication using interrupt ................................................................................ 31
Figure 14 : Wave showing 40% and 80% duty cycle using PCA timer................................................... 33

iv | P a g e
Monday, 19 August 2019

1. Shifting bits from right to left


keeping previous LEDs off

1.1 Aim: -
Write an 8051 program to toggle LEDs connected to the
port P1 from right to left, turning off the previous
LEDs.
1.2 Source Code: -
/*
********************************************************************
* Author: Yachit Saksham *
* Roll No.: 170102055 *
* Institute: IIIT BHAGALPUR *
* Department: ECE *
* Date: 19 August 2019 *
* *
********************************************************************
*/

#include <reg51.h>
int i,j;
void main()
{
P1=0x01; // initial state
while(1)
{
for(i=0;i<8;i++)
{
P1=P1<<1; // Shift Left
if(P1==0x80) // Reset to initial state
P1=0x01;
}
}
}
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% This function file is created to shift blinking of LED connected to port 1. %
% %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*/

1.3 Hex Code: -


:03000000020800F3
:0C080000787FE4F6D8FD758109020847F6
:0408CA000208000020
:10088C00759001E59025E0F590E4F508F509E4F59F
:10089C0008F5090509E50970020508646445087046
:1008AC00F20509E50970020508C39464E508940093

1|Page
Monday, 19 August 2019

:0E08BC0040DCAF90EF648070CA75900180C57B
:10080C0002088CE493A3F8E493A34003F68001F26E
:10081C0008DFF48029E493A3F85407240CC8C333ED
:10082C00C4540F4420C8834004F456800146F6DFBC
:10083C00E4800B01020408102040809008CAE47E7A
:10084C00019360BCA3FF543F30E509541FFEE493B1
:10085C00A360010ECF54C025E060A840B8E493A378
:10086C00FAE493A3F8E493A3C8C582C8CAC583CAA3
:10087C00F0A3C8C582C8CAC583CADFE9DEE780BE5B
:0108CE000029
:00000001FF

1.4 Simulated Output: -

Figure 1 :Shifting of bits from right to left

1.5 Conclusion: -
We Successfully got blinking LED from right to left,
turning off the previous LEDs.

2. Shifting bits from left to right


keeping previous LEDs on

2|Page
Monday, 19 August 2019

2.1 Aim: -
Write an 8051 C program to toggle LEDs connected to the
port P1, from left to right, keeping previous LEDs on.
2.2 Source Code: -
/*
********************************************************************
* Author: Yachit Saksham *
* Roll No.: 170102055 *
* Institute: IIIT BHAGALPUR *
* Department: ECE *
* Date: 19 August 2019 *
* *
********************************************************************
*/
#include <reg51.h>
unsigned int i;
unsigned int j;
void main()
{
while(1)

{
P1=0x80; // initial state
for(j=0;j<8;j++)
{

for(i=0;i<60000;i++); // software delay


P1=P1>>1; // Shift right
for(i=0;i<60000;i++);
P1=P1+0x80; // adding 1 to right of port P1
}
}

}
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% This function file is created to shift blinking of LED connected to port 1. %
% %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*/

2.3 Hex Code: -


:03000000020800F3
:0C080000787FE4F6D8FD758109020847F6
:0408F50002080000F5
:10088C00E4F590E59025E004F590E4F508F509E42D
:10089C00F508F5090509E50970020508B490F5E5B8
:1008AC0008B401F00509E50970020508C39464E574
:1008BC0008940040DAAF90EFF470C8F590F508F5A5

2|Page
Monday, 19 August 2019

:1008CC0009C3E5099464E508940050B7E4F508F50C
:1008DC00090509E509700205086464450870F2050C
:0908EC0009E50970DC050880D85B
:10080C0002088CE493A3F8E493A34003F68001F26E
:10081C0008DFF48029E493A3F85407240CC8C333ED
:10082C00C4540F4420C8834004F456800146F6DFBC
:10083C00E4800B01020408102040809008F5E47E4F
:10084C00019360BCA3FF543F30E509541FFEE493B1
:10085C00A360010ECF54C025E060A840B8E493A378
:10086C00FAE493A3F8E493A3C8C582C8CAC583CAA3
:10087C00F0A3C8C582C8CAC583CADFE9DEE780BE5B
:0108F90000FE
:00000001FF

2.4 Simulated Output: -

Figure 2 : shifting of LED from left to right keeping previous LED on

2.5 Conclusion: -
We Successfully got blinking LED from left to right,
keeping the previous LEDs On.

3. Blinking of LEDs using keys

3|Page
Monday, 26 August 2019

3.1 Aim: -
Write an 8051 C program to perform blinking of LEDs using
keys.
3.2 Source Code: -
/*
********************************************************************
* Author: Yachit Saksham *
* Roll No.: 170102055 *
* Institute: IIIT BHAGALPUR *
* Department: ECE *
* Date: 26 August 2019 *
* *
********************************************************************
*/
#include <reg51.h>
unsigned int i=0;
void delaying() // delay function
{
for(i=0;i<100;i++)
{for(i=0;i<100;i++);}
}

// specifing switches

sbit ukey = P2^1;


sbit rkey = P2^0;
sbit lkey = P2^2;
sbit dkey = P2^3;
sbit mkey = P2^7;

int main()
{
P1=0x00; // initial state
P2=0X00; // initial state

while(1)
{

if(ukey==1 && (dkey==0 && lkey==0 && rkey==0))


{P1=0x01;delaying();} // LED on at 1
if(rkey==1 && (dkey==0 && lkey==0 && ukey==0 ))
{P1=0x03;delaying();} // LEDs on at 1,2
if(lkey==1 && (dkey==0 && ukey==0 && rkey==0 ))
{P1=0x07;delaying();} // LEDs on at 1,2,3
if(dkey==1 && (ukey==0 && lkey==0 && rkey==0 ))
{P1=0x15;delaying();} // LEDs on at 1,2,3,4
if(mkey==1)
{P1=0x00; return 0;} // switch off all LED
if(ukey==0 && dkey==0 && lkey==0 && rkey==0 )
{P1=0x00;delaying();}
else if((rkey==1 && (dkey==1 || lkey==1 || ukey==1))||
(lkey==1 && (dkey==1 || rkey==1 || ukey==1))||
(ukey==1 && (dkey==1 || lkey==1 || rkey==1))||
(dkey==1 && (rkey==1 || lkey==1 || ukey==1)))
{P1=0xFF;delaying();} // all LEDs are on
}

4|Page
Monday, 26 August 2019

}
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% This function file is created to perform user-based operations at LED %
% connected to port 1. %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*/

3.3 Hex Code: -


:030000000208A64D
:0C08A600787FE4F6D8FD7581090208EDAA
:04095C00020800008D
:10093200E4F508F509E4F508F5090509E509700289
:1009420005086464450870F20509E50970020508A6
:0A095200C39464E508940040DC2221
:10080000E4F590F5A030A10F20A30C20A20920A0B0
:100810000675900112093230A00F20A30C20A20906
:1008200020A10675900312093230A20F20A30C20DC
:10083000A10920A00675900712093230A30F20A14C
:100840000C20A20920A00675901512093230A706C7
:10085000E4F590FEFF2220A10E20A30B20A2082089
:10086000A005E4F590803930A00920A33020A22D06
:1008700020A12A30A20920A32420A02120A11E30DB
:10088000A10920A31820A21520A01220A30302086A
:100890000520A00920A20620A1030208057590FFEB
:0608A000120932020805F6
:1008B200020800E493A3F8E493A34003F68001F254
:1008C20008DFF48029E493A3F85407240CC8C33347
:1008D200C4540F4420C8834004F456800146F6DF16
:1008E200E4800B010204081020408090095CE47E41
:1008F200019360BCA3FF543F30E509541FFEE4930B
:10090200A360010ECF54C025E060A840B8E493A3D1
:10091200FAE493A3F8E493A3C8C582C8CAC583CAFC
:10092200F0A3C8C582C8CAC583CADFE9DEE780BEB4
:010960000096
:00000001FF

5|Page
Monday, 26 August 2019

3.4 Simulated Output: -

Figure 3 : Shifting of LED from right to left and left to right respectively

Figure 4 : Shifting of LED from right to left and left to right respectively keeping previous LED on

6|Page
Monday, 26 August 2019

3.5 Conclusion: -
We have successfully performed the blinking of LEDs
using keys .

7|Page
Monday, 2 September 2019

4. Software delay using an external


function

4.1 Aim: -
Write an 8051 program to perform software delay using an
external function.
4.2 Source Code: -
/*
********************************************************************
* Author: Yachit Saksham *
* Roll No.: 170102055 *
* Institute: IIIT BHAGALPUR *
* Department: ECE *
* Date: 2 September 2019 *
* *
********************************************************************
*/

#include <reg51.h>
void wait(int);
void main()
{
P1=0x01; // initial state
while(1)
{
P1=0x00; // all off
wait(10);
P1=0xFF; // all on
wait(10);
}
}

int i,j;

void wait(int d) // delay function


{
for(i=0;i<=d;i++)
{
for(j=0;j<28000;j++);
}
}
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% This function file is created to perform a software delay of 1 sec using an %
% external function. %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*/

8|Page
Monday, 2 September 2019

4.3 Hex Code: -


:03000000020824CF
:0C082400787FE4F6D8FD75810902081603
:0E081600E4F5901208007590FF12080080F2C1
:10080000E4F508F5090509E50970020508B4F5F5F0
:06081000E508B41DF02212
:00000001FF

4.4 Simulated Output: -

Figure 5 : Software delay of 1ms

4.5 Conclusion: -
We have successfully performed Software Delay using
external function and we got delay of 1ms.

9|Page
Monday, 2 September 2019

5. Hardware delay of 1 sec

5.1 Aim: -
Write an 8051 program to perform hardware delay of 1 sec.
5.2 Source Code: -
/*
********************************************************************
* Author: Yachit Saksham *
* Roll No.: 170102055 *
* Institute: IIIT BHAGALPUR *
* Department: ECE *
* Date: 2 September 2019 *
* *
********************************************************************
*/
#include <reg51.h>
void delay();
int i;
int main()
{
while(1)
{
P1=~P1; // complement the port bits
for(i=0;i<100;i++)
{
delay(); // loop for 100times
} // produces 1 s delay
}
}

void delay()
{
TMOD=0X01; // mode 0
TH0=0xDB;
TL0=0xFF;
TR0=1; // timer start
while(TF0==0); // wait till flag is set to 1
TF0=0; // reset flag value
TR0=0; // stop timer
}
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% This function file is created to perform a hardware delay of 1 sec using an %
% external function. %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*/

5.3 Hex Code: -


:03000000020800F3
:0C080000787FE4F6D8FD75810B020847F4

10 | P a g e
Monday, 2 September 2019

:0808BA0002080001020A00001F
:10088C006390FFE4F50AF50B1208AA050BE50B7053
:0E089C0002050AC39490E50A940140EC80E244
:1008AA00758CDCE4F58AD28C308DFDC28DC28C2227
:10080C0002088CE493A3F8E493A34003F68001F26E
:10081C0008DFF48029E493A3F85407240CC8C333ED
:10082C00C4540F4420C8834004F456800146F6DFBC
:10083C00E4800B01020408102040809008BAE47E8A
:10084C00019360BCA3FF543F30E509541FFEE493B1
:10085C00A360010ECF54C025E060A840B8E493A378
:10086C00FAE493A3F8E493A3C8C582C8CAC583CAA3
:10087C00F0A3C8C582C8CAC583CADFE9DEE780BE5B
:0108C2000035
:00000001FF

5.4 Simulated Output: -

Figure 6 : Hardware delay of 1sec

5.5 Conclusion: -
We Successfully performed Hardware Delay using LEDs
blinking and we got delay of 1s.

11 | P a g e
Monday, 9 September 2019

6. Hardware delay using LEDs blinking in a


single bit of any port

6.1 Aim: -
Write an 8051 program to perform hardware delay using
LEDs blinking in a single bit of any port.
6.2 Source Code: -
/*
********************************************************************
* Author: Yachit Saksham *
* Roll No.: 170102055 *
* Institute: IIIT BHAGALPUR *
* Department: ECE *
* Date: 9 September 2019 *
* *
********************************************************************
*/
#include<reg51.h>
TMOD0=0x01; // timer mode 0
unsigned int i=0;
void delay();
sbit x=P1^0;
void main()
{
P1=0x00; // initial state
x=0xFF; // pin 0 of port P1 is set to 1
while(1)
{

x=~x; // complement for producing blinking effect


for(i=0;i<400;i++)
delay();

}
}
void delay()
{
TH0=0x78;
TL0=0x01;
TR0=1; // timer start
while(TF0==0); // wait till flag is 1
TF0=0; // reset flag
TR0=0; // stop timer

}
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% This function file is created to perform a hardware delay using LED blinking %
% in a single bit of any port. %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*/

6.3 Hex Code: -


:03000000020800F3

12 | P a g e
Monday, 9 September 2019

:0C080000787FE4F6D8FD75810B020847F4
:0808BE0002080001020A00001B
:10088C00E4F590D290B290E4F50AF50B1208AE059F
:10089C000BE50B7002050AC39490E50A940140EC39
:0208AC0080E3E7
:1008AE00758C78758A01D28C308DFDC28DC28C22EA
:10080C0002088CE493A3F8E493A34003F68001F26E
:10081C0008DFF48029E493A3F85407240CC8C333ED
:10082C00C4540F4420C8834004F456800146F6DFBC
:10083C00E4800B01020408102040809008BEE47E86
:10084C00019360BCA3FF543F30E509541FFEE493B1
:10085C00A360010ECF54C025E060A840B8E493A378
:10086C00FAE493A3F8E493A3C8C582C8CAC583CAA3
:10087C00F0A3C8C582C8CAC583CADFE9DEE780BE5B
:0108C6000031
:00000001FF

6.4 Simulated Output: -

Figure 7 : Wave showing LED blinking with hardware delay of 0.5 sec

6.5 Conclusion: -
We Successfully performed Hardware Delay using using LEDs
blinking and we got delay of 1.5s.

13 | P a g e
Monday, 16 September 2019

7. Hardware delay using interrupt

7.1 Aim: -
Write an 8051 program to perform hardware delay using
interrupt.

7.2 Source Code: -


/*
********************************************************************
* Author: Yachit Saksham *
* Roll No.: 170102055 *
* Institute: IIIT BHAGALPUR *
* Department: ECE *
* Date: 16 September 2019 *
* *
********************************************************************
*/
#include<reg51.h>
TMOD0=0x01; // timer mode 0
unsigned int i=0;
void delay();
sbit x=P1^0;
void main()
{
TMOD = 0x01;
IE=0x82; // interrupt register value
P1=0x00; // initial value
TH0=0xF4;
TL0=0x00;
TR0=1; // timer start

x=1; i=0;
while(1)
{
if(i==8)
x=~x; // blink
if(i==10) {
x=~x; // blink
i=0;
}
}
}
void func() interrupt 1 // interrupt function
{
i++;
TH0=0xF4;
TL0=0x00;
}
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% This function file is created to perform a hardware delay using interrupt. %
% %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*/

14 | P a g e
Monday, 16 September 2019

7.3 Hex Code: -


:03000000020800F3
:0C080000787FE4F6D8FD75810B020847F4
:0808CF0002080001020A00000A
:10088C0075890175A882E4F590758CF4F58AD28C83
:10089C00D290F50AF50BE50B6408450A7002B2908C
:1008AC00E50B640A450A70EEB290F50AF50B80E68A
:03000B000208BC2C
:1008BC00C0E0050BE50B7002050A758CF4758A0017
:0308CC00D0E03247
:10080C0002088CE493A3F8E493A34003F68001F26E
:10081C0008DFF48029E493A3F85407240CC8C333ED
:10082C00C4540F4420C8834004F456800146F6DFBC
:10083C00E4800B01020408102040809008CFE47E75
:10084C00019360BCA3FF543F30E509541FFEE493B1
:10085C00A360010ECF54C025E060A840B8E493A378
:10086C00FAE493A3F8E493A3C8C582C8CAC583CAA3
:10087C00F0A3C8C582C8CAC583CADFE9DEE780BE5B
:0108D7000020
:00000001FF

7.4 Simulated Output: -

Figure 8 : Wave showing interrupt signal

15 | P a g e
Monday, 16 September 2019

7.5 Hardware Output: -

Figure 9 : Interrupt signal in DSO

7.6 Conclusion: -
We have successfully implemented Hardware Delay of 1s as
specified in mode 1 using Timer T1 interrupt.

16 | P a g e
Monday, 21 October 2019

8. Display 0 to 9 using 7 segment display

8.1 Aim: -
Write an 8051 program to display 0 to 9 using 7 segment
display. control lines are p2.7, p2.6, p2.5. Display is
connected to p0.

8.2 Source Code: -


/*
********************************************************************
* Author: Yachit Saksham *
* Roll No.: 170102055 *
* Institute: IIIT BHAGALPUR *
* Department: ECE *
* Date: 21 October 2019 *
* *
********************************************************************
*/
#include <reg51.h>
// 0 to 9 in hexadecimal
char
disp[10]={0x3F,0x06,0x5B,0x8F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
int i;
// switches
sbit a2=P2^5;
sbit a1=P2^6;
sbit a0=P2^7;
int seg;
void findseg() // check for segment
{
switch(seg)
{
case 0:
a0=0;a1=0;a2=0;break;
case 1:
a0=0;a1=0;a2=1;break;
case 2:
a0=0;a1=1;a2=0;break;
case 3:
a0=0;a1=1;a2=1;break;
default:
break;
}
}
void delay()
{
TMOD=0X01; // timer mode 0
for(i=0;i<400;i++)
{
TH0=0x78;
TL0=0x01;
TR0=1; // timer start
while(TF0==0); // loop till flag is set to 1
TF0=0; // reset flag
TR0=0; // stop timer

17 | P a g e
Monday, 21 October 2019

}
}

void main()
{

int i;
P0=0x00;
P2=0x00;
seg=0;
while(1)
{
findseg(); // call findseg function
for(i=0;i<10;i++)
{
P0=disp[i];
delay();
}
seg++;
if(seg==4) seg=0;
}
}
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% This function file is created to display 0 to 9 using 7 segment LED display. %
% %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*/

8.3 Hex Code: -


:03000000020800F3
:0C080000787FE4F6D8FD758115020847EA
:0C091B000A083F065B8F666D7D077F6F4A
:1008C400E512702AE51314601114601514601924DC
:1008D40003701BC2A7C2A6C2A522C2A7C2A6D2A5E4
:0F08E40022C2A7D2A6C2A522C2A7D2A6D2A522FF
:1008F300758901E4F514F515758C78758A01D28C28
:10090300308DFDC28DC28C0515E51570020514B43A
:0809130090E6E514B401E122B5
:10088C00E4F580F5A0F512F5131208C4E4FFFE742C
:10089C00082FF8E6F5801208F30FBF00010EEF6485
:1008AC000A4E70EB0513E513700205126404451231
:0808BC0070D7F512F51380D18D
:10080C0002088CE493A3F8E493A34003F68001F26E
:10081C0008DFF48029E493A3F85407240CC8C333ED
:10082C00C4540F4420C8834004F456800146F6DFBC
:10083C00E4800B010204081020408090091BE47E28
:10084C00019360BCA3FF543F30E509541FFEE493B1

18 | P a g e
Monday, 21 October 2019

:10085C00A360010ECF54C025E060A840B8E493A378
:10086C00FAE493A3F8E493A3C8C582C8CAC583CAA3
:10087C00F0A3C8C582C8CAC583CADFE9DEE780BE5B
:0109270000CF
:00000001FF

8.4 Conclusion: -
We used four 7 segment LED display to continuously
display series of numbers from 0 to 9. We kept shifting
this series from first segment to last segment

19 | P a g e
Monday, 4 November 2019

9. LCD Interfacing

9.1 Aim: -
Interface the LCD with 8051 micro controller and display
Name and Roll number in first and second line
respectively.
9.2 Source Code: -
/*
********************************************************************
* Author: Yachit Saksham *
* Roll No.: 170102055 *
* Institute: IIIT BHAGALPUR *
* Department: ECE *
* Date: 4 November 2019 *
* *
********************************************************************
*/
#include <reg51.h>
void lcd_init(void);
void lcd_cmd(unsigned char command);
void lcd_data(unsigned char disp_data);
void delay(unsigned int t);
unsigned int i;
// switches
sbit RS=P2^7;
sbit RW=P2^6;
sbit EN=P2^5;
void delay(unsigned int t)
{
for(i=0;i<t;i++)
{
TMOD=0X01; // timer mode 0
TH0=0XF4;
TL0=0X00;
TR0=1; // timer start
while(TF0==0); // loop till flag is set to 1
TF0=0; // reset flag
TR0=0; // stop timer
}
}
void lcd_init() // initial the LCD
{
lcd_cmd(0x38); // turn on LCD
delay(10);
lcd_cmd(0x0F); // clear display
delay(10);
lcd_cmd(0x01); // cursor glow
delay(10);
}
void lcd_cmd(char command)
{
P0=command; // input command
RS=0; // register select
RW=0; // register read/write
EN=1; // enable set

20 | P a g e
Monday, 4 November 2019

delay(10);
EN=0; // enable reset
}
void lcd_data(char disp_data)
{
P0=disp_data; // output data
RS=1;
RW=0;
EN=1;
delay(10);
EN=0;
}
void main()
{
// string to output
char name[8]= {'V','A','T','S','A','L','Y','A'};
char roll[8]= {'U','1','6','E','E','0','5','9'};
int i;
char command;
while(1)
{
lcd_init(); // initialize the LCD
lcd_cmd(0x80); // select 1st line
for(i=0;i<9;i++)
{
command=name[i]; // read 1st string
lcd_data(command); // display input string
}
lcd_cmd(0xc0); // selct 2nd line
for(i=0;i<9;i++)
{
command=roll[i]; // read 2nd string
lcd_data(command); // display input sting
}
}
}
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% This function file is created to display name and roll number in first and %
% second line respectively. %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*/

9.3 Hex Code: -


:030000000209C52D
:0C09C500787FE4F6D8FD75811A0208F670
:1009B50056415453414C59415531364545303539E9
:08095600C2A6D2A57F0A7E00B3
:10095E00E4F519F51AC3E51A9FE5199E501C758921
:10096E0001758CF4E4F58AD28C308DFDC28DC28C6B
:0B097E00051AE51A70DF051980DB2266
:100989007F381209D112095A7F0F1209D112095A57
:0C0999007F011209D17F0A7E0002095E76
:0A09D1008F80C2A7120956C2A522AA

21 | P a g e
Monday, 4 November 2019

:0609A5002DF8E6F518FF35
:0A09AB008F80D2A7120956C2A522C0
:1008F60078087C007D007BFF7A0979B57E007F0849
:100906001208D078107C007D007BFF7A0979BD7EC5
:10091600007F081208D01209897F801209D1E4FDF0
:10092600FC74081209A50DBD00010CED64064C709F
:10093600F07FC01209D1E4FCFDC3ED9409EC64809C
:10094600948050D274101209A50DBD00010C80E9E7
:10080000E709F608DFFA8046E709F208DFFA803EDA
:1008100088828C83E709F0A3DFFA8032E309F608C7
:10082000DFFA8078E309F208DFFA807088828C832F
:10083000E309F0A3DFFA806489828A83E0A3F608E3
:10084000DFFA805889828A83E0A3F208DFFA804CBD
:1008500080D280FA80C680D4806980F28033801094
:1008600080A680EA809A80A880DA80E280CA8033FD
:1008700089828A83ECFAE493A3C8C582C8CCC58375
:10088000CCF0A3C8C582C8CCC583CCDFE9DEE78045
:100890000D89828A83E493A3F608DFF9ECFAA9F0C4
:1008A000EDFB2289828A83ECFAE0A3C8C582C8CC1A
:1008B000C583CCF0A3C8C582C8CCC583CCDFEADE33
:1008C000E880DB89828A83E493A3F208DFF980CC95
:1008D00088F0EF60010E4E60C388F0ED2402B4048E
:1008E0000050B9F582EB2402B4040050AF23234535
:0608F00082239008507302
:00000001FF

22 | P a g e
Monday, 4 November 2019

9.4 Hardware Interfaced Output: -

Figure 10 : Hardware showing name and roll no of student

9.5 Conclusion: -
In this experiment we have learnt how to interface an LCD
using 8051 micro controller. We use lcd_init function to
initialize LCD, lcd_cmd function to pass a command to
LCD, lcd_data function to send data to be displayed to
LCD. RS, RW & EN are control signals of LCD.

23 | P a g e
Monday, 11 November 2019

10. UART communication

10.1 Aim: -
Write a program for UART communication.

10.2 Source Code: -


/*
********************************************************************
* Author: Yachit Saksham *
* Roll No.: 170102055 *
* Institute: IIIT BHAGALPUR *
* Department: ECE *
* Date: 11 November 2019 *
* *
********************************************************************
*/
#include <reg51.h>
int i;
unsigned char A[15]="IIIT BHAGALPUR "; // input string

void main()
{
TMOD=0X20; // timer mode 2
TH1=0XFD;
TCON=0X50; // start timer
TR1=1; // timer start
while(1)
{
for (i=0; i<15; i++)
{
SBUF=A[i]; // serial buffer reads string bit
while(TI==0); // loop till interrupt flag is set to 1
TI=0; // reset flag
}
}
}
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% This function file is created to set a UART serial communication by loading %
% data into SBUF. %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*/

10.3 Hex Code: -


:03000000020800F3
:0C080000787FE4F6D8FD758118020847E7
:1008B9000F08494949542042484147414C50555233
:0108C900200E
:10088C00758920758DFD758850D28EE4F517F51895
:10089C0074082518F8E6F5993099FDC2990518E504

24 | P a g e
Monday, 11 November 2019

:0D08AC001870020517640F451770E580DE17
:10080C0002088CE493A3F8E493A34003F68001F26E
:10081C0008DFF48029E493A3F85407240CC8C333ED
:10082C00C4540F4420C8834004F456800146F6DFBC
:10083C00E4800B01020408102040809008B9E47E8B
:10084C00019360BCA3FF543F30E509541FFEE493B1
:10085C00A360010ECF54C025E060A840B8E493A378
:10086C00FAE493A3F8E493A3C8C582C8CAC583CAA3
:10087C00F0A3C8C582C8CAC583CADFE9DEE780BE5B
:0108CA00002D
:00000001FF

10.4 Simulated Output: -

Figure 11 : Printing IIIT BHAGALPUR in UART #1

10.5 Conclusion: -
We did UART Serial Communication by loading data into SBUF. We
used the baud rate of 9600 Hz and sent the data “IIIT
BHAGALPUR” at this baud rate.

25 | P a g e
Monday, 11 November 2019

11.Generation of PWM signals


11.1 Aim: -
Generate following PWM signal as mentioned below using timer 1
mode 1 (polling method)
a) P2.1=1, Generate PWM signal of frequency 1kHz and duty
cycle = 80% on P1.1
b) P2.2=1, Generate PWM signal of frequency 2kHz and duty
cycle = 20 % on P1.2
c) P2.3=1, Generate PWM signal of frequency 500Hz and duty
cycle = 40% on P1.3
11.2 Source Code: -
/*
********************************************************************
* Author: Yachit Saksham *
* Roll No.: 170102055 *
* Institute: IIIT BHAGALPUR *
* Department: ECE *
* Date: 11 November 2019 *
* *
********************************************************************
*/
#include <reg51.h>
int i;
void delay(int r);
// switches
sbit a=P1^1;
sbit b=P1^2;
sbit c=P1^3;
// ouput ports
sbit PIN1=P2^1;
sbit PIN2=P2^2;
sbit PIN3=P2^3;
void main()
{
while(1)
{
TMOD=0x10; // timer mode 1
// intial pin values
PIN1=0;
PIN2=0;
PIN3=0;
if(a==1) // 1st PWM
{
delay(2);
PIN1=1; // off for 2/10 halves
delay(8);
PIN1=0; // on for 8/10 halves
}
if(b==1) // 2nd PWM
{
delay(1);
PIN2=1; // off for 1/5 halves
delay(4);
PIN2=0; // on for 4/5 halves

26 | P a g e
Monday, 11 November 2019

}
if(c==1) // 3rd PWM
{
delay(12);
PIN3=1; // off for 6/10 halves
delay(8);
PIN3=0; // on for 4/10 halves

}
}
}
void delay(int r)
{
while(r!=0)
{TH1=0xDC;
TL1=0x03;
TR1=1;
while(TF1==0);
TR1=0;
TF1=0;
r--;
}
}
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% This function file is created to generate PWM waves. %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*/

11.3 Hex Code: -


:030000000208599A
:0C085900787FE4F6D8FD758107020800E6
:10080000758910C291C292C29330A10E7F02120864
:100810003ED2917F0812083EC29130A20E7F041290
:10082000083ED2927F0112083EC29230A3D27F0CC2
:0E08300012083ED2937F0812083EC29380C287
:10083E00EF4E6016758DFE758BE0D28E308FFDC239
:0B084E008EC28FEF1F70E91E80E622B3
:00000001FF

11.4 Simulated Output: -

27 | P a g e
Monday, 11 November 2019

28 | P a g e
Monday, 11 November 2019

Figure 12 : Wave showing 80%,20% and 40% duty cycle respectively

11.5 Conclusion
Hence, we have generated PWM signal of different duty cycle on
port 1 based on switching condition of port 2 bits, results
have been shown in above figures for different case.

29 | P a g e
Monday, 18 November 2019

12. Serial Communication using interrupt

12.1 Aim: -
Write a program for Serial Communication using interrupt.

12.2 Source Code: -


/*
********************************************************************
* Author: Yachit Saksham *
* Roll No.: 170102055 *
* Institute: IIIT BHAGALPUR *
* Department: ECE *
* Date: 11 November 2019 *
* *
********************************************************************
*/
#include <reg51.h>
unsigned int i;
unsigned char name[8]="SANTOSH";
void main(){
TMOD = 0x20; //timer 1 in mode 2
SCON = 0x50; //setup serial control reg
IE=0x98; //setup serial interrupt with timer
1 overflow interrupt
TH1 = 0XFA; //4800 baud rate
TR1 = 1; //start tick
TI=1; //enable serial transmission
response
}
void ser_delay() interrupt 4 //serial interrupt
{
while(1)
{
for(i=0;i<=8;i++)
{
SBUF = name[i]; //assign serial buffer reg
while(TI == 0);
TI = 0;
}
}
}
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% This function file is created to set a serial communication by loading %
% data into SBUF using interrupt. %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*/

12.3 Hex Code: -


:03000000020800F3

30 | P a g e
Monday, 18 November 2019

:0C080000787FE4F6D8FD758111020847EE
:0A08C900080853414E544F534800F5
:1008B80075892075985075A898758DFAD28ED29939
:0108C800220D
:0300230002088C44
:10088C00C0E0C0D075D000C00075100075110074A8
:10089C00082511F8E6F5993099FDC2990511E51175
:0C08AC00700205106409451070E580DD45
:10080C000208B8E493A3F8E493A34003F68001F242
:10081C0008DFF48029E493A3F85407240CC8C333ED
:10082C00C4540F4420C8834004F456800146F6DFBC
:10083C00E4800B01020408102040809008C9E47E7B
:10084C00019360BCA3FF543F30E509541FFEE493B1
:10085C00A360010ECF54C025E060A840B8E493A378
:10086C00FAE493A3F8E493A3C8C582C8CAC583CAA3
:10087C00F0A3C8C582C8CAC583CADFE9DEE780BE5B
:0108D3000024
:00000001FF

12.4 Simulation Output: -

Figure 13 : Serial communication using interrupt

12.5 Conclusion: -
We did UART Serial Communication using interrupt by
loading data into SBUF. We used the baud rate of 9600 Hz
and sent the data “SANTOSH” at this baud rate.

31 | P a g e
Monday, 18 November 2019

13. Generate a PWM of following duty cycle


using PCA timer
13.1 Aim: -
Write a program to generate a PWM of following duty cycle
using PCA timer.
When P2.1=1, Generate a PWM of duty cycle of 40%.
When P2.1=0, Generate a PWM of duty cycle of 80%.
13.2 Source Code: -
/*
********************************************************************
* Author: Yachit Saksham *
* Roll No.: 170102055 *
* Institute: IIIT BHAGALPUR *
* Department: ECE *
* Date: 11 November 2019 *
* *
********************************************************************
*/
#include "header.h" // changed header file
sbit sw=P2^1; // switch
void main()
{
P2=0x00; // initial state
CMOD=0x02; // setup PCA timer without interrupt enable
CCAPM0=0x42; // setup PCA module 0 in PWM mode
CR=1; // start PCA timer

while(1)
{
if(sw==0)
{
CCAP0L=0X40; // set initial same as CCAP0H
CCAP0H=0X40; // duty cycle of 40%
}
if(sw==1)
{
CCAP0L=0X99; // set same as CCAP0H
CCAP0H=0X99; // duty cycle of 80%
}
}
}

13.3 Hex Code: -


:10080000E4F5A0F5D975DA42D2DE20A10675EA40FA
:0E08100075FA4030A1F475EA9975FA9980ECFA
:0300000002081ED5
:0C081E00787FE4F6D8FD75810702080021
:00000001FF
13.4 Simulated Output: -

32 | P a g e
Monday, 18 November 2019

Figure 14 : Wave showing 40% and 80% duty cycle using PCA timer

13.5 Conclusion: -
We generated PWM of different duty cycle using PCA timer
for different states of switch. We programmed module 0 of
PCA timer in 8bit PWM mode.

33 | P a g e
Monday, 18 November 2019

14. Basics of Python(loops)

14.1 Use of while loop

Program : 01

table=8
while(table<=9):
print('Table of ', table)
mul=1
while(mul<=10):
print(table, ' * ', mul, ' = ', table*mul)
mul +=1
table+=1
print()

Output: -
Table of 8
8 * 1 = 8
8 * 2 = 16
8 * 3 = 24
8 * 4 = 32
8 * 5 = 40
8 * 6 = 48
8 * 7 = 56
8 * 8 = 64
8 * 9 = 72
8 * 10 = 80

Table of 9
9 * 1 = 9
9 * 2 = 18
9 * 3 = 27
9 * 4 = 36
9 * 5 = 45
9 * 6 = 54
9 * 7 = 63
9 * 8 = 72
9 * 9 = 81
9 * 10 = 90

14.2 Use of for loop

Program : 02

for row in range(0,11):


print(row,end=' ');print('Stud',row,'Hi')

Output: -
0 Stud 0 Hi
1 Stud 1 Hi
2 Stud 2 Hi
3 Stud 3 Hi
4 Stud 4 Hi
5 Stud 5 Hi

34 | P a g e
Monday, 18 November 2019

6 Stud 6 Hi
7 Stud 7 Hi
8 Stud 8 Hi
9 Stud 9 Hi
10 Stud 10 Hi

Program : 03

num=int(input("Enter the number of rows : "))


for row in range(1,num+1):
for col in range(1,row+1):
print("*", end="")
print()

Output: -

Enter the number of rows : 5


*
**
***
****
*****

Program : 04
string=input("Enter the string :")
length=len(string)
print(length)
for row in range(length):
for col in range(row+1):
print(string[col],end="")
print()

Output: -
Enter the string :IIITBhagalpur
13
I
II
III
IIIT
IIITB
IIITBh
IIITBha
IIITBhag
IIITBhaga
IIITBhagal
IIITBhagalp
IIITBhagalpu
IIITBhagalpur

35 | P a g e
Monday, 18 November 2019

Program : 05

for row in range(6):


for col in range(7):
if(row==0 and col % 3 !=0) or (row==1 and col % 3 == 0) or
(row - col ==2)or (row + col ==8):
print("*" , end=" ")
else:
print("" , end =" ")
print()

Output: -
* * * *
* * *
* *
* *
* *
*

36 | P a g e
Monday, 18 November 2019

15. Basics of Python (Pandas library)

15.1 Program: 01

# Import pandas library


import pandas as pd
# initialize list of lists
data = [['IIIT Bhagalpur', 10], ['BCE Bhagalpur', 15], ['Agri
Bhagalpur', 14]]
# Create the pandas DataFrame
df = pd.DataFrame(data, columns = ['Institute', 'Rank'])
# print dataframe.
df

Output: -
Institute Rank
0 IIIT Bhagalpur 10
1 BCE Bhagalpur 15
2 Agri Bhagalpur 14

15.2 Program: 02
# Import pandas library
import pandas as pd
# initialize list of lists
data = [['Aman', 10], ['Rashi', 15], ['Sadik', 14]]
# Create the pandas DataFrame
df = pd.DataFrame(data, columns = ['Name', 'Marks'])
# print dataframe.
df
Output: -
Name Marks
0 Aman 10
1 Rashi 15
2 Sadik 14

15.3 Program: 03
# Python code demonstrate creating DataFrame from dict narray / lists
By default addresses.
import pandas as pd
# intialise data of lists.
data = {'Name':['Neeraj', 'Anuranjan', 'Aakash', 'Gunjan'],
'Age':[20, 21, 19, 18]}
# Create DataFrame
df = pd.DataFrame(data)
# Print the output.
df
Output: -

Name Age
0 Neeraj 20
1 Anuranjan 21
2 Aakash 19
3 Gunjan 18

37 | P a g e
Monday, 18 November 2019

df.max()
Output: -
Name Neeraj
Age 21
dtype: object

df
Output: -
Name Age
0 Neeraj 20
1 Anuranjan 21
2 Aakash 19
3 Gunjan 18

df.head(2)
Output: -

Name Age
0 Neeraj 20
1 Anuranjan 21

df.tail(2)
Output: -

Name Age
2 Aakash 19
3 Gunjan 18

38 | P a g e
Monday, 18 November 2019

16. Basics of Python (Numpy library)


16.1 Program: 01
import numpy as np
import time
#import sys
SIZE=10000
L1=range(SIZE)
L2=range(SIZE)
A1=np.arange(SIZE)
A2=np.arange(SIZE)
start=time.time()
result=[(x+y) for x,y in zip(L1,L2)]
#print(result)
print((time.time()-start)*1000)
start=time.time()
result=A1+A2
#print(result)
print((time.time()-start)*1000)

Output: -

3.988027572631836
0.9958744049072266

16.2 Program: 02
def myFibonacci(n):
if n<0:
print("Incorrect input")
# First Fibonacci number is 0
elif n==1:
return 0
# Second Fibonacci number is 1
elif n==2:
return 1
else:
return myFibonacci(n-1)+myFibonacci(n-2)
print(myFibonacci(9))

Output: -
21

39 | P a g e
Monday, 18 November 2019

17. Basics of Python (Image Processing)


17.1 Program: 01
from matplotlib import pyplot as suji
suji.show()
suji.ylabel('y val')
suji.xlabel('x val')
suji.title('title of plot')

Output: -
Text(0.5, 1.0, 'title of plot')

x=[1,2,3]
y=[4,5,1]
suji.plot(x,y)

Output: -
[<matplotlib.lines.Line2D at 0x20f30664a20>]

17.2 Program: 02
import matplotlib
import numpy as np
import matplotlib.pyplot as plt
import cv2
img=cv2.imread('logo.png',0)
print(img)

Output: -
[[255 255 255 ... 255 255 255]
[255 255 255 ... 255 255 255]
[255 255 255 ... 255 255 255]
...
[255 255 255 ... 255 255 255]
[255 255 255 ... 255 255 255]
[255 255 255 ... 255 255 255]]

40 | P a g e
Monday, 18 November 2019

Img

Output: -
array([[255, 255, 255, ..., 255, 255, 255],
[255, 255, 255, ..., 255, 255, 255],
[255, 255, 255, ..., 255, 255, 255],
...,
[255, 255, 255, ..., 255, 255, 255],
[255, 255, 255, ..., 255, 255, 255],
[255, 255, 255, ..., 255, 255, 255]], dtype=uint8)

plt.imshow(img)

Output: -
<matplotlib.image.AxesImage at 0x203c705db48>

plt.imshow(img,'gray')

Output: -
<matplotlib.image.AxesImage at 0x203c721ecc8>

41 | P a g e
Monday, 18 November 2019

img.min()

Output: -
0

img.max()

Output: -
255

img.mean()

Output: -
235.23600380527603

15.3 Program: 03

import matplotlib
import numpy as np
import matplotlib.pyplot as plt
import cv2
img=cv2.imread('Suraj.jpg',0)
print(img)

Output: -
[[182 182 183 ... 181 182 182]
[182 183 184 ... 181 181 182]
[183 184 185 ... 181 181 182]
...
[ 60 61 53 ... 41 33 29]
[ 56 55 43 ... 29 26 27]
[ 72 57 48 ... 19 22 28]]

type(img)

Output: -
numpy.ndarray

img.shape

Output: -
(360, 279)

img.size

Output: -
100440

plt.imshow(img[50:300,30:200],'gray')

Output: -
<matplotlib.image.AxesImage at 0x1d8f1b95608>

42 | P a g e
Monday, 18 November 2019

plt.imshow(img,'gray')

Output: -
<matplotlib.image.AxesImage at 0x1d8f1bf45c8>

plt.imshow(img,cmap=plt.cm.gray, vmin=10, vmax=200)

Output: -
<matplotlib.image.AxesImage at 0x1d8f1cb1f88>

plt.contour(img,[50,100])

Output: -
<matplotlib.contour.QuadContourSet at 0x1d8f1d0d5c8>

43 | P a g e
Monday, 18 November 2019

ultaImg=np.flipud(img)
plt.imshow(ultaImg,'gray')

Output: -
<matplotlib.image.AxesImage at 0x1d8f1dd3f88>

noisySu = img + 0.4 * img.std() * np.random.random(img.shape)


plt.imshow(noisySu,'gray')

Output: -
<matplotlib.image.AxesImage at 0x1d8f1e95f88>

44 | P a g e

You might also like