Blink Led with Pic18f2550,PIC18F2455,PIC18F4455,Pic18F4550 Microcontroller….20Mhz Crystal frequency…

This is a simple project aims to start with microchip pic 18 series USB built in microcontrollers. Before going to use USB feature of pic microcontroller i thought to first start with blink led program. Simple Blink led will introduce us with the configuration bits of pic microcontroller. It took me 2 days to study about the architecture of Pic18f2550 and set the configuration bits. After many trials i finally set configuration bits with correct bits. Setting correct configuration bits is not an easy task. This configuration learning will help us in future USB project. 

Pic microcontroller code Mplab ide

I wrote the code in both MPLAB IDE and in MPLABX IDE using the same C18 compiler. I just only want to show you the difference between the two ide’s used to write pic microcontroller code. 

In simple MPLAB IDE I am manually setting the configuration bits in the configuration window and after compiling the code with c18 compiler i am exporting the hex file containing the configuration bit code. Note that the output compiled code hex file in MPLAB ide does not contains the config info. For proper config bits hex file, you have to import it from the file>Import-Hex.

The code in MPLABX contains the fuses bits included in it so the hex file generated in this case contains the config bit info in it. We don’t need to export file from any other window feature.

Pic microcontroller code Mplab ide

Pic18 microcontroller blink led – Project circuit

I am blinking led connected to Port-A Pin#0 of Pic18f2550 microcontroller. Actually i am toggling the whole Port-A of pic microcontroller. In circuit i connected the Led to Pin#0 of Port-A of pic microcontroller. You can also connect a series string of led’s with whole Port-A pins of Pic18f2550 microcontroller. They all will blink at the same time. An external 20 Mhz crystal is used to provide a stable clock to Pic18f2550 micrococntroller. Circuit diagram of the blink led with pic18 microcontroller is given below.
 
Pic18f2550 blink Led Circuit Diagram

Configuration bits settings for pic microcontroller in mplab ide

Setting correct configuration bits is a difficult task i manged to set them correctly by going through the datasheet of pic18f2550 microcontroller. Each crystal has its own settings so be careful if you are not using 20 Mhz crystal and you are using this code. Your microcontroller circuit wont work if you connect a crystal other than 20 Mhz with the same code downloaded in your pic microcontroller given below.

​You have to set PLLDIV, CPUDIV and USBPLL configuration bits correctly which matches to the crystal frequency you are using. Configuration settings are given below.
 
Configuration bits settings for pic microcontroller in mplab ide

Pic18 microcontroller mplab ide code

Code is written in MpLab IDE and compiled using high tec c compiler. I wrote code to manipulate the Port-A bits using individual pin control or full Port control. I tested it with both individual controlling each pin and controlling the whole port, its works fine. I commented the individual pin control code. If some one want to control individual pin then un comment the commented lines and comment the ones that are previously not commented. Do not comment the delay function.

TRISAbits.XXX command is used to initialize the individual pins of pic microcontroller in mplab ide as input or output. PORTAbits.XXX command is used to write to individual microcontroller pins in mplab ide. We can directly initialize the whole port by accessing the whole TRISA register simply by TRISA=input/output. Same as we can write to whole port at once by PORTA=high/low. 

#include<stdio.h>
#include”p18f2550.h”
#define _XTAL_FREQ 20e6
void delay();
void delay(){
unsigned int i=0,j=0;
for(i=0;i< 20;i++)
for(j=0;j< 5000;j++);
}

void main()
{
/*TRISAbits.RA0=0; //Port-B as Output
TRISAbits.RA1=0; //Port-B as Output
TRISAbits.RA2=0; //Port-B as Output
TRISAbits.RA3=0; //Port-B as Output
TRISAbits.RA4=0; //Port-B as Output*/
TRISA=0b00000000;

while(1){
/*PORTAbits.RA0=1;
PORTAbits.RA1=1;
PORTAbits.RA2=1;
PORTAbits.RA3=1;
PORTAbits.RA4=1;*/
PORTA=0xFF;
//PORTA=0x00;
delay();
/*PORTAbits.RA0=0;
PORTAbits.RA1=0;
PORTAbits.RA2=0;
PORTAbits.RA3=0;
PORTAbits.RA4=0;*/
PORTA=0x00;
delay();
//PORTA=0xFF;
}

}

Pic microcontroller code in mplabx ide

Below code is written and compiled in mplabx ide. Note the pic microcontroller configuration bits are defined in the code. If you match the configuration settings of both the mplab and mplabx ide you will find no difference. For both the ide’s same header files are imported with slight difference in syntax. One has ts enclosed in double quotes and other in parenthesis. If you open the header files back in the installation directory of each ide they are also same their. In mplabx ide code i am writing to whole port. I did not included the individual bit access code in the below code.
 

/*
* File: blinky.c
* Author: Usman Ali Butt
* Property: microcontroller-project.com
* Created on 28 May, 2016, 12:17 PM
*/

//#include <stdio.h>
//#include <stdlib.h>
#include <p18f2550.h>
#define _XTAL_FREQ 20000000
void delay();

// CONFIG1L
#pragma config PLLDIV = 5 // PLL Prescaler Selection bits (Divide by 5 (20 MHz oscillator input))
#pragma config CPUDIV = OSC1_PLL2// System Clock Postscaler Selection bits ([Primary Oscillator Src: /1][96 MHz PLL Src: /2])
#pragma config USBDIV = 2 // USB Clock Selection bit (used in Full-Speed USB mode only; UCFG:FSEN = 1) (USB clock source comes from the 96 MHz PLL divided by 2)

