PIC18F4550 Tutorial: Blinking an LED

Blinking an led using pic18f4550 , Chapter 3 , pic18f4550 programming tutorial

 

 

PIC18F4550 Tutorial: Blinking an LED | Chapter 3


Welcome to my third chapter of this programming tutorial for pic18f4550. 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.

This is the first method of programming a pic18f4550. For the sake of example, we are going to take a simple case of blinking an LED,  where we are going to blink two led's on pin RB0 and RB1. In coming chapters we are going to see how to blink the same thing in different ways. Our primary target is to learn various methodologies of Programming same thing in different ways to develop a perfect coding habit. 

Choose the coding method at your own discretion which suits perfect for your project requirement. Remember, the way you code, can save a lot of space.

First method is a direct and most common approach, where we are going to do all coding in one project file. MAIN.C. Here we are going to use Mplab ide and c18 compiler. It can be also done with Mplab X IDE , where there is no need of C18 compiler as the X8 compiler already includes all the necessary compiler files. However for this tutorial we will stick to Mplab IDE and C18.

Schematic

pic18f4550 schematic diagram to blink led

As mentioned before we are going to blink two led's on port B , RB0 and RB1.  Here is a sample schematic. The code can be easily modified to blink other led as well.

The input voltage 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.

 

The circuit can be easily built on a breadboard in no time.

 

Let's Start with Coding

-Start MPLAB IDE and create a New Project as explained in the previous post for pic18f4550.

-Add a new file and save it as MAIN.C and save it in the project folder (Don’t forget to Check mark on “ADD to Project file”). You can use any name you wish..

- Now we have a blank empty project with a C file HOME.C

-Then start typing the source code as below.


Some of the sections in the code below are click able to jump to the code explanation.

Source Code:





#include<p18f4550.h>                                                // Include Header for PIC18f455          
 

       /* ******COMPILER DIRECTIVES FOR CHIP CONFIGURATION BITS ***   */
#pragma config PLLDIV = 5 , CPUDIV = OSC1_PLL2 , USBDIV = 2    // You can write this way
// OR
#pragma config FOSC = INTOSCIO_EC
#pragma config FCMEN = OFF                                 // OR this way
#pragma config BORV = 3
#pragma config WDT = OFF
#pragma config CPB = OFF
#pragma config CPD = OFF

/*  ***************  TIMER *************** */
       void delayzz(void)
        {              int i, j;
                        for(i=0;i<5000;i++)
                {
                          for(j=0;j<2;j++)
                      {           /* Well its Just a Timer */            }    }   }
                           


                /* ****************** MAIN ****************** */

      void main(void)
         {
                        TRISB = 0 ;                  // PORT B Setting: Set all the pins in port B to Output.
     
           while(1)  
                            {
                                LATBbits.LATB0 = 1;   // RB-1 to High  
                                LATBbits.LATB1 = 1;   // RB-1 to High

                                 delayzz();

                                LATBbits.LATB0 = 0;    // RB-0 to LOW
                                LATBbits.LATB1 = 0;    // RB-0 to LOW

                                 delayzz();


                              }
        }

/* THE END */

 

 

Download the code main.c or Download the entire Source code as Mplab Project.

Next step is to compile the code.

After the project is built the output files will be dumped into the same project folder, if a separate output folder is not defined for saving all the compiled files.

Now you can use your Programmer to burn the output .hex into the microcontroller.

Code Flow

-Define the header files for pic18f4550.
-Set the Microcontroller configuration settings with “compiler directives”.
-Define PORT settings:- The pins you want to use for blinking. Set the pin to output.
-Then set the timer:- For the time delay in-between the blinks.
-Define Pins to set as ON or OFF with a delay in-between.
-And finally make a loop where the main code will keep executing.
 

 

#include


Like most of the programming language first stage is to include/Import header files that we need to define. We are going to code a pic18f4550 hence we are going to include <p18f4550> header that contains all the respective settings and definitions of the microcontroller. 

Next stage is the pic18f4550 Configuration Bit settings. We need to set the chip configuration, settings for the pic18f4550, which is going to define the Configuration bits for the pic18f4550. For example: whether to use an internal or external oscillator, whether to keep watchdog timer on or off etc. All such settings are handled in Configuration bits setting in the beginning of the code block. If we don’t define the values then the microcontroller will take the default values which might lead to some trouble.


These configuration bits can be assigned with a Compiler directive “#pragma config

for example
             #pragma config WDT = ON // watchdog timer on.

For each Configuration Bits, you can write a new line or you can write multiple Chip config bit setting on single #pragma config Directive.

             #pragma config FCMEN = OFF  // Each line with individual setting
             #pragma config IESO = OFFF                            

OR
             #pragma config FCMEN = OFF , IESO = OFFF  //Single line with multiple settings.

You can find the all the Configuration bit settings in the Mplab notes.

-On the top menu on MPLAB IDE click on Help  >>  Topics..  >> PIC18F Config Settings 

-From the left pane go to PIC18F4xxx Configuration Settings and then select PIC18F4550 . It would list you all the configuration settings for pic18f4550.

 

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 pins 7,6,5,4 as input and 3,2,1,0 as Output.

Set pins according to the binary equivalent of the HEX value (0xF0), which is 11110000, Hence first 4 bits from RB0 would be set to output and rest of the pins starting from RB4 would become input.

PORTS

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 registers, TRISx register is one of such register to access the corresponding port.

 

TRISB

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 pins in Port B  11110000.

Instead of TRISB = 0xF0 ; you can also write

TRISB = 0;


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 delayzz() 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 LATB Register

                              LATBbits.LATB0 = 1;   // RB-0 to High 
                              LATBbits.LATB1 = 1;   // RB-1 to High
                                 delayzz(); // calling the delay function.
                              LATBbits.LATB0 = 0;    // RB-0 to LOW
                              LATBbits.LATB1 = 0;    // RB-1 to LOW

The code can be easily modified to blink alternatively to emit a dipping effect.
               
                             LATBbits.LATB0 = 1;   // RB-0 to High 
                             LATBbits.LATB1 = 0;   // RB-1 to LOW
                                 delayzz(); // calling the delay function.
                             LATBbits.LATB0 = 0;    // RB-0 to LOW
                             LATBbits.LATB1 = 1;    // RB-1 to High
                                 delayzz(); // calling the delay function.

For more information Visit my post on PIC18F4550

 

Download the Source code or entire project file below

MAIN.C

Mplab IDE - Project .zip

The project files above are compiled with traditional Mplab IDE and C18 compiler.

However now microchip have also introduced a free and very advanced version of IDE which is Mplab X IDE. It is available for both windows and Linux machine. Apart from better looks and easy navigation, One of the most shocking thing which I noticed after coding this pic18f4550 with Mplab X IDE is that, the compiled output (.hex files) is way smaller then that of the regular Mplab IDE hex.

The same source code in this tutorial for blinking an led can be created on Mplab X with X C8 compiler for Linux or windows with no difficulty.

Download the Mplab X IDE version of this same blinking project which is coded a Linux Ubuntu machine.

MPLAB X - Project.zip

In my coming tutorials i will shed some more light on Mplab X ide and XC8 compiler.

NEXT CHAPTER4: Tutorial 2 – Method 2- Blinking led with pic18f4550 – header files

Previous Next
 

Thanks for Reading
Ron


PIC18F4550 Programming tutorial chapter 4 and method 2 for programming