0% found this document useful (0 votes)
19 views7 pages

EXPT 04 - LED - Blinking

The document outlines Experiment No. 4, which involves interfacing LEDs with a PIC Microcontroller using Embedded C to create a blinking effect with delays. It covers the objectives, theory behind microcontroller ports, the importance of various registers (TRIS, PORT, LAT), and provides a sample code for implementation. Additionally, it includes references for further reading on PIC microcontrollers and their functionalities.

Uploaded by

Sanjay Thorat
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)
19 views7 pages

EXPT 04 - LED - Blinking

The document outlines Experiment No. 4, which involves interfacing LEDs with a PIC Microcontroller using Embedded C to create a blinking effect with delays. It covers the objectives, theory behind microcontroller ports, the importance of various registers (TRIS, PORT, LAT), and provides a sample code for implementation. Additionally, it includes references for further reading on PIC microcontrollers and their functionalities.

Uploaded by

Sanjay Thorat
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/ 7

ExperimentN0.

Experiment No.04

TITLE : Interfacing LED’s to PICMicrocontroller with Delay using


Simple Loop.

PROBLEM STATEMENT:
InterfaceLED‟stoPICMicrocontroller.WriteanEmbeddedCprogramtoblink the LED‟s at
specificinterval with Delay using Simple Loop.

OBJECTIVE:
a. To understand the PORT Structure of PICMicrocontroller.
b. To study the SFRs to control the PORTPins.
c. To interface common peripherals like LEDs, Relay,Buzzer.
d. To understand the use of MPLAB IDE and C18Compiler.
e. To write a simple program in EmbeddedC.

S/W PACKAGES AND H/W USED:


MPLABX IDE / MPLAB IDE, X8 / C18 Compiler, PIC Development Board

THEORY
What is Boot loader?
The boot loader is a program which helps to program the microcontroller device without
using an external programmer. It is possible to burn the hex code even if the PC doesn’t
have a serial port or any other tools.
Required tools for Boot loader are
 USB cable to connect board to the PC
 A microcontroller with already flashed bootloader program.
Depending on the device selected, there are up to five general purpose I/O ports
available in PIC18F Microcontroller devices. Some pins of the I/O ports are multiplexed
with an alternate function from the peripheral features on the device. In general, when
a peripheral is enabled, that pin may not be used as a general purpose I/Opin.

Ports are not only used for simple I/O functions(Data in and out), but also can be used
other functions such as ADC, timers, interrupts, and serial communication pins.
PIC 18fxx PORT Basic Properties:

It has 5 Ports in total. ( PortA, PortB, PortC, PortD and PortE). can be used for input or
output.

1
ExperimentN0. 4

I/O Port Control Registers

All I/O ports have four registers directly associated with the operation of the port, where
'x' is a letter that denotes the particular I/O port:

 TRISx: PORTx Data Direction Control register


 PORTx: I/O Port register
 LATx: PORTx Data Latch register

TRISx Registers
The TRISx register control bits determine whether each pin associated with the I/O port is
an input or an output. If the TRIS bit for an I/O pin is a 1, then the pin is an input. If it is a
0, then the pin is configured as an output. It is important to remember that all port pins
will be defined as inputs after a Reset. By defaulting to inputs, the pins will not conflict
with any logic outputs or other voltage sources connected to them.

PORTx Registers

Data on an I/O pin is accessed through a PORTx register. A read of the PORTx register
reads the value of the I/O pin, while a write to the PORTx register writes the value to the
port data latch.
Many instructions on the 16-bit MCUs are read-modify-write operations; therefore, a write
to a port implies that the port pins are read; the value is modified and then written back
to the port data latch. You should be careful when using these instructions on the PORTx
registers. If an I/O pin configured as an input is changed to an output later, an
unexpected value may be output on the I/O pin. This effect occurs because the read-
modify-write instruction reads the instantaneous value on the input pin and loads that
value into the port data latch. Similarly, if an I/O pin is configured as an output,
unintended I/O behavior may occur based on the device speed and I/O capacitive loading.

