EXPT 04 - LED - Blinking
EXPT 04 - LED - Blinking
Experiment No.04
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.
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
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 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
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
4. InterfacingDiagram
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
Simple Loop.
//Function Prototypes
msdelay(250); //Delay
msdelay(250); //Delay
}
} //End of theProgram
int i, j;
7. References:
a. Mazidi, PIC microcontroller & embedded system 3rd Edition,Pearson
b. Datasheet of PIC18F4550 / PIC18F4520 / PIC18F458,www.microchip.com
c. PIC18 Development BoardManual