PIC 18F4550 blinking LED steps (Initial help for you)


From here we will start on programming with pic18f4550, my previous tutorials ( Tutorial 1 and Tutorial 2 ) must have given you some outlines regarding the tools that we need and how to setup the project in details to get started.

For the sake of example, we are going to take a simple case of blinking an LED,  where we are going to blink one led on pin RB6.  Currently, i am on giving any emphasis on the style of programming, otherwise, there are much more difficult and good way of programming simple programs too. For example: firstly making a custom header file for PIC and then use functions from there. The reason, we do this, is because there are lot of low level settings to be change for PIC chip and due to that code becomes so ugly which leads to bad logic building. You will observe that there are much more lines to code and to take care, just to blink a LED.

There are certain basic things which you should know like...


40 pins of PIC18F4550 are divided into 5 ports. Out of which, 35 pins are Input-Output pins which can be configured for general  Input or Output by setting registers associated with them. Please Refer the Pinout diagram above for a clear idea about location of these pins on the microcontroller.


Ports           Number of pins   Pin Name

PORTA                  7            RA0-RA6
PORTB                  8            RB0-RB7
PORTC                  7            RC0-RC2, RC4-RC7 (Check the Pinout Diagram)
PORTD                  7            RD0-RD7
PORTE                  4            RE0-RE3


Registers Associated with Ports in PIC18F4550

Each port in pic18f4450 is associated with three 8 bit registers for IO operations.

  1. TRISX   (8 bit)
  2. LATx     (8 bit)
  3. PORTx  (8 bit)

TRISx : where X is the name of the ports either of A, B, C, D, E. For example TRISA, TRISB etc.This register assigns the direction of the pins (Input or Output). For example “TRISB = 0xF0”, will set all the pins in port B to Output.

LATX: The latch registers reds and modifies the write operation on the value of I/O pin and stored the output data that is to be passed on to the external hardware.

PORTX: Reads the device level, stores the Input level of the pins and reads and registers the input signal from the external device if the pin is configured as Input and vice-versa if configured as output,

Please note that here PORTX is used for both input and output data opersations. There is a small difference between LAT and PORT.

The latch is the output latch onto which values are written. The port is the voltage at the actual pin. There are a few situations where they can be different. The one that I've encountered most frequently is if you have a pin (accidentally) shorted to ground. If you set the latch high, the latch will read high, but the port will read low because the voltage on the pin is still approximately ground. Another situation leading to what you've described is when the port pin hasn't been configured correctly. I (and everyone I work with) have spent many hours trying to figure out why our PIC isn't working to expectations, to eventually find out that we glossed over turning off the analog modules.

Similarly, A read of the port latch register returns the settings of the output drivers, while a read of the port register returns the logic levels seen on the pins.

In this tutorial, we will only use TRIS and PORT for simplicity.



Microchip is very well known for its extremely good and detailed documentation. Please download a copy PIC18F4550 Data sheet from their official website for more detailed information.

Download PIC18f4550 Datasheet

PIC18f4550 Section

Now lets see, how we program some pins. I am expecting that you all already know about pin description and other features of PIC, if not go to microchip website and read there about the basic pin diagram and functionality. By the way, we will only cover the concept which are important and are comming in my way to explain you.


PORT settings: TRISB = 0xF0

IF you want to use any pin in the microcontroller for any operation then you have to first set the pin to either for an input or out put. So you have to define the corresponding PORT to either Input or out put.

  TRISB = 0xF0 ;             // Set all the pins under PORT B to output.



A typical pic18f4550 has 40 pins in it (if it’s a DIP). These 40 pins are divided (classified) into a Logical name PORTS. There are 5 ports in a pic18f4550, PORTB is one of such ports.
Each port is associated with few pins. These ports are accessed by some registersTRISx register is one of such register to access the corresponding port.


TRISB Register is used to access port B, which is to define the pins under port B as input or output. Port B has 8 pins, From RB0 to RB7. Check the pin diagram.Port setting , pic18f4550 pin diagram in the tutorial for programming pic18f4550

For example:

TRISB = 0xF0 ;   // Set all the pins in Port B  to output.

Instead of TRISB = 0xF0 ; you can also write

TRISB = 0;  both are same.

For Setting all the pins in PORTB to input

TRISB = 1;   // Set all the pins in port B to input.

TRISB port settings for accessing all the pins in a PIC18F4550, TRISB = 0;


Instead of assigning each pin one by one, you can reduce the coding by defining the entire TRISB to output, only if all the pins are for same purpose.


Accessing Individual pins

