0% found this document useful (0 votes)
14 views2 pages

PIC18 Microcontroller LED Programs

The document provides exam answers for programming the PIC18 microcontroller, including interfacing an LED with a switch, generating delays for LED toggling, and blinking LEDs on different ports. It includes C programs for each task, demonstrating how to configure input and output pins, implement delays, and control LED states. Key concepts such as interfacing, delay generation types, and continuous loops for LED control are explained.

Uploaded by

2002.saumukhvai
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)
14 views2 pages

PIC18 Microcontroller LED Programs

The document provides exam answers for programming the PIC18 microcontroller, including interfacing an LED with a switch, generating delays for LED toggling, and blinking LEDs on different ports. It includes C programs for each task, demonstrating how to configure input and output pins, implement delays, and control LED states. Key concepts such as interfacing, delay generation types, and continuous loops for LED control are explained.

Uploaded by

2002.saumukhvai
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

PIC18 Microcontroller Programs - Exam Answers

Q1. LED Interfacing with PIC18 (Switch at RB1, LED at RB7)

Introduction
Interfacing means connecting external devices (like switches, LEDs, motors, sensors) with a
microcontroller so that it can interact with the outside world. Here, we connect a switch at RB1 and
an LED at RB7 of PIC18 microcontroller.

Explanation
Switch is input device → it is connected to RB1 pin which is configured as input. LED is output
device → it is connected to RB7 pin which is configured as output. When the switch is pressed, RB1
becomes HIGH. The program reads this input and copies its value to RB7. As a result, LED glows
when switch is ON, and turns OFF when switch is released.

C Program
#include <xc.h>
void main(void) {
TRISBbits.TRISB1 = 1; // RB1 as input
TRISBbits.TRISB7 = 0; // RB7 as output

while(1) {
if(PORTBbits.RB1 == 1) // if switch pressed
LATBbits.LATB7 = 1; // LED ON
else
LATBbits.LATB7 = 0; // LED OFF
}
}

Q2. Delay Generation & LED Toggle on Port D with 50ms Delay

Introduction
In microcontroller programming, delay is required to control the speed of operations such as LED
blinking, motor control, communication timing etc.

Types of Delay Generation


1. Software delay – Using simple loops in program. 2. Hardware delay – Using built-in timers. 3.
Interrupt delay – Using timer interrupts for accurate timing.

Explanation
LEDs are connected to Port D pins. Program continuously toggles all LEDs. Each ON/OFF action is
separated by a 50 ms delay. Delay is created using a simple software delay function.

C Program
#include <xc.h>

void delay_ms(unsigned int ms) {


unsigned int i, j;
for(i=0; i<ms; i++) {
for(j=0; j<333; j++); // approximate delay
}
}

void main(void) {
TRISD = 0x00; // Port D as output

while(1) {
LATD = 0xFF; // All LEDs ON
delay_ms(50); // 50 ms delay
LATD = 0x00; // All LEDs OFF
delay_ms(50); // 50 ms delay
}
}

Q3. Blink LEDs on Port C

Introduction
Blinking of LEDs is the simplest program to test a microcontroller. In this program, LEDs are
connected to Port C of PIC18.

Explanation
Port C pins are configured as outputs. Program turns ON all LEDs for some delay, then turns them
OFF for some delay. This process repeats continuously and creates a blinking effect.

C Program
#include <xc.h>

void delay_ms(unsigned int ms) {


unsigned int i, j;
for(i=0; i<ms; i++) {
for(j=0; j<333; j++);
}
}

void main(void) {
TRISC = 0x00; // Port C as output

while(1) {
LATC = 0xFF; // LEDs ON
delay_ms(200); // delay
LATC = 0x00; // LEDs OFF
delay_ms(200); // delay
}
}

You might also like