Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

PIC18F Crystal + delay function

Status
Not open for further replies.

adrianvon

Member
Hi all,

Can someone help me choose the value for the Xtal to be used with the PIC18F4550?? Is the Xtal value that important?

Also, can someone explain how the delay routine below works?

Code:
void Slow_Delay(void)
{
  int i, j;
  for(i=0;i<4000;i++)
  {
  for(j=0;j<2;j++)
  {
  }
  }
}

Thanks in advance.
 
Choosing a crystal frequency will very much depend on what you are doing with the device. You need the oscillator to be running fast enough so that you can comfortably perform whatever you are trying to achieve within your time frame. For serial communication for example, you will want something fast enough to be able to process what you receive before being overrun by another reception etc. If power saving is critical in your design, you may want to choose a lower frequency etc.

I'm no expert in C, I normally write in assembler, but my reading of the delay routine is that it is a nested for loop, it has an inner and an outer loop. The outer loop is counting up the variable i, the inner loop is counting the variable j.

It works something like this:
The function is declared (Slow_Delay) and does not pass or expect any return variables (void)
The variables i and j are defined as integers
The value of zero is placed once in the variable i (i=0), the condition is then tested for true/ false (i<4000) each pass through the loop. Providing it remains true, it is incremented by 1 each time through the loop. The value of zero is placed once into variable j (j=0) and it too is tested for true/ false condition (j<2). Providing it remains true it will increment the variable j by 1 each pass through the loop. So the function is effectively counting 4000, twice, providing you with the delay.

I'm sure one of the guys that regularly use C will chime in with a better explanation, but this should get you going :)
 
I use 4Mhz, simply because it makes delays etc so easy. There may be a ton of reasons to use a different frequency but so far I've never strayed except for experimentation purposes. That chip has an internal oscillator so you don't have to use a crystal with it.
Aaron
I also write in assembler
 
the pic PIC18F4550 has USB if your using that then i think it's 20Mghz crystal you need
 
the pic PIC18F4550 has USB if your using that then i think it's 20Mghz crystal you need
No!! Not quite... All crystal frequencies "Except 24Mhz" can yield 48Mhz for the USB.. Page 30 and 31 in the datasheet.. They are all derived from 4Mhz.... 4Mhz * 12 ( HSPLL) = 48Mhz...
 
No!! Not quite... All crystal frequencies "Except 24Mhz" can yield 48Mhz for the USB.. Page 30 and 31 in the datasheet.. They are all derived from 4Mhz.... 4Mhz * 12 ( HSPLL) = 48Mhz...

yeah sorry mr Ian i checked my lab book from when i tried to mess with USB and i didnt read all my notes! very sorry OP i should of checked my info
 
Hi again. Thanks for your help.

Another quick question. Which is the best type of communication, USB or UART ?? I want to interface Visual Studio in C# with the PIC18F4550.
 
depends what speed you need? and how good your coding skills are? for simple go with uart for speed go with USB. if you want to try USB have a look at the pyro electro forum there is a simple (ish) tutorial on there for USB with some sample c# code
 
personally i like to use a usb to serial converter and use the uart, c# is said to be easy to connect to serial comms but i havnt tried myself yet but it is on the list :D
 
Again, I would say that would very much depend on your application. If your project is to be remotely located, for example a weather station type thing, you may want to go with using the usart because of the longer media supported and lower communication bit rate. If however, it's a widget sitting on the desk beside your laptop, USB would be just as usable, faster, but a bit more involved. One nice thing about using the usart and doing straight rs232, is that there are loads of 3rd party adapters to different physical media available, including USB, Zigbee wireless etc which keeps your options open for easily achievable future upgrades etc :)
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top