Newsgroups: comp.robotics
Path: cantaloupe.srv.cs.cmu.edu!das-news.harvard.edu!news2.near.net!MathWorks.Com!europa.eng.gtefsd.com!howland.reston.ans.net!cs.utexas.edu!convex!cnn.exu.ericsson.se!news
From: exujet@exu.ericsson.se (Jerry Ethridge)
Subject: Re: ADC on PIC Questions and Answers
Message-ID: <1994Oct6.230729.13738@exu.ericsson.se>
Sender: news@exu.ericsson.se
Nntp-Posting-Host: s08b22.exu.ericsson.se
Reply-To: exujet@exu.ericsson.se
Organization: Ericsson North America Inc.
References: <Pw9RTc2w165w@sfrsa.com>
Date: Thu, 6 Oct 1994 23:07:29 GMT
X-Disclaimer: This article was posted by a user at Ericsson.
              Any opinions expressed are strictly those of the
              user and not necessarily those of Ericsson.
Lines: 132

In article Pw9RTc2w165w@sfrsa.com, bsmall@sfrsa.com (bsmall) writes:
>Hello everyone. I have a need to collect data with a PIC16C71.
>I took this program off the Parallax BBS (916)624-7101. I 
>ordered the PIC16C71-16/JW-ND from DigiKey 1-800-DigiKey today.
>Ouch $25.96, I thought PIC's were cheap.

--------
The EPROM windowed versions of the chips have really shot up
in price, more so than the OPT versions compared to when
Digi-Key started carrying the PIC chips.

So far, the PIC16C71 is my favorite because of the built in
A/D, the 8 level hardware stack, and none of that page switching
stuff for far calls. It still has page switching used for registers
and context switching for interrupt handling.
---------

> 
>Edited for Internet with some questions from me:
> 
>; PROGRAM: ADC71.SRC
> 
>; Taken from Parallax PIC Application Note: 
>; The PIC16C71 A-to-D Converter
> 
>; July 15, 1993
> 
>; This program demonstrates use of the ADC in a simple circuit 
>; that samples a voltage and flashes an LED at a proportional 
>; rate. The header contains a number of constants representing 
>; setup constants for the ADC control registers. Uncomment the 
>; constant corresponding to the desired ADC setting. 
> 
>; The following constants set the ADC clock source and speed. 
>; Uncomment one. 
>;AD_clk  =  0  ;PIC oscillator period x 2  (<= 1 MHz).
>;AD_clk  =  64 ;PIC oscillator period x 8  (<= 4 MHz).
>;AD_clk  = 128 ;PIC oscillator period x 32 (<= 16 MHz)
>AD_clk   = 192 ;Independent RC oscillator, 2-6 us. 
> 
>; Why would one use the oscillator? Do I lose a pin to set
>; up the RC oscillator?

---------

If you have the databook, on page 2-186 (DS30150C-page38) it
says:

   At low frequencies, the RC oscillator can be selected to
   maintain shorter conversion time. The RC oscillator
   frequency varies considerably with voltage, temperature
   and process parameters (2 us to 6 us period, nominal 4 us)

The decision for choosing the ADC clock source depends on
your sampling frequency and your crystal frequency. 6 us
to 2 us translates to 166.6KHz to 500KHz with a nominal
of 250KHz. Another very important point is that a conversion
takes 10 clock periods and one must insure that the conversion
time is > 2 us.

For AD_clk=0  : Conversion time = 1/crystal freq x 2 x 10.
For AD_clk=64 : Conversion time = 1/crystal freq x 8 x 10.
For AD_clk=128: Conversion time = 1/crystal freq x 32 x 10.

Example:  

If you wanted to sample at 300KHz, this will take 3.33us conversion
time (actually it will require a faster time for you to be able
to actually process the data before it is time to take the next sample).
As you can see, the RC oscillator will (in most cases) not be fast enough
to handle this conversion rate. Therefore, you need to use the other
options.

If you were running with a 16MHz crystal, your clock period would
be 62.5 ns and ten clock periods would add up to .625 us. If the
above AD_clk = 0, then oscillator period would be multiplied by
2 which would give us a total conversion time of 1.25 us.
Since we must ensure that conversion time be >2 us, then AD_clk=0
would be unsatisfactory. If we choose AD_clk=8 that would result 
in a conversion time of 5 us. This is also unsatsfactory because 
we would be limited to a sample rate of 200KHz and our target sample
rate is 300Khz.

By choosing a 10MHz crystal, we will have a conversion time of
100 ns x 2 (AD_clk = 0) x 10 = 2 us. This is the highest frequency
crystal we can choose in order to use the AD_clk=0 option.

I hope this example helps you in understanding the ADC clock
options.

I used this processor to build a recording accelerometer for model
rockets. I sample at 100Hz save to several EEPROMS and use an 
RS232 converter chip to download the data to a PC.

---------- other stuff deleted -----------

> 
>;The application doesn't say what clock frequency to use or what to 
>;expect from the delay loops. I'd actually be more interested in
>;sending an RS232 signal out to the serial port.
> 

------------
As you can see from the above example, the clock frequency and
sampling rates are very important. The PIC16C71 does not have
a dedicated serial port, you must choose a couple of pins and
bit bang a serial port in code. This however is pretty trivial
and does not take up very much processor time (at 10MHz).

BTW, I use the PIC native code. There are only about 33 opcodes
and of those I probably have used only 13 or 14. When writing
a serial routine, timing is important so it is much easier to
keep track of clock cycles. Also, you learn more about the 
processor by thinking in PIC :) One of the first things
I do when learning about a new microcontroller is write a
serial port routine if it does not already have one built in.
This allows me to quickly get a handle on timing cycles and
execution time for different opcodes. Since the PICs are RISC
controllers, it was pretty simple to work out their timing.


Good luck!

---
===============================================================================
Jerry Ethridge		     Richardson, Tx.                 |
ERICSSON NETWORK SYSTEMS                                    /O\
exujet@exu.ericsson.se                            \_______[|(.)|]_______/
                                                    o   ++   O   ++   o
===============================================================================


