On Sat, Feb 24, 2007 at 06:43:42AM +0100, Rikard Bosnjakovic wrote: > On 2/23/07, Harold Hallikainen wrote: > > > Yes, Fosc is divided by 4. So, if you have a 4MHz oscillator, the PIC runs > > at 1,000 instructions per second (some instructions take twice as long, > > like those that modify PC). > > I read about this (and some other crystal-based text) in my PIC-book, > but what I cannot understand is why a 32768Hz-crystal is magical > enough to use for clock-applications. Dividing 32768 with 4 gives me > 8192, and that's a value that I certainly don't connect with anything > that's got to do with timing. It's magical because it's a power of 2. You divide it enough and you get a nice round number to work with without having to do anything special. Let me talk about my clock application. I applied a 32768 Hz watch crystal to timer 1 of a 16F877, which is a 16 bit timer that counts up to 65536. If you divide 32768 Hz/65536 you get exactly 0.5 Hz. Note that Hz is the inverse of seconds, so if you invert 0.5 Hz you get 2 seconds. And if the crystal is exactly on time at 32768 Hz then that time would be exactly 2 seconds. Now the other point is that by using timer 1, I can decouple that 2 Hz clock from the speed of the controller, which can now use a separate clock. It's useful to be able to compute things faster without having to worry about counting oddball frequencies in order to get exactly one second. That's the magic of the 32768 Hz crystal. > On the other hand, I haven't yet got a good grip of why to use > crystals. Supposedly they are a stable time base. That's the theory. I can tell you that in practice crystal timebases drift especially based on temperature. > To time the controller, sure, but...how? I have read that > an xtal oscillates due to some quartz-magic in it, but that's all. No > clue of how it works. You can treat it as a black box in this application. The PIC has all of the necessary circuitry to make the crystal do it's job, with the exception of the crystal capacitors required to get the circuit to oscillate. > >This is probably a good incitament to start this day with a visit to Wikipedia. Don't bite off more than you can chew here. All you need to know right now is that the crystal provides a stable time base. Over researching minute details is a project killer. BTW you never did tell me your application, which I think I asked about in a previous post. All I've seen so far is you're using a 16F84 to build a clock. I have a page that describes why the 16F84 may not be your best starting point. You can find it here: http://www.finitesite.com/d3jsys/16F88.html The peripheral packages (including the multiple timers which you should use for this clock project) extends across many of the new PIC parts. The 16F84 will limit you in a lot of ways. I'm starting to test what I think should be the gold standard for the PIC 16F family: the 16F886 and 16F887. Full peripheral packages, nanowatt features which means a decent internal oscillator up to 8 Mhz and the ability to add up to 3 more I/O pins (well 2 I/O and one input) by repurposing the MCLR and CLK pins, and bigger hobbyist friendly DIP packages (28 and 40 pins). Also for the bootloader crowd (of which I am a member), they are self programmable. I reiterate my thought that a newbie should start with the biggest, best package and work their way down instead of the other way around. By doing so, when you need a feature (more timers, more I/O pins, PWM, ADC, serial, etc) then it's already there for you to use instead of having to hunt up another part with the feature you need. Hobbyist generally build a series of one off projects, so the bigger packages and the slightly higher costs are small factors in the usage decision. The upshot is that if you start with the 16F84, then you'll learn from tutorials and webpages all of the twisted, convoluted ways to program stuff around the part's limitations. Then you'll start thinking that's the normal right way to go about handling serial, multiple timers, PWM and the like. However, if you start with the best, right part from the start, then you'll start designing with best practices from the start. One last point. You need to download, print, and read the 16F midrange manual. You can find it here: http://ww1.microchip.com/downloads/en/DeviceDoc/33023a.pdf It'll be a much better use of your study time than trying to understand the ins and out of Colpitts and Pierce crystal oscillators. BAJ -- http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist