0% found this document useful (0 votes)
30 views44 pages

Processor Architecture Lab Manual Updated24-25

The document is a laboratory manual for the Programming Skill Development Lab at SKN College of Engineering, focusing on Embedded C programming for second-year Information Technology students. It outlines various experiments, objectives, and outcomes related to embedded systems, including programming microcontrollers and interfacing with hardware. The manual also includes a certification section and details on using MPLAB for programming tasks.

Uploaded by

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

Processor Architecture Lab Manual Updated24-25

The document is a laboratory manual for the Programming Skill Development Lab at SKN College of Engineering, focusing on Embedded C programming for second-year Information Technology students. It outlines various experiments, objectives, and outcomes related to embedded systems, including programming microcontrollers and interfacing with hardware. The manual also includes a certification section and details on using MPLAB for programming tasks.

Uploaded by

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

Programming Skill Development Lab

SE-IT

Sinhgad Technical Education Society’s

SKN COLLEGE OF ENGINEERING,


VADGOAN, PUNE 411041

NAAC Accredited with ‘A’ Grade

LABORATORY MANUAL

Programming Skill Development Lab [214455]

Second Year of Information Technology


(2019 Course)

A.Y. 2024- 2025

Semester – II

Prepared by- Prof. A. S. Narote


Prof. M. P. Desai
Prof. P. S. Pise

DEPARTMENT OF INFORMATION TECHNOLOGY


Programming Skill Development Lab SE-IT

Sinhgad Technical Education Society’s

SKN COLLEGE OF ENGINEERING,


VADGOAN, PUNE 411041
NAAC Accredited with “A” Grade

CERTIFICATE

This is to certify that Mr. /Ms. __________________________of class SE IT Div __ Roll


No. ______Examination Seat No./PRN No.______________________________ has
completed all the practical work in the Programming Skill Development Lab [214455]
satisfactorily, as prescribed by Savitribai Phule Pune University, Pune in the academic year
2024-25 (Sem II)

Place:

Date:
INDEX

Marks
Sr. Date of Date of Signature
Title of Experiment Obtaine
No perfor Submis of Faculty
d (10)
mance sion
Study of Embedded C programming language
1
(Overview, syntax, One simple program like
addition of two numbers).
Write an Embedded C program to add array of n
2 numbers
Group
3 A Write an Embedded C program to transfer
elements from one location to another
4 Write an Embedded C program for sorting the
numbers in ascending and descending order.
5 Write an Embedded C program to interface
PIC 18FXXX with LED & blinking it using
specified delay
Group
6 Write an Embedded C program for Timer
B
programming ISR based buzzer on/off.
7 Write an Embedded C program for External
interrupt input switch press,output at relay
Write an Embedded C program for LCD
8
interfacing with PIC 18FXXX

9 Group Write an Embedded C program for Generating


C PWM signal for servo motor/DC motor
Study of Arduino board and understand the
10 OS installation process on Raspberry-pi.
Group
11 D Write simple program using Opensource
prototype platform like Raspberry-Pi/Beagle
board/Arduino for digital read/write using
LED and switch Analog read/write using
sensor and actuators.

Name & Signature of Course In-charge


Name of the Student: Roll no:

CLASS: - S.E [IT] Division: Course: - PSDL


Experiment No. 1
** Study of Embedded C programming language **

Marks: / 10

Date of Performance: / /25 Sign with Date

Title: Study of Embedded C programming language (Overview, syntax, One simple program like
addition of two numbers).

Objective: To learn embedded C programming and PIC18FXXXmicrocontrollers.

Outcome: On completion of this Assignment student will be able to

Understand the concepts of embedded C programming

Develop and Execute embedded C program to perform array addition

Pre-requisites: Computer Organization and Architecture

Lab facility: MPLAB X IDE simulator, XC8 Compiler, PIC18FXXXmicrocontroller kit

Theory:
Embedded C programming plays a key role to make the microcontroller run & perform the preferred
actions. At present, we normally utilize several electronic devices like mobile phones, washing machines,
security systems, refrigerators, digital cameras, etc. The controlling of these embedded devices can be done
with the help of an embedded C program. For example, in a digital camera, if we press a camera button to
capture a photo then the microcontroller will execute the required function to click the image as well as to
store it.

Embedded C programming builds with a set of functions where every function is a set of statements that
are utilized to execute some particular tasks. Both the embedded C and C languages are the same and
implemented through some fundamental elements like a variable, character set, keywords, data types,
declaration of variables, expressions, statements. All these elements play a key role while writing an
embedded C program.
In embedded system programming C code is preferred over other language. Due to the following reasons:

• Easy to understand

• High Reliability

• Portability

• Scalability

Embedded Systems consists of both Hardware and Software. If we consider a simple Embedded System,
the main Hardware Module is the Processor. The Processor is the heart of the Embedded System and it can
be anything like a Microprocessor, Microcontroller, DSP, CPLD (Complex Programmable Logic Device)
or an FPGA (Field Programmable Gated Array).

All these devices have one thing in common: they are programmable i.e. we can write a program (which
is the software part of the Embedded System) to define how the device actually works.

Embedded Software or Program allow Hardware to monitor external events (Inputs / Sensors) and control
external devices (Outputs) accordingly. During this process, the program for an Embedded System may
have to directly manipulate the internal architecture of the Embedded Hardware (usually the processor)
such as Timers, Serial Communications Interface, Interrupt, Handling and I/O Ports etc.

From the above statement, it is clear that the Software part of an Embedded System is equally important
as the Hardware part. There is no point in having advanced Hardware Components with poorly written
programs (Software).

There are many programming languages that are used for Embedded Systems like Assembly (low-level
Programming Language), C, C++, JAVA (high-level programming languages), Visual Basic, JAVA Script
(Application level Programming Languages), etc.

In the process of making a better embedded system, the programming of the system plays a vital role and
hence, the selection of the Programming Language is very important.

Some of the benefits of using Embedded C as the main Programming Language:

• Significantly easy to write code in C

• Consumes less time when compared to Assembly

• Maintenance of code (modifications and updates) is very simple

• Make use of library functions to reduce the complexity of the main code

• You can easily port the code to other architecture with very little modifications
Introduction to Embedded C Programming Language

