How to connect and program MCP3008, SPI with Mbed NXP LPC1768

23 Feb 2010

Dear all,

I have connected P5,P6,P7, P8 to Din, Dout, CLK and CS of MCP3008. (I wonder it is correct)

I use format(8,0) rather than (8,1). (actually both format are able to display outputs)

I have tried with http://mbed.org/handbook/SPI, but could not interpret the output 0x??. What is 0x?

How do I know which anlog inputs from MCP3008, I intended to read.?

cheers

26 Feb 2010

With my friend's help, we have figured it out.

Just to share you all. Here is the MCP3008 datasheet. http://www.microchip.com/wwwproducts/Devices.aspx?dDocName=en010530

Here is the code.

#include "mbed.h"
#include "TextLCD.h"

SPI _spi(p5,p6,p7);                             //mosi, miso, sck
DigitalOut cs(p8);                              // chip select of MCP3008
TextLCD lcd(p24, p25, p26, p27, p28, p29, p30); // rs, rw, e, d0, d1, d2, d3


int main() {
        int _data1;
        int _data2;
        cs=1;                       // deselect everything
        wait(0.001);
        _spi.format(8,0);           //see page1 of MCP3008 manual
        _spi.frequency(1000000);    //see page4 of MCP3008 manual

//******************************************************************
//    MCP3008 clock sequence and control bits
//Once cs low, send start bit ==> 0x01  Not expecting any return
//next, send control bits   Expecting return B9, B8
//next, send anything, say 0x00, expecting return B7-B0
//next shift B9,B8 left 8bits
//next merge B9,B8 and B7-B0 to form 16bits data
//next, mask off bit15-bit10, to get a clean 10 bit data
//*******************************************************************

//*******************************************************************
//      1st 8bits         2nd 8bits           3rd 8bits
//ch0      0x01              0x80                 0x00
//ch1      0x01              0x90                 0x00
//ch2      0x01              0xA0                 0x00
//ch3      0x01              0xB0                 0x00
//ch4      0x01              0xC0                 0x00
//ch5      0x01              0xD0                 0x00
//ch6      0x01              0xE0                 0x00
//ch7      0x01              0xF0                 0x00
//*******************************************************************

  

    while (1) {

//ch0.
        cs=0;                               //cs low. start data collection
        _spi.write(0x01);                   //write start bit. Not expecting return
        _data1=_spi.write(0x80);            //write control bit, expecting return B9, B8
        _data2=_spi.write(0x00);            //write anything, expecting return B7-B0
        int ch0 =(_data1<<8) | _data2;          //merge two 8bits data becoming 16bits data
        ch0 = ch0 & 0x03FF;                 // mask off bit15 - bit10
        cs=1;                               //cs high. end of data collection

//ch1  
        cs=0;                     
        _spi.write(0x01);
        _data1=_spi.write(0x90);
        _data2=_spi.write(0x00);
        int ch1 =(_data1<<8) | _data2;
        ch1 = ch1 & 0x03FF;
        cs=1;


//ch2  
        cs=0;                     
        _spi.write(0x01);
        _data1=_spi.write(0xA0);
        _data2=_spi.write(0x00);
        int ch2 =(_data1<<8) | _data2;
        ch2 = ch2 & 0x03FF;
        cs=1;


//ch3  
        cs=0;                     
        _spi.write(0x01);
        _data1=_spi.write(0xB0);
        _data2=_spi.write(0x00);
        int ch3 =(_data1<<8) | _data2;
        ch3 = ch3 & 0x03FF;
        cs=1;


//ch4  
        cs=0;                     
        _spi.write(0x01);
        _data1=_spi.write(0xC0);
        _data2=_spi.write(0x00);
        int ch4 =(_data1<<8) | _data2;
        ch4 = ch4 & 0x03FF;
        cs=1;


//ch5  
        cs=0;                     
        _spi.write(0x01);
        _data1=_spi.write(0xD0);
        _data2=_spi.write(0x00);
        int ch5 =(_data1<<8) | _data2;
        ch5 = ch5 & 0x03FF;
        cs=1;


//ch6  
        cs=0;                     
        _spi.write(0x01);
        _data1=_spi.write(0xE0);
        _data2=_spi.write(0x00);
        int ch6 =(_data1<<8) | _data2;
        ch6 = ch6 & 0x03FF;
        cs=1;


//ch7 
        cs=0;                     
        _spi.write(0x01);
        _data1=_spi.write(0xF0);
        _data2=_spi.write(0x00);
        int ch7 =(_data1<<8) | _data2;
        ch7 = ch7 & 0x03FF;
        cs=1;


//Display the results
        lcd.cls();
        lcd.locate(0,0);
        lcd.printf("%d%d%d%d",ch0,ch1,ch2,ch3);
        lcd.locate(0,1);
        lcd.printf("%d%d%d%d",ch4,ch5,ch6,ch7);

        wait(0.01);
    }
}

 

