0% found this document useful (0 votes)
31 views41 pages

Mechatronics Lab Manual: Embedded Systems

The document is a lab manual for the Embedded System Design course at Marathwada Mitramandal’s Institute of Technology, detailing experiments related to the PIC 18 microcontroller. It includes objectives, apparatus, theory, procedures, and sample programs for various experiments such as arithmetic operations, timer programming, and interfacing with keypads and LEDs. Additionally, it contains oral questions to assess understanding of microcontroller concepts and programming.

Uploaded by

srbeva06
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)
31 views41 pages

Mechatronics Lab Manual: Embedded Systems

The document is a lab manual for the Embedded System Design course at Marathwada Mitramandal’s Institute of Technology, detailing experiments related to the PIC 18 microcontroller. It includes objectives, apparatus, theory, procedures, and sample programs for various experiments such as arithmetic operations, timer programming, and interfacing with keypads and LEDs. Additionally, it contains oral questions to assess understanding of microcontroller concepts and programming.

Uploaded by

srbeva06
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

Marathwada Mitramandal’s

Institute of Technology
Lohagaon, Pune-411 047

Department of Mechatronics Engineering


Lab Manual
Embedded System Design (2019 Course)

Name :..................................................................
Class : T .E. MTRX
Roll No. : ..............................
Year : 20….-20….
Exam No. :..................

Prepared by:
Ms. Apurva Rajiv Ulhe.
Assistant Professor, Mechatronics Engineering,
MMIT, Lohgaon Pune
INDEX

Sr . No. Title of Experiment

1 Introduction to MPLAB. ii) Addition, Subtraction and Multiplication

2 Timer, Counter, Delay programming

3 Interfacing 18F458 to Keypad, Switch and LED

4 Generation of square, positive ramp, negative ramp, triangular


waveforms using DAC interface

5 Generating PWM waveform using PWM mode of 18F458 timer

6 Driving relay from 18F458 using software and hardware interrupts

7 Interfacing Stepper motor with PIC18F458

8 Interfacing of LM35 with PIC18F458 and displaying of temperature

9 Measurement of level using sensors and PIC18F458

Marathwada Mitramandal’s Institute of Technology, Lohgaon


EXPERIMENT NO.1

TITLE: - I) Introduction to MPLAB and II.) Addition, Subtraction and Multiplication

OBJECTIVE: - To study the concept of Introduction to MPLAB and Addition, Subtraction


and Multiplication

APPARATUS: - PIC 18 Microcontroller Kit

HARDWARE USED: PIC 18 Microcontroller kit, Power supply, connecting wires.

SOFTWARE USED: MPLAB IDE

THEORY:
.MPLAB IDE is a software program that runs on your PC to provide a development
environment for your embedded system design. [Link]/mplab. What is MPLAB
IDE? MPLAB IDE is a software program that runs on your development environment for your
embedded microcontroller design. You can download MPLAB IDE for free at
[Link]/mplab. The design cycle for developing an embedded controller application is:

1) Do the high level design – From the features and performance desired, decide which
PICmicro or dsPIC device you need, then design the associated hardware circuitry.

2) Knowing which peripherals and pins control your hardware, write the software. Use either
assembly language, which is directly translatable into machine code, or using a compiler that
allows a more natural language for creating programs. With these Language Tools you can
write and edit code that is understandable, with constructs that help you organize your code.

3) Compile or assemble the software using a Language Tool to convert your code into machine
code for the PIC micro device. This machine code will eventually become firmware, the code
programmed into the microcontroller.

4) Test your code. Usually, a complex program does not work exactly the way you might have
imagined, and “bugs” need to be removed from your design to get it to act properly.

5) “Burn” your code into a microcontroller and verify that it executes correctly in your finished
application.

Marathwada Mitramandal’s Institute of Technology, Lohgaon


PROCEDURE: -

1. After you have downloaded and installed MPLAB IDE, you can start it up, and you’ll see a
standard windows graphical user interface…with menus…toolbar icons…… and a status bar