Before going in to the details of Embedded C Programming Language and basics of Embedded C Program,
we will first talk about the C Programming Language.

The C Programming Language, developed by Dennis Ritchie in the late 60’s and early 70’s, is the most
popular and widely used programming language. The C Programming Language provided low level
memory access using an uncomplicated compiler (a software that converts programs to machine code) and
achieved efficient mapping to machine instructions.

The C Programming Language became so popular that it is used in a wide range of applications ranging
from Embedded Systems to Super Computers.

Embedded C Programming Language, which is widely used in the development of Embedded Systems, is
an extension of C Program Language. The Embedded C Programming Language uses the same syntax and
semantics of the C Programming Language like main function, declaration of datatypes, defining variables,
loops, functions, statements, etc.

The extension in Embedded C from standard C Programming Language include I/O Hardware Addressing,
fixed point arithmetic operations, accessing address spaces, etc.

Difference between C and Embedded C

There is actually not much difference between C and Embedded C apart from few extensions and the
operating environment. Both C and Embedded C are ISO Standards that have almost same syntax,
datatypes, functions, etc.

Embedded C is basically an extension to the Standard C Programming Language with additional features
like Addressing I/O, multiple memory addressing and fixed-point arithmetic, etc.

C Programming Language is generally used for developing desktop applications, whereas Embedded C is
used in the development of Microcontroller based applications.

Basics of Embedded C Program

Now that we have seen a little bit about Embedded Systems and Programming Languages, we will dive in
to the basics of Embedded C Program. We will start with two of the basic features of the Embedded C
Program: Keywords and Data types.

Keywords in Embedded C

A Keyword is a special word with a special meaning to the compiler (a C Compiler for example, is a
software that is used to convert program written in C to Machine Code).
following are some of the keywords:

bit

sbit

sfr

small

large

Data Types in Embedded C

Data Types in C Programming Language (or any programming language for that matter) help us declaring
variables in the program. There are many data types in C Programming Language like signed int, unsigned
int, signed char, unsigned char, float, double, etc.

Basic Structure of an Embedded C Program (Template for Embedded C Program)

The next thing to understand in the Basics of Embedded C Program is the basic structure or Template of
Embedded C Program. This will help us in understanding how an Embedded C Program is written. The
following part shows the basic structure of an Embedded C Program.

Multiline Comments . . . . . Denoted using /*……*/

Single Line Comments . . . . . Denoted using //

Preprocessor Directives . . . . . #include<…> or #define

Global Variables . . . . . Accessible anywhere in the program

Function Declarations . . . . . Declaring Function

Main Function Main Function, execution begins here


{
Local Variables . . . . . Variables confined to main function
Function Calls . . . . . Calling other Functions
Infinite Loop . . . . . Like while(1) or for(;;)
Statements . . . . .

}

Function Definitions . . . . . Defining the Functions


{
Local Variables . . . . . Local Variables confined to this Function
Statements . . . . .
….
….
}

Different Components of an Embedded C Program

Comments:

Comments are readable text that are written to help us (the reader) understand the code easily. They are
ignored by the compiler and do not take up any memory in the final code (after compilation).There are two
ways you can write comments: one is the single line comments denoted by // and the other is multiline
comments denoted by /*….*/.

Preprocessor Directive:

A Preprocessor Directive in Embedded C is an indication to the compiler that it must look in to this file
for symbols that are not defined in the program.

In C Programming Language (also in Embedded C), Preprocessor Directives are usually represented using
# symbol like #include… or #define….

In Embedded C Programming, we usually use the preprocessor directive to indicate a header file specific
to the microcontroller, which contains all the SFRs and the bits in those SFRs.

In case of 8051, Keil Compiler has the file “reg51.h”, which must be written at the beginning of every
Embedded C Program.

Global Variables:

Global Variables, as the name suggests, are Global to the program i.e., they can be accessed anywhere in
the program.

Local Variables:

Local Variables, in contrast to Global Variables, are confined to their respective function.

Main Function:

Every C or Embedded C Program has one main function, from where the execution of the program begins.

Basic Embedded C Program

Till now, we have seen a few Basics of Embedded C Program like difference between C and Embedded
C, basic structure or template of an Embedded C Program and different components of the Embedded C
Program.

Main Factors of Embedded C Program


The main factors to be considered while choosing the programming language for developing an embedded
system include the following.

Program Size

Every programming language occupies some memory where embedded processor like microcontroller
includes an extremely less amount of random-access memory.

Speed of the Program

The programming language should be very fast, so should run as quickly as possible. The speed of
embedded hardware should not be reduced because of the slow-running software.

Portability

For the different embedded processors, the compilation of similar programs can be done.

• Simple Implementation

• Simple Maintenance

• Readability

Advantages

• It is very simple to understand.

• It executes simply a single task at once

• The cost of the hardware used in the embedded c is typically so much low.

• The applications of embedded are extremely appropriate in industries.

• It takes less time to develop an application program.

• It reduces the complexity of the program.

• It is easy to verify and understand.

• It is portable from one controller to another.

Disadvantages

• At a time, it executes only one task but can’t execute the multi-tasks

• If we change the program then need to change the hardware as well

• It supports only the hardware system.

• It has a scalability issue

• It has a restriction like limited memory otherwise compatibility of the computer.


Applications of Embedded C Program

Embedded C programming is used in industries for different purposes

The programming language used in the applications is speed checker on the highway, controlling of traffic
lights, controlling of street lights, tracking the vehicle, artificial intelligence, home automation, and auto
intensity control.

What is MPLAB?

MPLAB is a software program that runs on your PC to provide a development environment for your
embedded system design. In other words, it is a program package that makes writing and developing a
program easier. It could best be described as developing environment for a standard program language that
is intended for programming microcontrollers.

Get started to MPLAB X Programming IDE.


Step1: Creating a new project
➢ Go to the File Tab.
➢ Click on New Project.
➢ Step1: Choose Project:

➢ Select: Microchip Embedded -> Standalone Project. Click


Next.
Step2: Select Device:
➢ Select: Family -> Advanced 8 Bit MCU (PIC18).
➢ Select: Device: PIC18F4550. Click Next.
Step3: Select Tool: Simulator. Click
Next.Step4: Select Compiler ->XC8.
Click Next.Step5: Select Project Name
and Folder.
➢ Give Project Name.
➢ Select project Location using Browse Button.
➢ Uncheck Set as main project option.
➢ Click Finish.
Step6: Creating a new Source file and Header File.
➢ Go to the Project location in the Project window.
➢ Click the + sign to open the project space.

