To use this file copy and paste this:    // #URL-lib "http://pin1.org/forthlib/flb/General/AtoD.flb"   into BV Terminal 3 or here to download.

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // A to D code for using the built in // a to d converters, provides some simple routines for using // the LCP ARM ADC facility. This only covers channel 0 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

// REQUIRES: // #URL-lib "http://pin1.org/forthlib/flb/General/pinsel.flb" // #URL-lib "http://pin1.org/forthlib/flb/General/soft1.flb"

// CONSTANTS: // ADC Registers &e0034000 constant AD0CR &e0034004 constant AD0GDR &e0034028 constant ADDR6 // division constant for A to D, creates a constant // cdiv based on PCLK pclk 3500000 / constant CDIV


Full Contents of File

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
// A to D code for using the built in
// a to d converters, provides some simple routines for using
// the LCP ARM ADC facility. This only covers channel 0
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

// REQUIRES:
// #URL-lib "http://pin1.org/forthlib/flb/General/pinsel.flb"
// #URL-lib "http://pin1.org/forthlib/flb/General/soft1.flb"

// CONSTANTS:
// ADC Registers
&e0034000  constant  AD0CR
&e0034004  constant  AD0GDR
&e0034028  constant  ADDR6 
// division constant for A to D, creates a constant
// cdiv based on PCLK
pclk  3500000  / constant CDIV

// AD pins on the LPC 3212 are:
// AD0 = p0.27: 27 1 PINSELECT
// AD1 = p0.28: 28 1 PINSELECT
// AD2 = p0.29: ETC.
// AD3 = p0.30
// AD4 = p0.25
// AD5 = p0.26
// AD6 = p0.4 4 3 PINSELECT
// AD7 = p0.5 5 3 PINSELECT
// NOTE channels are always connected, must use
// pinselect first. to select function 1:
// e.g. 27 1 pinselect

// *******************************************************
// History:
// Mar 2007 Adjustments for ANSI and new board
// *******************************************************

// get 10 bit value when conversion complete
: <0>getad
        begin
                AD0GDR  @
                &80000000  and
        until
        AD0GDR  @  6  rshift  // move
        &3ff  and                        // mask relevant
;

// *** Use pinselect first ***
// *** To set up required channels ****
// e.g. 27 1 pinselect // sets AD0
// select channel to convert 0..7
// stays until conversion complete
// ( channel --- )
: <0>AtoD
        1  swap  lshift      // move to correct bit
        CDIV  8  lshift  +  // shift clock and add
        &01200000              // enable + start convertor
        +
        AD0CR  !
        getad
;       

// Gets the voltage on the selected AtoD pin
// the voltage range is 0.00 to 3.3 and Vref is 3.3V
// 3.3V / 1023 is 0.003226V per unit
// the calculation is (3.3/1023)*AtoD or
// 0.0032223 * AtoD
// ( channel --- mV)
: <0>Vin@
        AtoD                // get channel value
        3226  *            // value in uV
        1000  u/mod // get mV value
        swap  500  >  if  1+  then  // round up
;