2
ExperimentN0. 4

Read, Modify and Write (RMW) Problem with PIC 16F Family

Microchip’s mid-range PIC Microcontrollers use a sequence of operations: Read, Modify


and Write to change output state of a pin. In certain circumstances RMW operations might
cause unexpected behavior of outputs.Mid-Range PIC Microcontroller uses two registers,
TRIS and PORT to control the direction and status of an IO pin respectively.

LAT Register Solves the Problem

In PIC 18F and PIC 16F1XXX familys Read Modify Write (RMW) Problem is solved by
adding LAT register. LAT stands for Port Latch. Thus each port is associated with three
registers TRIS, PORT and LAT. As before TRIS register determines the direction of each
digital IO pin, ie Input or Output. PORT register should be used to read data from Input
pins, ie it reads Actual Voltage Levels of the pins as in 16F. LAT register is used to write
data to Output pins. Writing data to LAT register is equivalent to a PORT register, but
reading LAT register reads the Port Latch (LAT register) regardless of the physical state
(voltage level) of the corresponding pin. Thus RMW problem will not occur in PIC 18F
microcontrollers if we are using LAT register to write data as below.

LATB7 = 1;
LATB0 = 1;

LATx Registers

The LATx register associated with an I/O pin eliminates the problems that can occur with
read-modify-write instructions. A read of the LATx register returns the values that are held
in the port output latches instead of the values on the I/O pins. A read-modify-write
operation on the LATx register associated with an I/O port avoids the possibility of writing
the input pin values into the port latches. A write to the LATx register has the same effect
as a write to the PORTx register.

The differences between the PORTx and LATx registers are summarized in the following
table:

PORTx LATx

Read Reads data value on the I/O pin Reads data value held in the port latch

Write Writes data value to the port latch Writes data value to the port latch

3
ExperimentN0. 4

All the ports of PIC18 are bidirectional and identical. They all have the
following four components in their structure as shown in figure 1.3.
1. DATALATCH
2. OUTPUT DRIVER
3. INPUTBUFFER
4. TRISLATCH
The PIC18 Ports have both the latch and buffer. Therefore, when reading the
ports there are two possibilities:
1. Reading the inputpin
2. Reading thelatch

4
ExperimentN0. 4

Figure: Port structure of PIC 18F :Generic I/O Port Operation

4. InterfacingDiagram

Figure: Interfacing LED’s to PIC18 Microcontroller


5
ExperimentN0. 4

5. Algorithm
1. Configure PORTD as output port writing 0x00 to the TRISD register since LED‟s
are interfaced toPORTD.
2. Turn ON theLED‟s
3. Call delayroutine
4. Turn OFF theLED‟s
5. Call delayroutine
6. Repeat step 3 to4

Conclusion:

6
ExperimentN0. 4

6. SourceCode

//Expt.4: Interfacing LED’s to PORTD of PIC18 Microcontroller Delay Using

Simple Loop.

#include<xc.h> //Include Controller specific.h

//Function Prototypes

void msdelay (unsigned int time);//Function for delay

//Start of Program Code


void main()
{
ADCON1=0x0F; //To disable the all analog inputs
TRISD=0x00; //To configure PORTD asoutput

while(1) //While loop for repeatedoperation


{
PORTD=0xFF; //Turn ON the all LED’s

msdelay(250); //Delay

PORTD=0x00; //Turn OFF the all LED’s

msdelay(250); //Delay

}
} //End of theProgram

//Function Definition for delay degeneration void

void msdelay (unsigned int time)


{

int i, j;

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


for (j = 0; j < 710; j++);//Calibrated for a 1 ms delay in MPLAB
}

7. References:
a. Mazidi, PIC microcontroller & embedded system 3rd Edition,Pearson
b. Datasheet of PIC18F4550 / PIC18F4520 / PIC18F458,www.microchip.com
c. PIC18 Development BoardManual

You might also like