Page 1 of 1

MCP3202 and spi problem.

PostPosted: Thu Jun 04, 2015 7:40 pm
by Claudione
Hi Guys.

I got a problem using MCP3202 , the circuit use a pic18F 3 DA and one MCP41010 and finally the MCP3202 , all devices are working properly , the SPI line is ok ,however MCP3202 wont work , i show strange value . Here is my code :
Code: Select all
unsigned int getADC(unsigned char channel) { // Returns 0..4095

unsigned int ch=0;
unsigned int datahigh,datalow;
CSD = 1;
if(channel==0){ch=0xA0;}else{ch=0xE0;}
OpenSPI(SPI_FOSC_64, MODE_00, SMPEND);
CSD = 0;
WriteSPI(0x01);
WriteSPI(ch);
datahigh = ReadSPI();
WriteSPI(0x00);
datalow = ReadSPI();
CSD = 1;
return ((datahigh << 8) | datalow);
}


Someone can help me ?

Re: MCP3202 and spi problem.

PostPosted: Fri Jun 05, 2015 3:56 am
by AussieSusan
The data sheets for the MCP41010 and MCP3202 show that they have differences in the SPI interfaces: the MCP41010 uses an 8-bit command and an 8-bit data word which means that the PIC18F will "naturally" do the right thing.
However the MCP3202 uses a shorter command and longer data value that make up 17 bits (as shown in 5-1 of its data sheet). You need to read section 6.1 of the MCP3202's data sheet to see how to use an 8-bit SPI peripheral with this ADC.
Also, I don't know the internal operation of the WriteSPI and ReadSPI functions (although they are library functions, they operate differently on different families of devices - I try to avoid them entirely as the SPI peripheral is very simple to use and then you know exactly what is going on) but the general rule is that you should always read the received value from an SPI exchange, even if you don't use it - it stops you reading "old" values if nothing else.
Also, why are you calling the OpenSPI function each time, but not closing it again. I would open it once in my initialisation section and then just use it as needed; there is no value in opening and closing it all the time.
It would help if we knew a bit more about the values you are receiving than they are "strange". Are they shifted by 1 bit; do they not match the incoming bit stream or totally random or what?
Susan

Re: MCP3202 and spi problem.

PostPosted: Fri Jun 05, 2015 2:07 pm
by Claudione
Hi Susan Thank you so much for your answer
You are right i missed closeSPI
Ok , as you see i take care (so i hope) of data sheet and i send start bit then i ask for 0xA0 as single channel 0 with msb first , then i read it , i send again a request (bit dont care) as 0x00 the i read LSB , i also added a string like datahigh = datahigh & 0x0F; to put 0 all other bit . Like this :

Code: Select all
uunsigned int getADC(char channel) { // Returns 0..4095

      unsigned int ch;

    CSD = 1;
    BUZ = 1;
      if (channel == 0) {
          ch = 0xA0;
      } else {
          ch = 0xE0;
      }
    OpenSPI(SPI_FOSC_64, MODE_00, SMPEND);
    CSD = 0;
    WriteSPI(0x01); //start bit
   // ReadSPI();
    WriteSPI(ch);
    datahigh = ReadSPI();
    WriteSPI(0x00);
    datalow = ReadSPI();
    CSD = 1;
    CloseSPI();
    datahigh = datahigh & 0x0F; //ignoro i 4 bit piu alti
    return ((datahigh << 8) | datalow);
}


The value returned is sometimes stable but incorrect , sometimes seems random.

Can you explain better about the bit sfited because i suspect is here the problem. The initialisation is showed in code to let you know what i did.

Re: MCP3202 and spi problem.

PostPosted: Mon Jun 08, 2015 11:50 pm
by ric
Note, the same question (and many of the same answers) are at http://www.microchip.com/forums/m874261.aspx

Re: MCP3202 and spi problem.

PostPosted: Tue Jun 09, 2015 4:36 am
by AussieSusan
You also missed the more important point: open the SPI peripheral once at the start of the program and close it once at the end if that is actually needed. Don't keep opening it and closing it again.
However I see the issue was resolved in the Microchip forum.
Susan

Re: MCP3202 and spi problem.

PostPosted: Fri Jan 29, 2016 8:06 pm
by Bryan Wilcutt
According to pp 15 of the MCP3202 manual (DS21034F), the first 4 bits going on are grouped.

You are sending 0x01 0xa0
It wants: Start:SGL/DIFF:ODD/SIGN:MSBF
1 1:0 1:0 1:0

Try sending out 0x0a instead.

Re: MCP3202 and spi problem.

PostPosted: Sun Jan 31, 2016 11:44 am
by ric
It's a good idea to read all the replies before adding your own. Then you'd see this problem was fixed back in June. :)