2. To get started, we’ll use the Project Wizard, which is selected from the Project menu. Many
of our development boards and starter kits come with finished projects that you can load and
examine. The Project Wizard makes it very easy to set up a new project.

3. The first thing the wizard asks for is to define which processor you are using. We’ll use one
of the 16-bit ds PIC Digital Signal Controllers for this project.

4. If you have installed any C compilers, they will show up in this list. If not, you may only see
the free assemblers that come with MPLAB IDE. Note that all of Microchip’s C Compilers can
be downloaded for free evaluation.

5. Next we’ll browse to the folder to store our project and name it “ProjectX.

6. We have already written the code for this project, so we simply add them to our project,
selecting them from the left file listing……and pressing the “Add>>” button…… to make them
show up in the right pane.. If you had not written any code yet, you can easily add files to the
project later.

7. And that’s all there is to creating the project. The summary screen tells us that we’re using
a dsPIC33 processor with MPLAB C for the PIC30 (also called C30), and that the project is in
a folder on drive C:.

Addition : #include <xc.h>


void main() {
// Initialize PIC 18 microcontroller

// Variables for addition


unsigned char number1 = 10;
unsigned char number2 = 5;
unsigned char result;

// Perform addition
result = number1 + number2;

Marathwada Mitramandal’s Institute of Technology, Lohgaon


// Perform required operations with the result
// ...

while (1) {
// Main program loop
}
}
OUTPUT: The main() function initializes the microcontroller and performs addition of two
numbers (number1 and number2). The result is stored in the result variable

Subtraction : #include <xc.h>


void main() {
// Initialize PIC 18 microcontroller

// Variables for subtraction


unsigned char number1 = 10;
unsigned char number2 = 5;
unsigned char result;

// Perform subtraction
result = number1 - number2;

// Perform required operations with the result


// ...

while (1) {
// Main program loop
}
}

Multiplication : #include <xc.h>

void main() {

Marathwada Mitramandal’s Institute of Technology, Lohgaon


// Initialize PIC 18 microcontroller

// Variables for multiplication


unsigned char number1 = 10;
unsigned char number2 = 5;
unsigned char result;

// Perform multiplication
result = number1 * number2;

// Perform required operations with the result


// ...

while (1) {
// Main program loop
}
}

ORAL QUESTIONS: -

1. What is a microcontroller?

2. What is the difference between a microcontroller and a microprocessor?

3. What is the purpose of an IDE (Integrated Development Environment)?

4. What is MPLAB IDE and what is its significance in PIC microcontroller


programming?

5. How do you configure the PIC 18 microcontroller using MPLAB IDE?

6. What are configuration bits in a microcontroller? How are they set?

7. Explain the basic structure of a C program for a microcontroller.

8. What are some commonly used data types in microcontroller


programming?

Marathwada Mitramandal’s Institute of Technology, Lohgaon


9. How can you perform addition, subtraction, and multiplication operations
in C on a microcontroller?

10. What is the purpose of a loop in a microcontroller program?

Signature and Remark --------------------------------------------------------------------.

Marathwada Mitramandal’s Institute of Technology, Lohgaon


EXPERIMENT NO.2

TITLE: - Timer, Counter, Delay programming

OBJECTIVE: - To study Timer, Counter, Delay programming in Programming in C

APPARATUS: - PIC 18 Microcontroller Kit

HARDWARE USED: PIC 18 Microcontroller kit, Power supply, connecting wires.

SOFTWARE USED: MPLAB IDE

PROGRAM :
1. TIMERS

#include <xc.h>

#define _XTAL_FREQ 8000000 // 8 MHz oscillator frequency

// Function to initialize the PIC18 microcontroller

void init(void)

TRISB = 0x00; // Set PORTB as output

PORTB = 0x00; // Initialize PORTB to 0

// Configure Timer1

T1CONbits.TMR1CS = 0; // Timer1 clock source: Internal clock (Fosc/4)