➢ Right Click on the Source Files folder (for a C file) and Header
files (for a .h file).
➢ New - > C Source file / or C Header File.
Step7: Opening an existing project.
➢ Go to the File Tab.
➢ Select Open Project.
Browse to the location and select the project name.Xfile
(project file). Click on Open Project

The LED Flash Program:

#include<pic18f4550.h>
void delay(int);
sbit a=PB^2;
sbit b=PB^3;
sbit c=PB^4;
sbit d=PB^5;
void main()
{

TRISB=0x00;
a=b=c=d=0x00;
delay(10);
a=b=c=d=0xFF;}

void delay(int a)
{
unsigned char c;
for(c=0;c<a;c++)
for(c=0;c<250;c++);
}

Conclusion: Based on the understanding of the basic concepts of Embedded C Programming along
with its environment setup used in a simple program for addition of two numbers.
Name of the Student: Roll no:

CLASS: - S.E [IT] Division: Course: - PSDL


Experiment No. 2
** Addition of array of n numbers **

Marks: / 10

Date of Performance: / /25 Sign with Date

Aim: Write an Embedded C program to add array of n numbers.

Objective: To calculate the sum of the array elements.

Outcome: On completion of this Assignment student will be able to


• Understand the concepts of embedded C programming
• Develop and Execute embedded C program to perform array addition

Pre-requisites: Fundamental knowledge of Microcontroller, Logic and knowing the


basics of C language
Lab facility: MPLAB X IDE simulator, XC8 Compiler, PIC18FXXXmicrocontroller
kit

Theory:

There is actually not much difference between C and Embedded C apart from fewextensions and the
operating environment. Both C and Embedded C are ISO Standards that have almost same syntax,
datatypes, functions, etc.
Embedded C is basically an extension to the Standard C Programming Language with additional
features like Addressing I/O, multiple memory addressing and fixed- point arithmetic, etc. C
Programming Language is generally used for developing desktop applications, whereas Embedded C
is used in the development of Microcontroller based applications.
C arrays are declared in the following form type name [number of elements];
For example, if we want an array of five integers , we write in C:
int numbers[5];
For a five character array char letters[5];
type name [number of elements] = {comma-separated values}
For example, if we want to initialize an array with five integers, with 1, 3, 5, 0, 9, as the initial values:
int number[5]={1,3,5,0,9};

Let’s see the logic to calculate the sum of the array elements. Suppose arr is an integer array of size
N (arr[N] ), the task is to write the C Program to sum the elements of an array.

Logic to calculate the sum of the array elements:


• Create an intermediate variable ‘sum’.
• Initialize the variable ‘sum’ with 0.
• To find the sum of all elements, iterate through each element, and add the currentelement to the sum.

//Logic within the loopsum =


sum + arr[i];

Input: Array of size N (arr[N] )


Output: Sum of the elements of an array

Algorithm :

1. Initialize counter to number of array elements.

2. Initialize pointer to the first element of an array.

3. Initialization of accumulated sum.

4. Addition of an elements to accumulated sum.

5. After each addition check whether carry is generated, if yes then increment carry counter by one.

6. Increment memory pointer by one to point to the next element.

7. Decrement count with each addition.

8. Go on adding numbers in internal memory till counter becomes zero. If not then go to step 4.
Flowchart :

Conclusion: Addition of n number of elements is done in embedded C code using MP lab


software.
Name of the Student: Roll no:

CLASS: - S.E [IT] Division: Course: - PSDL


Experiment No. 3
** Transfer elements from one location to another **

Marks: / 10

Date of Performance: / /25 Sign with Date

Aim: Write an Embedded C program to transfer elements from one location toanother for
following:
• Internal to internal memory transfer
• Internal to external memory transfer

Objective: To learn memory transfer using embedded C programming and


PIC18FXXXmicrocontrollers.
Outcome: On completion of this Assignment student will be able to
– Understand the concepts of memory transfer using embedded C
programming
Pre-requisites: Computer Organization and Architecture

Lab facility: MPLAB X IDE simulator, XC8 Compiler, PIC18FXXXmicrocontrollerkit

Theory:

Memory types in microcontrollers Architecture


The microcontrollers units (MCUs) consist of three types of memory.

1. Program Memory

2. Data Memory

3. Data EEPOM
Program Memory type
This is common which have all the microcontroller and its purposes is to store the
instructions.it consist of further four different types of memory.

0
1. ROM (Read only memory)
2. EPROM (Erasable programmable read only memory)
3. OTP (On time programmable)
4. FLASH EEPROM (Electrical erasable programmable read only
memory)

ROM

In microcontrollers first type memory is ROM and during the manufacturing process once the
program codes are set in ROM that can’t be changed after the manufacturing process, therefore
it is called read only memory mean just read thecode but can’t be changed. Due to this reason
the microcontrollers which have theROM memory are considers best for that applications where
there is no need of program change only need of program read. These microcontrollers are less
expensive as compared to the microcontrollers which have the OTP or FLAS programmable
memory and these are ordered in large quantities.

EPROM

The second type is erasable programmable read only and this is used in two different type of
packages. When EPROM is used in ceramic package with quartzwindow then microcontroller
can be erased the program many times by using ultraviolet eraser and erase time depends upon
the intensity of light. Normally theerase time is in between 5 and 30 minutes. In this the
microcontroller can also reprogrammed the program. It is very expensive due to the high cost
of the windowed ceramic package.

OTP
The one-time programmable memory used the same type of die as the EPROP windowed
packaged devices. Its packaging makes it unique. These microcontrollers are in an opaque
plastic packing and its program can’t be erase through ultraviolet light. The OTP devices are
first transfer to customer side then these are programmed therefore these devices are called one
time programmable.

Flash EPROM
This the type which provides the alternate flexibility because its program can be erased
electrically and also reprogram in few seconds. It’s no need of any ultraviolet light to erase the
program. Once the program is erased the program can reprogramwith new code. The devices
0
which have the flash memory can also be self-program by using some special sequence of
instructions. These devices also contain a small amount non-volatile data EPROM and that can
be written thousands of time. In these devices “F” is denoted by part number

