Simple test code with an max11626eee

Dependencies:   mbed

/media/uploads/jeroen3/ds1104z_20160727-210831.png

Committer:
jeroen3
Date:
Wed Jul 27 19:35:48 2016 +0000
Revision:
0:afa6a5e2d11a
max11626 example code

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jeroen3 0:afa6a5e2d11a 1 #include "mbed.h"
jeroen3 0:afa6a5e2d11a 2
jeroen3 0:afa6a5e2d11a 3 DigitalOut myled(LED1);
jeroen3 0:afa6a5e2d11a 4 DigitalOut convst(p18); // Conversion Start (active low)
jeroen3 0:afa6a5e2d11a 5 DigitalOut cs(p16); // Chip Select (active low)
jeroen3 0:afa6a5e2d11a 6 DigitalIn eoc(p20); // End of Conversion (active low)
jeroen3 0:afa6a5e2d11a 7 SPI spi(p11, p12, p13); // mosi, miso, sclk
jeroen3 0:afa6a5e2d11a 8 Serial pc(USBTX, USBRX); // tx, rx
jeroen3 0:afa6a5e2d11a 9
jeroen3 0:afa6a5e2d11a 10 #define AIN0 0u
jeroen3 0:afa6a5e2d11a 11 #define AIN1 1u
jeroen3 0:afa6a5e2d11a 12 #define AIN2 2u
jeroen3 0:afa6a5e2d11a 13 #define AIN3 3u
jeroen3 0:afa6a5e2d11a 14 #define AIN4 4u
jeroen3 0:afa6a5e2d11a 15 #define AIN5 5u
jeroen3 0:afa6a5e2d11a 16 #define AIN6 6u
jeroen3 0:afa6a5e2d11a 17 #define AIN7 7u
jeroen3 0:afa6a5e2d11a 18 #define AIN8 8u
jeroen3 0:afa6a5e2d11a 19 #define AIN9 9u
jeroen3 0:afa6a5e2d11a 20 #define AIN10 10u
jeroen3 0:afa6a5e2d11a 21 #define AIN11 11u
jeroen3 0:afa6a5e2d11a 22 #define AIN12 12u
jeroen3 0:afa6a5e2d11a 23 #define AIN13 13u
jeroen3 0:afa6a5e2d11a 24 #define AIN14 14u
jeroen3 0:afa6a5e2d11a 25 #define AIN15 15u
jeroen3 0:afa6a5e2d11a 26
jeroen3 0:afa6a5e2d11a 27 #define SCAN_TO_N 0u // Scans channels 0 through N.
jeroen3 0:afa6a5e2d11a 28 #define SCAN_N_TO 1u // Scans channels N through the highest numbered channel.
jeroen3 0:afa6a5e2d11a 29 #define SCAN_N_AVG 2u // Scans channel N repeatedly. The averaging register sets the number of results.
jeroen3 0:afa6a5e2d11a 30 #define SCAN_OFF 3u // No scan. Converts channel N once only
jeroen3 0:afa6a5e2d11a 31
jeroen3 0:afa6a5e2d11a 32 #define CLKSEL_INT_INT_CNVST 0u // Internal clock, internal aqcuisition, convst
jeroen3 0:afa6a5e2d11a 33 #define CLKSEL_INT_EXT_CNVST 1u // Internal clock, external aqcuisition convst, convst
jeroen3 0:afa6a5e2d11a 34 #define CLKSEL_INT_INT_ADC 2u // Internal clock, internal aqcuisition, extra channel
jeroen3 0:afa6a5e2d11a 35 #define CLKSEL_EXT_EXT_ADC 3u // External clock, external aqcuisition sclk, extra channel
jeroen3 0:afa6a5e2d11a 36
jeroen3 0:afa6a5e2d11a 37 #define REFSEL_INT_SLEEP 0u // Reference off after scan; need wake-up delay
jeroen3 0:afa6a5e2d11a 38 #define REFSEL_EXT 1u // Reference off; no wake-up delay.
jeroen3 0:afa6a5e2d11a 39 #define REFSEL_INT_NOSLEEP 2u // Reference always on; no wake-up delay.
jeroen3 0:afa6a5e2d11a 40 #define REFSEL_RESERVED 3u // Reserved. Do not use.
jeroen3 0:afa6a5e2d11a 41
jeroen3 0:afa6a5e2d11a 42 #define NAVG_4 0u // Performs four conversions and returns the average for each requested result.
jeroen3 0:afa6a5e2d11a 43 #define NAVG_8 1u // Performs eight conversions and returns the average for each requested result.
jeroen3 0:afa6a5e2d11a 44 #define NAVG_16 2u // Performs 16 conversions and returns the average for each requested result.
jeroen3 0:afa6a5e2d11a 45 #define NAVG_32 3u // Performs 32 conversions and returns the average for each requested result.
jeroen3 0:afa6a5e2d11a 46
jeroen3 0:afa6a5e2d11a 47 #define NSCAN_4 0u // Scans channel N and returns four results.
jeroen3 0:afa6a5e2d11a 48 #define NSCAN_8 1u // Scans channel N and returns eight results.
jeroen3 0:afa6a5e2d11a 49 #define NSCAN_12 2u // Scans channel N and returns 12 results.
jeroen3 0:afa6a5e2d11a 50 #define NSCAN_16 3u // Scans channel N and returns 16 results.
jeroen3 0:afa6a5e2d11a 51
jeroen3 0:afa6a5e2d11a 52 uint8_t compile_reset(bool fifo_only){
jeroen3 0:afa6a5e2d11a 53 if(fifo_only)
jeroen3 0:afa6a5e2d11a 54 return 0x18;
jeroen3 0:afa6a5e2d11a 55 else
jeroen3 0:afa6a5e2d11a 56 return 0x10;
jeroen3 0:afa6a5e2d11a 57 }
jeroen3 0:afa6a5e2d11a 58
jeroen3 0:afa6a5e2d11a 59 uint8_t compile_conversion(uint8_t ch, uint8_t scan){
jeroen3 0:afa6a5e2d11a 60 return 0x80 | ((ch&0xF)<<3)|((scan&0x3)<<1);
jeroen3 0:afa6a5e2d11a 61 }
jeroen3 0:afa6a5e2d11a 62
jeroen3 0:afa6a5e2d11a 63 uint8_t compile_setup(uint8_t clk, uint8_t ref){
jeroen3 0:afa6a5e2d11a 64 return 0x40 | ((clk&0x3)<<4)|((ref&0x3)<<2);
jeroen3 0:afa6a5e2d11a 65 }
jeroen3 0:afa6a5e2d11a 66
jeroen3 0:afa6a5e2d11a 67 uint8_t compile_avg(bool avgon, uint8_t navg, uint8_t nscan){
jeroen3 0:afa6a5e2d11a 68 uint8_t y = 0x20;
jeroen3 0:afa6a5e2d11a 69 if(avgon) y |= 0x10;
jeroen3 0:afa6a5e2d11a 70 y |= ((navg&0x3)<<2)|(nscan&0x3);
jeroen3 0:afa6a5e2d11a 71 return y;
jeroen3 0:afa6a5e2d11a 72 }
jeroen3 0:afa6a5e2d11a 73
jeroen3 0:afa6a5e2d11a 74 int main() {
jeroen3 0:afa6a5e2d11a 75 int data[4];
jeroen3 0:afa6a5e2d11a 76 convst = 1;
jeroen3 0:afa6a5e2d11a 77 cs = 1;
jeroen3 0:afa6a5e2d11a 78
jeroen3 0:afa6a5e2d11a 79 // The max11626 uses normal 8 bit spi for the registers.
jeroen3 0:afa6a5e2d11a 80 // However, >8 bit reads can be used to read the data (mosi would be 0 during read).
jeroen3 0:afa6a5e2d11a 81 spi.format(8,3);
jeroen3 0:afa6a5e2d11a 82 spi.frequency(10000);
jeroen3 0:afa6a5e2d11a 83
jeroen3 0:afa6a5e2d11a 84 // MAX11626 addressing.
jeroen3 0:afa6a5e2d11a 85 // Addressing is performed by the leading zeros.
jeroen3 0:afa6a5e2d11a 86 // - no leading zero (0x80) would be the conversion register.
jeroen3 0:afa6a5e2d11a 87 // - one leading zeros (0x40) would be the setup register.
jeroen3 0:afa6a5e2d11a 88 // - two leading zeros (0x20) would be the averaging register.
jeroen3 0:afa6a5e2d11a 89 // - 3 leading zeros (0x10) would be the reset register.
jeroen3 0:afa6a5e2d11a 90 // Transfers are MSB first.
jeroen3 0:afa6a5e2d11a 91 // Reading data is always safe when you set MOSI to 0.
jeroen3 0:afa6a5e2d11a 92
jeroen3 0:afa6a5e2d11a 93 // Let the scope settle after reset
jeroen3 0:afa6a5e2d11a 94 wait(1);
jeroen3 0:afa6a5e2d11a 95
jeroen3 0:afa6a5e2d11a 96 while(1) {
jeroen3 0:afa6a5e2d11a 97 // Write reset full.
jeroen3 0:afa6a5e2d11a 98 cs = 0;
jeroen3 0:afa6a5e2d11a 99 spi.write(compile_reset(0));
jeroen3 0:afa6a5e2d11a 100 cs = 1;
jeroen3 0:afa6a5e2d11a 101
jeroen3 0:afa6a5e2d11a 102 wait_us(100);
jeroen3 0:afa6a5e2d11a 103
jeroen3 0:afa6a5e2d11a 104 // Write conversion
jeroen3 0:afa6a5e2d11a 105 cs = 0;
jeroen3 0:afa6a5e2d11a 106 spi.write(compile_conversion(AIN3, SCAN_TO_N));
jeroen3 0:afa6a5e2d11a 107 cs = 1;
jeroen3 0:afa6a5e2d11a 108
jeroen3 0:afa6a5e2d11a 109 wait_us(100);
jeroen3 0:afa6a5e2d11a 110
jeroen3 0:afa6a5e2d11a 111 convst = 0;
jeroen3 0:afa6a5e2d11a 112 wait_us(100);
jeroen3 0:afa6a5e2d11a 113 convst = 1;
jeroen3 0:afa6a5e2d11a 114
jeroen3 0:afa6a5e2d11a 115 wait_us(100);
jeroen3 0:afa6a5e2d11a 116
jeroen3 0:afa6a5e2d11a 117 cs = 0;
jeroen3 0:afa6a5e2d11a 118 // Read 4
jeroen3 0:afa6a5e2d11a 119 for(int i=0; i<4; i++){
jeroen3 0:afa6a5e2d11a 120 data[i] = 0;
jeroen3 0:afa6a5e2d11a 121 data[i] = spi.write(0);
jeroen3 0:afa6a5e2d11a 122 data[i] <<= 8;
jeroen3 0:afa6a5e2d11a 123 data[i] |= spi.write(0);
jeroen3 0:afa6a5e2d11a 124 }
jeroen3 0:afa6a5e2d11a 125 cs = 1;
jeroen3 0:afa6a5e2d11a 126
jeroen3 0:afa6a5e2d11a 127 pc.printf("%05d, %05d, %05d, %05d\n",data[0],data[1],data[2],data[3]);
jeroen3 0:afa6a5e2d11a 128 wait(0.2);
jeroen3 0:afa6a5e2d11a 129
jeroen3 0:afa6a5e2d11a 130 myled = !myled;
jeroen3 0:afa6a5e2d11a 131 }
jeroen3 0:afa6a5e2d11a 132 }