T1CONbits.T1CKPS = 0b11; // Timer1 prescaler: 1:8

TMR1H = 0; // Clear Timer1 high register

TMR1L = 0; // Clear Timer1 low register

T1CONbits.TMR1ON = 1; // Enable Timer1

Marathwada Mitramandal’s Institute of Technology, Lohgaon


}

// Function to delay in milliseconds using Timer1

void delay_ms(unsigned int milliseconds)

unsigned int i;

for (i = 0; i < milliseconds; i++)

TMR1H = (65536 - 1250) >> 8; // Load Timer1 high register for a delay of 1ms

TMR1L = (65536 - 1250) & 0xFF; // Load Timer1 low register for a delay of 1ms

PIR1bits.TMR1IF = 0; // Clear Timer1 interrupt flag

while (!PIR1bits.TMR1IF); // Wait for Timer1 to overflow

// Main function

int main(void)

init(); // Initialize the microcontroller

while (1)

PORTBbits.RB0 = 1; // Set RB0 pin high

delay_ms(500); // Delay for 500ms

PORTBbits.RB0 = 0; // Set RB0 pin low

delay_ms(500); // Delay for 500ms

Marathwada Mitramandal’s Institute of Technology, Lohgaon


}

return 0;

In this example, we configure Timer1 to generate a delay of 1ms using a prescaler of 1:8. The
delay_ms function utilizes Timer1 to create a delay in milliseconds. The main function blinks
an LED connected to RB0 by setting the pin high for 500ms and then low for another 500ms
in an infinite loop.

2. COUNTER:

#include <xc.h> // Include the PIC18 library

#define _XTAL_FREQ 8000000 // 8 MHz oscillator frequency

// Function to initialize the PIC18 microcontroller

void init(void)

TRISB = 0x00; // Set PORTB as output

PORTB = 0x00; // Initialize PORTB to 0

// Function to delay in milliseconds

void delay_ms(unsigned int milliseconds)

unsigned int i;

for (i = 0; i < milliseconds; i++)

Marathwada Mitramandal’s Institute of Technology, Lohgaon


__delay_ms(1);

// Main function

int main(void)

unsigned int counter = 0; // Counter variable

init(); // Initialize the microcontroller

while (1)

// Display the counter value on PORTB

PORTB = counter;

// Increment the counter

counter++;

// Delay for 500ms

delay_ms(500);

return 0;

In this example, we have a counter variable counter that starts from 0 and increments by 1

Marathwada Mitramandal’s Institute of Technology, Lohgaon


every 500ms. The counter value is displayed on the PORTB pins, assuming LEDs or some
other output devices are connected to those pins.

3. DELAY PROGRAMMING :

#include <xc.h> // Include the PIC18 library

// Define oscillator frequency

#define _XTAL_FREQ 8000000 // 8 MHz oscillator frequency

// Function to initialize the PIC18 microcontroller

void init(void)

TRISB = 0x00; // Set PORTB as output

PORTB = 0x00; // Initialize PORTB to 0

// Function to create a delay in milliseconds

void delay_ms(unsigned int milliseconds)

unsigned int i, j;

for (i = 0; i < milliseconds; i++)

for (j = 0; j < 100; j++)

__delay_us(10); // Delay for 10us (adjust as needed)

// Main function

Marathwada Mitramandal’s Institute of Technology, Lohgaon


int main(void)

init(); // Initialize the microcontroller

while (1)

PORTB = 0xFF; // Set all PORTB pins high

delay_ms(1000); // Delay for 1 second

PORTB = 0x00; // Set all PORTB pins low

delay_ms(1000); // Delay for 1 second

return 0;

In this example, we have a simple delay function delay_ms that utilizes nested loops to create
a delay in milliseconds. The main function turns on all the PORTB pins by setting them high
for 1 second, and then turns them off by setting them low for another 1 second in an infinite
loop.

Marathwada Mitramandal’s Institute of Technology, Lohgaon


ORAL QUESTIONS: -

