PIC18F4550 - using external oscillator(crystal) and delay calculations

Status
Not open for further replies.

johnny91

New Member
hi,
i am using PIC18F4550, with crystal of 8 MHz.
i am not able to understand how use configuration bits for this and how to calculate delay value, delay function if i want to make led blink for one second
i am using MPLAB IDE and C18 Compiler.
please help.
 
Without looking I expect the fuse setting is for the crystal is HS. You can get the C18 setting from the MPLAB help menu. For what they mean read the processor data sheet.

#pragma config OSC=HS, WDT=OFF, LVP=OFF, DEBUG=ON

You are using an 8 MHz crystal. The process runs at 1/4 the crystat so you are at 2 MHz.

The period is 1/freq or 1/2,000,000 or 0.0000005 seconds. Each instruction takes 0.0000005 seconds. An instruction time is also know as a Tcy.

In the header file C:\MCC18\h\delays.h you will find various functions for delaying based on the number of Tcy's required. Be sure to #include <delays.h>

You can use the stopwatch in MPLAB's simulator to verify your delay times.
 
The 18F4550 has a PLL that needs to be supplied with a 4MHz signal. So with an 8MHz crystal you need to set the PLL divide value to 2. This will give you a 96MHz signal out of the PLL and so you need to divide this by (at least) 2 to get 48MHz.
Using
Code:
#pragma config WDT = OFF, FOSC = HSPLL_HS, LVP = OFF , DEBUG = ON, CPUDIV = OSC1_PLL2, PLLDIV = 2
Will give you a 48MHz clock speed so calculate your delays using a 12MHz instruction speed.

Mike.
 
Thanks 3v0 and Mike.
I appreciate your help.
I tried to write program in C18, just to check different delays.
But it gives error - something wrong with "int i and int j".
the code is as below :


#include <p18f4550.h>
#include <delays.h>
#include <stdlib.h>

#pragma config FOSC = INTOSCIO_EC //Internal oscillator, port function on RA6, EC used by USB
//#pragma config WDT = OFF, FOSC = HSPLL_HS, LVP = OFF , DEBUG = ON, CPUDIV = OSC1_PLL2, PLLDIV = 2
#pragma config WDT = OFF //Disable watchdog timer

void main()
{
ADCON1 = 0x0F; // Configure all ports with analog function as digital
CMCON = 7; // Disable comparators

TRISB = 0;
PORTB = 0;
int i;
int j;

for(i=0;i<10;i++)
{
PORTB = ~PORTB;
delay(250);
delay(250);
delay(250);
delay(250);
delay(250);
}
for(i=0;i<10;i++)
{
PORTB = ~PORTB;
delay(700);
delay(700);
delay(700);
delay(700);
delay(700);
}
for(i=0;i<10;i++)
{
PORTB = ~PORTB;
_delay_ms(250);
_delay_ms(250);
_delay_ms(250);
_delay_ms(250);
}
for(i=0;i<10;i++)
{
PORTB = ~PORTB;
for(j=0;j<1000;j++)
{
PORTB = ~PORTB;
_delay_us(250);
_delay_us(250);
_delay_us(250);
_delay_us(250);
}
}
for(i=0;i<10;i++)
{
PORTB = ~PORTB;
DelayMs(250);
DelayMs(250);
DelayMs(250);
DelayMs(250);
}
for(i=0;i<10;i++)
{
PORTB = ~PORTB;
for(j=0;j<1000;j++)
{
PORTB = ~PORTB;
DelayUs(250);
DelayUs(250);
DelayUs(250);
DelayUs(250);
}
}
for(i=0;i<10;i++)
{
PORTB = ~PORTB;
for(j=0;j<1000;j++)
{
PORTB = ~PORTB;
Delay10TCYx(100);
}
}
for(i=0;i<10;i++)
{
PORTB = ~PORTB;
for(j=0;j<10000;j++)
{
PORTB = ~PORTB;
Delay10TCYx(10);
}
}
for(i=0;i<10;i++)
{
PORTB = ~PORTB;
for(j=0;j<100;j++)
{
PORTB = ~PORTB;
Delay10TCYx(1000);
}
}
for(i=0;i<10;i++)
{
PORTB = ~PORTB;
for(j=0;j<100;j++)
{
PORTB = ~PORTB;
Delay100TCYx(100);
}
}
for(i=0;i<10;i++)
{
PORTB = ~PORTB;
for(j=0;j<100;j++)
{
PORTB = ~PORTB;
Delay1KTCYx(10);
}
}
for(i=0;i<10;i++)
{
PORTB = ~PORTB;
for(j=0;j<10;j++)
{
PORTB = ~PORTB;
Delay10KTCYx(10);
}
}
}


What is wrong with the code ?
Please Help.
Sanjay.
 
The problem is with where you declared i and j. Change the code to put the variable definitions first in the code block. Ahead of any executable statements.

Code:
...
void main()
{
    int i;
    int j;
    ADCON1 = 0x0F;                         // Configure all ports with analog function as digital
    CMCON  = 7;                            // Disable comparators
    
    TRISB = 0;
    PORTB = 0;
    
...
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…