Oliver Broad
/
MAX110
Simple readout code for MAX110/111 14-bit ADC
Diff: main.cpp
- Revision:
- 1:46c26c1de51f
- Parent:
- 0:177a872748b8
- Child:
- 2:41e59824652f
diff -r 177a872748b8 -r 46c26c1de51f main.cpp --- a/main.cpp Sun Jul 08 16:49:43 2012 +0000 +++ b/main.cpp Thu Jul 12 07:40:48 2012 +0000 @@ -1,32 +1,143 @@ -#include "mbed.h" -// Use SPI port to write an 8 bit pattern to MIC5821 open collector driver device. -// MBED led1 lights if pattern read back from MIC5821 matches pattern written -// Ordinarily the device would be write-only but loopback may be usefull in testing. -// Cascade output is NOT tristate. Strobe is NOT a CS/SS signal, needs a pulse. -// -// Note that the MIC has a speed limit, 3.3MHz at 5v, mine works OK at 1MHz at 3.3v -// Also input has CMOS thresholds so it may not recognise MBED logic HI when powered -// from 5v so power it from Vout. -// -// Data is clocked in on rising edges so SPI 0,0 or SPI 1,1. - -SPI spi(p5, p6, p7); // mosi, miso, sclk - -DigitalOut strobe(p21); -DigitalOut led(LED1); - -int main() { - spi.format(8,0); - spi.frequency(1000000); - char leds,prev; - leds=0; - while(1) { - prev=spi.write(leds)+1; - led=(prev == leds); - leds++; - strobe=1; - wait_us(1); - strobe=0; - wait(0.5); - } -} +#include "mbed.h" +// Use SPI port to control MAX110/111 Analog to digital converter +// MAX110 is split-supply device, MAX111 single supply. +// Devices require 5v for full performance, may be testable at 3.3v +// +// +// Conversion is SLOW. +// +// Data is clocked in on rising edges so SPI 0,0 or SPI 1,1. + +Serial pc(USBTX, USBRX); // tx, rx + +SPI spi(p5, p6, p7); // mosi, miso, sclk +DigitalOut max_cs(p22); +DigitalIn max_busy(p23); + +//module libmax110 +// +//const +#define max_12bit 0x9200 +#define max_13bit 0x8600 +#define max_14bit 0x8c00 +#define max_div4 0x0100 +#define max_div2 0x0080 +#define max_chan1 0x0000 +#define max_chan2 0x0010 +#define max_full 0x000c +//#define max_gain 0b0000000000001000 +#define max_ofs 0x0004 +// +// procedure max_init(dim mode as word, dim byref port as //byte, dim cs,bf as byte) +// +// mode must be one of max_12bit, max_13bit, max_14bit +// mode may be modified by "or-ing" with max_div2 or max_div4 to set clock divider +// mode may be modified by "or-ing" with max_chan1 or //max_chan2 to set input channel +// mode may be modified by "or-ing" with a calibration type //max_full,max_ofs +// +// port is the port the busy and cs pins are connected to (doesn't have to be PORTC) +// cs is the bit number for the chip select pin +// bf is the bit number for the busy flag pin +// +// function max_read(dim byref data as integer) as boolean +// +// notes returns true if a value was read, immediately restarts converter +// otherwise returns false +// +// result is integer, valid results are from -16384 through 16383. +// values beyond this are overrange +// +// +// MAX110/111 Control register bits +// 15 !NOOP Set to 1 for action, 0 for pass-through +// 14 NU, set to 0 +// 13 NU, set to 0 +// CONV 4,3,2,1 +// 12 CONV4 1 0 0 1 20ms +// 11 CONV3 0 0 1 1 40ms +// 10 CONV2 0 1 1 0 160ms +// 9 CONV1 0 0 0 0 200ms SPECIAL +// SPECIAL = NO INTERNAL GAIN CALIBRATION, FIX IN SOFTWARE +// 8 DV4 1=DIVIDE XCLK BY 4 +// 7 DV2 1=DIVIDE XCLK BY 2 +// +// 6 NU, set to 0 +// 5 NU, set to 0 + +// 4 CHS 0:CHANNEL 1 1:CHANNEL2 +// +// CAL NUL FUNCTION +// 1 1 INTERNAL ZERO (PERFORM FIRST) +// 3 GAIN CAL 1 0 INTERNAL GAIN (NEXT) +// 2 OFFSET NUL 0 1 INPUT NUL (LAST) +// 0 0 NORMAL CONVERT +// 1 PDX POWER DOWN OSC +// 0 PD POWER DOWN ADC +// +// +unsigned int max_mode; +// +void max_setup(unsigned int mmode) +{ + max_mode=mmode; + pc.printf("setup %x\n",max_mode); + max_cs=0; + wait_us(1); + spi.write((max_mode & 0xff00) >> 8); + spi.write(max_mode & 0xff); + max_cs=1; +} + + +int max_read(int *data) +{ + if (max_busy) + { + if (max_mode & 0x0c) + { + max_mode = max_mode - 0x04; + pc.printf("cal %x\r\n",max_mode); + + max_cs=0; + wait_us(1); + spi.write((max_mode & 0xff00) >> 8); + spi.write(max_mode & 0xff); + max_cs=1; + wait(0.1); + return(false); + } + else + { + short t; + max_cs=0; + wait_us(1); + t =spi.write((max_mode & 0xff00) >> 8); + t =spi.write(max_mode & 0xff) | (t<<8); + *data= (int) t; + max_cs=1; + return(true); + } + } + else + { + return(false); + } +} + + +int main() { + spi.format(8,0); + spi.frequency(100000); + pc.printf("Start converter\r\n"); + max_setup(max_14bit | max_full); +// max_busy.mode(PullUp); + while(1) { + int val; + val=0; + if (max_read(&val)) + { + pc.printf("Readout: %d \r",val); +// wait(1); + } + } +}