Input: Elements in a array.

Output: Array after transferred elements

Algorithm :

1. Initialize memory pointer to first element of destination memory block.


2. Initialize memory pointer to first element of source memory block.
3. Initialize counter to no. of elements in source memory block.
4. Go on transferring the element from source memory block to destination memory block.
5. Increment source memory pointer by 1. Increment destination memory pointer by 1 and decrement counter
by 1.
6. If (counter 0), go to step 4.
Flowchart:

Code:

Conclusion: Transfer and exchange of elements from one memory location to other is checked.

0
Name of the Student: Roll no:

CLASS: - S.E [IT] Division: Course: - PSDL


Experiment No. 4
** Sorting the numbers in ascending and descending order. **

Marks: / 10

Date of Performance: / /25 Sign with Date

Aim: Write an Embedded C program for sorting the numbers in ascending anddescending order.

Objective: To learn sorting using embedded C programming and PIC18FXXXmicrocontrollers.

Outcome: On completion of this Assignment student will be able to


– Understand the concepts of sorting using embedded C programming

Pre-requisites: Computer Organization and Architecture

Lab facility: MPLAB X IDE simulator, XC8 Compiler, PIC18FXXX microcontrollerkit

Algorithm :

1. Initialized ptr to point to first element in array stored in internal program memory.
2. Initialize R0 to point to location 30h in internal data memory
3. Initialize count to number of elements to transfer from internal program memory to internal data memory.
4. Transfer data byte by byte from internal program memory to internal data memory for count number of
times
5. Initialize outer loop counter for sorting (bubble sort)
6. Initialize internal loop counter for number of comparisons
7. Initialize R0 to point to 30H internal data memory location
8. Transfer first number in b and second number in a register
9. Compare a and b register contents, if equal goto step 12
10. If a register contents are greater then goto step 12
11. If b register contents are greater then swap contents of a and b register
0
12. Decrement internal comparison count. If not zero goto step 8
13. Decrement outer loop counter. If it is not zero goto step 6
Flowchart:

Conclusion: Sorting in both ascending and descending is checked in memory locations.

0
Name of the Student: Roll no:

CLASS: - S.E [IT] Division: Course: - PSDL


Experiment No. 5
** Embedded C program to interface PIC 18FXXX with LED & blinking it
using specified delay ** Marks: / 10
Date of Performance: / /25 Sign with Date

Title: Write an Embedded C program to interface PIC 18FXXX with LED & blinking it using
specified delay.
Objective: Connect LED to the microcontroller and it should start blinking.

Outcome: On completion of this Assignment student will be able to


– Understand the concepts writing PIC program using embedded C programming and
write the .exe code on the kit or use the Proteus softwareto blink the LED (real or
virtual)
Pre-requisites: Embedded C programming, MPLAB, Proteus

Lab facility: MPLAB X IDE simulator, XC8 Compiler, PIC18FXXX microcontrollerkit, Proteus,
LED
Theory: Timer is used to control the output on a bit of a port.
There are 4 timers in PIC18. Each have corresponding timer registers TMRxH and
TMRxL where x is the timer number that ranges from 0 to 3
There is a control register correspond to every timer TxCON. In this program Timer0 is
used. Following is the T0CON register.

0
The program is written such that a LED is connected to a pin of any port (PORTB) and that pin is toggled after
a cycle controlled by the timer. This is repeated for infinite time. Thus when the port bit is 0, LED is off and
when it is 1, LED is on. Sothe LED blinks after every cycle.

ALGORITHM

1. As LED connected to PORTD configure these pins as output by writing 0x00 to the
appropriated TRES register.
2. As buttons are connected to RB.4 and RB.5 so configure these pins as input by
writing 1 to the appropriated bits of TRES register.
3. Check the status of RB.4, if it is zero then move 1bit of PORTB from right to left.
4. Else check the status of RB.5, if it is zero then move 1 bit of PORTB from left to
right.
5. Repeat step 3 to 4

Input: make all the required connections

Output: LED starts blinking with an equal interval

Interfacing Diagram:

Conclusion: Seen LED blinking with specified delay.

0
Name of the Student: Roll no:

CLASS: - S.E [IT] Division: Course: - PSDL


Experiment No. 6
** Embedded C program for Timer programming ISR based buzzer on/off.**

Marks: / 10

Date of Performance: / /25 Sign with Date

Aim: Write an Embedded C program for Timer programming ISR based buzzer on/off.

Objective: Connect buzzer to the microcontroller and it should be on or off as perthe


user input.
Outcome: On completion of this Assignment student will be able to
– Understand the concepts writing PIC program using embedded C programming
and write the .exe code on the kit or use the Proteus softwareto switch the buzzer
on or off (real or virtual)

Pre-requisites: Embedded C programming, MPLAB, Proteus

Lab facility: MPLAB X IDE simulator, XC8 Compiler, PIC18FXXX microcontroller


kit, Proteus, Buzzer
Theory: Timer is used to generate the delay for which the buzzer should be on.
In this program Timer 1 is used. Following is the T1CON register.

D7 D6 D5 D4 D3 D2 D1 D0
not used

0
1 = Do not synchronize external clock input
0 = Synchronize external clock input
1 = Do not synchronize external clock input
0 = Synchronize external clock input

The program is written such that a buzzer is connected to a pin of any port (PORTB) and that pin is
set when the switch is pressed. The duration for which thepin should be set is controlled by the timer.
This is repeated for infinite time.

ALGORITHM:
Step1: Reset TRISB so that PORTB is in output mode
Step2: Reset PORTB
Step3: check the switch is pressed
Step4: if yes then
Step5: Set
PORTB Step6:
Call the delay
Step7:end if
Step8: goto
Step3

Input: make all the required connections


Output: The buzzer is on when the switch is pressed.

Conclusion: The student is able to understand the working of the timer, buzzer,and
ports.

0
Name of the Student: Roll no:

CLASS: - S.E [IT] Division: Course: - PSDL


Experiment No. 7
** Write an Embedded C program for External interrupt input
switch press,output at relay.
**
Marks: / 10

Date of Performance: / /25 Sign with Date

Aim: Write an Embedded C program for External interrupt input switch press,output at relay.

Objective: To understand the working of interrupts in PIC18