// CONFIG1H
#pragma config FOSC = HS // Oscillator Selection bits (HS oscillator (HS))
#pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor disabled)
#pragma config IESO = OFF // Internal/External Oscillator Switchover bit (Oscillator Switchover mode disabled)

// CONFIG2L
#pragma config PWRT = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOR = ON // Brown-out Reset Enable bits (Brown-out Reset enabled in hardware only (SBOREN is disabled))
#pragma config BORV = 3 // Brown-out Reset Voltage bits (Minimum setting 2.05V)
#pragma config VREGEN = OFF // USB Voltage Regulator Enable bit (USB voltage regulator disabled)

// CONFIG2H
#pragma config WDT = OFF // Watchdog Timer Enable bit (WDT disabled (control is placed on the SWDTEN bit))
#pragma config WDTPS = 32768 // Watchdog Timer Postscale Select bits (1:32768)

// CONFIG3H
#pragma config CCP2MX = ON // CCP2 MUX bit (CCP2 input/output is multiplexed with RC1)
#pragma config PBADEN = OFF // PORTB A/D Enable bit (PORTB<4:0> pins are configured as digital I/O on Reset)
#pragma config LPT1OSC = OFF // Low-Power Timer 1 Oscillator Enable bit (Timer1 configured for higher power operation)
#pragma config MCLRE = ON // MCLR Pin Enable bit (MCLR pin enabled; RE3 input pin disabled)

// CONFIG4L
#pragma config STVREN = OFF // Stack Full/Underflow Reset Enable bit (Stack full/underflow will not cause Reset)
#pragma config LVP = OFF // Single-Supply ICSP Enable bit (Single-Supply ICSP disabled)
#pragma config XINST = OFF // Extended Instruction Set Enable bit (Instruction set extension and Indexed Addressing mode disabled (Legacy mode))

// CONFIG5L
#pragma config CP0 = OFF // Code Protection bit (Block 0 (000800-001FFFh) is not code-protected)
#pragma config CP1 = OFF // Code Protection bit (Block 1 (002000-003FFFh) is not code-protected)
#pragma config CP2 = OFF // Code Protection bit (Block 2 (004000-005FFFh) is not code-protected)
#pragma config CP3 = OFF // Code Protection bit (Block 3 (006000-007FFFh) is not code-protected)

// CONFIG5H
#pragma config CPB = OFF // Boot Block Code Protection bit (Boot block (000000-0007FFh) is not code-protected)
#pragma config CPD = OFF // Data EEPROM Code Protection bit (Data EEPROM is not code-protected)

// CONFIG6L
#pragma config WRT0 = OFF // Write Protection bit (Block 0 (000800-001FFFh) is not write-protected)
#pragma config WRT1 = OFF // Write Protection bit (Block 1 (002000-003FFFh) is not write-protected)
#pragma config WRT2 = OFF // Write Protection bit (Block 2 (004000-005FFFh) is not write-protected)
#pragma config WRT3 = OFF // Write Protection bit (Block 3 (006000-007FFFh) is not write-protected)

// CONFIG6H
#pragma config WRTC = OFF // Configuration Register Write Protection bit (Configuration registers (300000-3000FFh) are not write-protected)
#pragma config WRTB = OFF // Boot Block Write Protection bit (Boot block (000000-0007FFh) is not write-protected)
#pragma config WRTD = OFF // Data EEPROM Write Protection bit (Data EEPROM is not write-protected)

// CONFIG7L
#pragma config EBTR0 = OFF // Table Read Protection bit (Block 0 (000800-001FFFh) is not protected from table reads executed in other blocks)
#pragma config EBTR1 = OFF // Table Read Protection bit (Block 1 (002000-003FFFh) is not protected from table reads executed in other blocks)
#pragma config EBTR2 = OFF // Table Read Protection bit (Block 2 (004000-005FFFh) is not protected from table reads executed in other blocks)
#pragma config EBTR3 = OFF // Table Read Protection bit (Block 3 (006000-007FFFh) is not protected from table reads executed in other blocks)

// CONFIG7H
#pragma config EBTRB = OFF // Boot Block Table Read Protection bit (Boot block (000000-0007FFh) is not protected from table reads executed in other blocks)

/*
*
*/
void main(void) {
TRISA=0x00; //Pic microcontroller Port-A as OUTPUT

while(1){
PORTA=0x00;
delay();
PORTA=0xFF;
delay();
}
}
void delay(){
int i=0,j=0;

for (i=0;i<=5000;i++)
for(j=0;j<=5;j++);

}

I tested both codes with pic micrococntroller. Both works fine. The only difference is in delay function. Mplab code delay is larger than the mplabx ide delay. I intentionally made difference to check if both the codes delay functions are working fine with the same 20 Mhz crystal.

Source: Blink Led with Pic18f2550,PIC18F2455,PIC18F4455,Pic18F4550 Microcontroller….20Mhz Crystal frequency…

About The Author

Muhammad Bilal

I am a highly skilled and motivated individual with a Master's degree in Computer Science. I have extensive experience in technical writing and a deep understanding of SEO practices.