COMPUTER
ORGANIZATION
AND
MICROPROCESSOR
INTERFACING
(IT254)
Practical Assignment Submission
Name: Jainam Bhavsar
ID : 19IT011
Lab Faculty : Rikin Nayak Sir
Charotar University of Science & Technology
Chandubhai S. Patel Institute of Technology
Department of Information Technology
Subject IT254 Computer Organization and Microprocessor Interfacing
Exp. No Name of Experiment Signature
1 A. Write a python program to sum of the first n positive integers. Note n will be defined by
user.
B. Write a Python program to find factorial of any number defined by user.
2 A. Write a Python program to calculate the hypotenuse of a right angled triangle.
B. Write a Python program to calculate the sum of the digits in an integer.
3 A. Write a Python program to find number of prime numbers between 1 to n where n is
defined by user.
B. Write a Python program to test whether a passed letter is a vowel or not using
vowel( character) function.
4 Getting Familiar with Raspberry Pi
4 LEDs are Interfaced with Raspberry Pi. Develop a python script for Raspberry Pi to
5
blink all LEDs with specific Time Interval.
6 Push Buttons (4 Nos.) and LEDs (4 Nos.) are Interfaced with Raspberry Pi.
Develop a python script for Raspberry Pi to turn on & off LED with respect to the switch
(Button pressed, LED ON & Button Released, LED OFF)
Single LED is interfaced with Raspberry Pi.
7
Develop a python script for Raspberry Pi to control the brightness of LED using PWM
A. Common Cathode Seven Segment (1 No.) is interfaced with Raspberry Pi Board.
8
Write a Python Script to generate counter of 0 to 9 on Seven segment display with
Specific Time Interval.
B. Common Cathode Seven Segment (2 No.) is interfaced with Raspberry Pi Board.
Write a Python Script to generate counter of 00 to 99 on Seven segment display with
Specific Time Interval.
ARM 7 TDMI
9 Introduction to ARM7 Based Microcontroller LPC2148 and IDE (MDK-ARM) software.
10 Write a program to interface LPC2148 with 8 LEDs, Turn on and turn off leds in even odd
pattern with specific interval.(10101010 01010101)
11 A. Write a program to interface LPC2148 with (1 Nos) 7 segment, generate counter of
0 to 9 on Seven segment display with Specific Time Interval.
B. Write a program to interface LPC2148 with (2 Nos) 7 segment, generate counter of
00 to 99 on Seven segment display with Specific Time Interval.
12 Write a program to glow leds with its priority with the help of switches (4 LED, 4 Switch)
using LPC2148.
13 Extra Practical’s on Raspberry Pi board
IT254 COMI 19IT011
1. A. Write a python program to sum of the first n positive integers.
Note n will be defined by user.
Code:
n = int (input("Enter any number : "))
sum=0
for i in range(n+1):
sum+=i
print(sum)
Output:
Conclusion: Here I have performed addition of n number of digits given by user.
B. Write a Python program to find factorial of any number defined
by user.
Code:
n = int (input("Enter number to calculate factorial : "))
fact = 1
for i in range(1,n+1):
fact *= i
print("Factorial of ",n," is ",fact)
Output:
Conclusion: Here I have calculated the factorial of n.
2. A. Write a Python program to calculate the hypotenuse of a right
angled triangle.
Code:
CSPIT-IT Page 2
IT254 COMI 19IT011
import math
side1 = int (input("Enter one side of triangle : "))
side2 = int (input("Enter another side of triangle : "))
c = math.sqrt(math.pow(side1,2)+math.pow(side2,2))
print("Hypotenuse of triangle is : ",c)
Output:
Conclusion: Here I have calculated the hypotenuse of a right angled triangle.
B. Write a Python program to calculate the sum of the digits in an integer.
Code:
num = input("Enter any number : ")
n = int(num)
sum = 0
for i in range(len(num)+1):
rem = n % 10
sum += rem
n = n // 10 # here // works as floor division which means it will not take floating values
print("Sum of digit is : ",int(sum))
Output:
Conclusion: Here I have performed the sum of the digits in an integer and given the ouput.
3. A. Write a Python program to find number of prime numbers
between 1 to n where n is defined by user.
Code:
n = int (input("Enter number to print prime number till number
: "))
for i in range(1,n+1):
if(i>1):
for j in range(2,i):
if(i%j==0):
break
else:
print(i)
CSPIT-IT Page 3
IT254 COMI 19IT011
Output:
Conclusion: Here I Have calculated all the prime numbers from 1 to n (n = 15 in this case).
B. Write a Python program to test whether a passed letter is a vowel
or not using vowel (character) function.
Code:
def vowel(char):
vowels = "aeiou"
return char in vowels
ch = input("Enter the character to be passed : ")
if(vowel(ch)):
print("Entered character is an vowel.")
else:
print("Entered character is not an vowel.")
Output:
Conlusion: Here I have passed a character and given the output if it is a vowel
or not.
4. Getting Familiar with Raspberry Pi.
Ans:
CSPIT-IT Page 4
IT254 COMI 19IT011
The Raspberry Pi has a number of ports which you will use to control the Raspberry Pi,
and it can use to control other devices. Your Raspberry Pi will have the following ports:
USB – USB ports are used to connect a wide variety of components, most commonly
a mouse and keyboard.
HDMI – The HDMI port outputs video and audio to your monitor.
Audio – The audio jack allows you to connect standard headphones and speakers.
Micro USB – The Micro USB port is only for power.
GPIO – The GPIO ports allow the Raspberry Pi to control and take input from any
electronic component.
SD card slot – The Raspberry Pi uses SD cards the same way a full-size computer
uses a hard drive. The SD card provides the Raspberry Pi with internal memory, and
stores the hard drive.
Introduction
The Raspberry Pi is a low cost, credit-card sized computer that plugs into a
computer monitor or TV, and uses a standard keyboard and mouse.
It is a capable little device that enables people of all ages to explore computing, and
to learn how to program in languages like Scratch and Python. It’s capable of
CSPIT-IT Page 5
IT254 COMI 19IT011
doing everything you’d expect a desktop computer to do like Browsing the
internet, playing high-definition video, Word-processing and much more.
Advantages of Raspberry Pie board are power Consumption is low, small size
factor, no noise while working, expansion capabilities using different add-ons, it is
affordable, huge community support, and many more.
Applications:
Personal Computer
Image Processing Tool
Build Internet-of –Things
Build Network Server
Develop Home Automation System
And much more.
Drawbacks:
ARM Architecture: While ARM is a highly efficient and low powered architecture,
it is not x86 and therefore any binaries that are compiled to run on x86 cannot run
on the Pi. The good news is that entire GNU/Linux distributions have been compiled
for the ARM architecture and new ones are appearing all of the time. There are very
few applications that absolutely need x86.
RAM not upgradable.
5. 4 LEDs are Interfaced with Raspberry Pi. Develop a python script
for Raspberry Pi to blink all LEDs with specific Time Interval.
Code:
import RPi.GPIO as IO
import time
delay = int (input("Enter delay in second : "))
pin = [22,24,26,28]
IO.setmode(IO.BOARD)
IO.setup(pin,IO.OUT)
while True:
for i in pin:
IO.output(i,IO.HIGH)
time.sleep(delay)
for i in pin:
IO.output(i,IO.LOW)
time.sleep(delay)
Output:
CSPIT-IT Page 6
IT254 COMI 19IT011
Conclusion: Here all the LEDs are blinking at rate that user defines in seconds.
6. Push Buttons (4 Nos.) and LEDs (4 Nos.) are Interfaced with
Raspberry Pi. Develop a python script for Raspberry Pi to turn on &
off LED with respect to the switch (Button pressed, LED ON & Button
Released, LED OFF).
Code:
# import modules
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)
button = [3,7,13,19]
Leds = [5,11,15,21]
for i in button:
GPIO.setup(i,GPIO.IN)
for i in Leds:
GPIO.setup(i,GPIO.OUT)
while True :
if(GPIO.input(3) == 1):
GPIO.output(5,1)
else:
GPIO.output(5,0)
if(GPIO.input(7) == 1):
GPIO.output(11,1)
else:
GPIO.output(11,0)
if(GPIO.input(13) == 1):
GPIO.output(15,1)
CSPIT-IT Page 7
IT254 COMI 19IT011
else:
GPIO.output(15,0)
if(GPIO.input(19) == 1):
GPIO.output(21,1)
else:
GPIO.output(21,0)
time.sleep(1)
Output:
Conclusion: Here I have performed the practical of 4 LEDs at 5,11,15,21 and 4 buttons at
3,7,13,19
7. Single LED is interfaced with Raspberry Pi.
Develop a python script for Raspberry Pi to control the brightness of
LED using PWM.
Code:
import RPi.GPIO as GPIO
from time import sleep
ledpin = 12
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(ledpin,GPIO.OUT)
pi_pwm = GPIO.PWM(ledpin,1000)
pi_pwm.start(0)
while True:
for duty in range(0,101,1):
pi_pwm.ChangeDutyCycle(duty)
sleep(0.01)
sleep(0.5)
for duty in range(100,-1,-1):
pi_pwm.ChangeDutyCycle(duty)
sleep(0.01)
CSPIT-IT Page 8
IT254 COMI 19IT011
sleep(0.5)
Output:
Conclusion: Here I have performed practical on the intensity changing of led bulb using
PWM duty cycle variation.
8. A. Common Cathode Seven Segment (1 No.) is interfaced with
Raspberry Pi Board. Write a Python Script to generate counter of 0 to
9 on Seven segment display with Specific Time Interval.
Code:
import RPi.GPIO as IO
import time
led = [22,24,26,28,31,29,27]
IO.setmode(IO.BOARD)
IO.setup(led,IO.OUT)
zero = "1111110"
one = "0110000"
two = "1101101"
three = "1111001"
four = "0110011"
five = "1011011"
six = "1011111"
seven = "1110000"
eight = "1111111"
nine = "1111011"
lst = [zero,one,two,three,four,five,six,seven,eight,nine]
while True:
for i in lst:
CSPIT-IT Page 9
IT254 COMI 19IT011
k=0
for j in i:
IO.output(led[k],int(j))
k += 1
time.sleep(1)
Output:
Conclusion: Here I have developed and done the seven segment programming in the
raspberry pie board.
B. Common Cathode Seven Segment (2 No.) is interfaced with
Raspberry Pi Board. Write a Python Script to generate counter of 00
to 99 on Seven segment display with specific Time Interval.
Code:
import RPi.GPIO as IO
import time
IO.setmode(IO.BOARD)
zero = "1111110"
one = "0110000"
two = "1101101"
three = "1111001"
four = "0110011"
five = "1011011"
six = "1011111"
seven = "1110000"
eight = "1111111"
nine = "1110011"
digit1_enable = 35
CSPIT-IT Page 10
IT254 COMI 19IT011
digit2_enable = 37
IO.setup(digit1_enable,IO.OUT)
IO.setup(digit2_enable,IO.OUT)
list_1 = [zero,one,two, three, four, five, six, seven, eight, nine]
pin_list=[3,5,38,11,13,15,40]
IO.setup (pin_list, IO.OUT)
while True:
for l in list_1:
for i in list_1:
for x in range (0,50):
IO.output (digit2_enable, 1)
IO.output (digit1_enable, 0)
m=0
for n in l:
IO.output(pin_list[m],int(n))
m+=1
time.sleep(0.01)
IO.output (digit2_enable, 0)
IO.output (digit1_enable, 1)
k=0
for j in i:
IO.output(pin_list[k],int(j))
k+=1
time.sleep(0.01)
Output:
CSPIT-IT Page 11
IT254 COMI 19IT011
Conclusion: Here I have developed two seven segment such that it will print from 0 to 99
in the raspberry pie board.
9. Introduction to ARM7 Based Microcontroller LPC2148 and IDE
(MDK-ARM) software.
Ans:
LPC2148 is the widely used IC from ARM-7 family. It is manufactured by Philips and
it is pre-loaded with many inbuilt peripherals making it more efficient and a reliable
option for the beginners as well as high end application developer.
8 to 40 kB of on-chip static RAM and 32 to 512 kB of on-chip flash program
memory.128 bit wide interface/accelerator enables high speed 60 MHz
operation. USB 2.0 Full Speed compliant Device Controller with 2 be of
endpoint RAM. In addition, the LPC2146/8 provides 8 kB of on-chip RAM
accessible to USB by DMA. One or two (LPC2141/2 vs. LPC2144/6/8) 10- bit
A/D converters provide a total of 6/14analog inputs, with conversion times as
low as 2.44 us per channel.
Single 10-bit D/A converter provide 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 with
independent power and dedicated 32 kHz clock input. Multiple serial interfaces
including two UARTs (16C550), two Fast I2C-bus (400 kbit/s), SPI and SSP
with buffering and variable data length capabilities. Up to 45 of 5 V tolerant
fast general purpose I/O pins in a tiny LQFP64 package. Power saving modes
include idle and Power-down.
Pin configuration in LPC 2148:
CSPIT-IT Page 12
IT254 COMI 19IT011
Steps for Generating HEX file using Keil
1. Run the KEIL IDE and go the
2. Select the CPU target as LPC2148 under the option of NXP in the list
CSPIT-IT Page 13
IT254 COMI 19IT011
3. Next click YES option in the pop-up window to copy startup code to project folder
4. Copy the main.c file under the folder where we have the project file saved.
5. Include the main.c file into the project as shown below.
CSPIT-IT Page 14
IT254 COMI 19IT011
CSPIT-IT Page 15
IT254 COMI 19IT011
6. Perform the following settings: Click on Target option icon as shown below and in the
next pop-up window select the Output tab and check the option Create HEX file as
shown below. Next go to Linker tab and check the Use Memory Layout from Target
Diaglog and click OK.
CSPIT-IT Page 16
IT254 COMI 19IT011
7. To compile the project click on the icon on top of the left pane, as highlighted below.
If the project is successfully compiled a HEX file would be generated in the same path
where we have stored the project file. Flash the HEX file using Flash Magic tool.
CSPIT-IT Page 17
IT254 COMI 19IT011
10. Write a program to interface LPC2148 with 8 LEDs, Turn on and
turn off leds in even odd pattern with specific interval.(10101010
01010101).
Code:
#include <LPC214X.H>
void delay()
{
int i,j;
for(i=0;i<500;i++)
for(j=0;j<5000;j++);
}
int main()
{
PINSEL1 = 0X000000;
IODIR0 = 0XFF;
while (1)
{
IOSET0 = 0x55;
delay();
IOCLR0 = 0x55;
IOSET0 = 0xaa;
delay();
IOCLR0 = 0xaa;
delay();
}
}
Output:
CSPIT-IT Page 18
IT254 COMI 19IT011
Conclusion: Here I have interfaced 8 leds to LPC2138 microcontroller that blink with
certain delay provided in odd & even pattern and program for same is written in c programming
language which I converted into hex file using keil software and dump into microcontroller.
11.A Write a program to interface LPC2148 with (1 Nos) 7 segment, generate
counter of 0 to 9 on Seven segment display with Specific Time Interval.
Code:
#include <LPC214X.H>
void delay()
{ int i,j;
for(i=0;i<500;i++)
for(j=0;j<5000;j++);
}
main()
{
int k=0;
unsigned char num[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
IODIR0 = 0x0000007f;
while(1)
CSPIT-IT Page 19
IT254 COMI 19IT011
{
for(k=0;k<=9;k++)
{
IOSET0 = num[k];
delay();
IOCLR0 = 0X07F;
}
}
}
Output:
Conclusion: Here I have interfaced a 7 segment to LPC2138 microcontroller that display
from 0 to 9 with certain delay provided and program for same is written in c programming
language which I converted into hex file using keil software and dump into microcontroller.
CSPIT-IT Page 20
IT254 COMI 19IT011
B. Write a program to interface LPC2148 with (2 Nos) 7 segment, generate
counter of 00 to 99 on Seven segment display with Specific Time Interval.
Code:
#include <LPC214X.H>
void delay(){
unsigned int k=0, l=0;
for (k=0; k < 200; k++){
for (l =0; l<3000; l++);
}
}
int main(){
int i,j;
int num[] = {0x3F,0x06,0x5B,0x4F,0x66,0x6D,0X7D,0X07,0x7F,0x6F};
IODIR0 = 0x0000FFFF;
PINSEL0 = 0X00000000;
while(1){
for(i=0;i<10;i++){
IOSET0 = num[i];
for (j=0;j<10;j++){
IOSET0 =num[j]<<7;
delay();
IOCLR0 =0x03F80;
}
IOCLR0 =0x07f;
}
}
}
Output:
CSPIT-IT Page 21
IT254 COMI 19IT011
Conclusion: Here I have interfaced a 2 seven segment to LPC2138 microcontroller such that
it count from 00 to 99 and program for same is written in c programming language which I
converted into hex file using keil software and dump into microcontroller.
12. Write a program to glow leds with its priority with the help of
switches (4 LED, 4 Switch) using LPC2148.
Code:
#include <LPC214X.H>
// four switches on P0.8 , P0.9, P0.10, P0.11
#define dip4 (1<<11)
#define dip3 (1<<10)
#define dip2 (1<<9)
#define dip1 (1<<8)
// four leds on P0.0 , P0.1, P0.2, P0.3
#define led4 (1<<3)
#define led3 (1<<2)
#define led2 (1<<1)
#define led1 (1<<0)
void delay()
CSPIT-IT Page 22
IT254 COMI 19IT011
{
int i;
for(i=0;i<300000;i++);
}
int main()
{
PINSEL0=0;
PINSEL1=0;
PINSEL2=0;
IO0DIR = 0X0f;
while(1)
{
if((IO0PIN & dip1))
{
IOSET0=led1;
delay();
IOCLR0=led1;
delay();
}
else if((IO0PIN & dip2)){
IOSET0=led2;
delay();
IOCLR0=led2;
delay();
}
else if((IO0PIN & dip3))
{
IOSET0=led3;
delay();
IOCLR0=led3;
delay();
}
else if((IO0PIN & dip4))
{
IOSET0=led4;
delay();
IOCLR0=led4;
delay();
}
}
}
CSPIT-IT Page 23
IT254 COMI 19IT011
Output:
Conclusion: Here I have interfaced a 4 leds and 4 switches to LPC2138 microcontroller such
that priority is given to switch1 followed by 2,3 and 4 with certain delay ( ex: so if switch 1 is
on and any other switch is on priority given to 1 and only led 1 will glow ) and program for
same is written in c programming language which I converted into hex file using keil software
and dump into microcontroller.
CSPIT-IT Page 24
IT254 COMI 19IT011
Extra practical’s on Raspberry Pi board
1. Pattern led with user input pin and delay
Code:
import RPi.GPIO as GPIO
import time
led11 = 11
led13 = 13
led15 = 15
led = [11,13,15]
pin1 = (int)(input("Enter pin1 : "))
pin2 = (int)(input("Enter pin2 : "))
pin3 = (int)(input("Enter pin3 : "))
delay = int (input("Enter delay in second : "))
pin = [pin1,pin2,pin3]
# setup pins
GPIO.setmode(GPIO.BOARD)
GPIO.setup(pin, GPIO.OUT)
while True:
for i in pin:
GPIO.output(i,1)
time.sleep(delay)
GPIO.output(i,0)
Output:
CSPIT-IT Page 25
IT254 COMI 19IT011
Conclusion: Here I have create code in such a way that user can enter pin and delay of
his/her choice and then led will blink in a pattern.
2. Password Authentication
Code:
# import modules
import RPi.GPIO as GPIO
import time
password = "User@123"
# setup pins
GPIO.setmode(GPIO.BOARD)
GPIO.setup(13, GPIO.OUT)
while True:
enteredPassword = input("Enter password : ")
if enteredPassword == password:
GPIO.output(13,1)
print("Authenticate Successfully")
break
else:
GPIO.output(13,0)
print("Access denied... Please try again : \n")
CSPIT-IT Page 26
IT254 COMI 19IT011
Output:
Conclusion: Here I have create this code in such a way that I have set password
“User@123” in the program so if the password matches with this when user
enter into raspberry pi board than it will glow led 13 and show Authenticate
Successfully message but if password does not matches than it will show
Access denied… and again give chance to enter password.
3. User Password Authentication
Code:
# import modules
import RPi.GPIO as GPIO
import time
password = input("Set password : ")
# setup pins
GPIO.setmode(GPIO.BOARD)
GPIO.setup(13, GPIO.OUT)
CSPIT-IT Page 27
IT254 COMI 19IT011
while True:
enteredPassword = input("Enter password : ")
if enteredPassword == password:
GPIO.output(13,1)
print("Authenticate Successfully")
break
else:
GPIO.output(13,0)
print("Access denied... Please try again : \n")
Output:
Conclusion: Here I have create this code in such a way that it will first ask user
to set password once and then ask user to enter the password for authentication
if the password matches with the password which was set earlier by the user than
it will glow led 13 and show Authenticate Successfully message but if password
does not matches than it will show Access denied… and again give chance to
enter password.
CSPIT-IT Page 28