Outcome: When PIC18 is interrupted, using RB0, the data at PORTD will betoggled

Pre-requisites: Embedded C programming, MPLAB, Proteus

Lab facility: MPLAB X IDE simulator, XC8 Compiler, PIC18FXXX microcontrollerkit

Theory:
Sometimes External devices are connected with microcontroller. If that external device has to send
some information to microcontroller, then microcontroller needsto know about this situation to get that
information. An example of such an external device is the digital thermometer. It measures the
temperature and at the end ofmeasurements transmits results to the microcontroller. Now the purpose
of this article to explain the fact that how does the microcontroller knows to get the required
information from an external device.
Types of interrupts
There are two methods of communication between the microcontroller and the external
device:

• By using Polling
• By using Interrupts

INTERRUPTS
Interrupt is the signal which is sent to the microcontroller to mark the eventthat requires
immediate attention. This signal requests the microcontroller to stopto perform the current program
temporarily time to execute a special code. It means when external device finishes the task imposed on
it, the microcontroller willbe notified that it can access and receive the information and use it.
0

INTERRUPT SOURCES in microcontrollers

The request to the microcontroller to stop to perform the current program temporarily can
come from various sources:
• Through external hardware devices like pressing specific key on the keyboard, which sends
Interrupt to the microcontroller to read the information of the pressed key.
• During execution of the program, the microcontroller can also send interrupts to itself to report an
error in the code. For example, division by 0 will causesan interrupt.
• In the multi-processor system, the microcontrollers can send interrupts to each other to
communicate. For example, to divide the work between themthey will send signals between them.

INTERRUPT TYPES in pic microcontrollers

There are 2 types of interrupts for PIC microcontroller that can cause break.

Software Interrupt: It comes from a program that is executed by microcontrolleror we can say that
it is generated by internal peripherals of the microcontroller andrequests the processor to hold the
running of program and go to make an interrupt. Hardware Interrupt: These interrupts are sent
by external hardware devices atcertain pins of microcontroller.
Following interrupts sources are present in PIC18F452

• Timer over interrupt


• Pins RB0, RB1, RB2 for external hardware interrupts (INT0, INT1, INT2)

• PORTB Change interrupts (any one of the upper four Port B pins. RB4-RB7)

• ADC (analog-to-digital converter) Interrupt

• CCP (compare capture pulse-width-modulation) Interrupt

• Serial communication’s USART interrupts (receive and transmit)

• Reset, Brown-Out Reset, Watch-dog Reset, Power On Reset

• Parallel Port Read/Write Interrupt

• Master Synchronous Serial Port Interrupt

• Data EEPROM Write Complete Interrupt

REGISTER CONFIGURATION for external interrupt

These are the registers for interrupt operation and minimum 1 register can be usedto control the
interrupt operation in PIC18F452 which are:

• RCON (Reset Control Register)

• INTCON, INTCON2, INTCON3 (Interrupt Control Registers)


0
• PIR1, PIR2 (Peripheral Interrupt Request Registers)

• PIE1, PIE2 (Peripheral Interrupt Enable Registers)


RCON Register:
• Reset control register

• IPEN bit to enable interrupt priority scheme, 1= enable priority level on interrupts
• Other bits used to indicate the cause of resetRI (Reset Instruction flag),
TO (Watchdog Time Out flag), PD (Power on Detection flag), POR (Power on Reset status) and
BOR (Brown Out Reset status bit)
INTCON Register:
• 3 Interrupt control registers INTCON, INTCON2, INTCON3

• Readable and writable register which contains various enable and flag bits

• Interrupt flag bits get set when an interrupt condition occurs

• Contain enable, priority and flag bits for external interrupt, port B pin changeand TMR0
overflow interrupt
PIE Register:
• Peripheral Interrupt Enable register

• May be multiple register (PIE1, PIE2), depending on the number ofperipheral interrupt
sources
• Contain the individual bits to enable/disable Peripheral interrupts for use

PIR Register:
• Peripheral Interrupt Flag register

• May be multiple register (PIR1, PIR2), depending on the number ofperipheral interrupt
sources
• Contain bits to identify which interrupt occurs (flags)

• Corresponding bits are set when the interrupt occurredEXTERNAL

• INTERRUPT registers setting

INTCON registers are just used to configure the external PIC interrupts.

INTCON REGISTER:

0
GIE: Global Interrupt Enable
This bit is set high to enable all interrupts of PIC18F452.1 = Enable all interrupts
0 = Disable all interrupts

PEIE: Peripheral Interrupt Enable


This bit is set high to enable all the peripheral interrupts (Internal interrupts) of the
microcontroller.
1 = Enable all peripheral interrupts
0 = Disable all peripheral interrupts

T0IE: TMR0 Overflow Interrupt Enable


This bit is set high to enable the External Interrupt 0.1
= Enable TMR0 overflow interrupt
0 = Disable TMR0 overflow interrupt

INTE: INT External Interrupt Enable


This bit is set high to enable the external interrupts.1 = Enables the INT external
interrupt
0 = Disables the INT external interrupt

RBIE: RB Interrupt Enable


This bit is set high to enable the RB Port Change interrupt pin.1 = Enables the RB port change
interrupt
0 = Disables the RB port change interrupt

T0IF: TMR0 Overflow Interrupt Flag


1 = TMR0 register has overflowed (it must be cleared in software)0 = TMR0 register has not
overflowed

INTF: INT External Interrupt Flag

1 = The INT external interrupt occurred (it must be cleared in software)

0 = The INT external interrupt did not occur


RBIF: RB Port Change Interrupt Flag
1 = At least one of the RB7:RB4 pins changed the state (must be cleared insoftware)

0
0 = None of RB7:RB4 pins have changed the state

ALGORITHM:
In this program, we configure the External Interrupt 0 (INT0). PORT D is used asoutput port
and it is monitored through a set of 8 LEDs. Reset is given through pin 1. Push button is connected
to RB0 for external interrupt source.

Step1: Enable the External Interrupt 0 by setting INT0IE bit high (INTCON=0x10).
Step2: Set the interrupt on falling edge by setting the INTEDG0 in INTCON2 to zero(INTCON2=0)
Step3: Set the Global Interrupt Enable in INTCON to high (INTCON.GIE=1)
Step4: Initialize PORTD with certain value (LATD=0xAA)
Step5: Write the ISR (Interrupt Service Routine) for the interrupt
Step6: Clear the INT0IF bit of INTCON (INTCON.INT0IF=0)
Step7: Invert or toggle the value at PORTD (LATD=~LATD)
Step8: go to step3.

