SPI communication with external ADC MCP3

This is my code for interacting with external ADCs called MCP3208 and MCP3204

  • DEVICE INFO: recives 7 bits and sends back 13 bits
  • SENDING: 1 start bit, 1 bit for mode selection - differential (0) or sigle ended (1), 3 bits for selecting the right channel where the analog sensor is and 1 final bit for ending the frame
  • RECIVE: first it sends 1 null bit and then it sends 12 bits which is the analog sensor reading
  • MORE INFO: for the communication I use the built in registers for SPI. the SPI protocoll is very simple. there is the chip select(cs) pin which identifies the chip we want to communicate with and there is the clock(clk) which gives the chip info when we are sending the next bit or when we want to recieve next bit and finally there are the mosi and miso pins which are for incoming and outgoing data
  • MANUAL: the chips manuals can be found http://www.ereshop.com/shop/free/MCP3208.pdf and more about SPI
  • PS: in this example I use a specific analog sensor, when you want to use another just change the 3 bits used for selecting the channel(analog sensor). on the MCP3204 there are 2 bits used for selecting channel, but 3 must be sent

/media/uploads/silbo/_scaled_mcp3208.png /media/uploads/silbo/_scaled_mcp3204.png

Quote:

The pins for the MCP3208 and MCP3204

Quote:

Soldering the circuit board for the MCP3208 or MCP3204 coming soon ...

/media/uploads/silbo/_scaled_connection.png

Quote:

Connecting mbed to the MCP3208 or MCP3204 to be updated ...

/media/uploads/silbo/_scaled_clock.png

Quote:

Which clock to use

/media/uploads/silbo/_scaled_communication.png

Quote:

Communication

#include "mbed.h"

SPI spi(p5, p6, p7); // mosi(out), miso(in), sclk(clock)
DigitalOut cs(p8); // cs (the chip select signal)

Serial pc(USBTX, USBRX); // tx, rx ( the usb serial communication )

int main() {
    // Setup the spi for 7 bit data, high steady state clock,
    // second edge capture, with a 1MHz clock rate
    spi.format(7,0);
    spi.frequency(1000000);

    // notify the user that we are starting with the ADC communication
    pc.printf("Starting ADC interaction\n");
    
    // lets just do this forever
    while (1) {

        // Select the device by seting chip select low
        cs = 0;

        // sending the 6 bits + 1 bit to ignore the null bit
        // coming from the device, so the data that is sent is 1100000
        spi.write(0x60);
        
        // now the device sends back the readings 12 bits, 7 bits at a time
        uint8_t high = spi.write(0x00);
        uint8_t low = spi.write(0x00);

        // shift out the right bits
        low = ( high << 5 ) | (low >> 2);
        high = high >> 3;
        
        // shift and or the result together
        int value = ( high << 8 ) | low;
        
        // and voila we have the value and we can print it for the user
        pc.printf("sensor 0 value = %u\n", value);

        // Deselect the device
        cs = 1;

        // delay some time before reading again
        wait(1);
    }
}

All wikipages