05 Dec 2010

Thanks for the help. I was able to work through my issues thanks to you.

I was use to a microcontroller that did a lot of this work for me and I am new to mbed. Because I am using the 12bit version (MCP3208) I was able to easily cut things down to 4 bit increments which allowed me to ignore null bits and extra bits so combining them is more streamlined. Here is my code snippet.

 

mcp3208.format(4,1); //mcp3208 is my spi object

mcp3208.write(0x0); //leading zeros
mcp3208.write(0x7); //start and command bits
mcp3208.write(0xc); //finish command bits and ignore null bit
spiRead1 = mcp3208.write(0x00); // first part of 12
spiRead2 = mcp3208.write(0x00); // second part of 12
spiRead3 = mcp3208.write(0x00); // third part of 12.

05 Dec 2010

I actually tried using the same codefor the mcp3208 and so far I can get only zeros.

I've connected a trimpot to channel 0. Why are the first first 4 bits set to zero?? Here's the code I used.

#include "mbed.h"              
//#include "MCP3208.h"


DigitalInOut cs(p14); 

/*
12-bit adc

14-vdd
13-vref
12-agnd
11-clk	   SCLK
10-dout	   MISO
9 -din	   MOSI
8 -cs 	   CS
7 -dgnd


*/

Serial pc(USBTX, USBRX);
SPI mcp3208(p11, p12, p13); // mosi, miso, sclk

int main() {
	  int spiRead1;
	  int spiRead2;
	  int spiRead3;	  
      
      //set CS pint high initially
	  cs=1;                       
      wait(0.001);
       
	  mcp3208.format(4,1); //mcp3208 is my spi object
	  
      mcp3208.frequency(1000000);

	
    while(1) {		 
	  uint16_t data;
	  
	  //channel 0			 
      cs=0; 			          
      mcp3208.write(0x0); //leading zeros
	  mcp3208.write(0x7); //start and command bits
	  mcp3208.write(0xc); //finish command bits and ignore null bit
	  spiRead1 = mcp3208.write(0x00); // first part of 12
	  spiRead2 = mcp3208.write(0x00); // second part of 12
	  spiRead3 = mcp3208.write(0x00); // third part of 12.
      cs=1;  
	  data  = (spiRead1 << 4)| (spiRead1 << 4)||(spiRead1 << 4); 
	  	
	  wait(1);
      printf("%d\n\r",data);
    }
}

05 Dec 2010

Hi Dimiter,

This line looks very suspect:

data  = (spiRead1 << 4)| (spiRead1 << 4)||(spiRead1 << 4);

i'm guessing you meant something like:

data  = (spiRead1 << 8) | (spiRead2 << 4) | spiRead3;

Simon

05 Dec 2010

Thanks Simon I fixed it.

I was loking at that right now since it was giving a (two actually) warning.

I am still getting a stream of  zeros though. I double checked the connections. time to read the datasheet again I guess.

05 Dec 2010

 

My version is reading channel 8 (channel 7 when thinking 0-7). So either changed your analog input channel to 8 or change the 0x7 to the correct channel. I also used the SPI specific outputs (p5, p6, p7) and then used p8 for the chip select.

It does look like the 4 leading zeros are not needed, they wher left after trying to follow 8bit blocks instead of 4.

look at page 21 for more info

http://ww1.microchip.com/downloads/en/DeviceDoc/21298e.pdf