Input: press the push button


Output: the LED’s connected to the PORTD are toggled when the button is
pressed.

Conclusion: The student is able to understand the working of the interrupts andable
to write the ISR (Interrupt Service Routine).

0
Name of the Student: Roll no:

CLASS: - S.E [IT] Division: Course: - PSDL


Experiment No. 8
**An Embedded C program for LCD interfacing with PIC 18FXXX **

Marks: / 10

Date of Performance: / /25 Sign with Date

Aim : To implement an Embedded C program for LCD interfacing with PIC 18FXXX

Objective: -
To understand LCD interfacing

Infrastructure: Desktop/ laptop system with MPLAB software, PIC Kit

Software used: MPLAB version2, Tiny Multi Bootloader

Theory:
ALGORITHM
General steps for programming of LCD

1. Initialize LCD using format – function set


2. Command word for display on Blinking/no blinking
3. Command word for clear LCD screen
4. Command for cursor shift (Right or Left )
5. Command for position of cursor 80+ for line 1 & C0+ for line 2
6. load characters to be displayed
7. While writing each command be sure that RS=0, R/W=0 E=1 H-L pulse, E=0 Latch in

[IMP]
Before start writing LCD for Display of information it is necessary to check for busy flag.

0
8. Check for busy flag (D7) bit
9. RS=0, and R/W=1, D7=1 -- LCD is busy taking care of internal operations and not accept any data.
10. RS=0, and R/W=1, D7=0 -- LCD is ready to accept any data.
11. For command RS=0 is passed through port line
12. For Data RS=1 is passed through port line
13. Send high to low pulse to ‘E’ pin to enable the internal latch of LCD
14. Call delay for completion of internal operations
15. Send different command words
16. Pass data to be displayed [ On any line ]

Algorithm for Checking the Busy Flag

You can use subroutine for checking busy flag or just a big (and safe) delay.

17. Set R/W Pin of the LCD HIGH(read from the LCD)
18. Select the instruction register by setting RS pin LOW
19. Enable the LCD by Setting the enable pin HIGH
20. The most significant bit of the LCD data bus is the state of the busy flag (1=Busy,0=readyto
accept instructions/data). The other bits hold the current value of the address counter.

If the LCD never come out from”busy" status because of some problems, The program will”hang," waiting
for DB7 to go low. So in real applications it would be wise to put some kind of time limit on the delay--for
example, a maximum of 100 attempts to wait for the busy signal to go low. This would guarantee that even
if the LCD hardware fails, the program would not lockup.

Algorithm for Command word

1. Select the instruction register by setting RS pin LOW


2. Set R/W Pin of the LCD low(write to LCD)
3. Enable the LCD by Setting the enable pin HIGH
4. Delay
5. Latch the data E=0

Algorithm for Data write to LCD

1. Select the instruction register by setting RS pin high


2. Set R/W Pin of the LCD low (write to LCD)
3. Enable the LCD by Setting the enable pin HIGH
4. Delay
5. Latch the data E=0
0
The above figure shows the connection of LCD with PIC18.
The resistor R1 is used for giving the contrast to the LCD. The crystal oscillator of
12 MHz is connected to the OSC1 and OSC2 pins of Pic microcontroller PIC18F4550 for system clock.
The capacitor C2 and C3 will act filters to the crystal oscillator. You can use different ports or pins for
interfacing the LCD before going todifferent ports please check the data sheet whether the pins for general
purpose orthey are special function pins.

The above figure shows pin description of LCD.

0
The above figure shows the hex codes and their corresponding instruction to LCD register. The
characters are sent ot the LCD without checking the busy flag. We need to wait 5-10 ms between
issuing each character to the LCD. In programmingan LCD we need a long delay for the power-up
process.

Initializing the LCD function:


lcdcmd(0x38);//Configure the LCD in 8-bit mode,2 line and 5×7 fontlcdcmd(0x0C);//
Display On and Cursor Off

lcdcmd(0x01);// Clear display screen


lcdcmd(0x06);// Increment cursor
lcdcmd(0x80);// Set cursor position to 1st line,1st column
Sending command to the LC:
• rs=0; Register select pin is low.
• rw=0; Read/write Pin is also for writing the command to the LCD.
• en=1;enable pin is high.
Sending data to the LCD:
• rs=1; Register select pin is high.

• rw=0; Read/write Pin is also for writing the command to the LCD.
• en=1; enable pin is high.
0
Input: Make all the connections and start
Output: two input strings are displayed on the LCD.
Conclusion: The student is able to understand the working of the PIC18 with LCDand able to
write the codes for the initialization of LCD, writing to LCD etc.
Name of the Student: Roll no:

CLASS: - S.E [IT] Division: Course: - PSDL


Experiment No. 9
** Generation of PWM signal for DC Motor control **

Marks: / 10

Date of Performance: / /25 Sign with Date

Aim: Write a program to Generate PWM signal for DC Motor control.

Objective: -
Program to Generate PWM signal for DC Motor control.

Infrastructure: Desktop/ laptop system with MPLAB software, PIC Kit

Software used: MPLAB version2, Tiny Multi Bootloader

Theory:
.
Pulse Width Modulation (PWM) is a technique by which the width of a pulse isvaried while
keeping the frequency of the wave constant.

A period of a pulse consists of an ON cycle (5V) and an OFF cycle (0V). Thefraction for
which the signal is ON over a period is known as a duty cycle.

E.g. A pulse with a period of 10ms will remain ON (high) for 2ms.Therefore, the dutycycle will be

D = 2ms / 10ms = 20%

Through the PWM technique, we can control the power delivered to the load by using the ON-OFF
signal. The PWM signals can be used to control the speed of DCmotors and to change the intensity of
0
the LED. Moreover, it can also be used to generate sine signals.

Pulse Width Modulated signals with different duty cycle are shown below
PIC18F4550 controller has an in-built 10-bit PWM module known as the CCP module. The pin
CCP1 (RC2) is used for generating PWM signals. It needs to be configured as an output.

