Even if you have set up the TRIS register and written a value to the
port, there are several things that can prevent such a seemingly simple program from
working.
- Make sure that the device’s Configuration registers are set up
correctly (see Configuration Bit Access). Make sure that you explicitly specify every bit in these registers and don’t
just leave them in their default state. All the configuration features are described
in your device data sheet. If the Configuration bits that specify the oscillator
source are wrong, for example, the device clock cannot even be running.
- If the internal oscillator is being used, in addition to
Configuration bits there can be SFRs you need to initialize to set the oscillator
frequency and modes, see Using SFRs From C Code and your device data sheet.
- Either turn off the Watchdog Timer in the Configuration bits or
clear the Watch Dog Timer in your code so that the device does not reset. If the
device is resetting, it can never reach the lines of code in your program that blink
the LED. Turn off any other features that can cause device Reset until your test
program is working.
- The device pins used by the port bits are often multiplexed with
other peripherals. A pin might be connected to a bit in a port, or it might be an
analog input, or it might the output of a comparator, for example. If the pin
connected to your LED is not internally connected to the port you are using, then
your LED will never operate as expected. The port function tables shown in your
device data sheets will show other uses for each pin that will help you identify
peripherals to investigate.
- Make sure you do not have a “read-modify-write” problem. If the
device you are using does not have a separate “latch” register (as is the case with
mid-range PIC devices) this problem can occur, particularly if the port outputs are
driving large loads, such as an LED. You can see that setting one bit turns off
another or other unusual events. Create your own latch by using a temporary
variable. Rather than read and write the port directly, make modifications to the
latch variable. After modifications are complete, copy the latch as a whole to the
port. This means you are never reading the port to modify it. Check the device
literature for more detailed information.