In real time programming there are certain situations where you would want some pins to work as input and some as output in the same PORT.
At such situation you can set individual pins as either input or output.

                TRISBbits.TRISB0 = 0;       //  Set only pin RB0 to output
                TRISBbits.TRISB1 = 1;       //  Set only pin RB1 to Input


 TRISBbits.TRISB , accessing individual pins in port in pic18f4550


In the figure above the pins from same PORTB , RB0-RB3 is set to output, and RB4-RB7 is defined as input. Incase of individual pin settings you have to specifically mention the individual pin.  The pin not defined will be left unused.

TRISB was just an example, same method can be used to gain access to other ports also (PORT A, B, C, D, E). Review the port settings for various Ports and pins associated with them in the datasheet.


void main(void)

The void main is the main section from where the code actually starts to execute. All the function prototypes that are defined outside the main can be called from inside. As like in delay() in the code above where we defined it outside the main code block.

The first stage is to Set the port settings, as shown above in PORTS subtopic

The while(1) defined inside is simple endless loop that keep executing the block inside the braces.

For our blinking project we have defined two led's to blink simultaneously together using PORTB Register

Download the Source code from my git repository.



Conect the input voltage (vdd is gnd and vss is +5) which must never exceed 5 V or it might damage the microcontroller, you can also use a IC 7805 Voltage regulator or mcp 1702 to ensure the input is 5V.  Usually power regulators are present in the development board. But, if you have made your own circuit, then dont forget to use a power regulating ciruit to save your chip.

Next, you have to connect the LEDs to the output pin of PIC which you are making high or low in your code (in our case its RB6). The schematic diagram here, will show you how to connect it with your PIC.

Please note that, in this diagram RB6 is marked with yellow rectangle. This is the pin where you have to connect the LED. You can see below that pin, several LEDs already connected to PIC with a series resistor (470 ohm standard). But usually its not compulsary with microcontrollers, because they do not output high current which can damage LEDs. that's why there is a saying "Microcontrollers are LED friendly".


Code (Logic)

Below is just a portion of Code, which is used to blink LEDs. Complete code, you can or you have already downloaded till now from my git repository.

Below, here is the given code explaination of main function.

 Firstly, as already explained by me, we have changed TRISB to 0. Here, this statement will tell PIC microcontroller to configure whole port B for output functioning. 

Next PORTB= 0 have changed the initial state of whole port to 0 ie low. (we does not require to use LAT here because we are just changing the internal state of port). 

While (1) is used to infintely run the statements in between the curly braces.

Main logic is inside the while loop, which is related to the funtionality of the PIC program. This is absolutely clear that we are changing the state of RB6 pin again and again in while loop and its toggling between 1 and 0 making the LED connected to it blink.


PORTBbits.RB6 gives the address iof the pin, for which we want to manipulate data. 

At last you may want to also see the delay function here because its actually making this blink visible. Without delay function, we can't see the blinking effect of LED. Instead, we will see a bit dim LED due to effect of PWM (Pulse width modulation).


Burning .hex file to PIC

Below is the screen shot of a software provided by Microchip called PICDEM tool to flash the hex file generated to PIC chip. You can download this software again from microchip official page or from any external link avaiable by searching on google. I downloaded it from here.

 There are very few steps to use this software.

1. Connect your device PIC through USB cable. pop new hardware found will come.

2. Install the PIC drivers by selecting local file at : C:\MCHPFSUSB\Pc\MCHPUSB Driver\Release\   in the new hardware wziard.

3. Complete the installation, by clicking next -> next and continue anyway and so on.

4. Select the hex file by clicking "load Hex file" button

5. After your driver installation is over , you will find your device on opening combo box (as shown in the image)

6. Select your device.

7. After selecting your board, click on Program device.

8. Then click "Execute" button


Please note that  If program does not run after pressing execute, then press reset on your board. Normally it gives an error on pressing execute button, saying "unable to reset". Ignore this error, just press reset on your board, if program does not run.


secondly. from my experience i have found that you have to run this software on windows XP. 

YES MAN YES, windows xp, because it does not detect the hardware in Vista or win 7. But dont worry! I have found the cure ;) You haveto a little hardwork to run this on win7.

Firstly you have to change the compaitiblity mode as XP service pack2 or 3 for this software, then each time you run this software, please right click on icon and select run as administrator.

I have not found out, why we have to do this. But it will definately work by this method. I didnt waste my time to google it and find the problem. neither i want you to waste your time. Some people also use virtual box for XP, but thats so hard as compared to the solution i have mentioned here.

Above is the screenshot of this software, when you will select your device. you can see that all buttons are now active.