Working of PWM in CCP module

The PWM feature allows us to create pulses with variable widths. Although, timers can beused to create PWM,
the CCP modules makes the programming much easier and less tedious. PWM is widely used in industrial
controls such as DC motor control. Indeed, PWM is so widely used, that Microchip has enhanced PWM
capabilities as Enhanced CPP, where motor can be connected through H bridge.The CCP module uses Timer 2
and its associated registers, PR2 for the PWM time base whichmeans that the frequency of the PWM is a fraction
of the Fosc, the crystal frequency. It uses the PR2 register to set the PWM period as follows:

PR2 = [Fosc/(Fpwm * 4 *N)] -1

Steps in Programming PWM The following steps are taken to program the PWM features of the CCPModule:

34
1. Set the PWM period by writing to the PR2 register

2. Set the PWM duty cycle by writing to CCP1L for the higher 8 bits

3. Set the CCP pin as an Output

4. Using the T2CON register, set the prescale value.

5. Clear the TMR2 register

6. Configure the CCP1CON register for PWM and set DC1B2:DC1B1 bits for the decimal portion ofthe
duty cycle

7. Start Timer 2

A motor driver L293D is provided on the Micro- PIC18F board for the user. The user can connect DCmotor or
stepper motor to connector CN9. In order to use DC motor put switch SW26 in position 2-3,while to use stepper
motor put switch26 in position 1-2
Input: Make all the connections and start

Output: DC motor shaft will rotate.

Conclusion: Thus, we have generated PWM signal using DC motor.

35
Name of the Student: Roll no:

CLASS: - S.E [IT] Division: Course: - PSDL


Experiment No. 10
** Study of Arduino board and understand the OS
installation process onRaspberry-pi.
**
Marks: / 10

Date of Performance: / /25 Sign with Date

Aim:
Study of Arduino board and understand the OS installation process onRaspberry-pi.
Objective: 1) To study different Arduino families and Arduino UNO.
2) To study OS installation process on Raspberry-pi.

Outcome: Introductory understanding of open source hardware Arduino andRaspberry-pi operating


system installation.

Theory:

Arduino is a prototype platform (open-source) based on an easy-to-use hardware and software. It


consists of a circuit board, which can be programed (referred to as a microcontroller) and a ready-
made software called Arduino IDE (Integrated Development Environment), which is used to write
and upload the computer code to the physical board.

The key features are −

• Arduino boards are able to read analog or digital input signals from different sensors and turn it
into an output such as activating a motor, turning LED on/off, connect to the cloud and many other
actions.
• You can control your board functions by sending a set of instructions to the microcontroller on the
board via Arduino IDE (referred to as uploading software).

36
• Unlike most previous programmable circuit boards, Arduino does not need an extra piece of
hardware (called a programmer) in order to load a new code onto the board. You can simply use a
USB cable.
• Additionally, the Arduino IDE uses a simplified version of C++, making it easier to learn to
program.
• Finally, Arduino provides a standard form factor that breaks the functions of the micro-controller
into a more accessible package.

Board Types
• Various kinds of Arduino boards are available depending on different microcontrollers used.
However, all Arduino boards have one thing in common: they are programed through the Arduino
IDE.
• The differences are based on the number of inputs and outputs (the number of sensors, LEDs, and
buttons you can use on a single board), speed, operating voltage, form factor etc. Some boards are
designed to be embedded and have no programming interface (hardware), which you would need to
buy separately. Some can run directly from a 3.7V battery, others need at least 5V.

1) Power USB

Arduino board can be powered by using the USB cable from your computer. All you need to do is
connect the USB cable to the USB connection (1).
37
Power (Barrel Jack)

Arduino boards can be powered directly from the AC mains power supply by connecting it to the
Barrel Jack (2).

3)Voltage Regulator

The function of the voltage regulator is to control the voltage given to the Arduino board and
stabilize the DC voltages used by the processor and other elements.

4)Crystal Oscillator

The crystal oscillator helps Arduino in dealing with time issues. How does Arduino calculate time?
The answer is, by using the crystal oscillator. The number printed on top of the Arduino crystal is
16.000H9H. It tells us that the frequency is 16,000,000 Hertz or 16 MHz.

5) Arduino Reset

You can reset your Arduino board, i.e., start your program from the beginning. You can reset the
UNO board in two ways. First, by using the reset button (17) on the board. Second, you can connect
an external reset button to the Arduino pin labelled RESET (5)

6)Pins (3.3, 5, GND, Vin)

• 3.3V (6) − Supply 3.3 output volt


• 5V (7) − Supply 5 output volt
• Most of the components used with Arduino board works fine with 3.3 volt and 5 volt.
• GND (8)(Ground) − There are several GND pins on the Arduino, any of which can be used
to ground your circuit.
• Vin (9) − This pin also can be used to power the Arduino board from an external power
source, like AC mains power supply.

7) Analog pins

The Arduino UNO board has six analog input pins A0 through A5. These pins can read the signal
from an analog sensor like the humidity sensor or temperature sensor and convert it into a digital
value that can be read by the microprocessor.

38
8)Main microcontroller

Each Arduino board has its own microcontroller (11). You can assume it as the brain of your
board. The main IC (integrated circuit) on the Arduino is slightly different from board to board.
The microcontrollers are usually of the ATMEL Company. You must know what IC your board
has before loading up a new program from the Arduino IDE. This information is available on
the top of the IC. For more details about the IC construction and functions, you can refer to the
data sheet.

9)ICSP pin

Mostly, ICSP (12) is an AVR, a tiny programming header for the Arduino consisting of MOSI,
MISO, SCK, RESET, VCC, and GND. It is often referred to as an SPI (Serial Peripheral
Interface), which could be considered as an "expansion" of the output. Actually, you are
slaving the output device to the master of the SPI bus.

10) Power LED indicator

This LED should light up when you plug your Arduino into a power source to indicate that
your board is powered up correctly. If this light does not turn on, then there is something
wrong with the connection.

11)TX and RX LEDs

On your board, you will find two labels: TX (transmit) and RX (receive). They appear in two
places on the Arduino UNO board. First, at the digital pins 0 and 1, to indicate the pins
responsible for serial communication. Second, the TX and RX led (13). The TX led flashes
with different speed while sending the serial data. The speed of flashing depends on the baud
rate used by the board. RX flashes during the receiving process.

