Developed during a conversation in the forum. http://www.sfcompiler.co.uk/forum/viewtopic.php?t=18

Swordfish default's the A/D module to the fastest Clock speed and 0 aquisition time, and assumes that the user will be setting the values that are appropriate for the specific conditions. But, what are good numbers to use?

If you use too Slow of a clock, or too much aquisition time, you can slow down the performance of the overall program. And, if you go with too Fast of a clock or not enough aquisition time, you may not get any readings at all, or worse, the wrong readings.

To avoid doing the math, most people will just set the A/D clock to the internal RC oscillator (mode 3), and use around a 50us aquisition time. That pretty much covers any worst case scenario, but it can take as much as 20 times longer than necessary.

MinADtime.bas is a simple module that will automatically set up the ADC for the Minimum clock and aquisition time according to your selected clock frequency and impedance. It's done mainly with constants, so it uses very little code space.

Using it is simple, just set the resistance option, and include the module...

#option AD_RESIST = 2000 
Include "MinADtime.bas" 

Where AD_RESIST is the external impedance of your analog circuit in OHMs. If omitted, it will default to 10k

You don't need to call any Sub's. It initializes it for you. Although you can call the SetMinADtime() Sub at any time if needed.

Here's the module...

Module MinADtime 

Include "ADC.bas" 

{ You can include this option in your main program 
  to indicate the external impedance in OHMs 
    #option AD_RESIST = 2000 
//---- Set Default AD_RESIST if needed ------------------------------ 
#if IsOption(AD_RESIST) 
  Const ADimpedance  = AD_RESIST 
  Const ADimpedance  = 10000 
Const  MinAD_AQ As Byte = (((ADimpedance +1000)*(120*7))/1000000)+1 

//---- Find Minimum A/D oscillator requirements --------------------- 
#variable  ADoscRaw = 64/(40.0/_clock) 
#if ADoscRaw > 64 
     Const MinAD_OSC = FRC 
#elseIf ADoscRaw > 32 
     Const MinAD_OSC = FOSC_64 
#elseif ADoscRaw > 16 
     Const MinAD_OSC = FOSC_32 
#elseif ADoscRaw > 8 
     Const MinAD_OSC = FOSC_16 
#elseif ADoscRaw > 4 
     Const MinAD_OSC = FOSC_8 
#elseif ADoscRaw > 2 
     Const MinAD_OSC = FOSC_4 
     Const MinAD_OSC = FOSC_2 

Public Sub SetMinADtime() 
End Sub 

Public Sub SetMinADtime(Mult As Byte) 
     SetAcqTime(MinAD_AQ * Mult) 
End Sub 


For instance, at 40mhz it will set the A/D clock to FOSC_64

And with AD_RESIST = 2000 it will set the aquisition time to 3uS.

Or, if you prefer to have a little safety factor thrown in, you can add a "multiplier" to the SetMinADtime() Sub to increase the aquisition time.


Would give an aquisition time of 6uS with the same numbers above.

Hope you can use it,

   Darrel Taylor