1 How does a hardware timer work, and what is its role in microcontroller programming?

2. Explain the concept of prescaler in timer programming. How does it affect the timer's
resolution and maximum delay value?

3. How can you configure a timer interrupt in a microcontroller program? What is its purpose,
and how does it differ from polling-based timing?

4. Describe the difference between a timer and a counter in microcontroller programming.


When would you choose to use one over the other?

5. What are the advantages and disadvantages of using hardware timers versus software-based
delay loops for creating delays in microcontroller programming?

Signature and Remark --------------------------------------------------------------------.

Marathwada Mitramandal’s Institute of Technology, Lohgaon


EXPERIMENT NO.3

TITLE: - Interfacing 18F458 to Keypad ,Switch and LED

OBJECTIVE: - To write a program on Interfacing 18F458 to 8 LED WITH 7 SEGMENT


Keypad AND 2 Switches

APPARATUS: - PIC 18 Microcontroller Kit

HARDWARE USED: PIC 18 Microcontroller kit, Power supply, connecting wires.

SOFTWARE USED: MPLAB IDE


THEORY: Below given is the circuit diagram of PIC 18F 458 interfacing with LED ,
SWITCHES AND KEYPAD

PROGRAM

#include <xc.h> // Include the PIC18 library

// Define oscillator frequency

Marathwada Mitramandal’s Institute of Technology, Lohgaon


#define _XTAL_FREQ 8000000 // 8 MHz oscillator frequency

// Function to initialize the PIC18F458 microcontroller

void init(void)

// Configure ports for LEDs, 7-segment display, keypad, and switches

TRISD = 0x00; // Set PORTD as output for LEDs

TRISA = 0x0F; // Set PORTA<3:0> as output for 7-segment display and PORTA<7:4> as
input for keypad

TRISBbits.RB0 = 1; // Set RB0 as input for switch 1

TRISBbits.RB1 = 1; // Set RB1 as input for switch 2

// Initialize PORTD to 0

PORTD = 0x00;

// Function to read the keypad

unsigned char readKeypad(void)

unsigned char keypad[4][4] = {

{'1', '2', '3', 'A'},

{'4', '5', '6', 'B'},

{'7', '8', '9', 'C'},

{'*', '0', '#', 'D'}

};

unsigned char row, col;

// Scan rows

for (col = 0; col < 4; col++)

Marathwada Mitramandal’s Institute of Technology, Lohgaon


{

PORTA = ~(1 << (4 + col));

// Read columns

for (row = 0; row < 4; row++)

if (![Link][row])

while (![Link][row]); // Wait for key release

return keypad[row][col]; // Return the pressed key

return 0; // No key pressed

// Main function

int main(void)

unsigned char key;

unsigned char switch1_state = 0;

unsigned char switch2_state = 0;

init(); // Initialize the microcontroller

Marathwada Mitramandal’s Institute of Technology, Lohgaon


while (1)

// Read the keypad

key = readKeypad();

// Check if a key is pressed

if (key != 0)

// Display the pressed key on the 7-segment display

PORTA = (PORTA & 0xF0) | (key & 0x0F);

else

// No key pressed, clear the 7-segment display

PORTA &= 0xF0;

// Check the state of switch 1

if (PORTBbits.RB0 == 0)

// Switch 1 is pressed

switch1_state = 1;

else

// Switch 1 is not pressed

Marathwada Mitramandal’s Institute of Technology, Lohgaon


switch1_state = 0;

// Check the state of switch 2

if (PORTBbits.RB1 == 0)

// Switch 2 is pressed

switch2_state = 1;

else

// Switch 2 is not pressed

switch2_state = 0;

// Control the LEDs based on the switch states

if (switch1_state == 1)

// Turn on LEDs

PORTD = 0xFF;

else if (switch2_state == 1)

// Turn off LEDs

PORTD = 0x00;

Marathwada Mitramandal’s Institute of Technology, Lohgaon


}

return 0;

ORAL QUESTIONS :

