Jeroen Lodder / Mbed 2 deprecated max11626_example

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 
00003 DigitalOut myled(LED1);
00004 DigitalOut convst(p18);     // Conversion Start (active low)
00005 DigitalOut cs(p16);         // Chip Select (active low)
00006 DigitalIn  eoc(p20);        // End of Conversion (active low)
00007 SPI spi(p11, p12, p13);     // mosi, miso, sclk
00008 Serial pc(USBTX, USBRX);    // tx, rx
00009 
00010 #define AIN0  0u
00011 #define AIN1  1u
00012 #define AIN2  2u
00013 #define AIN3  3u
00014 #define AIN4  4u
00015 #define AIN5  5u
00016 #define AIN6  6u
00017 #define AIN7  7u
00018 #define AIN8  8u
00019 #define AIN9  9u
00020 #define AIN10 10u
00021 #define AIN11 11u
00022 #define AIN12 12u
00023 #define AIN13 13u
00024 #define AIN14 14u
00025 #define AIN15 15u
00026 
00027 #define SCAN_TO_N               0u // Scans channels 0 through N.
00028 #define SCAN_N_TO               1u // Scans channels N through the highest numbered channel.
00029 #define SCAN_N_AVG              2u // Scans channel N repeatedly. The averaging register sets the number of results.
00030 #define SCAN_OFF                3u // No scan. Converts channel N once only
00031 
00032 #define CLKSEL_INT_INT_CNVST    0u // Internal clock, internal aqcuisition, convst
00033 #define CLKSEL_INT_EXT_CNVST    1u // Internal clock, external aqcuisition convst, convst
00034 #define CLKSEL_INT_INT_ADC      2u // Internal clock, internal aqcuisition, extra channel
00035 #define CLKSEL_EXT_EXT_ADC      3u // External clock, external aqcuisition sclk, extra channel
00036 
00037 #define REFSEL_INT_SLEEP        0u // Reference off after scan; need wake-up delay
00038 #define REFSEL_EXT              1u // Reference off; no wake-up delay.
00039 #define REFSEL_INT_NOSLEEP      2u // Reference always on; no wake-up delay.
00040 #define REFSEL_RESERVED         3u // Reserved. Do not use.
00041 
00042 #define NAVG_4                  0u // Performs four conversions and returns the average for each requested result.
00043 #define NAVG_8                  1u // Performs eight conversions and returns the average for each requested result.
00044 #define NAVG_16                 2u // Performs 16 conversions and returns the average for each requested result.
00045 #define NAVG_32                 3u // Performs 32 conversions and returns the average for each requested result.
00046                             
00047 #define NSCAN_4                 0u // Scans channel N and returns four results.
00048 #define NSCAN_8                 1u // Scans channel N and returns eight results.
00049 #define NSCAN_12                2u // Scans channel N and returns 12 results.
00050 #define NSCAN_16                3u // Scans channel N and returns 16 results.
00051 
00052 uint8_t compile_reset(bool fifo_only){
00053     if(fifo_only)
00054         return 0x18;
00055     else
00056         return 0x10;
00057 }
00058 
00059 uint8_t compile_conversion(uint8_t ch, uint8_t scan){
00060     return 0x80 | ((ch&0xF)<<3)|((scan&0x3)<<1);
00061 }
00062 
00063 uint8_t compile_setup(uint8_t clk, uint8_t ref){
00064     return 0x40 | ((clk&0x3)<<4)|((ref&0x3)<<2);
00065 }
00066 
00067 uint8_t compile_avg(bool avgon, uint8_t navg, uint8_t nscan){
00068     uint8_t y = 0x20;
00069     if(avgon) y |= 0x10;
00070     y |= ((navg&0x3)<<2)|(nscan&0x3);
00071     return y;
00072 }
00073 
00074 int main() {
00075     int data[4];
00076     convst = 1;
00077     cs = 1;
00078 
00079     // The max11626 uses normal 8 bit spi for the registers.
00080     // However, >8 bit reads can be used to read the data (mosi would be 0 during read).
00081     spi.format(8,3);
00082     spi.frequency(10000);
00083     
00084     // MAX11626 addressing.
00085     // Addressing is performed by the leading zeros.
00086     // - no leading zero (0x80) would be the conversion register.
00087     // - one leading zeros (0x40) would be the setup register.
00088     // - two leading zeros (0x20) would be the averaging register.
00089     // - 3 leading zeros (0x10) would be the reset register.
00090     // Transfers are MSB first.
00091     // Reading data is always safe when you set MOSI to 0.
00092 
00093     // Let the scope settle after reset
00094     wait(1);
00095 
00096     while(1) {
00097         // Write reset full.
00098         cs = 0;
00099         spi.write(compile_reset(0)); 
00100         cs = 1;
00101         
00102         wait_us(100);
00103         
00104         // Write conversion
00105         cs = 0;
00106         spi.write(compile_conversion(AIN3, SCAN_TO_N)); 
00107         cs = 1;
00108 
00109         wait_us(100);
00110         
00111         convst = 0;
00112         wait_us(100);
00113         convst = 1;
00114         
00115         wait_us(100);
00116         
00117         cs = 0;
00118         // Read 4
00119         for(int i=0; i<4; i++){
00120             data[i] = 0;
00121             data[i] = spi.write(0);
00122             data[i] <<= 8;
00123             data[i] |= spi.write(0);
00124         }
00125         cs = 1;
00126         
00127         pc.printf("%05d, %05d, %05d, %05d\n",data[0],data[1],data[2],data[3]);
00128         wait(0.2);
00129         
00130         myled = !myled;
00131     }
00132 }