12)Digital I/O

The Arduino UNO board has 14 digital I/O pins (15) (of which 6 provide PWM (Pulse Width
Modulation) output. These pins can be configured to work as input digital pins to read logic
values (0 or 1) or as digital output pins to drive different modules like LEDs, relays, etc. The

39
pins labeled “~” can be used to generate PWM.

13) AREF

AREF stands for Analog Reference. It is sometimes, used to set an external reference voltage
(between 0 and 5 Volts) as the upper limit for the analog input pins.

Installing the Operating System on Raspberry Pi

Raspberry Pi recommend the use of Raspberry Pi Imager to install an operating system on your
SD card. You will need another computer with an SD card reader toinstall the image.

SD Cards for Raspberry Pi


Raspberry Pi computers use a micro SD card, except for very early models whichuse a full-sized SD card.

Using an SD card of 8GB or greater capacity with Raspberry Pi OS. If you are usingthe lite version of
Raspberry Pi OS, you can use a 4GB card. Other operating systems have different requirements: for
example, LibreELEC can run from a smaller card. Please check with the supplier of the operating system to
find out whatcapacity of card they recommend.

Using Raspberry Pi Imager


Raspberry Pi have developed a graphical SD card writing tool that works on Mac OS, Ubuntu 18.04, and
Windows called Raspberry Pi Imager; this is the easiest option for most users since it will download the
image automatically and install it tothe SD card.

Download the latest version of Raspberry Pi Imager and install it. If you want to useRaspberry Pi Imager from
a second Raspberry Pi, you can install it from a terminal using sudo apt install rpi-imager. Then:
• Connect an SD card reader with the SD card inside.

• Open Raspberry Pi Imager and choose the required OS from the listpresented.

• Choose the SD card you wish to write your image to.

• Review your selections and click on the Write button to begin writing data tothe SD Card.
40
Downloading an Image

If you are using a different tool than Raspberry Pi Imager to write to your SD Card, most
require you to download the image first, then use the tool to write it to the card.Official images
for recommended operating systems are available to download fromthe Raspberry Pi website
downloads page. Alternative operating systems for Raspberry Pi computers are also
available from some third-party vendors.

You may need to unzip the downloaded file (.zip) to get the image file (.img) youneed to write
to the card.

Configuration on First Boot

If you have not already configured your operating system using the Advanced Menu of
Raspberry Pi Imager when Raspberry Pi OS starts up for the first time youwill be guided
through initial setup.

The Raspberry Pi OS configuration wizard will run on the first boot. The wizardstarts off by
allowing you to configure international settings and your timezone information.

After hitting "Next" you’ll be prompted to create a user account. Here you canchoose
your username, and a password.

If you want to you can set your username to the old default username of pi, whichwas used on
older versions of Raspberry Pi OS.

However, if you do choose to create this account you will trigger a warning message,
and we’d advise you to avoid the old default password of raspberry.

After creating an user account you can configure your screen, and your wirelessnetwork.

Once your wireless network is configured and your Raspberry Pi has access to theInternet you
will be prompted to update the operating system to the latest version. This will automatically
download any patches and updates needed to bring your new operating system right up to
date.Once the operating system is updated you will be prompted to reboot yourRaspberry Pi.

Conclusion: The student is able to understand different families kits, boards andshields of
Arduino and installation of OS on Raspberry-Pi SBC.

41
Name of the Student: Roll no:

CLASS: - S.E [IT] Division: Course: - PSDL


Experiment No. 11
** Write simple program using Open-source prototype platform like
Raspberry-Pi/Beagle board/Arduino for digital read/write using LED
and switch Analog read/write using sensor and actuators.
**
Marks: / 10
Date of / /25 Sign with
Performance: Date

Aim: Write simple program using Open-source prototype platform like Raspberry-
Pi/Beagle board/Arduino for digital read/write using LED and switch Analog read/write
using sensor and actuators.
Objective: 1) To study Arduino / Raspberry-pi GPIO programming

Outcome: Introductory understanding of GPIO programming using C / Python


programming language.

Theory :

Tinkercad - https://www.tinkercad.com is an excellent tool that allows you to simulate Arduino-


based systems and a lot more. We can simulate all exercises and even your own designs before trying
them on real hardware. It also allows us to do programming using blocks. We can download / copy-
paste the generated code laterinto Arduino IDE to program the real Arduino board, rather than
having to write it from scratch.
Hardware - In Components Basic, you can select Arduino Uno R3. We can add more
components and wire them up as desired. Clicking on the lead of a component allows you to start
a connecting wire from there. Clicking on a wire allows you to change its color.

Programming and Simulation

To program the

Arduino,1.Click on

Code

1. You can choose Blocks or Blocks+Text or Text. For beginners, it is recommendedto

42
use Blocks + Text.
a. This allows you to see the C++ code generated corresponding to your blocks.
b. You can copy this code later into Arduino IDE to program the real Arduino, rather
than having to write it from scratch.
c. You can also download the code as an Arduino-compatible .ino file.

2. You can code by selecting the blocks and connecting them appropriately.

4.You can start the simulation by clicking Start Simulation.

Debugging -

More often than not, the code written by a programmer does not work as expectedthe very first time
he/she runs it. We need to find out the logical flaws in our code and fix them before we are able to
achieve full functionality. Figuring out flaws usually boil down to inspecting variable values at
various points in our code, andcomparing it with the expected values at those points based on the
program logic and data inputs. The usual way Arduino programmers do it is by printing out the
variable values to Serial console.

Tinkercard allows for debugging without having to print the values you want toinspect through Serial.
43
The example below shows debugging of the Ultrasonic Distance Sensor example.

Press the Debugger button.

1.Select the line(s) where you want the execution is to be paused. Such a line whereyou wish to
pause execution is called a breakpoint.

2. Click Start Simulation.

3. Hover over the variable values you want to inspect, and determine if the valuesare along the
expected lines. If not, there is something wrong, and use your logic todetermine what could be
wrong.

4.You can press the Resume execution button to run until the next breakpoint. 5.You can also step

line by line by clicking the Step Over Next Function button.

Conclusion : The student is able to understand GPIO programming of Arduino byusing


sensors and actuators.

44

You might also like