1. What is the purpose of the init() function in the program? How does it configure
the ports for the LEDs, 7-segment display, keypad, and switches?

2. Explain how the readKeypad() function works to read the keypad input. How
does it use the row and column scanning technique?

3. How does the program display the pressed key on the 7-segment display?
Describe the logic used to update the display based on the key value.

4. How are the states of switch 1 and switch 2 determined in the program? What
is the significance of these states?

5. Describe how the program controls the LEDs based on the switch states. How
does it turn on all LEDs when switch 1 is pressed and turn off all LEDs when
switch 2 is pressed.

Signature and Remark --------------------------------------------------------------------.

Marathwada Mitramandal’s Institute of Technology, Lohgaon


EXPERIMENT NO.4

TITLE: - Generation of square, positive ramp, negative ramp, triangular waveforms using
DAC interface

OBJECTIVE: -

APPARATUS: - PIC 18 Microcontroller Kit

HARDWARE USED: PIC 18 Microcontroller kit, Power supply, connecting wires.

SOFTWARE USED: MPLAB IDE

PROGRAM :
#include <xc.h>

void DAC_Write(unsigned int value);

void main(void) {

TRISB = 0x00; // Set Port B as output

while(1) {

// Generate square wave

DAC_Write(0x3FF); // Set DAC output to maximum

__delay_ms(500); // Delay for 500 milliseconds

DAC_Write(0x000); // Set DAC output to minimum

__delay_ms(500); // Delay for 500 milliseconds

// Generate positive ramp

unsigned int i;

for(i = 0; i < 1024; i++) {

DAC_Write(i); // Set DAC output from 0 to 1023

Marathwada Mitramandal’s Institute of Technology, Lohgaon


__delay_us(10); // Delay for 10 microseconds

// Generate negative ramp

for(i = 1023; i >= 0; i--) {

DAC_Write(i); // Set DAC output from 1023 to 0

__delay_us(10); // Delay for 10 microseconds

// Generate triangular waveform

for(i = 0; i < 1024; i++) {

DAC_Write(i); // Set DAC output from 0 to 1023

__delay_us(10); // Delay for 10 microseconds

for(i = 1023; i >= 0; i--) {

DAC_Write(i); // Set DAC output from 1023 to 0

__delay_us(10); // Delay for 10 microseconds

// Function to write a 10-bit value to the DAC

void DAC_Write(unsigned int value) {

PORTB = value; // Write the value to Port B

Marathwada Mitramandal’s Institute of Technology, Lohgaon


The __delay_ms() and __delay_us() functions are used to provide delays, and the values
passed to these functions may need to be adjusted based on your specific microcontroller's
clock frequency.

ORAL QUESTIONS :

1. How does the program generate a square wave using the DAC?

2. What is the purpose of using delays in the program when generating


waveforms?

3. How does the program generate a positive ramp waveform using the DAC?

4. How does the program generate a negative ramp waveform using the DAC?

5. Explain the steps involved in generating a triangular waveform using the DAC.

Signature and Remark --------------------------------------------------------------------.

Marathwada Mitramandal’s Institute of Technology, Lohgaon


EXPERIMENT NO.5

TITLE: - Generating PWM waveform using PWM mode of 18F458 timer

OBJECTIVE: - To write a program in C Generating PWM waveform using PWM mode of


18F458 timer
APPARATUS: - PIC 18 Microcontroller Kit

HARDWARE USED: PIC 18 Microcontroller kit, Power supply, connecting wires.

SOFTWARE USED: MPLAB IDE

PROGRAM :

#include <p18f458.h>

// Function to initialize the CCP module in PWM mode

void CCP_PWM_Init() {

CCP1CONbits.CCP1M3 = 1; // Set CCP1 module to PWM mode

CCP1CONbits.CCP1M2 = 1;

TRISCbits.TRISC2 = 0; // Set CCP1 pin as output

void main() {

CCP_PWM_Init(); // Initialize the CCP module in PWM mode

// Configure the CCP module for a 50% duty cycle (initially)

CCPR1L = 0x80; // MSB of duty cycle

CCP1CONbits.DC1B = 0x00; // LSB of duty cycle

// Set the PWM frequency

PR2 = 249; // Set the period (249 for a 20 kHz frequency)

T2CONbits.T2CKPS = 0b00; // Set the prescaler to 1:1

Marathwada Mitramandal’s Institute of Technology, Lohgaon


T2CONbits.TMR2ON = 1; // Enable Timer2

// Enable PWM output

CCP1CONbits.CCP1M3 = 1;

while (1) {

// Increase the duty cycle gradually

for (int i = 0x00; i <= 0xFF; i++) {

CCPR1L = i; // Update the duty cycle

__delay_ms(10); // Delay for smooth transition

// Decrease the duty cycle gradually

for (int i = 0xFF; i >= 0x00; i--) {

CCPR1L = i; // Update the duty cycle

__delay_ms(10); // Delay for smooth transition

ORAL QUESTIONS:

1. What is PWM, and how does it work?

2. What are the applications of PWM in electronic systems?

3. How do you calculate the duty cycle of a PWM signal?

4. What is the relationship between PWM frequency and resolution?

5. Can you explain the advantages and disadvantages of using PWM for
controlling analog signals?

Signature and Remark --------------------------------------------------------------------.

Marathwada Mitramandal’s Institute of Technology, Lohgaon


EXPERIMENT NO.6

TITLE: - Driving relay from 18F458 using software and hardware interrupts

OBJECTIVE: - To write a program In C for Driving relay from 18F458 using software and
hardware interrupts

APPARATUS: -

-PIC 18 Microcontroller Kit

- Power supply

- Connecting wires

HARDWARE USED: PIC 18 Microcontroller kit, Power supply, connecting wires.

SOFTWARE USED: MPLAB IDE

PROGRAM :

#include <p18f458.h>

#include <xc.h>

#pragma interrupt HighISR

void HighISR(void) {

// Implement the necessary code for the high-priority interrupt service routine.

Marathwada Mitramandal’s Institute of Technology, Lohgaon


#pragma interrupt LowISR

void LowISR(void) {

// Implement the necessary code for the low-priority interrupt service routine.

void main(void) {

// Implement the necessary code for the main function.

ORAL QUESTIONS :

1. What is the purpose of using software and hardware interrupts in this experiment?

2. How do you define the high-priority and low-priority interrupt service routines in the
program?

3. What precautions should be taken while connecting the relay to the microcontroller?

Signature and Remark -------------------------------------------------------------------

Marathwada Mitramandal’s Institute of Technology, Lohgaon


EXPERIMENT NO.7

TITLE: - Interfacing Stepper motor with PIC18F458

OBJECTIVE: - To write a program in C on Interfacing Stepper motor with PIC18F458

APPARATUS: - PIC 18 Microcontroller Kit

HARDWARE USED: PIC 18 Microcontroller kit, Power supply, connecting wires.

SOFTWARE USED: MPLAB IDE

PROGRAM :

#include <p18f458.h>

#include <xc.h>

// Define the necessary variables and configurations for stepper motor control.

void main(void) {

// Initialize the necessary configurations for the microcontroller and stepper motor.

while (1) {

// Implement the necessary code for controlling the stepper motor.

Marathwada Mitramandal’s Institute of Technology, Lohgaon


Circuit Diagram

#include <xc.h>

#include <pic18f458.h>

// Define the stepper motor pins

#define STEP_PIN RB0

#define DIR_PIN RB1

// Define delay function

void delay(unsigned int milliseconds) {

unsigned int i;

for (i = 0; i < milliseconds; i++) {

__delay_ms(1);

Marathwada Mitramandal’s Institute of Technology, Lohgaon


// Function to rotate stepper motor clockwise

void rotateClockwise() {

DIR_PIN = 0; // Set direction pin to low for clockwise rotation

// Rotate the stepper motor for 200 steps

for (int i = 0; i < 200; i++) {

STEP_PIN = 1;

delay(5); // Delay for motor speed control

STEP_PIN = 0;

delay(5); // Delay for motor speed control

// Function to rotate stepper motor counterclockwise

void rotateCounterClockwise() {

DIR_PIN = 1; // Set direction pin to high for counterclockwise rotation

// Rotate the stepper motor for 200 steps

for (int i = 0; i < 200; i++) {

STEP_PIN = 1;

delay(5); // Delay for motor speed control

STEP_PIN = 0;

delay(5); // Delay for motor speed control

Marathwada Mitramandal’s Institute of Technology, Lohgaon


void main() {

// Initialize ports

TRISB = 0x00; // Set PORTB as output

// Main program loop

while (1) {

rotateClockwise(); // Rotate the motor clockwise

delay(1000); // Delay between rotations

rotateCounterClockwise(); // Rotate the motor counterclockwise

delay(1000); // Delay between rotations

ORAL QUESTIONS :

1. How does a stepper motor differ from other types of motors, such as DC motors
or servo motors?

2. What are the advantages of using a stepper motor in certain applications


compared to other motor types?

3. Explain the concept of stepper motor control modes, such as full-step and half-
step modes. How do these modes affect the motor's movement?

4. What is the purpose of initializing the microcontroller's configurations in the


program? How does it impact the stepper motor's operation?

5. Describe the sequence of steps involved in controlling the stepper motor. How
can the direction and speed of the motor's rotation be controlled in the program?

Signature and Remark --------------------------------------------------------------------.

Marathwada Mitramandal’s Institute of Technology, Lohgaon


EXPERIMENT NO.8

TITLE: - Interfacing of LM35 with PIC18F458 and displaying of temperature

OBJECTIVE: - To write a program in C Interfacing of LM35 with PIC18F458 and displaying


of temperature

APPARATUS: - PIC 18 Microcontroller Kit

HARDWARE USED: PIC 18 Microcontroller kit, Power supply, connecting wires.

SOFTWARE USED: MPLAB IDE

PROGRAM :

#include <p18f458.h>

#include <xc.h>

// Define the necessary variables and configurations for LM35 sensor interfacing.

void main(void) {

// Initialize the necessary configurations for the microcontroller and LM35 sensor.

while (1) {

// Implement the necessary code for reading the temperature from the LM35 sensor.

// Convert the sensor output to temperature and display it on an output device.

Marathwada Mitramandal’s Institute of Technology, Lohgaon


}

Program :

#include <xc.h>

#include <pic18f458.h>

#include <stdio.h>

#include "lcd.h"

// Define LM35 sensor pin

#define LM35_PIN RA0

// Function to initialize ADC module

void initADC() {

[Link] = 1; // Enable ADC module

ADCON1 = 0b00000000; // Use internal Vref+, Vref-; Configure all other pins as analog
inputs

ADCON2 = 0b10001010; // Right justify the result, Tad = 2us, Acquisition time = 4 Tad

// Function to read temperature from LM35 sensor

float readTemperature() {

[Link] = 0b0000; // Select channel 0 (RA0) for temperature reading

[Link] = 1; // Start ADC conversion

while ([Link]); // Wait for conversion to complete

Marathwada Mitramandal’s Institute of Technology, Lohgaon


unsigned int adcResult = ((unsigned int)ADRESH << 8) | ADRESL; // Read ADC result

float temperature = (adcResult * 5.0) / 1023.0; // Convert ADC reading to voltage

temperature = temperature * 100.0; // Convert voltage to temperature in degrees Celsius

return temperature;

void main() {

// Initialize LCD module

Lcd_Init();

Lcd_Clear();

Lcd_Set_Cursor(1, 1);

Lcd_Write_String("Temperature:");

// Initialize ADC module

initADC();

// Main program loop

while (1) {

float temperature = readTemperature(); // Read temperature from LM35 sensor

char temperatureStr[10];

sprintf(temperatureStr, "%.2f", temperature); // Convert temperature to string

Lcd_Set_Cursor(2, 1);

Lcd_Write_String(temperatureStr); // Display temperature on LCD

__delay_ms(1000); // Delay for 1 second

Lcd_Clear(); // Clear LCD display

Marathwada Mitramandal’s Institute of Technology, Lohgaon


}

ORAL QUESTIONS :

1. How does the LM35 temperature sensor work? Explain its operating principle.

2. What is the purpose of interfacing the LM35 sensor with the PIC18F458 microcontroller in
this experiment?

3. How can the analog output of the LM35 sensor be converted into a temperature value?

4. Describe the steps involved in reading the temperature from the LM35 sensor and displaying
it on an output device.

5. What precautions should be taken while connecting the LM35 sensor to the microcontroller?

Signature and Remark --------------------------------------------------------------------.

Marathwada Mitramandal’s Institute of Technology, Lohgaon


EXPERIMENT NO.9

TITLE: - Measurement of level using sensors and PIC18F458

OBJECTIVE: - To write a program in C for the Measurement of level using sensors and
PIC18F458

APPARATUS: - PIC 18 Microcontroller Kit

HARDWARE USED: PIC 18 Microcontroller kit, Power supply, connecting wires.

SOFTWARE USED: MPLAB IDE

PROGRAM :

#include <p18f458.h>

#include <xc.h>

// Define the necessary variables and configurations for the level sensor(s) and PIC18F458.

void main(void) {

// Initialize the necessary configurations for the microcontroller and level sensor(s).

while (1) {

// Implement the necessary code for measuring the level using the sensor(s).

// Process the sensor data and take appropriate actions based on the level measurement.

Marathwada Mitramandal’s Institute of Technology, Lohgaon


}

Program :

#include <pic18f458.h>

// Define sensor pin and other variables

#define SENSOR_PIN RB0

int level = 0;

// Function to initialize ADC

void ADC_Init()

// Set ADC configuration and ports

ADCON0 = 0b10000001; // Enable ADC with Fosc/32 and channel 0

ADCON1 = 0b00000000; // Set result format as right-justified

ADCON2 = 0b10111110; // Set ADC conversion clock and acquisition time

// Set sensor pin as input

TRISB0 = 1;

// Function to read ADC value

int ADC_Read()

[Link] = 1; // Start ADC conversion

Marathwada Mitramandal’s Institute of Technology, Lohgaon


while ([Link]); // Wait for conversion to complete

return ((ADRESH << 8) + ADRESL); // Return ADC result

// Function to map ADC value to level

int Map(int value, int inMin, int inMax, int outMin, int outMax)

return ((value - inMin) * (outMax - outMin) / (inMax - inMin) + outMin);

// Main function

void main()

// Initialize the ADC

ADC_Init();

// Main program loop

while (1)

// Read the ADC value

int adcValue = ADC_Read();

// Map the ADC value to level (customize the mapping based on your sensor)

level = Map(adcValue, 0, 1023, 0, 100);

Marathwada Mitramandal’s Institute of Technology, Lohgaon


// Print the level value or perform further actions based on the level

// (you can use UART or LCD to display the level, or control other devices)

// Add delay if necessary

ORAL QUESTIONS :

1. How does the level measurement using sensors work? Explain the basic principle behind the
level sensing techniques used in this experiment.

2. What is the purpose of interfacing the level sensor(s) with the PIC18F458 microcontroller
in this experiment?

3. What types of sensors can be used for level measurement, and what are their advantages and
disadvantages?

4. Describe the steps involved in measuring the level using the sensor(s) and processing the
sensor data in the program.

5. What factors should be considered while selecting a suitable sensor for a specific level
measurement application?

Signature and Remark --------------------------------------------------------------------.

Marathwada Mitramandal’s Institute of Technology, Lohgaon


Marathwada Mitramandal’s Institute of Technology, Lohgaon
Marathwada Mitramandal’s Institute of Technology, Lohgaon

You might also like